Jump to content

Lisp lệnh VTL1 trong chủ đề “Lisp rải taluy trên đường cong”

Đây là lisp tôi sưu tầm và chỉnh sửa lại chút ít, có thể rải taluy cho các loại line, pline, spline, arc, circle ...

(Mới dừng ở việc vẽ taluy cho 1 đường, phần vẽ mái taluy giữa 2 đường tôi chưa sửa xong)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;vtl;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun nsl () 
(if (/= scale nil)
   (progn
     (setq thongbao (strcat "Ty le ban ve ?, <1/" (itoa scale) ">:"))
     (if (not (setq scaletmp (getint thongbao)))
        (setq scaletmp scale)
     )
   )
   (progn
     (setq thongbao "Ty le ban ve ? <1/1000>:")
     (if (not (setq scaletmp (getint thongbao)))
         (setq scaletmp 1000)
     )
  )
)
(setq scale scaletmp)
(setq Defaultdist (* (* scale 2) 0.002))
(if (setq tg (getreal (strcat "\nKhoang cach ky hieu ta luy <" (rtos Defaultdist 2 2) ">:" ))) 
    (setq Defaultdist tg)
)
(setq	chieutaluy1 	1	sodoan 0  )
)
;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun nsl1 ()
(setq ktdoantaluy1 2    tg (getreal (strcat "\nChieu dai doan ngan<"  (rtos ktdoantaluy1 2 2)  ">:" ))  )
(if tg 
  (setq ktdoantaluy1 tg)
)
(setq ktdoantaluy2 6    tg (getreal (strcat "\nChieu dai doan dai<" (rtos ktdoantaluy2 2 2) ">:" ))  )
(if tg
   (setq ktdoantaluy2 tg)
)
(setq khoangcachtl 2    tg (getreal (strcat "\nKhoang cach giua cac doan<"  (rtos khoangcachtl 2 2)  ">:"  ))  )
(if tg
   (setq khoangcachtl tg)
)
(setq sodoanngan 3    tg (getint (strcat "\nSo doan ngan trong 1 doan dai<" (rtos sodoanngan 2 0)  ">:"  ))  )
(if tg 
   (setq sodoanngan tg)
)
)
(Defun PlMake (Plist)			;  Create polyline entities
(entmake '((0 . "POLYLINE")))
(setq	n  (length Plist)	ic 0  )
(while (< ic n)
   (entmake (list (cons 0 "VERTEX") (cons 10 (nth ic Plist)))) 
   (setq ic (1+ ic))
)
(entmake '((0 . "SEQEND")))
)
;;;----------------------------------------------------------------
(defun ve1doantaluy (p1 p2 / pvt diemcu ktdoantaluy ketthuc)
 (setq pvt (+ (angle p1 p2) (* (/ pi 2) chieutaluy)))
 (setq ketthuc 1)
 (if (< sodoan sodoanngan)
   (progn
     (setq ktdoantaluy ktdoantaluy1)
     (setq sodoan (1+ sodoan))
   )
   (progn
     (setq ktdoantaluy ktdoantaluy2)
     (setq sodoan 0)
   )
)
(setq p2 (polar p1 pvt ktdoantaluy))
(plmake (list p1 p2))
(setq dem (1+ dem))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(Defun xddsd (com epl kc / e0 e p dsd)
 (setq e0 (entlast))
 (while e0
   (setq e e0)
   (setq e0 (entnext e0))
 )
 (command com epl kc)
 (setq e (entnext e))
 (while e
   (setq p (cdr (assoc 10 (entget e))))
   (if	p
     (setq dsd (cons p dsd))
   )
   (setq e (entnext e))
 )
 (command "_.Undo" 1)
 (setq dsd dsd)
)				   
;;; ve ta luy cho 1 doi tuong
(Defun vetaluy (ep / le e ketthuc them dsd thutu)
 (setq dem 0)
 (setq e (entget (car ep)))
 (if (or (= (cdr (assoc 0 e)) "LWPOLYLINE") (= (cdr (assoc 0 e)) "POLYLINE") (= (cdr (assoc 0 e)) "SPLINE") (= (cdr (assoc 0 e)) "LINE")
          (= (cdr (assoc 0 e)) "ARC") (= (cdr (assoc 0 e)) "CIRCLE") )
     (setq ketthuc 1)
     (prompt "\nDoi tuong duoc chon khong hop le")
 )
 (if ketthuc
   (progn
     (setq thutu 0)
     (setq dsd (xddsd "_.Measure" ep khoangcachtl))
     (setq p1 (car dsd))
     (repeat (1- (length dsd))
         (setq thutu (1+ thutu))
         (setq p2 (nth thutu dsd))
         (ve1doantaluy p1 p2)
         (setq p1 p2)
     )
   )
 )
 (setq dem dem)
)
;;;==================================================
(Defun C:vtl1 (/ ep chon lai solan chon)
 (setvar "cmdecho" 0)
 (setvar "blipmode" 0)
 (command "undo" "g")
 (nsl)
 (setq ep 1)
 (while ep
   (setq solan	0  	chieutaluy 1    )
   (setq ep (entsel "\nChon doi tuong ve ta luy..."))
   (if	ep
     (progn
       (nsl1)
       (setq solan (vetaluy ep))
       (initget "Undo Change")
       (while  	(setq chon (getkword "Undo/Change <enter for exit>: "))
            (if (= chon "Undo")
                (command "_.Undo" solan)
            )
            (if (= chon "Change")
                (progn
                   (nsl1)
                   (setq chieutaluy -1)
                   (command "_.Undo" solan)
                   (setq solan (vetaluy ep))
                )
            )
            (initget "Undo Change")
        )
     )
   )
)
(command "undo" "e")
)

Nhờ các bác chỉnh sửa lại lisp giúp em các vấn đề sau:

 

1. Lưu số liệu lần nhập trước

2. Sau khi kết thúc lệnh bằng Enter (Undo/Change <enter for exit>) thì tự đóng block đối tượng vừa sinh ra với tên block cho nhập mới vào (với layer hiện hành)

3. Kết thúc bằng Enter không thực hiện tiếp lệnh VTL1

4. Khắc phục nill khi kết thúc lệnh Enter

5. Khi thực hiện lệnh để thay đổi phía rải taluy, không hỏi lại các tham số nữa mà thực hiện luôn

6. Khắc phục không đổi lệnh VLT1 thành lệnh khác được (giả sử thành lệnh VTL) (em thấy các lisp khác đổi lệnh được thông qua ký tự C:LLL thành tên lệnh mong muốn)

6. Khắc phụ lỗi không rải taluy hết đường và không vuông góc với đường như hình:17200_17200_hinh.jpg

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 204638_vtl1.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 VTL1 để chạy lisp.

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

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;vtl;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun nsl () 
(if (/= scale nil)
   (progn
     (setq thongbao (strcat "Ty le ban ve ?, <1/" (itoa scale) ">:"))
     (if (not (setq scaletmp (getint thongbao)))
        (setq scaletmp scale)
     )
   )
   (progn
     (setq thongbao "Ty le ban ve ? <1/1000>:")
     (if (not (setq scaletmp (getint thongbao)))
         (setq scaletmp 1000)
     )
  )
)
(setq scale scaletmp)
(setq Defaultdist (* (* scale 2) 0.002))
(if (setq tg (getreal (strcat "\nKhoang cach ky hieu ta luy <" (rtos Defaultdist 2 2) ">:" ))) 
    (setq Defaultdist tg)
)
(setq	chieutaluy1 	1	sodoan 0  )
)
;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun nsl1 ()
(setq ktdoantaluy1 2    tg (getreal (strcat "\nChieu dai doan ngan<"  (rtos ktdoantaluy1 2 2)  ">:" ))  )
(if tg 
  (setq ktdoantaluy1 tg)
)
(setq ktdoantaluy2 6    tg (getreal (strcat "\nChieu dai doan dai<" (rtos ktdoantaluy2 2 2) ">:" ))  )
(if tg
   (setq ktdoantaluy2 tg)
)
(setq khoangcachtl 2    tg (getreal (strcat "\nKhoang cach giua cac doan<"  (rtos khoangcachtl 2 2)  ">:"  ))  )
(if tg
   (setq khoangcachtl tg)
)
(setq sodoanngan 3    tg (getint (strcat "\nSo doan ngan trong 1 doan dai<" (rtos sodoanngan 2 0)  ">:"  ))  )
(if tg 
   (setq sodoanngan tg)
)
)
(Defun PlMake (Plist)			;  Create polyline entities
(entmake '((0 . "POLYLINE")))
(setq	n  (length Plist)	ic 0  )
(while (< ic n)
   (entmake (list (cons 0 "VERTEX") (cons 10 (nth ic Plist)))) 
   (setq ic (1+ ic))
)
(entmake '((0 . "SEQEND")))
)
;;;----------------------------------------------------------------
(defun ve1doantaluy (p1 p2 / pvt diemcu ktdoantaluy ketthuc)
 (setq pvt (+ (angle p1 p2) (* (/ pi 2) chieutaluy)))
 (setq ketthuc 1)
 (if (< sodoan sodoanngan)
   (progn
     (setq ktdoantaluy ktdoantaluy1)
     (setq sodoan (1+ sodoan))
   )
   (progn
     (setq ktdoantaluy ktdoantaluy2)
     (setq sodoan 0)
   )
)
(setq p2 (polar p1 pvt ktdoantaluy))
(plmake (list p1 p2))
(setq dem (1+ dem))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(Defun xddsd (com epl kc / e0 e p dsd)
 (setq e0 (entlast))
 (while e0
   (setq e e0)
   (setq e0 (entnext e0))
 )
 (command com epl kc)
 (setq e (entnext e))
 (while e
   (setq p (cdr (assoc 10 (entget e))))
   (if	p
     (setq dsd (cons p dsd))
   )
   (setq e (entnext e))
 )
 (command "_.Undo" 1)
 (setq dsd dsd)
)				   
;;; ve ta luy cho 1 doi tuong
(Defun vetaluy (ep / le e ketthuc them dsd thutu)
 (setq dem 0)
 (setq e (entget (car ep)))
 (if (or (= (cdr (assoc 0 e)) "LWPOLYLINE") (= (cdr (assoc 0 e)) "POLYLINE") (= (cdr (assoc 0 e)) "SPLINE") (= (cdr (assoc 0 e)) "LINE")
          (= (cdr (assoc 0 e)) "ARC") (= (cdr (assoc 0 e)) "CIRCLE") )
     (setq ketthuc 1)
     (prompt "\nDoi tuong duoc chon khong hop le")
 )
 (if ketthuc
   (progn
     (setq thutu 0)
     (setq dsd (xddsd "_.Measure" ep khoangcachtl))
     (setq p1 (car dsd))
     (repeat (1- (length dsd))
         (setq thutu (1+ thutu))
         (setq p2 (nth thutu dsd))
         (ve1doantaluy p1 p2)
         (setq p1 p2)
     )
   )
 )
 (setq dem dem)
)
;;;==================================================
(Defun C:vtl1 (/ ep chon lai solan chon)
 (setvar "cmdecho" 0)
 (setvar "blipmode" 0)
 (command "undo" "g")
 (nsl)
 (setq ep 1)
 (while ep
   (setq solan	0  	chieutaluy 1    )
   (setq ep (entsel "\nChon doi tuong ve ta luy..."))
   (if	ep
     (progn
       (nsl1)
       (setq solan (vetaluy ep))
       (initget "Undo Change")
       (while  	(setq chon (getkword "Undo/Change <enter for exit>: "))
            (if (= chon "Undo")
                (command "_.Undo" solan)
            )
            (if (= chon "Change")
                (progn
                   (nsl1)
                   (setq chieutaluy -1)
                   (command "_.Undo" solan)
                   (setq solan (vetaluy ep))
                )
            )
            (initget "Undo Change")
        )
     )
   )
)
(command "undo" "e")
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Lisp rải taluy trên đường cong. Xem đúng bài viết #204638 để đọc hướng dẫn và trao đổi thêm.

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