Jump to content

Lisp lệnh CAT trong chủ đề “nhờ các cao thủ viết dùm lisp hatch,ve thang cat.”

hatch:

lisp 1:khi nhập lệnh h thì tự chuyển layer hatch và xuất hiện hợp thoại hatch cho chọn vật liệu và hatch bình thường như lệch hatch của cad.

lisp 2:khi nhập lệnh hh thì tự chuyển layer hatch và cho ta hatch = mặt cắt solid (lưu ý:không xuất hiện hộp thoại hatch,chỉ cần nhập lệnh tự chuyển layer hatch,pick chon miền,enter->được hatch solid)

 

vẽ thang cắt:

khi nhập lệnh,pick 2 điểm cad tự tính ra chiều cao của thang,pick điểm bắt đầu,nhập bề rộng bậc và số bật->ta duoc thang cắt (lưu ý:vì pick 2 điểm(chiều cao của tầng)nên chiều cao thang có thể lẽ nên lisp này là tối ưu nhất)

Bạn có thể dùng cái vẽ mặt vắt thang này do mình viết.

Mình là KTS giống như bao KTS khác đều ngại vẽ mặt cắt cầu thang. Chính vì vậy mình muốn xây dựng lisp để làm công việc này được nhanh chóng thuận lợi.

Mình đã xây dựng được code như thế này. Mong các bạn đóng góp ý kiến thêm để mình hoàn chỉnh.

thang.jpg

đây là code

(defun c:cat ()
(setvar "cmdecho" 0)
(setq old_layer (getvar "clayer"))
(setq tbl (tblsearch "layer" "_cat"))
(if (= tbl nil) (command "-layer" "n" "_cat" "c" "4" "_cat" ""))
(setq tbl1 (tblsearch "layer" "_hatch"))
(if (= tbl1 nil) (command "-layer" "n" "_hatch" "c" "8" "_hatch" ""))
(setq tbl2 (tblsearch "layer" "_thay"))
(if (= tbl2 nil) (command "-layer" "n" "_thay" "c" "23" "_thay" ""))
(setvar "clayer" "_cat")
(setq pc1 (getpoint "\nChon diem thu nhat: "))
(setq pc2 (getpoint "\nChon diem thu hai: " pc1))
(command "-view" "s" "v1")
(setq p (getpoint "\nChon diem de ve mat cat: "))
(command "-view" "s" "v2")
(setq ct (getreal "\nVao chieu cao tang: "))
(setq sb1 (getint "\nVao so bac: "))
(command "-view" "r" "v1")
(setq catb (ssget "f" (list pc1 pc2)))
(setq i 0)
(setq listpoint nil)
(setq listbac nil)
(while (< i (sslength catb))
(setq n (ssname catb i))
(setq pl1 (cdr (assoc 10 (entget n))))
(setq pl2 (cdr (assoc 11 (entget n))))
(setq pcat (inters pc1 pc2 pl1 pl2))
(setq listpoint (append listpoint (list pcat)))
(setq i (1+ i))
)
(setq i1 0)
(setq sb 0)
(while (< (1+ i1) (length listpoint))
(setq po1 (nth i1 listpoint))
(setq po2 (nth (1+ i1) listpoint))
(setq bac (distance po1 po2))
(setq listbac (append listbac (list bac)))
(if (equal (nth 0 (reverse listbac)) (nth 1 (reverse listbac)))
(setq sb (+ i1 2))
)
(setq i1 (1+ i1))
)
(vethangthang)
)

