Jump to content

Nhờ viết lisp tạo nhanh wipeout

Code cũ của gile, ketxu sửa thêm 1 chút :

;; WO2PL (gile)
;; Re-creates a wipeout boundary (lwpolyline)
(defun c:wo2pl (/ ss n wo elst pts norm ans)
(if (setq ss (ssget '((0 . "WIPEOUT"))))
(progn
(initget "Yes No")
(setq ans (getkword "\nDelete source object? <No>: "))
(foreach wo (ST:Ss->ListEnt ss)
(setq
elst (entget wo)
norm (vunit (v^v (cdr (assoc 11 elst)) (cdr (assoc 12 elst))))
pts (wipeout2plst wo)
)
(entmake
(append
(list '(0 . "LWPOLYLINE")
'(100 . "AcDbEntity")
'(100 . "AcDbPolyline")
(cons 90 (length pts))
(cons 38 (caddr (trans (car pts) 0 norm)))
'(70 . 1)
(cons 210 norm)
)
(mapcar '(lambda (pt)
(setq pt (trans pt 0 norm))
(list 10 (car pt) (cadr pt))
)
pts
)
)
)
(if (or (not ans) (wcmatch (strcase ans) "YES"))(entdel wo))
(princ)
)))
)
;;==================SUB ROUTINES==================;;
;; returns the wipeout point list (WCS)
(defun wipeout2plst (wo / elst u v mat)
(setq elst (entget wo)
u (cdr (assoc 11 elst))
v (cdr (assoc 12 elst))
mat (list u (mapcar '- v) '(0. 0. 1.))
)
(mapcar
'(lambda (p)
(mapcar '+
(mxv (trp mat) p)
(mapcar '(lambda (x y) (/ (+ x y) 2.)) u v)
(cdr (assoc 10 elst))
)
)
(cdr
(mapcar 'cdr
(vl-remove-if-not '(lambda (x) (= (car x) 14)) elst)
)
)
)
)
;; Transpose a matrix Doug Wilson
(defun trp (m)
(apply 'mapcar (cons 'list m))
)
;; Apply a transformation matrix to a vector by Vladimir Nesterovsky
(defun mxv (m v)
(mapcar '(lambda (r) (apply '+ (mapcar '* r v))) m)
)
;; V^V
;; Returns the cross product of 2 vectors
(defun v^v (v1 v2)
(list (- (* (cadr v1) (caddr v2)) (* (caddr v1) (cadr v2)))
(- (* (caddr v1) (car v2)) (* (car v1) (caddr v2)))
(- (* (car v1) (cadr v2)) (* (cadr v1) (car v2)))
)
)
;; VUNIT
;; Returns the single unit vector
(defun vunit (v)
((lambda (l)
(if (/= 0 l)
(mapcar (function (lambda (x) (/ x l))) v)
)
)
(distance '(0 0 0) v)
)
)
(defun ST:Ss->ListEnt (ss / n e l)
(setq n (sslength ss))
(while (setq e (ssname ss (setq n (1- n))))
(setq l (cons e l))
)
)

Cách dùng

  1. Tải tệp 195444_wo2pl.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 WO2PL để chạy lisp.

Mã nguồn 195444_wo2pl.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/63065-y%C3%AAu-c%E1%BA%A7u-nh%E1%BB%9D-vi%E1%BA%BFt-lisp-t%E1%BA%A1o-nhanh-wipeout/
;; WO2PL (gile)
;; Re-creates a wipeout boundary (lwpolyline)
(defun c:wo2pl (/ ss n wo elst pts norm ans)
 (if (setq ss (ssget '((0 . "WIPEOUT"))))
 (progn
(initget "Yes No")    
(setq ans (getkword "\nDelete source object? [Yes/No] <No>: "))    
(foreach wo (ST:Ss->ListEnt ss)  
 	(setq
elst (entget wo)
norm (vunit (v^v (cdr (assoc 11 elst)) (cdr (assoc 12 elst))))
pts  (wipeout2plst wo)
 	)
 	(entmake
(append
  (list '(0 . "LWPOLYLINE")
 '(100 . "AcDbEntity")
 '(100 . "AcDbPolyline")
 (cons 90 (length pts))
 (cons 38 (caddr (trans (car pts) 0 norm)))
 '(70 . 1)
 (cons 210 norm)
  )
  (mapcar '(lambda (pt)
  	(setq pt (trans pt 0 norm))
  	(list 10 (car pt) (cadr pt))
)
pts
  )
)
 	)
  (if (or (not ans) (wcmatch (strcase ans) "YES"))(entdel wo))
(princ)  
 )))
)
;;==================SUB ROUTINES==================;;
;; returns the wipeout point list (WCS)
(defun wipeout2plst (wo / elst u v mat)
 (setq elst (entget wo)
u	(cdr (assoc 11 elst))
v	(cdr (assoc 12 elst))
mat  (list u (mapcar '- v) '(0. 0. 1.))
 )
 (mapcar
'(lambda (p)
  	(mapcar '+
   	(mxv (trp mat) p)
   	(mapcar '(lambda (x y) (/ (+ x y) 2.)) u v)
   	(cdr (assoc 10 elst))
  	)
)
(cdr
 	(mapcar 'cdr
  	(vl-remove-if-not '(lambda (x) (= (car x) 14)) elst)
 	)
)
 )
)
;; Transpose a matrix Doug Wilson
(defun trp (m)
 (apply 'mapcar (cons 'list m))
)
;; Apply a transformation matrix to a vector by Vladimir Nesterovsky
(defun mxv (m v)
 (mapcar '(lambda (r) (apply '+ (mapcar '* r v))) m)
)
;; V^V
;; Returns the cross product of 2 vectors
(defun v^v (v1 v2)
 (list (- (* (cadr v1) (caddr v2)) (* (caddr v1) (cadr v2)))
(- (* (caddr v1) (car v2)) (* (car v1) (caddr v2)))
(- (* (car v1) (cadr v2)) (* (cadr v1) (car v2)))
 )
)
;; VUNIT
;; Returns the single unit vector
(defun vunit (v)
 ((lambda (l)
(if (/= 0 l)
  	(mapcar (function (lambda (x) (/ x l))) v)
)
  )
(distance '(0 0 0) v)
 )
)
(defun ST:Ss->ListEnt (ss / n e l)
 (setq n (sslength ss))
 (while (setq e (ssname ss (setq n (1- n))))
   (setq l (cons e l))
 )  
) 

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Yêu cầu] Nhờ viết lisp tạo nhanh wipeout. Xem đúng bài viết #195444 để đọc hướng dẫn và trao đổi thêm.

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