Lisp lệnh DST trong chủ đề “Lisp đánh số thửa cho thửa đất”
Loay hoay cả tối qua mới ra được thế này, cũng chưa kịp xem qua gợi ý của Ssg. elle up lên để Ssg dòm lại qua 1 lượt vì mình viết ko được sáng sủa lắm, dần dần sẽ update sau.
Hiện giờ đòi hỏi chỉ cần có Closed_Polyline còn việc tìm Centroid và tính diện tích, đánh số thửa đã giải quyết xong:
file vd chỉ cần như vầy :
https://www.cadviet.com/upfiles/Out_2.dwg
Chắc tới thì Ssg làm thêm cho cái GUI để user chọn option vì elle cũng thử cái dialog bằng file DCL nhưng sao lisp xử lý chuối thế. Thiết kế 1 cái dialog vất thật !
Hiện giờ đòi hỏi chỉ cần có Closed_Polyline còn việc tìm Centroid và tính diện tích, đánh số thửa đã giải quyết xong:
file vd chỉ cần như vầy :
https://www.cadviet.com/upfiles/Out_2.dwg
Chắc tới thì Ssg làm thêm cho cái GUI để user chọn option vì elle cũng thử cái dialog bằng file DCL nhưng sao lisp xử lý chuối thế. Thiết kế 1 cái dialog vất thật !
- Tải lisp
- 44262_dst.lsp
Cách dùng
- Tải tệp 44262_dst.lsp về máy.
- 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 DST để chạy lisp.
Mã nguồn 44262_dst.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/8207-lisp-%C4%91%C3%A1nh-s%E1%BB%91-th%E1%BB%ADa-cho-th%E1%BB%ADa-%C4%91%E1%BA%A5t/
;;;*****************************************************************;;;
;;; Tks to Ssg & all Cadviet members who make lisp impressed me much;;;
;;;*****************************************************************;;;
;;;*****************************************************
;;; Danh so thua tu dong cho thua dat (Closed_Polyline)
(defun c:dst ( / ent tongsothua)
(command "Undo" "Begin")
(command "osnap" "none")
(Tao_lop)
(setq ent (ssget "X" (list (cons 8 "gioithua")(cons 0 "LWPOLYLINE"))))
(if(= ent nil)(progn (alert "Kh«ng t×m thÊy thöa, h·y t¹o Closed_PolyLine trªn layer 'GioiThua' !"))
(progn
(setq ent (ssget "X" (list (cons 8 "GioiThua")(cons 0 "LWPOLYLINE"))))
(if (/= nil ent) (setq tongsothua(sslength ent) ))
(danhsothua tongsothua)
(alert (strcat "Cã tæng sè " (itoa tongsothua) " thöa ®Êt ®· ®îc ®¸nh sè !" ))
);prog
);if
(command "Undo" "End")
)
;;;*****************************************************************************
**************
(defun danhsothua (tongsothua / ent List_cen List_sort List_ok i e1 e2)
(vl-load-com)
(setq ent (ssget "X" (list (cons 8 "GioiThua")(cons 0 "LWPOLYLINE"))))
(VE_CENTROID_DIENTICH ent)
(command "layer" "s" "SoThua" "")
; Buoc 1 - sort kieu descending cho point CentroID.Y (X trac dia)
(setq list_sort (vl-sort list_cen (function (lambda (e1 e2)(> (cadr e1)(cadr e2))))) )
; Buoc 2 - sort kieu ascending point CentroID tu trai sang phai truc X (Y trac dia)
(sort_trai_phai tongsothua list_sort)
; Ve so thua ra ban ve;
(setq i 0)
(while (< i tongsothua)
(wtxt2 (itoa (1+ i)) (nth i List_ok) "SoThua" "SoThua" 1 "C" "B" )
(setq i (1+ i))
)
)
;;;*****************************************************************************
**************
(defun VE_CENTROID_DIENTICH (ent / ent_gioithua i et Centroid Obj-vla dientich)
(command "layer" "s" "tmp_region" "")
(setq ent_gioithua (command "copy" ent "" (list 0 0) (list 0 0)) ) ; Luu lai ent tren layer gioithua;
(command "region" ent "")
(setq ent (ssget "X" (list (cons 8 "tmp_region")(cons 0 "REGION"))))
(command "layer" "s" "DienTich" "")
(setq i 0)
(while (< i (sslength ent))
(setq et (ssname ent i))
(setq Obj-vla (vlax-ename->vla-object et))
(setq centroid (vlax-safearray->list (vlax-variant-value (vla-get-centroid Obj-vla))))
(setq dientich (vla-get-area Obj-vla))
(wtxt2 (rtos dientich 2 1) centroid "DienTich" "DienTich" 1 "C" "T" )
(make_point centroid "CentroID")
(setq List_cen(append List_cen (list centroid)))
(setq i (1+ i))
)
(command "erase" ent "")
)
;;;*****************************************************************************
**************
(defun sort_trai_phai (tongsothua list_sort / list_tmp step sp ep m k p last_point chan dn)
(setq step (getreal "\n Nhap do rong cua giai danh so thua <20> : "))
(if (= step nil) (setq step 20) )
(setq
m (- tongsothua 1)
sp (+ (cadr(nth 0 list_sort)) (* step 0.5)) ; sp.Y + step*0.5
ep (cadr (nth m list_sort)) ; ep.Y
;num_down (+ (fix(/(- sp ep)step)) 1)
last_point (list (car(nth m list_sort)) (cadr(nth m list_sort)) )
chan 0 ; neu chan danh tu Trai sang phai, neu le danh tu Phai sang trai
dn 0
)
(while (> sp ep)
(setq k 0)
(setq list_tmp nil)
(while (< k m)
(if (and (<=(cadr(nth k list_sort)) sp) (>(cadr(nth k list_sort)) (- sp step)) )
(progn
(setq p (list (car(nth k list_sort)) (cadr(nth k list_sort))))
(setq List_tmp (append List_tmp (list p) ) )
)
)
(setq k (1+ k))
);while k
(setq dn (1+ dn))
(setq chan(rem dn 2))
(if (= chan 0)
(setq list_tmp (vl-sort list_tmp (function (lambda (e1 e2)(> (car e1)(car e2))))) ) ; chan
(setq list_tmp (vl-sort list_tmp (function (lambda (e1 e2)(< (car e1)(car e2))))) ) ; le
);if chan
(setq List_ok (append List_ok list_tmp))
(setq sp (- sp step))
);while sp;
(setq List_ok (append List_ok (list last_point) ) )
)
;;;*****************************************************************************
**************
(defun wtxt2 (txt p lay sty k hj vj / d h) ;;;Write txt on graphic screen (from Ssg - Cadviet forum)
;;;Specify txt, point, layer, style, scale factor, hor justify, ver justify
(setq
d (tblsearch "style" sty)
h (cdr (assoc 40 d))
LH (list "L" "C" "R") ;;;Left, Center, Right
LV (list "" "B" "M" "T") ;;;Bottom, Middle, Top
hcode (vl-position hj LH)
vcode (vl-position vj LV)
)
(if (= h 0) (setq h (cdr (assoc 42 d))))
(entmake
(list (cons 0 "TEXT") (cons 8 lay) (cons 7 sty) (cons 1 txt) (cons 10 p) (cons 11 p)
(cons 40 (* k h)) (assoc 41 d) (cons 72 hcode) (cons 73 vcode))
)
)
;;;*****************************************************************************
**************
(defun make_point (P Layer)
(entmake
(List
(cons 0 "POINT")
(cons 8 Layer)
(cons 10 P)
)
)
)
;;;*****************************************************************************
**************
(defun Tao_lop ( / tbl)
(setq tbl (TBLSEARCH "LAYER" "SoThua" ))
(if (= tbl nil)(command "layer" "n" "SoThua" "c" "Yellow" "SoThua" ""))
(setq tbl (TBLSEARCH "style" "SoThua" ))
(if (= tbl nil)(command "style" "SoThua" "romans.shx" "1.2" "0.8" "" "" "" ""))
(setq tbl (TBLSEARCH "LAYER" "CentroID" ))
(if (= tbl nil)(command "layer" "n" "CentroID" "c" "white" "CentroID" ""))
(setq tbl (TBLSEARCH "LAYER" "tmp_region" ))
(if (= tbl nil)(command "layer" "n" "tmp_region" "c" "8" "tmp_region" ""))
(setq tbl (TBLSEARCH "LAYER" "DienTich" ))
(if (= tbl nil)(command "layer" "n" "DienTich" "c" "Yellow" "DienTich" ""))
(setq tbl (TBLSEARCH "style" "DienTich" ))
(if (= tbl nil)(command "style" "DienTich" "romans.shx" "1.2" "0.8" "" "" "" ""))
)
;;;*****************************************************************************
**************Thảo luận gốc
Lisp được chia sẻ trong chủ đề Lisp đánh số thửa cho thửa đất. Xem đúng bài viết #44262 để đọc hướng dẫn và trao đổi thêm.