Lisp lệnh P2T trong chủ đề “Viết Lisp theo yêu cầu”
Chưa có ai giúp chắc vì còn Tết.
Lisp num.lsp này cho phép đánh cả Chử + Số +Chử (TD: X1a, X2a, X3a...)
Nếu thêm được vào option để cho phép đánh theo cấp số nhân thì hay hơn (TD: +5m, +10m, +15m...)
Lệnh NUM
;; ============================================================= ;;
;; ;;
;; NUM.LSP - This program for fast dynamic numbering. To number a ;;
;; suffix and a prefix can be added. ;;
;; ;;
;; ==================================================================== ;;
;; ;;
;; Command(s) to call: NUM ;;
;; ;;
;; Specify the text size, a suffix, a prefix and starting number ;;
;; (for erase the old suffix or prefix you should press Spacebar). ;;
;; Insert a numbers or press Esc to quit. The program remembers old ;;
;; properties and it is possible to confirm it pressing of Spacebar ;;
;; key. ;;
;; ;;
;; ==================================================================== ;;
;; ;;
;; THIS PROGRAM AND PARTS OF IT MAY REPRODUCED BY ANY METHOD ON ANY ;;
;; MEDIUM FOR ANY REASON. YOU CAN USE OR MODIFY THIS PROGRAM OR ;;
;; PARTS OF IT ABSOLUTELY FREE. ;;
;; ;;
;; THIS PROGRAM PROVIDES THIS PROGRAM 'AS IS' WITH ALL FAULTS AND ;;
;; SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY ;;
;; OR FITNESS FOR A PARTICULAR USE. ;;
;; ;;
;; ==================================================================== ;;
;; ;;
;; V1.3, 12 May, 2005, Riga, Latvia ;;
;; © Aleksandr Smirnov (ASMI) ;;
;; For AutoCAD 2000 - 2008 (isn't tested in a next versions) ;;
;; ;;
;; http://www.asmitools.com ;;
;; ;;
;; ==================================================================== ;;
(defun c:num (/ oldPref oldSuf oldStart curStr newNum
actDoc actSp oldEcho oldSize *error*)
(defun *error* (msg)
(setvar "CMDECHO" oldEcho)
(princ)
); end *error*
(vl-load-com)
(if(not num:Size)(setq num:Size(getvar "DIMTXT")))
(if(not num:Pref)(setq num:Pref ""))
(if(not num:Suf)(setq num:Suf ""))
(if(not num:Num)(setq num:Num 1))
(setq oldPref num:Pref
oldSuf num:Suf
oldStart num:Num
oldSize num:Size
actDoc(vla-get-ActiveDocument
(vlax-get-acad-object))
oldEcho(getvar "CMDECHO")
); end setq
(setvar "CMDECHO" 0)
(if(=(vla-get-ActiveSpace actDoc)1)
(setq actSp(vla-get-ModelSpace actDoc))
(setq actSp(vla-get-PaperSpace actDoc))
); end if
(setq num:Size
(getreal
(strcat "\nText size <"(rtos num:Size)">: ")))
(if(null num:Size)(setq num:Size oldSize))
(setq num:Pref
(getstring T
(strcat "\nPrefix: <"num:Pref">: ")))
(if(= "" num:Pref)(setq num:Pref oldPref))
(if(= " " num:Pref)(setq num:Pref ""))
(setq num:Suf
(getstring T
(strcat "\nSuffix: <"num:Suf">: ")))
(if(= "" num:Suf)(setq num:Suf oldSuf))
(if(= " " num:Suf)(setq num:Suf ""))
(setq num:Num
(getint
(strcat "\nStarting number <"(itoa num:Num)">: ")))
(if(null num:Num)(setq num:Num oldStart))
(princ "\n<<< Insert numbers or press Esc to quit >>> ")
(while T
(setq curStr(strcat num:Pref(itoa num:Num)num:Suf)
newNum(vla-AddText actSp
curStr (vlax-3d-point '(0.0 0.0 0.0)) num:Size))
(vla-put-Alignment newNum acAlignmentMiddleCenter)
(command "_.copybase"(trans '(0.0 0.0 0.0)0 1)(entlast)"")
(command "_.erase" (entlast) "")
(command "_.pasteclip" pause)
(setq num:Num(1+ num:Num))
); end while
(princ)
); end of c:num
(princ "\n*** Dynamic numbering tool. Type NUM to run.*** ")
... nhưng phải select từng point, không được như P2T.lsp
LISP P2T.lsp của CADViet chỉ đánh Số thứ tự từ Trái qua Phải và Trên xuống Duới. Nhờ các Bác bổ sung thêm các options để:+ Cho phép đánh cả Chử + Số +Chử (TD: X1a, X2a, X3a...)
+ Theo hướng user chọn
+ Dọc theo Line/Pline đi qua các points.
Thanks you.
(defun c:P2T (/ sst lstent pp p soht strht) (defun ss2ent (ss / sodt index lstent) (princ "\nCADViet.com © 2007") (setq sodt (cond (ss (sslength ss)) (t 0) ) index 0 ) (repeat sodt (setq ent (ssname ss index) index (1+ index) lstent (cons ent lstent) ) ) (reverse lstent) ) (defun getp (ent) (cdr (assoc 10 (entget ent))) ) (defun sosanh (ent1 ent2 / p1 p2 x1 y1 x2 y2) (setq p1 (getp ent1) x1 (car p1) y1 (cadr p1) p2 (getp ent2) x2 (car p2) y2 (cadr p2) ) (or (< x1 x2) (and (= x1 x2) (< y1 y2)) ) ) ;;------------- Main ------------------------------- (princ "\nPoint to Text © 2007 CADViet.com") (setq sst (ssget '((0 . "POINT"))) caochu (getdist "\nVao chieu cao text: ") soht (1- (getint "\nVao so bat dau danh: ")) lstent (vl-sort (ss2ent sst) 'sosanh) ) (foreach pp lstent (setq soht (1+ soht) strht (itoa soht) p (getp pp) ) (entmake (list (cons 0 "TEXT") (cons 10 P) (cons 40 caochu) (cons 1 strht) ) ) ) (princ) ) (princ "\nSu dung lenh P2T bat dau") (princ "\nfree lisp from www.cadviet.com") (princ)
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 49241_p2t.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 P2T để chạy lisp.
Mã nguồn 49241_p2t.lsp (trích từ bài viết gốc)
;; ============================================================= ;;
;; ;;
;; NUM.LSP - This program for fast dynamic numbering. To number a ;;
;; suffix and a prefix can be added. ;;
;; ;;
;; ==================================================================== ;;
;; ;;
;; Command(s) to call: NUM ;;
;; ;;
;; Specify the text size, a suffix, a prefix and starting number ;;
;; (for erase the old suffix or prefix you should press Spacebar). ;;
;; Insert a numbers or press Esc to quit. The program remembers old ;;
;; properties and it is possible to confirm it pressing of Spacebar ;;
;; key. ;;
;; ;;
;; ==================================================================== ;;
;; ;;
;; THIS PROGRAM AND PARTS OF IT MAY REPRODUCED BY ANY METHOD ON ANY ;;
;; MEDIUM FOR ANY REASON. YOU CAN USE OR MODIFY THIS PROGRAM OR ;;
;; PARTS OF IT ABSOLUTELY FREE. ;;
;; ;;
;; THIS PROGRAM PROVIDES THIS PROGRAM 'AS IS' WITH ALL FAULTS AND ;;
;; SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY ;;
;; OR FITNESS FOR A PARTICULAR USE. ;;
;; ;;
;; ==================================================================== ;;
;; ;;
;; V1.3, 12 May, 2005, Riga, Latvia ;;
;; © Aleksandr Smirnov (ASMI) ;;
;; For AutoCAD 2000 - 2008 (isn't tested in a next versions) ;;
;; ;;
;; [url="http://www.asmitools.com"]http://www.asmitools.com[/url] ;;
;; ;;
;; ==================================================================== ;;
(defun c:num (/ oldPref oldSuf oldStart curStr newNum
actDoc actSp oldEcho oldSize *error*)
(defun *error* (msg)
(setvar "CMDECHO" oldEcho)
(princ)
); end *error*
(vl-load-com)
(if(not num:Size)(setq num:Size(getvar "DIMTXT")))
(if(not num:Pref)(setq num:Pref ""))
(if(not num:Suf)(setq num:Suf ""))
(if(not num:Num)(setq num:Num 1))
(setq oldPref num:Pref
oldSuf num:Suf
oldStart num:Num
oldSize num:Size
actDoc(vla-get-ActiveDocument
(vlax-get-acad-object))
oldEcho(getvar "CMDECHO")
); end setq
(setvar "CMDECHO" 0)
(if(=(vla-get-ActiveSpace actDoc)1)
(setq actSp(vla-get-ModelSpace actDoc))
(setq actSp(vla-get-PaperSpace actDoc))
); end if
(setq num:Size
(getreal
(strcat "\nText size <"(rtos num:Size)">: ")))
(if(null num:Size)(setq num:Size oldSize))
(setq num:Pref
(getstring T
(strcat "\nPrefix: <"num:Pref">: ")))
(if(= "" num:Pref)(setq num:Pref oldPref))
(if(= " " num:Pref)(setq num:Pref ""))
(setq num:Suf
(getstring T
(strcat "\nSuffix: <"num:Suf">: ")))
(if(= "" num:Suf)(setq num:Suf oldSuf))
(if(= " " num:Suf)(setq num:Suf ""))
(setq num:Num
(getint
(strcat "\nStarting number <"(itoa num:Num)">: ")))
(if(null num:Num)(setq num:Num oldStart))
(princ "\n<<< Insert numbers or press Esc to quit >>> ")
(while T
(setq curStr(strcat num:Pref(itoa num:Num)num:Suf)
newNum(vla-AddText actSp
curStr (vlax-3d-point '(0.0 0.0 0.0)) num:Size))
(vla-put-Alignment newNum acAlignmentMiddleCenter)
(command "_.copybase"(trans '(0.0 0.0 0.0)0 1)(entlast)"")
(command "_.erase" (entlast) "")
(command "_.pasteclip" pause)
(setq num:Num(1+ num:Num))
); end while
(princ)
); end of c:num
(princ "\n*** Dynamic numbering tool. Type NUM to run.*** ")
(defun c:P2T (/ sst lstent pp p soht strht)
(defun ss2ent (ss / sodt index lstent)
(princ "\nCADViet.com © 2007")
(setq
sodt (cond
(ss (sslength ss))
(t 0)
)
index 0
)
(repeat sodt
(setq ent (ssname ss index)
index (1+ index)
lstent (cons ent lstent)
)
)
(reverse lstent)
)
(defun getp (ent)
(cdr (assoc 10 (entget ent)))
)
(defun sosanh (ent1 ent2 / p1 p2 x1 y1 x2 y2)
(setq
p1 (getp ent1)
x1 (car p1)
y1 (cadr p1)
p2 (getp ent2)
x2 (car p2)
y2 (cadr p2)
)
(or (< x1 x2)
(and (= x1 x2) (< y1 y2))
)
)
;;------------- Main -------------------------------
(princ "\nPoint to Text © 2007 CADViet.com")
(setq
sst (ssget '((0 . "POINT")))
caochu (getdist "\nVao chieu cao text: ")
soht (1- (getint "\nVao so bat dau danh: "))
lstent (vl-sort (ss2ent sst) 'sosanh)
)
(foreach pp lstent
(setq
soht (1+ soht)
strht (itoa soht)
p (getp pp)
)
(entmake
(list
(cons 0 "TEXT")
(cons 10 P)
(cons 40 caochu)
(cons 1 strht)
)
)
)
(princ)
)
(princ "\nSu dung lenh P2T bat dau")
(princ "\nfree lisp from www.cadviet.com")
(princ)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Viết Lisp theo yêu cầu. Xem đúng bài viết #49241 để đọc hướng dẫn và trao đổi thêm.