Lisp lệnh BRC trong chủ đề “Xin Giúp Đỡ Lisp Chèn 1 Block Vào Tâm Của Nhiều Hình Chữ Nhật”
(defun c:brc (/ retcen osmode idx rectangles ins );Chen Block vao tam rectang (defun rectcen (rect / pl p1 p2 p3 p4 pm1 pm2 an di ceo) (setq ceo (vlax-ename->vla-object rect)) (setq pl (vlax-safearray->list (vlax-variant-value (vla-get-coordinates ceo)))) (if (> (length pl) 6) (progn (setq p1 (list (nth 0 pl) (nth 1 pl))) (setq p2 (list (nth 2 pl) (nth 3 pl))) (setq p3 (list (nth 4 pl) (nth 5 pl))) (setq p4 (list (nth 6 pl) (nth 7 pl))) (setq pm1 (mapcar '/ (mapcar '+ p1 p3) '(2.0 2.0 2.0))) (setq pm2 (mapcar '/ (mapcar '+ p2 p4) '(2.0 2.0 2.0))) (cond ((and (equal (distance pm1 p1)(distance pm2 p1) 0.001) (equal (distance pm1 p2)(distance pm2 p2) 0.001) (equal (distance pm1 p3)(distance pm2 p3) 0.001) (equal (distance pm1 p4)(distance pm2 p4) 0.001) ) (inters p1 p3 p2 p4 nil) ) ) ) ) ) ;---------------------------------------------- (vl-load-com) (command "undo" "begin") (command "ucs" "w") (setq osmode (getvar "osmode")) (setvar "osmode" 0) (if (setq rectangles (ssget '((0 . "LWPOLYLINE")))) (progn (setq idx 0) (repeat (sslength rectangles) (setq ins (rectcen (ssname rectangles idx))) (if ins (command "-insert" "s" ins "" "" "")) (setq idx (1+ idx)) ) ) (princ "\n Selected(s) object(s) are not Lwpolyline!") ) (command "undo" "end") (setvar "osmode" osmode) (princ) )
thay từ "S" bôi đỏ bằng tên block của bạn
Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.
Cách dùng
- Chép mã nguồn, lưu thành tệp 419384_brc.lsp.
- 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ẽ.
- Gõ lệnh BRC để chạy lisp.
Mã nguồn 419384_brc.lsp (trích từ bài viết gốc)
(defun c:brc (/ retcen osmode idx rectangles ins );Chen Block vao tam rectang
(defun rectcen (rect / pl p1 p2 p3 p4 pm1 pm2 an di ceo)
(setq ceo (vlax-ename->vla-object rect))
(setq pl (vlax-safearray->list (vlax-variant-value (vla-get-coordinates ceo))))
(if (> (length pl) 6)
(progn
(setq p1 (list (nth 0 pl) (nth 1 pl)))
(setq p2 (list (nth 2 pl) (nth 3 pl)))
(setq p3 (list (nth 4 pl) (nth 5 pl)))
(setq p4 (list (nth 6 pl) (nth 7 pl)))
(setq pm1 (mapcar '/ (mapcar '+ p1 p3) '(2.0 2.0 2.0)))
(setq pm2 (mapcar '/ (mapcar '+ p2 p4) '(2.0 2.0 2.0)))
(cond
((and
(equal (distance pm1 p1)(distance pm2 p1) 0.001)
(equal (distance pm1 p2)(distance pm2 p2) 0.001)
(equal (distance pm1 p3)(distance pm2 p3) 0.001)
(equal (distance pm1 p4)(distance pm2 p4) 0.001)
)
(inters p1 p3 p2 p4 nil)
)
)
)
)
)
;----------------------------------------------
(vl-load-com)
(command "undo" "begin")
(command "ucs" "w")
(setq osmode (getvar "osmode"))
(setvar "osmode" 0)
(if
(setq rectangles (ssget '((0 . "LWPOLYLINE"))))
(progn
(setq idx 0)
(repeat (sslength rectangles)
(setq ins (rectcen (ssname rectangles idx)))
(if ins (command "-insert" "s" ins "" "" ""))
(setq idx (1+ idx))
)
)
(princ "\n Selected(s) object(s) are not Lwpolyline!")
)
(command "undo" "end")
(setvar "osmode" osmode)
(princ)
) Thảo luận gốc
Lisp được chia sẻ trong chủ đề Xin Giúp Đỡ Lisp Chèn 1 Block Vào Tâm Của Nhiều Hình Chữ Nhật. Xem đúng bài viết #419384 để đọc hướng dẫn và trao đổi thêm.