(defun vethangthang ()
(setq nb 20.0 bk 10.0
MBTong1 "ANSI32" tl1 100 angh1 0
MBTong2 "ar-conc" tl2 10 angh2 0
MBTong3 "ANSI31" tl3 200 angh3 0
MBTong4 "ar-sand" tl4 4 angh4 0
r (car listbac)
c (/ ct sb1)
d 100
oldos (getvar "osmode")
di (* sb (sqrt (+ (* c c) (* r r))))
ang (atan (/ c r))
p01 (polar p 0 10)
p02 (polar p01 (/ (* 270 pi) 180) 20)
p03 (polar p02 ang (/ 20 (sin ang)))
p2 (polar p02 ang di)
p22 (polar p2 (/ (* 90 pi) 180) 20)
p222 (polar p22 (/ (* 180 pi) 180) 10)
p3 (polar p03 0 (/ d (sin ang)))
p33 (polar p02 0 (/ d (sin ang)))
p4 (polar p2 (/ (* 3 pi) 2) (/ d (cos ang))) 
dibt (/ 10 (cos ang))
pbt1 p02
pbt3 (polar p02 ang (/ (distance p02 p2) sb) ) 
pbt2 (list (car pbt1) (cadr pbt3) 0)
pbt4 (polar pbt2 (/ (* 90 pi) 180) 10)
)
(setvar "osmode" 0 )
(command "-view" "r" "v2")
(command ".pline")
(command p)
(repeat sb
(command
(strcat "@0," (rtos (- c (* 2.0 bk))))
(strcat "@" (rtos (- bk nb)) ",0")
"a"
(strcat "@0," (rtos (* 2.0 bk)))
"l"
(strcat "@" (rtos (+ (- nb bk) r)) ",0")
)
)
(command "")
(setvar "cmdecho" 0)
(command "line" p p01 "")
(setq el4 (entlast))
(command "pline" pbt1 pbt2 pbt3 "C")
(setq eL1 (entlast))
(command "hatch" MBTong3 tl3 angh3 eL1 "")
(command "change" "l" "" "p" "la" "_hatch" "")
(setq eL2 (entlast))
(command "line" pbt3 (list (car p) (cadr pbt3) 0) "")
(setq eL3 (entlast))
(command "ucs" "z" p02 p2)
(command "ARray" el1 el2 el3 el4 "" "R" "1" sb (/ di sb))
(command "ucs" "")
(command "-BOUNDARY" pbt4 "")
(setq eL5 (entlast))
(command "rectang" p pbt2)
(setq eL6 (entlast))
(command "hatch" MBTong4 tl4 angh4 eL5 "")
(command "change" "l" "" "p" "la" "_hatch" "")
(setq eL8 (entlast))
(command "hatch" MBTong4 tl4 angh4 el6 "")
(command "change" "l" "" "p" "la" "_hatch" "")
(setq eL7 (entlast))
(command "ucs" "z" p02 p2)
(command "ARray" eL5 eL6 el7 el8 "" "R" "1" sb (/ di sb))
(command "ucs" "")
(vethangcong)
(setvar "osmode" oldos)
(setvar "clayer" old_layer)
(princ)
) 

(defun vethangcong ()
(setq goc01 (polar p ang di))
(setq i2 sb)
(setq ptt goc01)
(while (< i2 (length listbac))
(setq rbc (nth i2 listbac))
(command ".pline")
(command ptt)
(command
(strcat "@0," (rtos (- c (* 2.0 bk))))
(strcat "@" (rtos (- bk nb)) ",0")
"a"
(strcat "@0," (rtos (* 2.0 bk)))
"l"
(strcat "@" (rtos (+ 10 rbc)) ",0")
""
)
(setq ptt1 (polar ptt (/ (* 90 pi) 180) (- c (* 2.0 bk))))
(setq ptt2 (polar ptt1 0 10))
(setq ptt3 (polar ptt2 (/ (* 90 pi) 180) 10))
(command "rectang" ptt ptt2)
(command "hatch" MBTong4 tl4 angh4 "l" "")
(command "change" "l" "" "p" "la" "_hatch" "")
(command ".pline")
(command ptt1)
(command
(strcat "@" (rtos (+ 10 rbc)) ",0")
(strcat "@0," (rtos (* 2.0 bk)))
(strcat "@" (rtos (- bk nb)) ",0")
""
)
(setq ptt (getvar "lastpoint"))
(command "-hatch" ptt3 "")
(command "change" "l" "" "p" "la" "_hatch" "")
(setq i2 (1+ i2))
)
(setq pocuoi (polar ptt (/ (* 270 pi) 180) (/ (* c 2) 3)))
(setq pocuoi1 (polar pocuoi (/ (* 270 pi) 180) (/ (* c 2) 3)))
(command ".pline" p02 p2 "a" pocuoi "")
(setq noi1 (entlast))
(command "offset" "100" noi1 pocuoi1 "")
(setq noi2 (entlast))
(setq popcuoi (cdr (assoc 10 (reverse (entget noi2)))))
(setq popdau (cdr (assoc 10 (entget noi2))))
(command ".pline")
(command p02)
(command
(strcat "@-200,0")
(strcat "@0,-300")
(list (car popdau) (- (cadr p02)300))
)
(command popdau "")
(command "")
(setq noi3 (entlast))
(command ".pline" pocuoi popcuoi "")
(setq noi4 (entlast))
(command "pedit" noi1 "j" noi1 noi2 noi3 noi4 "" "")
(setq banbt (entlast))
(command "hatch" MBTong1 tl1 angh1 banbt "")
(command "change" "l" "" "p" "la" "_hatch" "")
(command "hatch" MBTong2 tl2 angh2 banbt "")
(command "change" "l" "" "p" "la" "_hatch" "")
(command "line" ptt pocuoi "")
(setq pohatch (polar goc01 0 20))
(command "-hatch" "p" MBTong3 tl3 angh3 pohatch "")
(command "change" "l" "" "p" "la" "_hatch" "")
)

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

  1. Chép mã nguồn, lưu thành tệp 108833_cat.lsp.
  2. 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ẽ.
  3. Gõ lệnh CAT để chạy lisp.

