Jump to content

Lisp tạo viewport từ khung chọn bên model.

Với vấn đề này mình nhờ bạn KangKung chỉnh giúp mình sao cho:
khung tên bên layout luôn ở tỉ lệ 1:1(dùng khung tên xref), còn khung bên model thì có các tỉ lệ khác nhau như:
1:10, 1:15, 1:20, ... Khi ta dùng lệnh lsp thì tất cả các khung bản vẽ bên model sẽ nằm gọn trong khung tên layout đúng tỉ lệ 1:1
(hiện tại khi xuất qua layout thì có rất nhiều tỉ lệ
Chọn khung view bên model trước thì nó xuất qua bên layout trước
Mong bạn sửa giúp
========LISP TAO VIEWPORT TREN LAYOUT BANG CACH CHON O MODEL========
;============================REV5====================================
;=========THEM LUA CHON XOAY HCN NGHIENG VA XREF KHUNG TEN===========
(defun C:mtl( / os lst khung pt0 pt1 pt2 pt3 Y index taphop xrefFile xref)
  (command "UNDO" "BE")
  (setq os(getvar "OSMODE"))
  (setvar "OSMODE" 0) 
  (setq taphop(ssget (LIST (CONS 0 "POLYLINE,LWPOLYLINE"))))
  (if (= Tyle nil)
    (setq Tyle1 1)
    (setq Tyle1 Tyle))
  (setq Tyle (getreal (strcat "\n Ty le: <" (rtos Tyle1 2 0) "> ")))
  (if (= Tyle nil)
    (setq Tyle Tyle1))
  (setq xref(getstring "\n Ban co muon chen file khung ten hay khong? <Y/N>:"))
  (if (= (strcase xref) "Y")
    (progn
      (if (not Path)
	(setq Path(getvar "dwgprefix")))
      (setq xrefFile(getfiled "Chon File khung ten" Path "dwg" 2))
      (setq Path xrefFile)))
  (setq soluong (sslength taphop))
  (setq index 0)
  (command "LAYOUT" "N" "Layout1")
  (command "LAYOUT" "S" "Layout1")
  (command "ERASE" "ALL" "")
  (command "ZOOM" "E")
  (command "MODEL")
  (setq Y 0)
  (command "ZOOM" "E")
  (while (< index soluong)
    (setq khung(ssname taphop index))
    (setq lst(acet-geom-vertex-list khung))
    (setq lst (vl-sort lst '(lambda (e1 e2) (if (/= (car e1) (car e2)) (< (car e1) (car e2)) (< (cadr e1) (cadr e2))))))
    (setq pt0(nth 0 lst) pt3(nth 3 lst))
    (if (> (cadr (nth 1 lst)) (cadr (nth 2 lst)))
      (setq pt1(nth 1 lst) pt2(nth 2 lst))
      (setq pt1(nth 2 lst) pt2(nth 1 lst))
      )
    (command "LAYOUT" "S" "Layout1")
    (if (> (distance pt2 pt0) (distance pt1 pt0))
      (command "RECTANG" (list 0 Y) (list (distance pt2 pt0) (+ Y (distance pt1 pt0))))
      (command "RECTANG" (list 0 Y) (list (distance pt1 pt0) (+ y (distance pt2 pt0))))
      )
    (command "SCALE" (entlast) "" (list 0 Y) (/ 1 tyle))
    (command "MVIEW" "O" (entlast))
    (if (= (strcase xref) "Y")
      (command "xref" "A" xrefFile (list 0 Y) "" "" ""))
    (command "MSPACE")
    (if (> (distance pt2 pt0) (distance pt1 pt0))
      (command "DVIEW" khung "" "TW" (- 90 (* (/ (angle pt0 pt1) pi) 180)) "")
      (command "DVIEW" khung "" "TW" (- 0 (* (/ (angle pt0 pt1) pi) 180)) "")
      )
    (command "ZOOM" "W" pt0 pt3)
    (command "PSPACE")
    (command "TEXT" "J" "MR" (list -50 (+ Y (/ (distance pt1 pt0) (* 2 tyle)))) (* 25 tyle) "0" (strcat "VP " (rtos (1+ index) 2 0)) "")
    (if (> (distance pt2 pt0) (distance pt1 pt0))
      (setq Y(- Y 50 (/ (distance pt1 pt0) tyle)))
      (setq Y(- Y 50 (/ (distance pt2 pt0) tyle)))
      )
    (command "ZOOM" "W" (list -100 (+ Y (distance pt3 pt0))) (list (distance pt3 pt0) (- Y 50 (distance pt3 pt0))))
    (setq index (+ index 1))
    )
  (command "ZOOM" "E")
  (command "MODEL")
  (command "UNDO" "END")
  (setvar "OSMODE" os)
  (princ)
  )

Cách dùng

  1. Tải tệp 407189_mtl.lsp về máy.
  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 MTL để chạy lisp.

Mã nguồn 407189_mtl.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=6
========LISP TAO VIEWPORT TREN LAYOUT BANG CACH CHON O MODEL========
;============================REV5====================================
;=========THEM LUA CHON XOAY HCN NGHIENG VA XREF KHUNG TEN===========
(defun C:mtl( / os lst khung pt0 pt1 pt2 pt3 Y index taphop xrefFile xref)
  (command "UNDO" "BE")
  (setq os(getvar "OSMODE"))
  (setvar "OSMODE" 0) 
  (setq taphop(ssget (LIST (CONS 0 "POLYLINE,LWPOLYLINE"))))
  (if (= Tyle nil)
    (setq Tyle1 1)
    (setq Tyle1 Tyle))
  (setq Tyle (getreal (strcat "\n Ty le: <" (rtos Tyle1 2 0) "> ")))
  (if (= Tyle nil)
    (setq Tyle Tyle1))
  (setq xref(getstring "\n Ban co muon chen file khung ten hay khong? <Y/N>:"))
  (if (= (strcase xref) "Y")
    (progn
      (if (not Path)
	(setq Path(getvar "dwgprefix")))
      (setq xrefFile(getfiled "Chon File khung ten" Path "dwg" 2))
      (setq Path xrefFile)))
  (setq soluong (sslength taphop))
  (setq index 0)
  (command "LAYOUT" "N" "Layout1")
  (command "LAYOUT" "S" "Layout1")
  (command "ERASE" "ALL" "")
  (command "ZOOM" "E")
  (command "MODEL")
  (setq Y 0)
  (command "ZOOM" "E")
  (while (< index soluong)
    (setq khung(ssname taphop index))
    (setq lst(acet-geom-vertex-list khung))
    (setq lst (vl-sort lst '(lambda (e1 e2) (if (/= (car e1) (car e2)) (< (car e1) (car e2)) (< (cadr e1) (cadr e2))))))
    (setq pt0(nth 0 lst) pt3(nth 3 lst))
    (if (> (cadr (nth 1 lst)) (cadr (nth 2 lst)))
      (setq pt1(nth 1 lst) pt2(nth 2 lst))
      (setq pt1(nth 2 lst) pt2(nth 1 lst))
      )
    (command "LAYOUT" "S" "Layout1")
    (if (> (distance pt2 pt0) (distance pt1 pt0))
      (command "RECTANG" (list 0 Y) (list (distance pt2 pt0) (+ Y (distance pt1 pt0))))
      (command "RECTANG" (list 0 Y) (list (distance pt1 pt0) (+ y (distance pt2 pt0))))
      )
    (command "SCALE" (entlast) "" (list 0 Y) (/ 1 tyle))
    (command "MVIEW" "O" (entlast))
    (if (= (strcase xref) "Y")
      (command "xref" "A" xrefFile (list 0 Y) "" "" ""))
    (command "MSPACE")
    (if (> (distance pt2 pt0) (distance pt1 pt0))
      (command "DVIEW" khung "" "TW" (- 90 (* (/ (angle pt0 pt1) pi) 180)) "")
      (command "DVIEW" khung "" "TW" (- 0 (* (/ (angle pt0 pt1) pi) 180)) "")
      )
    (command "ZOOM" "W" pt0 pt3)
    (command "PSPACE")
    (command "TEXT" "J" "MR" (list -50 (+ Y (/ (distance pt1 pt0) (* 2 tyle)))) (* 25 tyle) "0" (strcat "VP " (rtos (1+ index) 2 0)) "")
    (if (> (distance pt2 pt0) (distance pt1 pt0))
      (setq Y(- Y 50 (/ (distance pt1 pt0) tyle)))
      (setq Y(- Y 50 (/ (distance pt2 pt0) tyle)))
      )
    (command "ZOOM" "W" (list -100 (+ Y (distance pt3 pt0))) (list (distance pt3 pt0) (- Y 50 (distance pt3 pt0))))
    (setq index (+ index 1))
    )
  (command "ZOOM" "E")
  (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 #407189 để đọc hướng dẫn và trao đổi thêm.

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