Jump to content

Lisp lệnh ADDVTX trong chủ đề “Viết lisp theo yêu cầu [phần 2]”


Về giá trị bulge
The bulge is the tangent of 1/4 of the included angle for the arc between the selected vertex and the next vertex in the polyline's vertex list.
A negative bulge value indicates that the arc goes clockwise from the selected vertex to the next vertex.
A bulge of 0 indicates a straight segment, and a bulge of 1 is a semicircle.
giá trị bulge là tang của 1/4 góc chắn phần tử (segment).
- Nếu bulge >0 : cung tròn theo ngược chiều kim đồng hồ, ngược lại cùng chiều kim đồng hồ.
- Nếu bulge = 0 : phần tử là đuờng thẳng
- Nếu bulge = 1 : phần tử là đuờng tròn

Gửi bạn Lisp thêm các đỉnh tại các vị trí giao nhau giữa đuờng LWPOLYLINE với tất cả các đối tuợng khác cắt qua nó (bao gồm : LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE).
Do không có thời gian test nhiều, nhờ các bạn kiểm tra dùm.

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 73658_addvtx.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 ADDVTX để chạy lisp.

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

(defun c:AddVtx (/ doc vl ov ss)
 (vl-load-com)
 (setq doc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
 (vla-StartUndoMark doc)
 (setq vl '("CMDECHO" "OSMODE" "ORTHOMODE") ; Sys Var list
       ov (mapcar 'getvar vl)) ; Get Old values  
 (mapcar 'setvar vl '(0 0 0)) ; Turn off CMDECHO, OSMODE, ORTHOMODE
 (prompt "\nChon Polyline them Vertex : ")
 (if (setq ss (ssget '((0 . "LWPOLYLINE"))))
   (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
     (foreach pt (get_interpts_with_touching ent)
(AddVtx ent pt)	) ) )
 (mapcar 'setvar vl ov) ; reset Sys Vars
 (vla-EndUndoMark doc)
 (princ)
 )

(defun AddVtx (entPL pt / obj pObj pa a1 a2 p1 p2 ce bu)
;; Transform any angle (in radians) into its equivalent between 0 and 2*pi
(defun ang<2pi (ang)
 (if (and (<= 0 ang) (< ang (* 2 pi)))
   ang
   (ang<2pi (rem (+ ang (* 2 pi)) (* 2 pi)))
   )
 )
; get center
(defun get_center (ent param / ang1 ang2 pt1 pt2)
 (setq ang1 (angle '(0 0 0) (vlax-curve-getFirstDeriv ent (+ 0.1 param)))
ang2 (angle '(0 0 0) (vlax-curve-getFirstDeriv ent (+ 0.9 param)))
)
 (if (or (/= ang1 ang2)
  (/= ang1 (angle '(0 0 0) (vlax-curve-getFirstDeriv ent (+ 0.5 param))))
  )
   (progn
     (setq pt1 (vlax-curve-getPointAtParam ent (+ 0.1 param))
    pt2 (vlax-curve-getPointAtParam ent (+ 0.9 param)))
     (inters pt1 (polar pt1 (- ang1 (/ pi 2)) 1.0) pt2 (polar pt2 (- ang2 (/ pi 2)) 1.0) nil)
     )
   )
 )
 ;--- AddVtx --------
 (setq obj (vlax-ename->vla-object entPL)
pObj t);PointAtObj
 (or
   (setq pa (vlax-curve-getParamAtPoint obj pt))
   (setq pa (vlax-curve-getParamAtPoint obj (vlax-curve-getClosestPointTo obj pt) )
  pObj nil)
   )
 (if (> (- pa (setq pa (fix pa))) 0.001); bo qua t/hop diem Pt trung voi dinh cua PLINE
   (progn
     (if (and (setq ce (get_center obj pa)) pObj)
(progn
  (setq p1 (vlax-curve-getPointAtParam obj pa)
	p2 (vlax-curve-getPointAtParam obj (1+ pa))  )
  (if (< pi (ang<2pi (- (angle pt p2) (angle p1 pt)))(* 2 pi))
    (setq a1 (- (ang<2pi (- (angle ce p1) (angle ce pt))))
	  a2 (- (ang<2pi (- (angle ce pt) (angle ce p2)))) )
    (setq a1 (ang<2pi (- (angle ce pt) (angle ce p1)))
	  a2 (ang<2pi (- (angle ce p2) (angle ce pt)))  )
    )
  (setq bu (list (cons pa (/ (sin (/ a1 4.0)) (cos (/ a1 4.0)))) ;gia tri Bulge tai dinh truoc v/tri chen
		 (cons (1+ pa) (/ (sin (/ a2 4.0)) (cos (/ a2 4.0)))) ;gia tri Bulge tai dinh moi chen
		 ))
  )
(setq bu nil)
)
     (vlax-invoke obj 'AddVertex (1+ pa) (list (car pt) (cadr pt)));them dinh
     (setq bu (mapcar '(lambda (x) (vla-setBulge obj (car x) (cdr x))) bu));cap nhat Bulge
     )
   )
 )

(defun get_interpts_with_touching (ent / obj bl tr ss lst intpts)
 ;;  return a list of lists grouped by 3 from a flat list
 (defun list->3pair (old / new)
   (while (setq new (cons (list (car old) (cadr old) (caddr old)) new)
	 old (cdddr old)))
   (reverse new)
   )
 ;------------ ss_interpts
 (setq obj (vlax-ename->vla-object ent))
 (vla-getBoundingBox obj 'bl 'tr)
 (and
   (setq bl (vlax-safearray->list bl)
  tr (vlax-safearray->list tr))
   (setq ss (ssget "_c" bl tr (list (cons 0 "*LINE,ARC,CIRCLE,ELLIPSE")
			     (cons 410 (getvar "ctab"))))
  )
   (setq lst_pt nil
  lst (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))))
   (foreach intoObj lst
     (if (setq intpts (vlax-invoke Obj 'IntersectWith intoObj acExtendNone))
(setq lst_pt (append (list->3pair intpts) lst_pt)) ))
     )
 lst_pt
 )

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Viết lisp theo yêu cầu [phần 2]. Xem đúng bài viết #73658 để đọc hướng dẫn và trao đổi thêm.

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