Lisp lệnh TRICHTHUA trong chủ đề “trích do”
Chào khaosat2009, Lisp cwb của bác Gia_bach viết rất tuyệt vời khi copy, xóa, move các đối tượng trong ngoài 1 vùng kín, hay giữa 2 vùng kín. Tuy nhiên, các đối tượng gốc bị bẻ gãy hết. Lisp Thiep viết sau đây sẽ trích thửa bản đồ, theo 1 ô vuông có kích thước cạnh do user tự chọn.Sau khi tạo ô vuông xong, user rê ô vuông này vào khu vực cần trích thửa, nó sẽ copy các đối tượng bên trong và giao với ô vuông (ô vuông giống như 1 nam châm). Sau đó, user rê các đối tượng này đến vị trí cần đặt, ví dụ đặt ở giữa bản vẽ TRÍCH ĐO ĐỊA CHÍNH THỬA ĐẤT. Lisp sẽ cắt bỏ những đường bên ngoài ô vuông.
;;;----------------------- (defun SS-enlst (ss / c L) (setq c -1) (repeat (sslength ss) (setq L (cons (ssname ss (setq c (1+ c))) L)) ) (reverse L) ) ;;;==================================================================== (defun break_with (Lstent enL / lst masterlist ss oc break_obj intpts) (princ "\nCalculating Break Points, Please Wait.\n") ;;======================================== ;; Break entity at break points in list ;;======================================== (defun break_obj (ent brkptlst / brkobjlst en enttype maxparam closedobj minparam obj obj2break p1param p2param brkpt2 dlst idx brkptS brkptE brkpt result result ignore dist tmppt #ofpts enddist lastent obj2break stdist ) (setq obj2break ent brkobjlst (list ent) enttype (dxf 0 ent) ) (if (not (or (eq (dxf 0 obj2break) "TEXT") (eq (dxf 0 obj2break) "MTEXT") ) ) (setq closedobj (vlax-curve-isclosed obj2break)) ) (setq spt (vlax-curve-getstartpoint ent) ept (vlax-curve-getendpoint ent) brkptlst (vl-remove-if '(lambda (x) (or (< (distance x spt) 0.0001) (< (distance x ept) 0.0001) ) ) brkptlst ) ) (if (and brkptlst (not (or (eq (dxf 0 obj2break) "TEXT") (eq (dxf 0 obj2break) "MTEXT") ) ) ) (progn (setq brkptlst (mapcar '(lambda (x) (list x (vlax-curve-getdistatparam obj2break (cond ((vlax-curve-getparamatpoint obj2break x) ) ((vlax-curve-getparamatpoint obj2break (vlax-curve-getclosestpointto obj2break x ) ) ) ) ) ) ) brkptlst ) ) (setq brkptlst (vl-sort brkptlst '(lambda (a1 a2) (< (cadr a1) (cadr a2))) ) ) (foreach brkpt (reverse brkptlst) (setq brkptS (car brkpt) brkptE brkptS ) ;; get last entity created via break in case multiple breaks (if brkobjlst (progn (setq tmppt brkptS) ; use only one of the pair of breakpoints ;; if pt not on object x, switch objects (if (not (numberp (vl-catch-all-apply 'vlax-curve-getdistatpoint (list obj2break tmppt) ) ) ) (progn ; find the one that pt is on (setq idx (length brkobjlst)) (while (and (not (minusp (setq idx (1- idx)))) (setq obj (nth idx brkobjlst)) (if (numberp (vl-catch-all-apply 'vlax-curve-getdistatpoint (list obj tmppt) ) ) (null (setq obj2break obj)) ; switch objects, null causes exit t ) ) ) ) ) ) ); end (if brkobjlst ;;; Handle any objects that can not be used with the Break Command ;;; using one point, gap of 0.000001 is used (if (not (or (eq (dxf 0 obj2break) "TEXT") (eq (dxf 0 obj2break) "MTEXT") ) ) (setq closedobj (vlax-curve-isclosed obj2break)) ) ;;; single breakpoint ---------------------------------------------------- (if (and closedobj (not (setq brkptE (vlax-curve-getPointAtDist obj2break (+ (vlax-curve-getdistatparam obj2break ;;(vlax-curve-getparamatpoint obj2break brkpts)) 0.00001)))) ;; ver 2.0 fix (cond ((vlax-curve-getparamatpoint obj2break brkpts ) ) ((vlax-curve-getparamatpoint obj2break (vlax-curve-getclosestpointto obj2break brkpts ) ) ) ) ) 0.00001 ) ) ) ) ) (setq brkptE (vlax-curve-getPointAtDist obj2break (- (vlax-curve-getdistatparam obj2break (cond ((vlax-curve-getparamatpoint obj2break brkpts ) ) ((vlax-curve-getparamatpoint obj2break (vlax-curve-getclosestpointto obj2break brkpts ) ) ) ) ) 0.00001 ) ) ); end setq brkptE ); end fi (and closedobj ;; (if (null brkptE) (princ)) ; debug (setq LastEnt (GetLastEnt)) (if (not (or (eq (dxf 0 obj2break) "TEXT") (eq (dxf 0 obj2break) "MTEXT") ) ) (command "._break" obj2break "_non" (trans brkptS 0 1) "_non" (trans brkptE 0 1) ) ) (and (= "CIRCLE" enttype) (setq enttype "ARC")) (if (and (not closedobj) ; new object was created (not (equal LastEnt (entlast))) ) (setq brkobjlst (cons (entlast) brkobjlst)) ); end (if (and ); end (foreach brkpt );end progn brkptlst ); end if brkptlst ); defun break_obj ;;==================================== ;; CAB - get last entity in datatbase (defun GetLastEnt (/ ename result) (if (setq result (entlast)) (while (setq ename (entnext result)) (setq result ename) ) ) result ) ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;; S T A R T S U B R O U T I N E H E R E ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ (if (and Lstent enL) (progn ;; CREATE a list of entity & it's break points (foreach en Lstent ; check each object in Lstent (if (not (acet-layer-locked (dxf 8 en))) (progn (setq lst nil) ;; check for break pts with other objects in Lstentwith (if (and (not (equal en enint)) (setq intpts (acet-geom-intersectwith en enL 0)) ) (setq lst (append intpts lst)) ; entity w/ break points ) (if lst (setq masterlist (cons (cons en lst) masterlist) ) ) ) ) ) (princ "\nBreaking Objects.\n") (if masterlist (foreach obj2brk masterlist (break_obj (car obj2brk) (cdr obj2brk)) ) ) ) ) );end break_with ;;=========================================================================== ;; get all objects touching entities in the sscross ;; limited obj types to "LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE" ;; returns a list of enames ;;=========================================================================== (defun gettouching (en / ss lst lstb lstc objl) (and (setq objl (vlax-ename->vla-object en)) (setq ss (ssget "_A" (list (cons 0 "LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE" ) (cons 410 (getvar "ctab")) ) ) ) (setq lst (SS-enlst ss) lst (mapcar 'vlax-ename->vla-object lst)) (mapcar '(lambda (x) (if (not (vl-catch-all-error-p (vl-catch-all-apply '(lambda () (vlax-safearray->list (vlax-variant-value (vla-intersectwith objl x acextendnone) ) ) ) ) ) ) (setq lstc (cons (vlax-vla-object->ename x) lstc)) ) ) lst ) ) lstc ) ;;;------------------------------------------------ (defun LWP (Lpoint *Model* / PntArr) (setq PntArr (vlax-make-safearray vlax-vbDouble (cons 0 (1- (length Lpoint))) ) ) (vlax-safearray-fill PntArr Lpoint) (vla-AddLightWeightPolyline *Model* PntArr) ) ;;;------------------------------------------------ (defun DXF (code en) (cdr (assoc code (entget en)))) ;;;============================================================ ;;;=======================MAIN LISP============================ ;;;============================================================ (defun c:trichthua (/ ss p2 encur lstss1 emin emax p3 LenssBR) (setq ActDoc (vla-get-ActiveDocument (vlax-get-acad-object)) *Model* (vla-get-ModelSpace ActDoc) ) (vla-StartUndoMark ActDoc) (setq a (cond (a) (50) ) ) (setq olda a) (setq a (getreal (strcat "\nChon kich thuoc cat hinh vuong cat <" (rtos olda 2 1) "> : "))) (if (null a) (setq a olda)) (setq emin (list (car (getvar "extmin")) (cadr (getvar "extmin")))) (setvar "cecolor" "104") (setq lstp (list (car emin) (cadr emin) (+ (car emin) a) (cadr emin) (+ (car emin) a) (+ (cadr emin) a) (car emin) (+ (cadr emin) a) ) ) (vla-put-closed (LWP lstp *Model*) :vlax-True) (setq ss (ssadd (entlast) (ssadd))) (setq p2 (ACET-SS-DRAG-MOVE ss (list (car emin) (cadr emin)) "Chon vi tri bat dau trich thua: " ) ) (command ".move" ss "" emin p2) (setq encur (entlast) lstp (acet-geom-VERTEX-LIST encur)) (setq ss (ssdel encur (ssget "_CP" lstp))) (command ".copy" ss "" p2 p2) (setq p3 (ACET-SS-DRAG-MOVE (ssadd encur ss) p2 "Chon vi tri dat ban do trich thua: " ) ) (command ".move" ss encur "" p2 p3) (setvar "cecolor" "0") (setq encur (ssname (ssget "X" '((62 . 104))) 0)) (setq lstobj1 (vl-remove encur (gettouching encur)) ss (acet-list-to-ss lstobj1) ) (acet-ss-zoom-extents ss) (break_with lstobj1 encur) (vlax-invoke-method ActDoc 'Regen acActiveViewport) (vla-offset (vlax-ename->vla-object encur) (* (getvar "viewsize") 0.002)) (setq lst3 (acet-geom-vertex-list (entlast))) (entdel (entlast)) (setq LenssBR (SS-enlst (ssget "F" lst3))) (foreach x LenssBR (if (or (not (eq (dxf 0 x) "TEXT")) (not (eq (dxf 0 x) "MTEXT")) ) (entdel x) ) ) (vla-EndUndoMark ActDoc) (princ "\nChuc cac ban gat hai nhieu thanh cong. Thiep") )
Anh Thiep ơi.
Qua Lisp của anh đã viết, anh giúp em thêm việc này nha:
Dựa vào bản đồ DC , mình chọn vào vị trí thửa thì nó trích ra thửa đó, cắt vùng bao quanh, ghi tọa độ , cạnh và chèn vào mẫu khung.
như file mẫu:
Rất mong được anh giúp
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 74550_trichthua.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 TRICHTHUA để chạy lisp.
Mã nguồn 74550_trichthua.lsp (trích từ bài viết gốc)
;;;-----------------------
(defun SS-enlst (ss / c L)
(setq c -1)
(repeat (sslength ss)
(setq L (cons (ssname ss (setq c (1+ c))) L))
)
(reverse L)
)
;;;====================================================================
(defun break_with (Lstent enL / lst masterlist ss oc break_obj intpts)
(princ "\nCalculating Break Points, Please Wait.\n")
;;========================================
;; Break entity at break points in list
;;========================================
(defun break_obj (ent brkptlst / brkobjlst en
enttype maxparam closedobj minparam
obj obj2break p1param p2param
brkpt2 dlst idx brkptS
brkptE brkpt result result
ignore dist tmppt #ofpts
enddist lastent obj2break stdist
)
(setq obj2break ent
brkobjlst (list ent)
enttype (dxf 0 ent)
)
(if (not (or (eq (dxf 0 obj2break) "TEXT")
(eq (dxf 0 obj2break) "MTEXT")
)
)
(setq closedobj (vlax-curve-isclosed obj2break))
)
(setq spt (vlax-curve-getstartpoint ent)
ept (vlax-curve-getendpoint ent)
brkptlst (vl-remove-if
'(lambda (x)
(or (< (distance x spt) 0.0001)
(< (distance x ept) 0.0001)
)
)
brkptlst
)
)
(if (and brkptlst
(not (or (eq (dxf 0 obj2break) "TEXT")
(eq (dxf 0 obj2break) "MTEXT")
)
)
)
(progn
(setq brkptlst
(mapcar
'(lambda (x)
(list
x
(vlax-curve-getdistatparam
obj2break
(cond
((vlax-curve-getparamatpoint obj2break x)
)
((vlax-curve-getparamatpoint
obj2break
(vlax-curve-getclosestpointto
obj2break
x
)
)
)
)
)
)
)
brkptlst
)
)
(setq
brkptlst (vl-sort brkptlst
'(lambda (a1 a2) (< (cadr a1) (cadr a2)))
)
)
(foreach brkpt (reverse brkptlst)
(setq brkptS (car brkpt)
brkptE brkptS
)
;; get last entity created via break in case multiple breaks
(if brkobjlst
(progn
(setq tmppt brkptS) ; use only one of the pair of breakpoints
;; if pt not on object x, switch objects
(if (not (numberp (vl-catch-all-apply
'vlax-curve-getdistatpoint
(list obj2break tmppt)
)
)
)
(progn ; find the one that pt is on
(setq idx (length brkobjlst))
(while
(and (not (minusp (setq idx (1- idx))))
(setq obj (nth idx brkobjlst))
(if (numberp (vl-catch-all-apply
'vlax-curve-getdistatpoint
(list obj tmppt)
)
)
(null (setq obj2break obj))
; switch objects, null causes exit
t
)
)
)
)
)
)
); end (if brkobjlst
;;; Handle any objects that can not be used with the Break Command
;;; using one point, gap of 0.000001 is used
(if (not (or (eq (dxf 0 obj2break) "TEXT")
(eq (dxf 0 obj2break) "MTEXT")
)
)
(setq closedobj (vlax-curve-isclosed obj2break))
)
;;; single breakpoint ----------------------------------------------------
(if
(and closedobj
(not (setq
brkptE (vlax-curve-getPointAtDist
obj2break
(+ (vlax-curve-getdistatparam
obj2break
;;(vlax-curve-getparamatpoint obj2break brkpts)) 0.00001))))
;; ver 2.0 fix
(cond
((vlax-curve-getparamatpoint
obj2break
brkpts
)
)
((vlax-curve-getparamatpoint
obj2break
(vlax-curve-getclosestpointto
obj2break
brkpts
)
)
)
)
)
0.00001
)
)
)
)
)
(setq
brkptE (vlax-curve-getPointAtDist
obj2break
(- (vlax-curve-getdistatparam
obj2break
(cond ((vlax-curve-getparamatpoint
obj2break
brkpts
)
)
((vlax-curve-getparamatpoint
obj2break
(vlax-curve-getclosestpointto
obj2break
brkpts
)
)
)
)
)
0.00001
)
)
); end setq brkptE
); end fi (and closedobj
;; (if (null brkptE) (princ)) ; debug
(setq LastEnt (GetLastEnt))
(if (not (or (eq (dxf 0 obj2break) "TEXT")
(eq (dxf 0 obj2break) "MTEXT")
)
)
(command "._break"
obj2break
"_non"
(trans brkptS 0 1)
"_non"
(trans brkptE 0 1)
)
)
(and (= "CIRCLE" enttype) (setq enttype "ARC"))
(if (and (not closedobj) ; new object was created
(not (equal LastEnt (entlast)))
)
(setq brkobjlst (cons (entlast) brkobjlst))
); end (if (and
); end (foreach brkpt
);end progn brkptlst
); end if brkptlst
); defun break_obj
;;====================================
;; CAB - get last entity in datatbase
(defun GetLastEnt (/ ename result)
(if (setq result (entlast))
(while (setq ename (entnext result))
(setq result ename)
)
)
result
)
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;; S T A R T S U B R O U T I N E H E R E
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(if (and Lstent enL)
(progn
;; CREATE a list of entity & it's break points
(foreach en Lstent
; check each object in Lstent
(if (not (acet-layer-locked (dxf 8 en)))
(progn
(setq lst nil)
;; check for break pts with other objects in Lstentwith
(if (and (not (equal en enint))
(setq intpts (acet-geom-intersectwith en enL 0))
)
(setq lst (append intpts lst))
; entity w/ break points
)
(if lst
(setq masterlist
(cons (cons en lst) masterlist)
)
)
)
)
)
(princ "\nBreaking Objects.\n")
(if masterlist
(foreach obj2brk masterlist
(break_obj (car obj2brk) (cdr obj2brk))
)
)
)
)
);end break_with
;;===========================================================================
;; get all objects touching entities in the sscross
;; limited obj types to "LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE"
;; returns a list of enames
;;===========================================================================
(defun gettouching (en / ss lst lstb lstc objl)
(and
(setq objl (vlax-ename->vla-object en))
(setq
ss
(ssget
"_A"
(list
(cons 0
"LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE"
)
(cons 410 (getvar "ctab"))
)
)
)
(setq lst (SS-enlst ss)
lst (mapcar 'vlax-ename->vla-object lst))
(mapcar
'(lambda (x)
(if (not
(vl-catch-all-error-p
(vl-catch-all-apply
'(lambda ()
(vlax-safearray->list
(vlax-variant-value
(vla-intersectwith objl x acextendnone)
)
)
)
)
)
)
(setq lstc (cons (vlax-vla-object->ename x) lstc))
)
)
lst
)
)
lstc
)
;;;------------------------------------------------
(defun LWP (Lpoint *Model* / PntArr)
(setq PntArr (vlax-make-safearray
vlax-vbDouble
(cons 0 (1- (length Lpoint)))
)
)
(vlax-safearray-fill PntArr Lpoint)
(vla-AddLightWeightPolyline *Model* PntArr)
)
;;;------------------------------------------------
(defun DXF (code en) (cdr (assoc code (entget en))))
;;;============================================================
;;;=======================MAIN LISP============================
;;;============================================================
(defun c:trichthua (/ ss p2 encur lstss1 emin emax p3 LenssBR)
(setq ActDoc (vla-get-ActiveDocument (vlax-get-acad-object))
*Model* (vla-get-ModelSpace ActDoc)
)
(vla-StartUndoMark ActDoc)
(setq a (cond (a)
(50)
)
)
(setq olda a)
(setq a (getreal (strcat "\nChon kich thuoc cat hinh vuong cat <"
(rtos olda 2 1)
"> : ")))
(if (null a) (setq a olda))
(setq emin (list (car (getvar "extmin")) (cadr (getvar "extmin"))))
(setvar "cecolor" "104")
(setq lstp (list (car emin)
(cadr emin)
(+ (car emin) a)
(cadr emin)
(+ (car emin) a)
(+ (cadr emin) a)
(car emin)
(+ (cadr emin) a)
)
)
(vla-put-closed (LWP lstp *Model*) :vlax-True)
(setq ss (ssadd (entlast) (ssadd)))
(setq p2 (ACET-SS-DRAG-MOVE
ss
(list (car emin) (cadr emin))
"Chon vi tri bat dau trich thua: "
)
)
(command ".move" ss "" emin p2)
(setq encur (entlast)
lstp (acet-geom-VERTEX-LIST encur))
(setq ss (ssdel encur (ssget "_CP" lstp)))
(command ".copy" ss "" p2 p2)
(setq p3 (ACET-SS-DRAG-MOVE
(ssadd encur ss)
p2
"Chon vi tri dat ban do trich thua: "
)
)
(command ".move" ss encur "" p2 p3)
(setvar "cecolor" "0")
(setq encur (ssname (ssget "X" '((62 . 104))) 0))
(setq lstobj1 (vl-remove encur (gettouching encur))
ss (acet-list-to-ss lstobj1)
)
(acet-ss-zoom-extents ss)
(break_with lstobj1 encur)
(vlax-invoke-method ActDoc 'Regen acActiveViewport)
(vla-offset (vlax-ename->vla-object encur) (* (getvar "viewsize") 0.002))
(setq lst3 (acet-geom-vertex-list (entlast)))
(entdel (entlast))
(setq LenssBR (SS-enlst (ssget "F" lst3)))
(foreach x LenssBR
(if (or (not (eq (dxf 0 x) "TEXT"))
(not (eq (dxf 0 x) "MTEXT"))
)
(entdel x)
)
)
(vla-EndUndoMark ActDoc)
(princ "\nChuc cac ban gat hai nhieu thanh cong. Thiep")
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề trích do. Xem đúng bài viết #74550 để đọc hướng dẫn và trao đổi thêm.