Mã nguồn 108833_cat.lsp (trích từ bài viết gốc)

(defun c:cat ()
(setvar "cmdecho" 0)
(setq old_layer (getvar "clayer"))
(setq tbl (tblsearch "layer" "_cat"))
(if (= tbl nil) (command "-layer" "n" "_cat" "c" "4" "_cat" ""))
(setq tbl1 (tblsearch "layer" "_hatch"))
(if (= tbl1 nil) (command "-layer" "n" "_hatch" "c" "8" "_hatch" ""))
(setq tbl2 (tblsearch "layer" "_thay"))
(if (= tbl2 nil) (command "-layer" "n" "_thay" "c" "23" "_thay" ""))
(setvar "clayer" "_cat")
(setq pc1 (getpoint "\nChon diem thu nhat: "))
(setq pc2 (getpoint "\nChon diem thu hai: " pc1))
(command "-view" "s" "v1")
(setq p (getpoint "\nChon diem de ve mat cat: "))
(command "-view" "s" "v2")
(setq ct (getreal "\nVao chieu cao tang: "))
(setq sb1 (getint "\nVao so bac: "))
(command "-view" "r" "v1")
(setq catb (ssget "f" (list pc1 pc2)))
(setq i 0)
(setq listpoint nil)
(setq listbac nil)
(while (< i (sslength catb))
(setq n (ssname catb i))
(setq pl1 (cdr (assoc 10 (entget n))))
(setq pl2 (cdr (assoc 11 (entget n))))
(setq pcat (inters pc1 pc2 pl1 pl2))
(setq listpoint (append listpoint (list pcat)))
(setq i (1+ i))
)
(setq i1 0)
(setq sb 0)
(while (< (1+ i1) (length listpoint))
(setq po1 (nth i1 listpoint))
(setq po2 (nth (1+ i1) listpoint))
(setq bac (distance po1 po2))
(setq listbac (append listbac (list bac)))
(if (equal (nth 0 (reverse listbac)) (nth 1 (reverse listbac)))
(setq sb (+ i1 2))
)
(setq i1 (1+ i1))
)
(vethangthang)
)

(defun vethangthang ()
(setq nb 20.0 bk 10.0
MBTong1 "ANSI32" tl1 100 angh1 0
MBTong2 "ar-conc" tl2 10 angh2 0
MBTong3 "ANSI31" tl3 200 angh3 0
MBTong4 "ar-sand" tl4 4 angh4 0
r (car listbac)
c (/ ct sb1)
d 100
oldos (getvar "osmode")
di (* sb (sqrt (+ (* c c) (* r r))))
ang (atan (/ c r))
p01 (polar p 0 10)
p02 (polar p01 (/ (* 270 pi) 180) 20)
p03 (polar p02 ang (/ 20 (sin ang)))
p2 (polar p02 ang di)
p22 (polar p2 (/ (* 90 pi) 180) 20)
p222 (polar p22 (/ (* 180 pi) 180) 10)
p3 (polar p03 0 (/ d (sin ang)))
p33 (polar p02 0 (/ d (sin ang)))
p4 (polar p2 (/ (* 3 pi) 2) (/ d (cos ang))) 
dibt (/ 10 (cos ang))
pbt1 p02
pbt3 (polar p02 ang (/ (distance p02 p2) sb) ) 
pbt2 (list (car pbt1) (cadr pbt3) 0)
pbt4 (polar pbt2 (/ (* 90 pi) 180) 10)
)
(setvar "osmode" 0 )
(command "-view" "r" "v2")
(command ".pline")
(command p)
(repeat sb
(command
(strcat "@0," (rtos (- c (* 2.0 bk))))
(strcat "@" (rtos (- bk nb)) ",0")
"a"
(strcat "@0," (rtos (* 2.0 bk)))
"l"
(strcat "@" (rtos (+ (- nb bk) r)) ",0")
)
)
(command "")
(setvar "cmdecho" 0)
(command "line" p p01 "")
(setq el4 (entlast))
(command "pline" pbt1 pbt2 pbt3 "C")
(setq eL1 (entlast))
(command "hatch" MBTong3 tl3 angh3 eL1 "")
(command "change" "l" "" "p" "la" "_hatch" "")
(setq eL2 (entlast))
(command "line" pbt3 (list (car p) (cadr pbt3) 0) "")
(setq eL3 (entlast))
(command "ucs" "z" p02 p2)
(command "ARray" el1 el2 el3 el4 "" "R" "1" sb (/ di sb))
(command "ucs" "")
(command "-BOUNDARY" pbt4 "")
(setq eL5 (entlast))
(command "rectang" p pbt2)
(setq eL6 (entlast))
(command "hatch" MBTong4 tl4 angh4 eL5 "")
(command "change" "l" "" "p" "la" "_hatch" "")
(setq eL8 (entlast))
(command "hatch" MBTong4 tl4 angh4 el6 "")
(command "change" "l" "" "p" "la" "_hatch" "")
(setq eL7 (entlast))
(command "ucs" "z" p02 p2)
(command "ARray" eL5 eL6 el7 el8 "" "R" "1" sb (/ di sb))
(command "ucs" "")
(vethangcong)
(setvar "osmode" oldos)
(setvar "clayer" old_layer)
(princ)
) 

