Jump to content

Lisp lệnh DPI DPO trong chủ đề “Nhờ chỉnh sửa lisp”

Sửa một cách nhanh nhất đây. Dư hay thừa gì  trong code thì kệ, kết quả như ý bạn là được.

;;  DimPolySeq.lsp 
;;  To dimension the lengths of all segments of a Polyline on the Inboard or Outboard
;;    side, adding a sequencing number and colon as prefix.  For self-intersecting or open
;;    Polyline without a clear "inside" and "outside," will determine a side -- if not as
;;    desired, undo and run other command.
;;  Dimensions along arc segments will be angular Dimensions, showing length of arc
;;    as text override, not included angle native to angular Dimensions.  They will not
;;    update if Polyline is stretched, as Dimensions along line segments will.
;;  Uses current Dimension and Units settings; dimension line location distance from
;;    Polyline segment = 1.5 x dimension text height for stacked fractions to clear .
;;  Sequencing number + colon & space are in text override; number is stored in non-
;;    localized variable *DPseq.  Remembers that, and continues sequence on subsequent
;;    usage within same editing session of same drawing, whether using all DPI or all
;;    DPO or a mixture of the two commands.
;;  Accepts LW and 2D "heavy" Polylines, but not 3D Polylines or meshes.
;;  Kent Cooper, 29 August 2016

(vl-load-com)

