Lisp tạo viewport từ khung chọn bên model.
- Chào bạn Conghoa ^^, nhoc đã chỉnh đc phần thô, chọn khung bên model rùi tạo viewport bên layout tương ứng với khu đã chọn và tỉ lệ do mình nhập như hình nhoc đăng, nhoc chỉnh theo ý tưởng lúc đầu của nhoc giống hình đó là mình có sẵn các khung bên model , gọi lệnh sẽ hỏi chọn khung sau đó nhập tỉ lệ cho khung đó, nhập xong sẽ tự chuyển qua layout cho mình chọn điểm đặt khung chọn xong quay trở về lại model cho mình chọn khung tiếp theo, cứ tiếp tục vậy khi nào xong thì phải chuột kết thúc lệnh, bạn test trước xem nhé ^^
(defun Makepline (listpoint closed Layer Linetype LTScale xdata / Lst)
(setq Lst (list '(0 . "LWPOLYLINE")'(100 . "AcDbEntity")
(cons 8 (if Layer Layer (getvar "Clayer")))
(cons 6 (if Linetype Linetype "bylayer"))
(cons 48 (if LTScale LTScale 1))
'(100 . "AcDbPolyline")
(cons 90 (length listpoint))
(cons 70 (if closed 1 0))))
(foreach PP listpoint (setq Lst (append Lst (list (cons 10 PP)))))
(if xdata (setq Lst (append lst (list (cons -3 (list xdata))))))
(entmakex Lst))
;end;=================================
;;;;;;;;;;;
(defun _layer2 ( name colour )
(if (null (tblsearch "LAYER" name))
(entmake
(list
'(0 . "LAYER")
'(100 . "AcDbSymbolTableRecord")
'(100 . "AcDbLayerTableRecord")
'(70 . 0)
(cons 2 name)
(cons 62 colour)
)
)
)
)
; ham luu gia tri
(defun getvalue ( a giatri dongnhac / astr)
(or a (setq a giatri))
(cond
((= (type a) 'INT) (setq a (cond ((getint (strcat "\n" dongnhac "(" (itoa a) ") :")))(a))))
((= (type a) 'REAL) (setq a (cond ((getreal (strcat "\n" dongnhac "(" (rtos a 2 0) ") :")))(a))))
((= (type a) 'STR) (setq a (cond ((= "" (setq astr (getstring 1 (strcat "\n" dongnhac " (" a "): ")))) a) (astr))))
))
;;;;
;============================================================
;========LISP TAO VIEWPORT TREN LAYOUT BANG CACH CHON O MODEL========
;=========================REV4ii=====================================
(defun C:mtll( / os lst khung X_min Y_min X_max Y_max X index taphop pt1)
(vl-load-com)
(if (null (tblsearch "LAYER" "khung")) (_layer2 "Khung" 3))
(command "UNDO" "BE")
(setq os(getvar "OSMODE"))
(setvar "OSMODE" 0)
(while (/= (setq taphop (ssget "+.:E:S" '((0 . "LWPOLYLINE")))) nil)
(progn
(setq tyleVT (getvalue tyleVT 1000.0 "Nhap ty le ban do: "))
(command "LAYOUT" "S" "Layout1")
(setq pt1 (getpoint "\nchon diem dat khung"))
(command "ZOOM" "E")
(setq khung (ssname taphop 0))
(setq lst (cdr (acet-geom-vertex-list khung)))
(setq X_min 1000000000
Y_min 1000000000
X_max -1000000000
Y_max -1000000000)
(foreach a lst
(if (< (car a) X_min) (setq X_min (car a)))
(if (< (cadr a) Y_min) (setq Y_min (cadr a)))
(if (> (car a) X_max) (setq X_max (car a)))
(if (> (cadr a) Y_max) (setq Y_max (cadr a)))
)
(command "LAYOUT" "S" "Layout1")
(command "ZOOM" "W" (list X_min Y_min) (list X_max Y_max))
(makepline lst 1 "Khung" nil nil nil)
(command "MOVE" (entlast) "" (list X_min Y_min) (list (car pt1) 0))
(command "ZOOM" "W" (list 0 0) (list (+ (car pt1) 100) 0))
(command "SCALE" (entlast) "" (list (car pt1) 0) (/ 1000 tyleVT))
(command "MVIEW" "O" (entlast))
(command "MSPACE")
(command "ZOOM" (list X_min Y_min) (list X_max Y_max))
(command "PSPACE")
(command "MVIEW" "L" "on" (entlast) "")
(command "ZOOM" "W" (list 0 0) (list (+ (car pt1) 100) 0))
(command "MODEL")
) ;ene progn while
) ; end while
(command "MODEL")
(command "UNDO" "END")
(setvar "OSMODE" os)
(princ)
)
;=======================================================
- Tải lisp
- 318859_mtll.lsp
Cách dùng
- Tải tệp 318859_mtll.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 MTLL để chạy lisp.
Mã nguồn 318859_mtll.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/68723-y%C3%AAu-c%E1%BA%A7u-lisp-t%E1%BA%A1o-viewport-t%E1%BB%AB-khung-ch%E1%BB%8Dn-b%C3%AAn-model/?page=5
(defun Makepline (listpoint closed Layer Linetype LTScale xdata / Lst)
(setq Lst (list '(0 . "LWPOLYLINE")'(100 . "AcDbEntity")
(cons 8 (if Layer Layer (getvar "Clayer")))
(cons 6 (if Linetype Linetype "bylayer"))
(cons 48 (if LTScale LTScale 1))
'(100 . "AcDbPolyline")
(cons 90 (length listpoint))
(cons 70 (if closed 1 0))))
(foreach PP listpoint (setq Lst (append Lst (list (cons 10 PP)))))
(if xdata (setq Lst (append lst (list (cons -3 (list xdata))))))
(entmakex Lst))
;end;=================================
;;;;;;;;;;;
(defun _layer2 ( name colour )
(if (null (tblsearch "LAYER" name))
(entmake
(list
'(0 . "LAYER")
'(100 . "AcDbSymbolTableRecord")
'(100 . "AcDbLayerTableRecord")
'(70 . 0)
(cons 2 name)
(cons 62 colour)
)
)
)
)
; ham luu gia tri
(defun getvalue ( a giatri dongnhac / astr)
(or a (setq a giatri))
(cond
((= (type a) 'INT) (setq a (cond ((getint (strcat "\n" dongnhac "(" (itoa a) ") :")))(a))))
((= (type a) 'REAL) (setq a (cond ((getreal (strcat "\n" dongnhac "(" (rtos a 2 0) ") :")))(a))))
((= (type a) 'STR) (setq a (cond ((= "" (setq astr (getstring 1 (strcat "\n" dongnhac " (" a "): ")))) a) (astr))))
))
;;;;
;============================================================
;========LISP TAO VIEWPORT TREN LAYOUT BANG CACH CHON O MODEL========
;=========================REV4ii=====================================
(defun C:mtll( / os lst khung X_min Y_min X_max Y_max X index taphop pt1)
(vl-load-com)
(if (null (tblsearch "LAYER" "khung")) (_layer2 "Khung" 3))
(command "UNDO" "BE")
(setq os(getvar "OSMODE"))
(setvar "OSMODE" 0)
(while (/= (setq taphop (ssget "+.:E:S" '((0 . "LWPOLYLINE")))) nil)
(progn
(setq tyleVT (getvalue tyleVT 1000.0 "Nhap ty le ban do: "))
(command "LAYOUT" "S" "Layout1")
(setq pt1 (getpoint "\nchon diem dat khung"))
(command "ZOOM" "E")
(setq khung (ssname taphop 0))
(setq lst (cdr (acet-geom-vertex-list khung)))
(setq X_min 1000000000
Y_min 1000000000
X_max -1000000000
Y_max -1000000000)
(foreach a lst
(if (< (car a) X_min) (setq X_min (car a)))
(if (< (cadr a) Y_min) (setq Y_min (cadr a)))
(if (> (car a) X_max) (setq X_max (car a)))
(if (> (cadr a) Y_max) (setq Y_max (cadr a)))
)
(command "LAYOUT" "S" "Layout1")
(command "ZOOM" "W" (list X_min Y_min) (list X_max Y_max))
(makepline lst 1 "Khung" nil nil nil)
(command "MOVE" (entlast) "" (list X_min Y_min) (list (car pt1) 0))
(command "ZOOM" "W" (list 0 0) (list (+ (car pt1) 100) 0))
(command "SCALE" (entlast) "" (list (car pt1) 0) (/ 1000 tyleVT))
(command "MVIEW" "O" (entlast))
(command "MSPACE")
(command "ZOOM" (list X_min Y_min) (list X_max Y_max))
(command "PSPACE")
(command "MVIEW" "L" "on" (entlast) "")
(command "ZOOM" "W" (list 0 0) (list (+ (car pt1) 100) 0))
(command "MODEL")
) ;ene progn while
) ; end while
(command "MODEL")
(command "UNDO" "END")
(setvar "OSMODE" os)
(princ)
)
;=======================================================
Thảo luận gốc
Lisp được chia sẻ trong chủ đề [Yêu cầu] Lisp tạo viewport từ khung chọn bên model.. Xem đúng bài viết #318859 để đọc hướng dẫn và trao đổi thêm.