(defun vethangcong ()
(setq goc01 (polar p ang di))
(setq i2 sb)
(setq ptt goc01)
(while (< i2 (length listbac))
(setq rbc (nth i2 listbac))
(command ".pline")
(command ptt)
(command
(strcat "@0," (rtos (- c (* 2.0 bk))))
(strcat "@" (rtos (- bk nb)) ",0")
"a"
(strcat "@0," (rtos (* 2.0 bk)))
"l"
(strcat "@" (rtos (+ 10 rbc)) ",0")
""
)
(setq ptt1 (polar ptt (/ (* 90 pi) 180) (- c (* 2.0 bk))))
(setq ptt2 (polar ptt1 0 10))
(setq ptt3 (polar ptt2 (/ (* 90 pi) 180) 10))
(command "rectang" ptt ptt2)
(command "hatch" MBTong4 tl4 angh4 "l" "")
(command "change" "l" "" "p" "la" "_hatch" "")
(command ".pline")
(command ptt1)
(command
(strcat "@" (rtos (+ 10 rbc)) ",0")
(strcat "@0," (rtos (* 2.0 bk)))
(strcat "@" (rtos (- bk nb)) ",0")
""
)
(setq ptt (getvar "lastpoint"))
(command "-hatch" ptt3 "")
(command "change" "l" "" "p" "la" "_hatch" "")
(setq i2 (1+ i2))
)
(setq pocuoi (polar ptt (/ (* 270 pi) 180) (/ (* c 2) 3)))
(setq pocuoi1 (polar pocuoi (/ (* 270 pi) 180) (/ (* c 2) 3)))
(command ".pline" p02 p2 "a" pocuoi "")
(setq noi1 (entlast))
(command "offset" "100" noi1 pocuoi1 "")
(setq noi2 (entlast))
(setq popcuoi (cdr (assoc 10 (reverse (entget noi2)))))
(setq popdau (cdr (assoc 10 (entget noi2))))
(command ".pline")
(command p02)
(command
(strcat "@-200,0")
(strcat "@0,-300")
(list (car popdau) (- (cadr p02)300))
)
(command popdau "")
(command "")
(setq noi3 (entlast))
(command ".pline" pocuoi popcuoi "")
(setq noi4 (entlast))
(command "pedit" noi1 "j" noi1 noi2 noi3 noi4 "" "")
(setq banbt (entlast))
(command "hatch" MBTong1 tl1 angh1 banbt "")
(command "change" "l" "" "p" "la" "_hatch" "")
(command "hatch" MBTong2 tl2 angh2 banbt "")
(command "change" "l" "" "p" "la" "_hatch" "")
(command "line" ptt pocuoi "")
(setq pohatch (polar goc01 0 20))
(command "-hatch" "p" MBTong3 tl3 angh3 pohatch "")
(command "change" "l" "" "p" "la" "_hatch" "")
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề nhờ các cao thủ viết dùm lisp hatch,ve thang cat.. Xem đúng bài viết #108833 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 272)