Lisp lệnh VETUONG VT VC trong chủ đề “Viết lisp theo yêu cầu [phần 2]”
Chết thật hôm up lộn code rồi xin lỗi các bạn. Các bác test lại xem.
Bạn cho mình hỏi code này sai ở chỗ nào mà nó báo lỗi (setq tldim (last (assoc 40 (tblsearch "dimstyle" "1")))). Mình có dim tên là 1 và muốn lấy overall scale của nó. mình dùng mã 140 lấy cao chũ cũng không được.
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 109620_vetuong_vt_vc.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 VETUONG VT VC để chạy lisp.
Mã nguồn 109620_vetuong_vt_vc.lsp (trích từ bài viết gốc)
;; free lisp from cadviet.com
(defun c:vetuong ()
(command "trim" "" "e" "e" "p" "n" "")
(command "undo" "be")
(setvar "cmdecho" 0)
(setq old_layer (getvar "clayer"))
(setq snap (getvar "osmode"))
(setq hl (getvar "highlight"))
(setq tbl (tblsearch "layer" "tuong"))
(if (= tbl nil) (command "-layer" "n" "tuong" "c" "4" "tuong" ""))
(setq tbl (tblsearch "layer" "_tim"))
(if (= tbl nil) (command "-layer" "n" "_tim" "c" "8" "_tim" "l" "center" "_tim" ""))
(setq tbl (tblsearch "layer" "template"))
(if (= tbl nil) (command "-layer" "n" "template" ""))
(setvar "clayer" "template")
(setvar "osmode" 0)
(setq ss (ssget '((0 . "line"))))
(setq day (cond (day) (220)))
(setq oldday day)
(setq day (getint (strcat "\nnhap chieu day tuong <"(rtos oldday 2 1)"> : ")))
(if (null day)
(setq day oldday)
)
(setq day1 (/ (* day 7) 15))
(command "change" ss "" "p" "la" "_tim" "")
(setq i 0)
(setq lp nil)
(setq ssml nil)
(setq ssml (ssadd))
(while (< i (sslength ss))
(setq name (ssname ss i)
ent (entget name)
p1 (cdr (assoc 10 ent))
p2 (cdr (assoc 11 ent))
lp (append (list (append (list p1) (list p2))) lp)
)
(command "mline" "j" "z" "s" day p1 p2 "")
(command "explode" "l")
(setq ssline (ssget "p"))
(setq line1 (ssname ssline 0))
(setq line2 (ssname ssline 1))
(setq ssml (ssadd line1 (ssadd line2 ssml)))
(setq i (1+ i))
)
(command "-layer" "off" "_tim" "")
(setq j 0)
(setvar "highlight" 0)
(while (< j (length lp))
(setq nhom (nth j lp)
pt1 (car nhom)
pt2 (last nhom)
angf (+ (angle pt1 pt2) (/ pi 2))
t1 (polar pt1 angf day1)
t2 (polar pt1 (+ angf pi) day1)
t3 (polar pt2 (+ angf pi) day1)
t4 (polar pt2 angf day1)
)
(command "trim" ssml "" "f" t1 t2 t3 t4 t1 "" "")
(setq j (1+ j))
)
(setq q 0)
(setq ssml (ssget "x" '((0 . "line") (8 . "template"))))
(while (< q (sslength ssml))
(setq l1 (ssname ssml q)
pf1 (cdr (assoc 10 (entget l1)))
pf2 (cdr (assoc 11 (entget l1)))
)
(setq k 0)
(while (< k (sslength ssml))
(setq l2 (ssname ssml k)
pf3 (cdr (assoc 10 (entget l2)))
pf4 (cdr (assoc 11 (entget l2)))
d1 (distance pf1 pf3)
d2 (distance pf1 pf4)
d3 (distance pf2 pf3)
d4 (distance pf2 pf4)
)
(if (or (and (< (fix d1) (* day1 2)) (> d1 0)) (and (< (fix d2) (* day1 2)) (> d2 0)) (and (< (fix d3) (* day1 2)) (> d3 0))
(and (< (fix d4) (* day1 2)) (> d4 0)))
(command "fillet" l1 l2)
)
(setq k (1+ k))
)
(setq q (1+ q))
)
(command "change" ssml "" "p" "la" "tuong" "")
(dimtuong)
(setvar "clayer" old_layer)
(command "-layer" "on" "_tim" "")
(setvar "osmode" snap)
(setvar "highlight" hl)
(setvar "cmdecho" 1)
(command "undo" "e")
(command "trim" "" "e" "n" "")
)
;******************************************
(defun c:vt ()
(command "trim" "" "e" "e" "p" "n" "")
(command "undo" "be")
(setvar "cmdecho" 0)
(setq old_layer (getvar "clayer"))
(setq snap (getvar "osmode"))
(setvar "clayer" "tuong")
(setq dt (cond (dt) (220)))
(setq olddt dt)
(setq dt (getint (strcat "\nrong tuong <"(rtos olddt 2 1)"> : ")))
(if (null dt)
(setq dt olddt)
)
(setq pt1 (getpoint "\ndiem thu nhat:")
pt2 (getpoint "\ndiem thu hai:" pt1)
)
(setvar "osmode" 0)
(command "-layer" "off" "_tim" "")
(command "mline" "j" "z" "s" dt pt1 pt2 "")
(setq mll (entlast))
(command "trim" mll "" "f" pt1 pt2 "" "")
(command "explode" mll)
(setvar "osmode" snap)
(setvar "clayer" old_layer)
(command "-layer" "on" "_tim" "")
(setvar "cmdecho" 1)
(command "trim" "" "e" "n" "")
)
;*****************************************
(defun c:vc ()
(setvar "cmdecho" 0)
(setq old_layer (getvar "clayer"))
(setq snap (getvar "osmode"))
(setvar "clayer" "tuong")
(setq cua (cond (cua) (900)))
(setq oldcua cua)
(setq cua (getint (strcat "\nrong cua <"(rtos oldcua 2 1)"> : ")))
(if (null cua)
(setq cua oldcua)
)
(setq pc1 (getpoint "\ndiem thu nhat:")
pc2 (getpoint "\ndiem thu hai:" pc1)
ang (+ (angle pc1 pc2) (/ (* 90 pi) 180))
pc3 (polar pc1 ang (+ 110 (/ cua 2)))
pc4 (polar pc2 ang (+ 110 (/ cua 2)))
)
(setvar "osmode" 0)
(command "-layer" "off" "_tim" "")
(command "mline" "j" "z" "s" cua pc3 pc4 "")
(setq mll (entlast))
(command "trim" mll "" "f" pc3 pc4 "" "")
(command "explode" mll)
(setvar "osmode" snap)
(setvar "clayer" old_layer)
(command "-layer" "on" "_tim" "")
(setvar "cmdecho" 1)
)
;*****************************************
(defun dimtuong ()
(setq tbl (tblsearch "layer" "_dim"))
(if (= tbl nil) (command "-layer" "n" "_dim" "c" "8" "_dim" ""))
(setvar "clayer" "_dim")
(setq a 0)
(setq lpx nil)
(setq lpy nil)
(while (< a (length lp))
(setq px1 (car (car (nth a lp)))
py1 (cadr (car (nth a lp)))
px2 (car (cadr (nth a lp)))
py2 (cadr (cadr (nth a lp)))
)
(if (/= py1 py2)
(setq lpx (append (list px1) lpx))
)
(if (= py1 py2)
(setq lpy (append (list py1) lpy))
)
(setq a (1+ a))
)
(setq lpx (vl-sort lpx '<))
(setq lpy (vl-sort lpy '<))
(setq dc (- (last lpx) (car lpx)))
(setq pdi (list (car lpx) (- (car lpy) (/ dc 30)) 0))
(setq pdim (polar pdi pi (/ day 2)))
(setq pdim1 (polar pdi (/ (* pi 3) 2) (/ dc 15)))
(command "_dimlinear" pdi pdim pdim1)
(setq c 0)
(while (< (1+ c) (length lpx))
(setq y (cadr pdim)
x1 (nth c lpx)
x2 (nth (1+ c) lpx)
dd1 (list x1 y 0)
dd2 (list x2 y 0)
)
(command "_dimlinear" dd1 dd2 pdim1)
(setq c (1+ c))
)
(setq pcc (polar dd2 0 (/ day 2)))
(command "_dimlinear" dd2 pcc pdim1)
(command "_dimlinear" pdim pcc (polar pdim1 (/ (* pi 3) 2) (/ dc 40)))
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Viết lisp theo yêu cầu [phần 2]. Xem đúng bài viết #109620 để đọc hướng dẫn và trao đổi thêm.