Jump to content

Lisp lệnh TEST trong chủ đề “Hỏi về Lisp (thuật toán, ý tưởng, coding,...)”

Mọi người cho hỏi ngu tí:

Tôi có 1 block_att chứa 1 pline và 1 att, đem chèn nó vào điểm A, sau đó move qua điểm B. Làm thế nào để lấy tọa độ các đỉnh pline khi chọn block đã move? Vì khi lấy entget của các đối tượng con của block thì tọa độ pline nó lấy tọa độ lúc chèn (điểm A) chứ không phải tọa độ sau move (điểm 'B). Không nổ block nhé.

Cách khác,

Bác Hạ tham khảo code này của Giles, pline thì bác thay ngon lành qua matrix ^_^

;; Entmatrix
;; Returns a list which first item is the 3X3 tranformation matrix and second item
;; the insertion point of a block refernce in its owner (space or block definition)
(defun EntMatrix (ename / elst ang norm)
  (setq	elst (entget ename)
	ang  (cdr (assoc 50 elst))
	norm (cdr (assoc 210 elst))
  )
  (list
    (mxm
      (mapcar (function (lambda (v) (trans v 0 norm T)))
	      '((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
      )
      (mxm
	(list (list (cos ang) (- (sin ang)) 0.0)
	      (list (sin ang) (cos ang) 0.0)
	      '(0.0 0.0 1.0)
	)
	(list (list (cdr (assoc 41 elst)) 0.0 0.0)
	      (list 0.0 (cdr (assoc 42 elst)) 0.0)
	      (list 0.0 0.0 (cdr (assoc 43 elst)))
	)
      )
    )
    (trans (cdr (assoc 10 elst)) norm 0)
  )
)

;; Blk2Coord
;; Returns a list of a block reference entities coordinates
(defun Blk2Coord (ref mat ins / blk ent lst)
  (setq blk (tblsearch "BLOCK" (cdr (assoc 2 (entget ref)))))
  (setq ent (cdr (assoc -2 blk)))
  (while ent
    (setq elst (entget ent)
	  typ  (cdr (assoc 0 elst))
    )
    (cond
      ((= "LINE" typ)
       (setq lst (cons (list typ
			     (mapcar '+ ins (mxv mat (cdr (assoc 10 elst))))
			     (mapcar '+ ins (mxv mat (cdr (assoc 11 elst))))
		       )
		       lst
		 )
       )
      )
      ((member typ '("POINT" "TEXT"))
       (setq lst (cons (list typ
			     (mapcar '+ ins (mxv mat (cdr (assoc 10 elst))))
		       )
		       lst
		 )
       )
      )
      ((= "INSERT" typ)
       (setq nent (EntMatrix ent))
       (setq lst
	      (append
		lst
		(Blk2Coord ent
			   (mxm  mat (car nent))
			   (mapcar '+ ins (mxv mat (cadr nent)))
		)
	      )
       )
      )
      (T nil)
    )
    (setq ent (entnext ent))
  )
  (cons (list (cdr (assoc 2 blk)) ins) lst)
)

;; 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 (function (lambda (r) (apply '+ (mapcar '* r v))))
	  m
  )
)

;; Multiply two matrices by Vladimir Nesterovsky
(defun mxm (m q)
  (mapcar (function (lambda (r) (mxv (trp q) r))) m)
)

;; Main function

(defun c:test (/ ss n ent mtx lst)
  (if (setq ss (ssget '((0 . "INSERT"))))
    (repeat (setq n (sslength ss))
      (setq ent	(ssname ss (setq n (1- n)))
	    mtx	(EntMatrix ent)
	    lst	(append (Blk2Coord ent (car mtx) (cadr mtx)) lst)
      )
    )
  )
  (mapcar 'print lst)
  (textscr)
  (princ)
)

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 413609_test.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 TEST để chạy lisp.

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

;; Entmatrix
;; Returns a list which first item is the 3X3 tranformation matrix and second item
;; the insertion point of a block refernce in its owner (space or block definition)
(defun EntMatrix (ename / elst ang norm)
  (setq	elst (entget ename)
	ang  (cdr (assoc 50 elst))
	norm (cdr (assoc 210 elst))
  )
  (list
    (mxm
      (mapcar (function (lambda (v) (trans v 0 norm T)))
	      '((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0))
      )
      (mxm
	(list (list (cos ang) (- (sin ang)) 0.0)
	      (list (sin ang) (cos ang) 0.0)
	      '(0.0 0.0 1.0)
	)
	(list (list (cdr (assoc 41 elst)) 0.0 0.0)
	      (list 0.0 (cdr (assoc 42 elst)) 0.0)
	      (list 0.0 0.0 (cdr (assoc 43 elst)))
	)
      )
    )
    (trans (cdr (assoc 10 elst)) norm 0)
  )
)

;; Blk2Coord
;; Returns a list of a block reference entities coordinates
(defun Blk2Coord (ref mat ins / blk ent lst)
  (setq blk (tblsearch "BLOCK" (cdr (assoc 2 (entget ref)))))
  (setq ent (cdr (assoc -2 blk)))
  (while ent
    (setq elst (entget ent)
	  typ  (cdr (assoc 0 elst))
    )
    (cond
      ((= "LINE" typ)
       (setq lst (cons (list typ
			     (mapcar '+ ins (mxv mat (cdr (assoc 10 elst))))
			     (mapcar '+ ins (mxv mat (cdr (assoc 11 elst))))
		       )
		       lst
		 )
       )
      )
      ((member typ '("POINT" "TEXT"))
       (setq lst (cons (list typ
			     (mapcar '+ ins (mxv mat (cdr (assoc 10 elst))))
		       )
		       lst
		 )
       )
      )
      ((= "INSERT" typ)
       (setq nent (EntMatrix ent))
       (setq lst
	      (append
		lst
		(Blk2Coord ent
			   (mxm  mat (car nent))
			   (mapcar '+ ins (mxv mat (cadr nent)))
		)
	      )
       )
      )
      (T nil)
    )
    (setq ent (entnext ent))
  )
  (cons (list (cdr (assoc 2 blk)) ins) lst)
)

;; 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 (function (lambda (r) (apply '+ (mapcar '* r v))))
	  m
  )
)

;; Multiply two matrices by Vladimir Nesterovsky
(defun mxm (m q)
  (mapcar (function (lambda (r) (mxv (trp q) r))) m)
)

;; Main function

(defun c:test (/ ss n ent mtx lst)
  (if (setq ss (ssget '((0 . "INSERT"))))
    (repeat (setq n (sslength ss))
      (setq ent	(ssname ss (setq n (1- n)))
	    mtx	(EntMatrix ent)
	    lst	(append (Blk2Coord ent (car mtx) (cadr mtx)) lst)
      )
    )
  )
  (mapcar 'print lst)
  (textscr)
  (princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Hỏi về Lisp (thuật toán, ý tưởng, coding,...). Xem đúng bài viết #413609 để đọc hướng dẫn và trao đổi thêm.

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