(defun DP (side / *error* clay cmde styht plsel pl cw inc pt1 pt2 pt3 pt4)

  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg))
    ); if
    (setvar 'clayer clay)
    (setvar 'osmode osm)
    (command-s "_.undo" "_end")
    (setvar 'cmdecho cmde)
    (princ)
  ); defun -- *error*

  (setq clay (getvar 'clayer) osm (getvar 'osmode) cmde (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (setvar 'osmode 0)
  (if (not *DPseq) (setq *DPseq 1))
  (command
    "_.undo" "_begin"
    "_.layer" "_make" "A-ANNO-DIMS" "_color" 2 "" "" ;; <---EDIT if desired
  ); command
  (setq styht (cdr (assoc 40 (tblsearch "style" (getvar 'dimtxsty))))); height of text style in current dimension style
  (if (= styht 0.0) (setq styht (* (getvar 'dimtxt) (getvar 'dimscale)))); if above is non-fixed-height
  (while
    (not
      (and
        (setq plsel (entsel "\nSelect Polyline: "))
        (wcmatch (cdr (assoc 0 (entget (car plsel)))) "*POLYLINE")
        (= (logand (cdr (assoc 70 (entget (car plsel)))) 88) 0)
          ;; not 3D or mesh 
      ); and
    ); not
    (prompt "\nNothing selected, or not a LW or 2D Polyline.")
  ); while
  (setq pl (vlax-ename->vla-object (car plsel)))
  (vla-offset pl styht); temporary
  (setq cw (< (vla-get-area (vlax-ename->vla-object (entlast))) (vla-get-area pl)))
    ;; clockwise for closed or clearly inside/outside open; may not give
    ;; desired result for open without obvious inside/outside
  (entdel (entlast))
  (repeat (setq inc (fix (vlax-curve-getEndParam pl)))
    (setq
      pt1 (vlax-curve-getPointAtParam pl inc)
      pt2 (vlax-curve-getPointAtParam pl (- inc 0.5)); segment midpoint
      pt3 (vlax-curve-getPointAtParam pl (1- inc))
    ); setq
    (if (equal (angle pt1 pt2) (angle pt2 pt3) 1e-8); line segment
      (command ; then
        "_.dimaligned" pt1 pt3
        "_text" "<>"
      ); 
      (command ; else 
        "_.dimangular" ""
        (inters ; arc center
          (setq pt4 (mapcar '/ (mapcar '+ pt1 pt2) '(2 2 2)))
          (polar pt4 (+ (angle pt1 pt2) (/ pi 2)) 1)
          (setq pt4 (mapcar '/ (mapcar '+ pt2 pt3) '(2 2 2)))
          (polar pt4 (+ (angle pt2 pt3) (/ pi 2)) 1)
          nil
        ); inters
        pt1 pt3
        "_text"
          (strcat
            (itoa *DPseq) ": "
            (rtos (abs (- (vlax-curve-getDistAtParam pl inc) (vlax-curve-getDistAtParam pl (1- inc)))) 2 0)
            "mm"
            ;; 
          ); strcat
      ); command 
    ); if
    (command ; complete Dimension: dimension line location
      (polar
        pt2
        (apply
          (if (or (and cw (= side "in")) (and (not cw) (= side "out"))) '- '+)
          (list
            (angle '(0 0 0) (vlax-curve-getFirstDeriv pl (- inc 0.5)))
            (/ pi 2)
          ); list
        ); apply
        (* styht 1.5)
          ;; 
      ); polar
    ); command
    (setq
      inc (1- inc)
      *DPseq (1+ *DPseq)
    ); setq
  ); repeat
  (setvar 'clayer clay)
  (setvar 'osmode osm)
  (command "_.undo" "_end")
  (setvar 'cmdecho cmde)
  (princ)
); defun -- C:DP

(defun C:DPI () (DP "in")); = Dimension Polyline Inside
(defun C:DPO () (DP "out")); = Dimension Polyline Outside

(prompt "\nType DPI to Dimension a Polyline on the Inside, DPO to do so on the Outside.")

 

Cách dùng

  1. Tải tệp 440969_dpi_dpo.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh DPI DPO để chạy lisp.

Mã nguồn 440969_dpi_dpo.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/177956-nh%E1%BB%9D-ch%E1%BB%89nh-s%E1%BB%ADa-lisp/
;;  DimPolySeq.lsp [command names: DPI, DPO]
;;  To dimension the lengths of all segments of a Polyline on the Inboard or Outboard
;;    side, adding a sequencing number and colon as prefix.  For self-intersecting or open
;;    Polyline without a clear "inside" and "outside," will determine a side -- if not as
;;    desired, undo and run other command.
;;  Dimensions along arc segments will be angular Dimensions, showing length of arc
;;    as text override, not included angle native to angular Dimensions.  They will not
;;    update if Polyline is stretched, as Dimensions along line segments will.
;;  Uses current Dimension and Units settings; dimension line location distance from
;;    Polyline segment = 1.5 x dimension text height for stacked fractions to clear [but
;;    see suggestion below if you don't use stacked fractions].
;;  Sequencing number + colon & space are in text override; number is stored in non-
;;    localized variable *DPseq.  Remembers that, and continues sequence on subsequent
;;    usage within same editing session of same drawing, whether using all DPI or all
;;    DPO or a mixture of the two commands.
;;  Accepts LW and 2D "heavy" Polylines, but not 3D Polylines or meshes.
;;  Kent Cooper, 29 August 2016

(vl-load-com)

(defun DP (side / *error* clay cmde styht plsel pl cw inc pt1 pt2 pt3 pt4)

  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg))
    ); if
    (setvar 'clayer clay)
    (setvar 'osmode osm)
    (command-s "_.undo" "_end")
    (setvar 'cmdecho cmde)
    (princ)
  ); defun -- *error*

  (setq clay (getvar 'clayer) osm (getvar 'osmode) cmde (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (setvar 'osmode 0)
  (if (not *DPseq) (setq *DPseq 1))
  (command
    "_.undo" "_begin"
    "_.layer" "_make" "A-ANNO-DIMS" "_color" 2 "" "" ;; <---EDIT if desired
  ); command
  (setq styht (cdr (assoc 40 (tblsearch "style" (getvar 'dimtxsty))))); height of text style in current dimension style
  (if (= styht 0.0) (setq styht (* (getvar 'dimtxt) (getvar 'dimscale)))); if above is non-fixed-height
  (while
    (not
      (and
        (setq plsel (entsel "\nSelect Polyline: "))
        (wcmatch (cdr (assoc 0 (entget (car plsel)))) "*POLYLINE")
        (= (logand (cdr (assoc 70 (entget (car plsel)))) 88) 0)
          ;; not 3D or mesh [88 = 8 (3D) + 16 (polygon mesh) + 64 (polyface mesh)]
      ); and
    ); not
    (prompt "\nNothing selected, or not a LW or 2D Polyline.")
  ); while
  (setq pl (vlax-ename->vla-object (car plsel)))
  (vla-offset pl styht); temporary
  (setq cw (< (vla-get-area (vlax-ename->vla-object (entlast))) (vla-get-area pl)))
    ;; clockwise for closed or clearly inside/outside open; may not give
    ;; desired result for open without obvious inside/outside
  (entdel (entlast))
  (repeat (setq inc (fix (vlax-curve-getEndParam pl)))
    (setq
      pt1 (vlax-curve-getPointAtParam pl inc)
      pt2 (vlax-curve-getPointAtParam pl (- inc 0.5)); segment midpoint
      pt3 (vlax-curve-getPointAtParam pl (1- inc))
    ); setq
    (if (equal (angle pt1 pt2) (angle pt2 pt3) 1e-8); line segment
      (command ; then
        "_.dimaligned" pt1 pt3
        "_text" "<>"
      ); [leaves at dimension line location prompt]
      (command ; else [arc segment]
        "_.dimangular" ""
        (inters ; arc center
          (setq pt4 (mapcar '/ (mapcar '+ pt1 pt2) '(2 2 2)))
          (polar pt4 (+ (angle pt1 pt2) (/ pi 2)) 1)
          (setq pt4 (mapcar '/ (mapcar '+ pt2 pt3) '(2 2 2)))
          (polar pt4 (+ (angle pt2 pt3) (/ pi 2)) 1)
          nil
        ); inters
        pt1 pt3
        "_text"
          (strcat
            (itoa *DPseq) ": "
            (rtos (abs (- (vlax-curve-getDistAtParam pl inc) (vlax-curve-getDistAtParam pl (1- inc)))) 2 0)
            "mm"
            ;; [edit mode and precision and suffix as desired -- this is per request on AutoCAD Forum]
          ); strcat
      ); command [leaves at dimension line location prompt]
    ); if
    (command ; complete Dimension: dimension line location
      (polar
        pt2
        (apply
          (if (or (and cw (= side "in")) (and (not cw) (= side "out"))) '- '+)
          (list
            (angle '(0 0 0) (vlax-curve-getFirstDeriv pl (- inc 0.5)))
            (/ pi 2)
          ); list
        ); apply
        (* styht 1.5)
          ;; [If you don't use stacked fractions, consider using styht without multiplier]
      ); polar
    ); command
    (setq
      inc (1- inc)
      *DPseq (1+ *DPseq)
    ); setq
  ); repeat
  (setvar 'clayer clay)
  (setvar 'osmode osm)
  (command "_.undo" "_end")
  (setvar 'cmdecho cmde)
  (princ)
); defun -- C:DP

(defun C:DPI () (DP "in")); = Dimension Polyline Inside
(defun C:DPO () (DP "out")); = Dimension Polyline Outside

(prompt "\nType DPI to Dimension a Polyline on the Inside, DPO to do so on the Outside.")

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Nhờ chỉnh sửa lisp. Xem đúng bài viết #440969 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 301)