Jump to content

Nhờ tiền bối viết giùm lisp chuyển hình tròn thành đa giác

1 giờ} trướ}c, hawking312 đã nói:

Cảm ơn bác đã giúp đỡ, có thể bỏ bảng thông báo này mà thay bằng dòng command phía dưới cho dễ chịu và tăng tốc hơn được không bác, xin cảm ơn bác nhiều.

 

;;;   LISP convert ARC, CIRCLE, ELLIPSE, SPLINE to LWPOLYLINE
;;;   By Trân Thiêp 0918.841230
;;;       05/2020.
(defun curve->Lstpo (ent num / LL_UR )
    (setq LL_UR (acet-ent-geomextents ent))
    (ACET-GEOM-SPLINE-POINT-LIST ent
                                 (/ (distance (car LL_UR) (cadr LL_UR)) num)
    )
)
(defun c:c2pl (/ ss lstpo obj col Ltype LtypeScale)
    (command "undo" "be")
    (acet-error-init '(("cmdecho" 0 "osmode" 0) 0))
    (or ucshold_thiep (setq ucshold_thiep (acet-ucs-get nil)))
    (acet-ucs-cmd '("w"))
    (PROMPT "\nSelect curves to convert it into Lwpolylines" )
    (while (NOT (setq ss (ssget '((0 . "ARC,CIRCLE,ELLIPSE,SPLINE")))))
        (PROMPT "\nSelect arn't right, please select curves again" )
    )
    (mapcar '(lambda (x)
                 (setq eng (entget x))
                 (cond ((wcmatch (acet-dxf 0 eng) "ARC,CIRCLE,ELLIPSE")
                        (acet-Lwpline-make (list (curve->Lstpo x 2020)))
                       )
                       (T (acet-Lwpline-make (list (curve->Lstpo x 3000))))
                 )
                 (setq obj (vlax-ename->vla-object (entlast)))
                 (if (setq Ltype (acet-dxf 6 eng)) (Vla-put-Linetype obj Linetype))
                 (if (setq LtypeScale (acet-dxf 48 eng)) (Vla-put-LinetypeScale obj LtypeScale))
                 (Vla-put-LinetypeGeneration obj :vlax-true)
                 (Vla-put-layer obj (acet-dxf 8 eng))
                 (if (setq col (acet-dxf 62 eng)) (Vla-put-color obj col))
                 (entdel x)
             )
            (acet-ss-to-list ss)
    )
    (acet-ucs-set ucshold_thiep)
    (acet-error-restore)
    (command "undo" "en")
    (princ "\nOk")
    (princ) 
)

Thử lại xem bạn. Nhân tiện fix luôn 1 lỗi nhỏ về color của đối tượng.

Cách dùng

  1. Tải tệp 447470_c2pl.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 C2PL để chạy lisp.

Mã nguồn 447470_c2pl.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/181508-y%C3%AAu-c%E1%BA%A7u-nh%E1%BB%9D-ti%E1%BB%81n-b%E1%BB%91i-vi%E1%BA%BFt-gi%C3%B9m-lisp-chuy%E1%BB%83n-h%C3%ACnh-tr%C3%B2n-th%C3%A0nh-%C4%91a-gi%C3%A1c/
;;;   LISP convert ARC, CIRCLE, ELLIPSE, SPLINE to LWPOLYLINE
;;;   By Trân Thiêp 0918.841230
;;;       05/2020.
(defun curve->Lstpo (ent num / LL_UR )
    (setq LL_UR (acet-ent-geomextents ent))
    (ACET-GEOM-SPLINE-POINT-LIST ent
                                 (/ (distance (car LL_UR) (cadr LL_UR)) num)
    )
)
(defun c:c2pl (/ ss lstpo obj col Ltype LtypeScale)
    (command "undo" "be")
    (acet-error-init '(("cmdecho" 0 "osmode" 0) 0))
    (or ucshold_thiep (setq ucshold_thiep (acet-ucs-get nil)))
    (acet-ucs-cmd '("w"))
    (PROMPT "\nSelect curves to convert it into Lwpolylines" )
    (while (NOT (setq ss (ssget '((0 . "ARC,CIRCLE,ELLIPSE,SPLINE")))))
        (PROMPT "\nSelect arn't right, please select curves again" )
    )
    (mapcar '(lambda (x)
                 (setq eng (entget x))
                 (cond ((wcmatch (acet-dxf 0 eng) "ARC,CIRCLE,ELLIPSE")
                        (acet-Lwpline-make (list (curve->Lstpo x 2020)))
                       )
                       (T (acet-Lwpline-make (list (curve->Lstpo x 3000))))
                 )
                 (setq obj (vlax-ename->vla-object (entlast)))
                 (if (setq Ltype (acet-dxf 6 eng)) (Vla-put-Linetype obj Linetype))
                 (if (setq LtypeScale (acet-dxf 48 eng)) (Vla-put-LinetypeScale obj LtypeScale))
                 (Vla-put-LinetypeGeneration obj :vlax-true)
                 (Vla-put-layer obj (acet-dxf 8 eng))
                 (if (setq col (acet-dxf 62 eng)) (Vla-put-color obj col))
                 (entdel x)
             )
            (acet-ss-to-list ss)
    )
    (acet-ucs-set ucshold_thiep)
    (acet-error-restore)
    (command "undo" "en")
    (princ "\nOk")
    (princ) 
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Yêu cầu] Nhờ tiền bối viết giùm lisp chuyển hình tròn thành đa giác. Xem đúng bài viết #447470 để đọc hướng dẫn và trao đổi thêm.

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