Chuyển đến nội dung
Diễn đàn CADViet
namhai

lisp xóa tất cả các đối tượng trong 1 vùng kín

Các bài được khuyến nghị

~

 

 

Bạn dùng lisp của bác Thiệp dưới đây, sau khi bắt đối tượng xong bạn muốn move, copy hay xóa thì tùy bạn.

 
(defun c:erC (/ sc cur p0 P1 L1 d L n ssgDEL glength)
  (princ "\nFree lisp from www.cadviet.com")
  (command "undo" "be")
  (setvar "osmode" 0)
  (setq sc 2009
cur (car (entsel "\nchon duong: "))
glength (lambda (e) (command ".lengthen" e "") (getvar "perimeter"))
d (/ (glength cur) sc)
l1 0.0
p0 (vlax-curve-getStartPoint cur)
L (list p0)
  )
  (redraw cur 4)
  (repeat sc
    (setq l1 (+ l1 d)
 p1 (vlax-curve-getPointAtDist cur l1)
    )
    (setq L (append L (List p1)))
  )
  (setq ssgDEL (ssget "WP" L))  
  (command "undo" "end")
  (princ  "\nChuc cac ban may man va thanh cong - Thiep 0918841230" )
  (sssetfirst nil ssgDel)
  (princ)
)
(vl-load-com)

mình chạy cái này rồi muốn xóa thì lại xóa mất dường bao. bác nào biết giúp tui với

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

~

 

mình chạy cái này rồi muốn xóa thì lại xóa mất dường bao. bác nào biết giúp tui với

Hề hề hề,

Bạn thử làm như sau xem có hiệu nghiệm không nhé.

Thêm dòng code sau:

(setq ssgdel (ssdel cur ssgdel))

vào bên dưới dòng code

(setq ssgDEL (ssget "WP" L))

Sau đó lưu lại file và test lại.

 

Hy vọng đúng ý bạn.

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

chào các anh, em muốn hỏi các anh làm sao để kết hợp với lisp này của em nhỉ, em muốn chọn tất cả các đám mây và xóa toàn bộ các hình bên ngoài còn lại bên ngoài các đám mây đó, file lisp chọn đám mây em có đính kèm ở dưới, mong các anh giúp đỡ, em cám ơn

 

Lisp chọn đám mây

 

(defun c:cdm (/ ss i sn name lst)
  (setq i 0 ssrc (ssadd))
  (if (setq ss(ssget "x" '((0 . "LWPOLYLINE") (410 . "Model")(70 . 1)(-4 . "/=")(42 . 0))))
    (repeat (sslength ss)
      (setq e (ssname ss i))
      (setq elst (vl-remove-if-not '(lambda (x) (= 42 (car x))) (entget e)))
      (if
        (and
          (< 2 (length elst))
          (vl-every '(lambda (x) (not (zerop (cdr x)))) elst)     
        )
        (ssadd e ssrc)
      )
      (setq i (1+ i))
    )
  )
  (sssetfirst nil ssrc)
  (princ)
) ;end
 

  • Vote giảm 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Các bác cho em hỏi, nếu giờ đối tượng xóa của em nằm trong một đối tượng là PolyLine 3d thi làm như thếnào .Em có file minh họa đây

http://www.cadviet.com/upfiles/5/140252_hoi_cad_viet.dwg

 

Cảm ơn các bác

 

P.S Đã giải quyết được rồi, xin lỗi các bác nhé

Chỉnh sửa theo quantvxd

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

mình đã sử dụng qua một số lisp ( xóa đối tượng trong, ngoài đường bao khép kín  có trên diễn đàn nhứ erc.lsp của bạn thiep, ewb.lsp của bạn Gia Bach, xoa.lsp của bạn kangkung  ) 

mình thấy các list đó chỉ mới xét đến yếu tố đối tượng dính or ko dính đường bao, chứ chưa xét đến tọa độ của đối tượng nằm trong hay ngoài đường bao.

giờ mình muốn xóa các text có tọa độ nằm trong or nằm ngoài đường bao khép kin thì làm thế nào ạh?

các bạn giúp mình vấn đề này với.

  • Vote giảm 2

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

mình đã sử dụng qua một số lisp ( xóa đối tượng trong, ngoài đường bao khép kín  có trên diễn đàn nhứ erc.lsp của bạn thiep, ewb.lsp của bạn Gia Bach, xoa.lsp của bạn kangkung  ) 

mình thấy các list đó chỉ mới xét đến yếu tố đối tượng dính or ko dính đường bao, chứ chưa xét đến tọa độ của đối tượng nằm trong hay ngoài đường bao.

giờ mình muốn xóa các text có tọa độ nằm trong or nằm ngoài đường bao khép kin thì làm thế nào ạh?

các bạn giúp mình vấn đề này với.

bạn có lisp erc.lsp của bạn thiep, ewb.lsp của bạn Gia Bach thì cho mình xin với

mình chỉ có lisp của bạn KangKung thôi còn 2 cái kia: cái thì link die, cái thì lỗi nhưng ko khắc phục được

thanks

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Bạn thích code hay là thích thuật toán ?

mình thích tất nhưng mình chưa biết về lisp nhiều chỉ mới biết đến tên lệnh thôi nên chỉ cần code thôi bạn à

nếu có cho mình xin với nhé

thanks bạn

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Về thuật tóan thì mình hay áp dụng cách này để kiểm tra 1 điểm d0 có ở trong 1 vùng kín hay không.

Xem xét giao điểm của vùng đó với đường nửa vô tận trong đó 1 đầu là điểm vô cùng và đầu kia là điểm d0

ví dụ đường (list (-1000000000.0 (cadr d0)) d0)

Nếu số giao điểm - lẻ là ở trong và - chẵn là ở ngoài.

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Về thuật tóan thì mình hay áp dụng cách này để kiểm tra 1 điểm d0 có ở trong 1 vùng kín hay không.

Xem xét giao điểm của vùng đó với đường nửa vô tận trong đó 1 đầu là điểm vô cùng và đầu kia là điểm d0

ví dụ đường (list (-1000000000.0 (cadr d0)) d0)

Nếu số giao điểm - lẻ là ở trong và - chẵn là ở ngoài.

còn lisp thì sao bạn????

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

còn lisp thì sao bạn????

Vấn đề Bạn lớn quá đành nhờ các AE trong CadViet vậy.

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác
Vào lúc 2/7/2009 tại 09:12, gia_bach đã nói:

Chào các bạn.

Về cơ bản thì LISP ERC của bạn Thiệp đã giải quyết đuợc các yêu cầu xóa các đối tuợng trong, ngoài và giữa 2 đuờng bao.

Tuy nhiên với các đối tuợng có giao với đuờng bao thì Lisp ERC chưa hoàn chỉnh.

Để giải quyết vấn đề xóa các đối tuợng có giao với đuờng bao, tui dùng giải pháp là cắt các đối tuợng này tại giao điểm với đuờng bao, sử dụng hàm break_with của CAB trên www.TheSwamp.org

Do hàm break_with chỉ cắt các đối tuợng lines, lwplines, plines, splines, ellipse, circles & arcs nên với các đối tuợng còn lại như Text, Dimension,... LISP không giải quyết triệt để. :s_big:

 

Các bạn chạy thử và cho ý kiến. File

 


(defun c:EWB (/ ov vl ss1 ss2 ptLst plSet) ;EWB -> Erase With Boundary
 (defun *error* (msg)    
   (if ov (mapcar 'setvar vl ov)) ; reset Sys vars
   (princ (strcat "\n<< Error: " msg " >>")) ; Print Error Message
   (princ) ; Exit Cleanly
   )
 (command "_.undo" "_begin")
 (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

 (initget "T N G")
 (setq	bit (getkword "\nBan muon xoa Trong hay Ngoai duong bao, hay Giua 2 duong bao : " ) )
 (cond
   ((= bit "T") ;xoa Trong duong bao
    (princ"\n<<< Chon duong bao >>> ")
    (if (and (setq ss (ssget "_:S" '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
      (setq ssInside (GetssInside ss))
      (> (sslength ssInside) 0))
      (mapcar 'entdel (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssInside))) ) ; xoa ssInside
      )
    )

   ((= bit "G") ;xoa giua 2 duong bao
    (princ"\n<<< Chon duong bao ngoai >>> ")
    (setq ssN (ssget "_:S" '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
    (princ"\n<<< Chon duong bao trong >>> ")
    (setq ssT (ssget "_:S" '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE")))
   curT (ssname ssT 0)
   ssT (GetssInside ssT)
   ssN (GetssInside ssN))
    (if (and ssT (> (sslength ssT) 0) ssN (> (sslength ssN) 0) )
      (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssT)))
 (if (ssmemb e ssN) (ssdel e ssN)))
      )
    (if (ssmemb curT ssN) (ssdel curT ssN))
    (mapcar 'entdel (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssN))) ) ; xoa ss
    )

   ((= bit "N") ;xoa Ngoai duong bao
    (initget "T G")
    (setq bit (getkword "\nXoa Tat ca doi tuong ngoai duong bao, hay chi doi tuong Giao voi duong bao : " ) )
    (princ"\n<<< Chon duong bao >>> ")
    (setq ss (ssget "_:S" '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE")))
   cur (ssname ss 0))
    (if (= bit "T")
      (progn ;xoa Tat ca doi tuong ngoai duong bao
 (setq ssInside (GetssInside ss)
       ssAll (ssget "x" (list (cons 410 (getvar "ctab")))) )
 (if (and ssInside (> (sslength ssInside) 0) )
   (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssInside)))
     (if (ssmemb e ssAll) (ssdel e ssAll)))
   )
 (if (ssmemb cur ssAll) (ssdel cur ssAll))
 (mapcar 'entdel (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssAll))) ) ; xoa ss
 )
      ;chi xoa doi tuong Giao voi duong bao
      (if (and (setq ssOutside (GetssOutside ss))
	(> (sslength ssOutside) 0))
 (mapcar 'entdel (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssOutside))) ) ; xoa ssOutside
 )
      );if
    );;xoa Ngoai duong bao
   );cond

 (mapcar 'setvar vl ov) ; reset Sys Vars
 (command "_.undo" "_end")
 (princ)
)

(defun GetssOutside (ss2 / ptLst cur ssInside lstss1 ss1 ssTouching)  
 (if (and (setq lstss1 (gettouching ss2))
   (setq ss1 (ssadd))
   (mapcar '(lambda (x) (ssadd x ss1)) lstss1)
   )
   (progn ; co ssTouching 
     (break_with ss1 ss2 nil 0)
     (setq cur (ssname ss2 0)
    ssTouching (ssadd)
    ssOutside (ssadd))
     (mapcar '(lambda (x) (ssadd x ssTouching)) (gettouching ss2))
     ;loc ssTouching -> ssOutside
     (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssTouching)))
(if
  (or
    (not(insidep (vlax-curve-getStartPoint e) cur))
    (not(insidep (vlax-curve-getEndPoint e) cur))
    (not(insidep (vlax-curve-getPointAtParam e (/(+(vlax-curve-getStartParam e)(vlax-curve-getEndParam e))2)) cur))
    );or
  (ssadd e ssOutside)
  );if
);foreach
     );progn
   );if
 (if (ssmemb cur ssOutside) (ssdel cur ssOutside))
 ssOutside
 )

(defun GetssInside (ss2 / ptLst cur ssInside lstss1 ss1 ssTouching)
 (setq ptLst (GetPtLst (setq cur (ssname ss2 0)))
ssInside (ssget "_WP" ptLst ) )  
 (if (and (setq lstss1 (gettouching ss2))
   (setq ss1 (ssadd))
   (mapcar '(lambda (x) (ssadd x ss1)) lstss1)
   )
   (progn ; co ssTouching
     (break_with ss1 ss2 nil 0)
     (setq ssTouching (ssadd))
     (mapcar '(lambda (x) (ssadd x ssTouching)) (gettouching ss2))
     ;loc ssTouching -> ssInside
     (or ssInside (setq ssInside (ssadd)) )
     (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssTouching)))
(if
  (and (insidep (vlax-curve-getStartPoint e) cur)
       (insidep (vlax-curve-getEndPoint e) cur)
       (insidep (vlax-curve-getPointAtParam e (/(+(vlax-curve-getStartParam e)(vlax-curve-getEndParam e))2))  cur)
       )
  (ssadd e ssInside)
  );if
);foreach
     );progn
   );if
 (if (ssmemb cur ssInside) (ssdel cur ssInside))
 ssInside
 )

(defun GetPtLst (obj / startparam endparam anginc delta div inc pt ptlst)
 (defun ZClosed (lst)
   (if (and (vlax-curve-isClosed obj)
      (not(equal (car lst)(last lst) 1e-6)))
     (append lst (list (car lst)))
     lst))

 (or (eq (type obj) 'VLA-OBJECT)
   (setq obj (vlax-ename->vla-object obj)))
 (setq typ (vlax-get obj 'ObjectName))
 (if (or (eq typ "AcDbCircle") (eq typ "AcDbEllipse"))
   (progn
     (setq param 0)
     (while (< param (* pi 2))
(setq pt (vlax-curve-getPointAtParam obj param)
      ptlst (cons pt ptlst)
      param (+ (/ (* pi 2) 72) param))
)
     (reverse ptlst)
     )
   (progn ;Pline (eq typ "AcDbPolyline")
     (setq param (vlax-curve-getStartParam obj)
    endparam (vlax-curve-getEndParam obj)
    anginc (* pi (/ 7.5 180.0)))
     (setq tparam param)
     (while (<= param endparam)
(setq pt (vlax-curve-getPointAtParam obj param))
(if (not (equal pt (car ptlst) 1e-12))
  (setq ptlst (cons pt ptlst)))
(if  (and (/= param endparam)
	  (setq blg (abs (vlax-invoke obj 'GetBulge param)))
	  (/= 0 blg))
  (progn
    (setq delta (* 4 (atan blg)) ;included angle
	  inc (/ 1.0 (1+ (fix (/ delta anginc))))
                 arcparam (+ param inc))
    (while (< arcparam (1+ param))
      (setq pt (vlax-curve-getPointAtParam obj arcparam)
                   ptlst (cons pt ptlst)
                   arcparam (+ inc arcparam))))
  )
(setq param (1+ param))
)
     (if (and (apply 'and ptlst)
       (> (length ptlst) 1))
(ZClosed (reverse ptlst))
)
     )
   )
 )



;;  Copyright © 2009, Lee McDonnell
;;  (Contact Lee Mac, CADTutor.net)
(defun insidep  (pt Obj / Obj Tol ang doc spc flag int lin xV yV)
 (defun vlax-list->3D-point (lst flag)
 (if lst
   (cons ((if flag car cadr) lst)
         (vlax-list->3D-point (cdddr lst) flag))))
 (or (eq 'VLA-OBJECT (type Obj))
     (setq Obj (vlax-ename->vla-object Obj)))
 (if (not(vlax-curve-getParamAtPoint Obj pt))
   (progn
 (setq Tol  (/ pi 6) ; Uncertainty
       ang  0.0 flag T)
 (setq doc (vla-get-ActiveDocument (vlax-get-Acad-Object))
       spc (if (zerop (vla-get-activespace doc))
             (if (= (vla-get-mspace doc) :vlax-true)
               (vla-get-modelspace doc)
               (vla-get-paperspace doc))
             (vla-get-modelspace doc)))
 (while (and (< ang (* 2 pi)) flag)
   (setq flag (and
                (setq int
                  (vlax-invoke
                    (setq lin
                      (vla-addLine spc
                        (vlax-3D-point pt)
                          (vlax-3D-point
                            (polar pt ang
                              (if (vlax-property-available-p Obj 'length)
                                (vla-get-length Obj) 1.0)))))
                                 'IntersectWith Obj
                                   acExtendThisEntity))
                (<= 6 (length int))
                (setq xV (vl-sort (vlax-list->3D-point int T) '<)
                      yV (vl-sort (vlax-list->3D-point int nil) '<))
                (or (<= (car xV) (car pt) (last xV))
                    (<= (car yV) (cadr pt) (last yV))))
         ang  (+ ang Tol))
   (vla-delete lin))
 flag
 )
   T
   ))


;;; Author: Copyrightゥ 2006-2008 Charles Alan Butler 
;;; Contact @  www.TheSwamp.org
;;===========================================================================
 ;;  get all objects touching entities in the sscross                         
 ;;  limited obj types to "LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE"
 ;;  returns a list of enames
 ;;===========================================================================
(defun gettouching (sscros / ss lst lstb lstc objl)
   (and
     (setq lstb (vl-remove-if 'listp (mapcar 'cadr (ssnamex sscros)))
           objl (mapcar 'vlax-ename->vla-object lstb)
     )
     (setq ss (ssget "_A" (list (cons 0 "LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE")
			 (cons 410 (getvar "ctab"))))
     )
     (setq lst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
     (setq lst (mapcar 'vlax-ename->vla-object lst))
     (mapcar
       '(lambda (x)
          (mapcar
            '(lambda (y)
               (if (not
                     (vl-catch-all-error-p
                       (vl-catch-all-apply
                         '(lambda ()
                            (vlax-safearray->list
                              (vlax-variant-value
                                (vla-intersectwith y x acextendnone)
                              ))))))
                 (setq lstc (cons (vlax-vla-object->ename x) lstc))
               )
             ) objl)
        ) lst)
   )
   lstc
 )
;;; Author: Copyrightゥ 2006-2008 Charles Alan Butler 
;;; Contact @  www.TheSwamp.org
(defun break_with (ss2brk ss2brkwith self Gap / cmd intpts lst masterlist ss ssobjs
                  onlockedlayer ssget->vla-list list->3pair GetNewEntities oc
                  get_interpts break_obj GetLastEnt LastEntInDatabase ss2brkwithList
                 )
 ;; ss2brk     selection set to break
 ;; ss2brkwith selection set to use as break points
 ;; self       when true will allow an object to break itself
 ;;            note that plined will break at each vertex
 ;;
 ;; return list of enames of new objects  
 (vl-load-com)  
 (princ "\nCalculating Break Points, Please Wait.\n")
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;                S U B   F U N C T I O N S                      
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 ;;  return T if entity is on a locked layer
 (defun onlockedlayer (ename / entlst)
   (setq entlst (tblsearch "LAYER" (cdr (assoc 8 (entget ename)))))
   (= 4 (logand 4 (cdr (assoc 70 entlst))))
 )

 ;;  return a list of objects from a selection set
;|  (defun ssget->vla-list (ss)
   (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss ))))
 )|;
 (defun ssget->vla-list (ss / i ename allobj) ; this is faster, changed in ver 1.7
      (setq i -1)
      (while (setq  ename (ssname ss (setq i (1+ i))))
        (setq allobj (cons (vlax-ename->vla-object ename) allobj))
      )
      allobj
 )

 ;;  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)
 )

;;=====================================
;;  return a list of intersect points  
;;=====================================
(defun get_interpts (obj1 obj2 / iplist)
 (if (not (vl-catch-all-error-p
            (setq iplist (vl-catch-all-apply
                           'vlax-safearray->list
                           (list
                             (vlax-variant-value
                               (vla-intersectwith obj1 obj2 acextendnone)
                             ))))))
   iplist
 )
)

;;========================================
;;  Break entity at break points in list  
;;========================================
(defun break_obj (ent brkptlst BrkGap / brkobjlst en enttype maxparam closedobj
                 minparam obj obj2break p1param p2param brkpt2 dlst idx brkptS
                 brkptE brkpt result GapFlg result ignore dist tmppt
                 #ofpts 2gap enddist lastent obj2break stdist
                )
 (or BrkGap (setq BrkGap 0.0)) ; default to 0
 (setq BrkGap (/ BrkGap 2.0)) ; if Gap use 1/2 per side of break point

 (setq obj2break ent
       brkobjlst (list ent)
       enttype   (cdr (assoc 0 (entget ent)))
       GapFlg    (not (zerop BrkGap)) ; gap > 0
       closedobj (vlax-curve-isclosed obj2break)
 )
 ;; when zero gap no need to break at end points
 (if (zerop Brkgap)
   (setq spt (vlax-curve-getstartpoint ent)
         ept (vlax-curve-getendpoint ent)
         brkptlst (vl-remove-if '(lambda(x) (or (< (distance x spt) 0.0001)
                                                (< (distance x ept) 0.0001)))
                                brkptlst)
   )
 )
 (if brkptlst
   (progn
 ;;  sort break points based on the distance along the break object
 ;;  get distance to break point, catch error if pt is off end
 ;; ver 2.0 fix - added COND to fix break point is at the end of a
 ;; line which is not a valid break but does no harm
 (setq brkptlst (mapcar '(lambda(x) (list x (vlax-curve-getdistatparam obj2break
                                              ;; ver 2.0 fix
                                              (cond ((vlax-curve-getparamatpoint obj2break x))
                                                  ((vlax-curve-getparamatpoint obj2break
                                                    (vlax-curve-getclosestpointto obj2break x))))))
                           ) brkptlst))
 ;; sort primary list on distance
 (setq brkptlst (vl-sort brkptlst '(lambda (a1 a2) (< (cadr a1) (cadr a2)))))

 (if GapFlg ; gap > 0
   ;; Brkptlst starts as the break point and then a list of pairs of points
   ;;  is creates as the break points
   (progn
     ;;  create a list of list of break points
     ;;  ((idx# stpoint distance)(idx# endpoint distance)...)
     (setq idx 0)
     (foreach brkpt brkptlst

       ;; ----------------------------------------------------------
       ;;  create start break point, then create end break point    
       ;;  ((idx# startpoint distance)(idx# endpoint distance)...)  
       ;; ----------------------------------------------------------
       (setq dist (cadr brkpt)) ; distance to center of gap
       ;;  subtract gap to get start point of break gap
       (cond
         ((and (minusp (setq stDist (- dist BrkGap))) closedobj )
          (setq stdist (+ (vlax-curve-getdistatparam obj2break
                            (vlax-curve-getendparam obj2break)) stDist))
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                        (vlax-curve-getparamatdist obj2break stDist))
                                 stDist) dlst))
          )
         ((minusp stDist) ; off start of object so get startpoint
          (setq dlst (cons (list idx (vlax-curve-getstartpoint obj2break) 0.0) dlst))
          )
         (t
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                        (vlax-curve-getparamatdist obj2break stDist))
                                 stDist) dlst))
         )
       )
       ;;  add gap to get end point of break gap
       (cond
         ((and (> (setq stDist (+ dist BrkGap))
                  (setq endDist (vlax-curve-getdistatparam obj2break
                                    (vlax-curve-getendparam obj2break)))) closedobj )
          (setq stdist (- stDist endDist))
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                        (vlax-curve-getparamatdist obj2break stDist))
                                 stDist) dlst))
          )
         ((> stDist endDist) ; off end of object so get endpoint
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                       (vlax-curve-getendparam obj2break))
                                 endDist) dlst))
          )
         (t
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                        (vlax-curve-getparamatdist obj2break stDist))
                                 stDist) dlst))
         )
       )
       ;; -------------------------------------------------------
       (setq idx (1+ IDX))
     ) ; foreach brkpt brkptlst


     (setq dlst (reverse dlst))
     ;;  remove the points of the gap segments that overlap
     (setq idx -1
           2gap (* BrkGap 2)
           #ofPts (length Brkptlst)
     )
     (while (<= (setq idx (1+ idx)) #ofPts)
       (cond
         ((null result) ; 1st time through
          (setq result (list (car dlst)) ; get first start point
                result (cons (nth (1+(* idx 2)) dlst) result))
         )
         ((= idx #ofPts) ; last pass, check for wrap
          (if (and closedobj (> #ofPts 1)
                   (<= (+(- (vlax-curve-getdistatparam obj2break
                           (vlax-curve-getendparam obj2break))
                         (cadr (last BrkPtLst))) (cadar BrkPtLst)) 2Gap))
            (progn
              (if (zerop (rem (length result) 2))
                (setq result (cdr result)) ; remove the last end point
              )
              ;;  ignore previous endpoint and present start point
              (setq result (cons (cadr (reverse result)) result) ; get last end point
                    result (cdr (reverse result))
                    result (reverse (cdr result)))
            )
          )
         )
         ;; Break Gap Overlaps
         ((< (cadr (nth idx Brkptlst)) (+ (cadr (nth (1- idx) Brkptlst)) 2Gap))
          (if (zerop (rem (length result) 2))
            (setq result (cdr result)) ; remove the last end point
          )
          ;;  ignore previous endpoint and present start point
          (setq result (cons (nth (1+(* idx 2)) dlst) result)) ; get present end point
          )
         ;; Break Gap does Not Overlap previous point 
         (t
          (setq result (cons (nth (* idx 2) dlst) result)) ; get this start point
          (setq result (cons (nth (1+(* idx 2)) dlst) result)) ; get this end point
         )
       ) ; end cond stmt
     ) ; while

     (setq dlst     (reverse result)
           brkptlst nil)
     (while dlst ; grab the points only
       (setq brkptlst (cons (list (cadar dlst)(cadadr dlst)) brkptlst)
             dlst   (cddr dlst))
     )
   )
 )
 ;;   -----------------------------------------------------

 ;; (if (equal  a ent) (princ)) ; debug CAB  -------------

 (foreach brkpt (reverse brkptlst)
   (if GapFlg ; gap > 0
     (setq brkptS (car brkpt)
           brkptE (cadr brkpt))
     (setq brkptS (car brkpt)
           brkptE brkptS)
   )
   ;;  get last entity created via break in case multiple breaks
   (if brkobjlst
     (progn
       (setq tmppt brkptS) ; use only one of the pair of breakpoints
       ;;  if pt not on object x, switch objects
       (if (not (numberp (vl-catch-all-apply
                           'vlax-curve-getdistatpoint (list obj2break tmppt))))
         (progn ; find the one that pt is on
           (setq idx (length brkobjlst))
           (while (and (not (minusp (setq idx (1- idx))))
                       (setq obj (nth idx brkobjlst))
                       (if (numberp (vl-catch-all-apply
                                      'vlax-curve-getdistatpoint (list obj tmppt)))
                         (null (setq obj2break obj)) ; switch objects, null causes exit
                         t
                       )
                  )
           )
         )
       )
     )
   )

   (setq closedobj (vlax-curve-isclosed obj2break))
   (if GapFlg ; gap > 0
     (if closedobj
       (progn ; need to break a closed object
         (setq brkpt2 (vlax-curve-getPointAtDist obj2break
                    (- (vlax-curve-getDistAtPoint obj2break brkptE) 0.00001)))
         (command "._break" obj2break "_non" (trans brkpt2 0 1)
                  "_non" (trans brkptE 0 1))
         (and (= "CIRCLE" enttype) (setq enttype "ARC"))
         (setq BrkptE brkpt2)
       )
     )

     (if (and closedobj 
              (not (setq brkptE (vlax-curve-getPointAtDist obj2break
                      (+ (vlax-curve-getdistatparam obj2break
                           ;;(vlax-curve-getparamatpoint obj2break brkpts)) 0.00001))))
                           ;; ver 2.0 fix
                           (cond ((vlax-curve-getparamatpoint obj2break brkpts))
                                 ((vlax-curve-getparamatpoint obj2break
                                     (vlax-curve-getclosestpointto obj2break brkpts))))) 0.00001)))))
       (setq brkptE (vlax-curve-getPointAtDist obj2break
                      (- (vlax-curve-getdistatparam obj2break
                           ;;(vlax-curve-getparamatpoint obj2break brkpts)) 0.00001)))
                           ;; ver 2.0 fix
                           (cond ((vlax-curve-getparamatpoint obj2break brkpts))
                                 ((vlax-curve-getparamatpoint obj2break
                                     (vlax-curve-getclosestpointto obj2break brkpts))))) 0.00001)))
      )
   ) ; endif

   ;; (if (null brkptE) (princ)) ; debug

   (setq LastEnt (GetLastEnt))
   (command "._break" obj2break "_non" (trans brkptS 0 1) "_non" (trans brkptE 0 1))
   (and *BrkVerbose* (princ (setq *brkcnt* (1+ *brkcnt*))) (princ "\r"))
   (and (= "CIRCLE" enttype) (setq enttype "ARC"))
   (if (and (not closedobj) ; new object was created
            (not (equal LastEnt (entlast))))
       (setq brkobjlst (cons (entlast) brkobjlst))
   )
 )
 )
 ) ; endif brkptlst

) ; defun break_obj

;;====================================
;;  CAB - get last entity in datatbase
(defun GetLastEnt ( / ename result )
 (if (setq result (entlast))
   (while (setq ename (entnext result))
     (setq result ename)
   )
 )
 result
)
;;===================================
;;  CAB - return a list of new enames
(defun GetNewEntities (ename / new)
 (cond
   ((null ename) (alert "Ename nil"))
   ((eq 'ENAME (type ename))
     (while (setq ename (entnext ename))
       (if (entget ename) (setq new (cons ename new)))
     )
   )
   ((alert "Ename wrong type."))
 )
 new
)

 ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ;;         S T A R T  S U B R O U T I N E   H E R E              
 ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   (setq LastEntInDatabase (GetLastEnt))
   (if (and ss2brk ss2brkwith)
   (progn
     (setq oc 0
           ss2brkwithList (ssget->vla-list ss2brkwith))
     (if (> (* (sslength ss2brk)(length ss2brkwithList)) 5000)
       (setq *BrkVerbose* t)
     )
     (and *BrkVerbose*
          (princ (strcat "Objects to be Checked: "
           (itoa (* (sslength ss2brk)(length ss2brkwithList))) "\n")))
     ;;  CREATE a list of entity & it's break points
     (foreach obj (ssget->vla-list ss2brk) ; check each object in ss2brk
       (if (not (onlockedlayer (vlax-vla-object->ename obj)))
         (progn
           (setq lst nil)
           ;; check for break pts with other objects in ss2brkwith
           (foreach intobj  ss2brkwithList
             (if (and (or self (not (equal obj intobj)))
                      (setq intpts (get_interpts obj intobj))
                 )
               (setq lst (append (list->3pair intpts) lst)) ; entity w/ break points
             )
             (and *BrkVerbose* (princ (strcat "Objects Checked: " (itoa (setq oc (1+ oc))) "\r")))
           )
           (if lst
             (setq masterlist (cons (cons (vlax-vla-object->ename obj) lst) masterlist))
           )
         )
       )
     )    
     (and *BrkVerbose* (princ "\nBreaking Objects.\n"))
     (setq *brkcnt* 0) ; break counter
     ;;  masterlist = ((ent brkpts)(ent brkpts)...)
     (if masterlist
       (foreach obj2brk masterlist
         (break_obj (car obj2brk) (cdr obj2brk) Gap)
       )
     )
     )
 )
;;==============================================================
  (and (zerop *brkcnt*) (princ "\nNone to be broken."))
  (setq *BrkVerbose* nil)
 (GetNewEntities LastEntInDatabase) ; return list of enames of new objects
)

 

 

Cảm ơn anh gia _bach đã search lisp hay cho anh em 4r, lisp này dùng rất hay. Nếu anh gia _bach bổ sung thêm tính năng lisp có thể nhớ lại thao tác lần trước mình ghi kiểu xóa đối tương T,N hoặc G thì lisp rất hoàn thiện.

  • Vote tăng 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác
Vào lúc 2/7/2009 tại 09:12, gia_bach đã nói:
Vào lúc 2/7/2009 tại 09:12, gia_bach đã nói:
 

 


(defun c:EWB (/ ov vl ss1 ss2 ptLst plSet) ;EWB -> Erase With Boundary
 (defun *error* (msg)    
   (if ov (mapcar 'setvar vl ov)) ; reset Sys vars
   (princ (strcat "\n<< Error: " msg " >>")) ; Print Error Message
   (princ) ; Exit Cleanly
   )
 (command "_.undo" "_begin")
 (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

 (initget "T N G")
 (setq	bit (getkword "\nBan muon xoa Trong hay Ngoai duong bao, hay Giua 2 duong bao : " ) )
 (cond
   ((= bit "T") ;xoa Trong duong bao
    (princ"\n<<< Chon duong bao >>> ")
    (if (and (setq ss (ssget "_:S" '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
      (setq ssInside (GetssInside ss))
      (> (sslength ssInside) 0))
      (mapcar 'entdel (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssInside))) ) ; xoa ssInside
      )
    )

   ((= bit "G") ;xoa giua 2 duong bao
    (princ"\n<<< Chon duong bao ngoai >>> ")
    (setq ssN (ssget "_:S" '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE"))))
    (princ"\n<<< Chon duong bao trong >>> ")
    (setq ssT (ssget "_:S" '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE")))
   curT (ssname ssT 0)
   ssT (GetssInside ssT)
   ssN (GetssInside ssN))
    (if (and ssT (> (sslength ssT) 0) ssN (> (sslength ssN) 0) )
      (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssT)))
 (if (ssmemb e ssN) (ssdel e ssN)))
      )
    (if (ssmemb curT ssN) (ssdel curT ssN))
    (mapcar 'entdel (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssN))) ) ; xoa ss
    )

   ((= bit "N") ;xoa Ngoai duong bao
    (initget "T G")
    (setq bit (getkword "\nXoa Tat ca doi tuong ngoai duong bao, hay chi doi tuong Giao voi duong bao : " ) )
    (princ"\n<<< Chon duong bao >>> ")
    (setq ss (ssget "_:S" '((0 . "LWPOLYLINE,CIRCLE,ELLIPSE")))
   cur (ssname ss 0))
    (if (= bit "T")
      (progn ;xoa Tat ca doi tuong ngoai duong bao
 (setq ssInside (GetssInside ss)
       ssAll (ssget "x" (list (cons 410 (getvar "ctab")))) )
 (if (and ssInside (> (sslength ssInside) 0) )
   (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssInside)))
     (if (ssmemb e ssAll) (ssdel e ssAll)))
   )
 (if (ssmemb cur ssAll) (ssdel cur ssAll))
 (mapcar 'entdel (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssAll))) ) ; xoa ss
 )
      ;chi xoa doi tuong Giao voi duong bao
      (if (and (setq ssOutside (GetssOutside ss))
	(> (sslength ssOutside) 0))
 (mapcar 'entdel (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssOutside))) ) ; xoa ssOutside
 )
      );if
    );;xoa Ngoai duong bao
   );cond

 (mapcar 'setvar vl ov) ; reset Sys Vars
 (command "_.undo" "_end")
 (princ)
)

(defun GetssOutside (ss2 / ptLst cur ssInside lstss1 ss1 ssTouching)  
 (if (and (setq lstss1 (gettouching ss2))
   (setq ss1 (ssadd))
   (mapcar '(lambda (x) (ssadd x ss1)) lstss1)
   )
   (progn ; co ssTouching 
     (break_with ss1 ss2 nil 0)
     (setq cur (ssname ss2 0)
    ssTouching (ssadd)
    ssOutside (ssadd))
     (mapcar '(lambda (x) (ssadd x ssTouching)) (gettouching ss2))
     ;loc ssTouching -> ssOutside
     (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssTouching)))
(if
  (or
    (not(insidep (vlax-curve-getStartPoint e) cur))
    (not(insidep (vlax-curve-getEndPoint e) cur))
    (not(insidep (vlax-curve-getPointAtParam e (/(+(vlax-curve-getStartParam e)(vlax-curve-getEndParam e))2)) cur))
    );or
  (ssadd e ssOutside)
  );if
);foreach
     );progn
   );if
 (if (ssmemb cur ssOutside) (ssdel cur ssOutside))
 ssOutside
 )

(defun GetssInside (ss2 / ptLst cur ssInside lstss1 ss1 ssTouching)
 (setq ptLst (GetPtLst (setq cur (ssname ss2 0)))
ssInside (ssget "_WP" ptLst ) )  
 (if (and (setq lstss1 (gettouching ss2))
   (setq ss1 (ssadd))
   (mapcar '(lambda (x) (ssadd x ss1)) lstss1)
   )
   (progn ; co ssTouching
     (break_with ss1 ss2 nil 0)
     (setq ssTouching (ssadd))
     (mapcar '(lambda (x) (ssadd x ssTouching)) (gettouching ss2))
     ;loc ssTouching -> ssInside
     (or ssInside (setq ssInside (ssadd)) )
     (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssTouching)))
(if
  (and (insidep (vlax-curve-getStartPoint e) cur)
       (insidep (vlax-curve-getEndPoint e) cur)
       (insidep (vlax-curve-getPointAtParam e (/(+(vlax-curve-getStartParam e)(vlax-curve-getEndParam e))2))  cur)
       )
  (ssadd e ssInside)
  );if
);foreach
     );progn
   );if
 (if (ssmemb cur ssInside) (ssdel cur ssInside))
 ssInside
 )

(defun GetPtLst (obj / startparam endparam anginc delta div inc pt ptlst)
 (defun ZClosed (lst)
   (if (and (vlax-curve-isClosed obj)
      (not(equal (car lst)(last lst) 1e-6)))
     (append lst (list (car lst)))
     lst))

 (or (eq (type obj) 'VLA-OBJECT)
   (setq obj (vlax-ename->vla-object obj)))
 (setq typ (vlax-get obj 'ObjectName))
 (if (or (eq typ "AcDbCircle") (eq typ "AcDbEllipse"))
   (progn
     (setq param 0)
     (while (< param (* pi 2))
(setq pt (vlax-curve-getPointAtParam obj param)
      ptlst (cons pt ptlst)
      param (+ (/ (* pi 2) 72) param))
)
     (reverse ptlst)
     )
   (progn ;Pline (eq typ "AcDbPolyline")
     (setq param (vlax-curve-getStartParam obj)
    endparam (vlax-curve-getEndParam obj)
    anginc (* pi (/ 7.5 180.0)))
     (setq tparam param)
     (while (<= param endparam)
(setq pt (vlax-curve-getPointAtParam obj param))
(if (not (equal pt (car ptlst) 1e-12))
  (setq ptlst (cons pt ptlst)))
(if  (and (/= param endparam)
	  (setq blg (abs (vlax-invoke obj 'GetBulge param)))
	  (/= 0 blg))
  (progn
    (setq delta (* 4 (atan blg)) ;included angle
	  inc (/ 1.0 (1+ (fix (/ delta anginc))))
                 arcparam (+ param inc))
    (while (< arcparam (1+ param))
      (setq pt (vlax-curve-getPointAtParam obj arcparam)
                   ptlst (cons pt ptlst)
                   arcparam (+ inc arcparam))))
  )
(setq param (1+ param))
)
     (if (and (apply 'and ptlst)
       (> (length ptlst) 1))
(ZClosed (reverse ptlst))
)
     )
   )
 )



;;  Copyright © 2009, Lee McDonnell
;;  (Contact Lee Mac, CADTutor.net)
(defun insidep  (pt Obj / Obj Tol ang doc spc flag int lin xV yV)
 (defun vlax-list->3D-point (lst flag)
 (if lst
   (cons ((if flag car cadr) lst)
         (vlax-list->3D-point (cdddr lst) flag))))
 (or (eq 'VLA-OBJECT (type Obj))
     (setq Obj (vlax-ename->vla-object Obj)))
 (if (not(vlax-curve-getParamAtPoint Obj pt))
   (progn
 (setq Tol  (/ pi 6) ; Uncertainty
       ang  0.0 flag T)
 (setq doc (vla-get-ActiveDocument (vlax-get-Acad-Object))
       spc (if (zerop (vla-get-activespace doc))
             (if (= (vla-get-mspace doc) :vlax-true)
               (vla-get-modelspace doc)
               (vla-get-paperspace doc))
             (vla-get-modelspace doc)))
 (while (and (< ang (* 2 pi)) flag)
   (setq flag (and
                (setq int
                  (vlax-invoke
                    (setq lin
                      (vla-addLine spc
                        (vlax-3D-point pt)
                          (vlax-3D-point
                            (polar pt ang
                              (if (vlax-property-available-p Obj 'length)
                                (vla-get-length Obj) 1.0)))))
                                 'IntersectWith Obj
                                   acExtendThisEntity))
                (<= 6 (length int))
                (setq xV (vl-sort (vlax-list->3D-point int T) '<)
                      yV (vl-sort (vlax-list->3D-point int nil) '<))
                (or (<= (car xV) (car pt) (last xV))
                    (<= (car yV) (cadr pt) (last yV))))
         ang  (+ ang Tol))
   (vla-delete lin))
 flag
 )
   T
   ))


;;; Author: Copyrightゥ 2006-2008 Charles Alan Butler 
;;; Contact @  www.TheSwamp.org
;;===========================================================================
 ;;  get all objects touching entities in the sscross                         
 ;;  limited obj types to "LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE"
 ;;  returns a list of enames
 ;;===========================================================================
(defun gettouching (sscros / ss lst lstb lstc objl)
   (and
     (setq lstb (vl-remove-if 'listp (mapcar 'cadr (ssnamex sscros)))
           objl (mapcar 'vlax-ename->vla-object lstb)
     )
     (setq ss (ssget "_A" (list (cons 0 "LINE,ARC,SPLINE,LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE")
			 (cons 410 (getvar "ctab"))))
     )
     (setq lst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
     (setq lst (mapcar 'vlax-ename->vla-object lst))
     (mapcar
       '(lambda (x)
          (mapcar
            '(lambda (y)
               (if (not
                     (vl-catch-all-error-p
                       (vl-catch-all-apply
                         '(lambda ()
                            (vlax-safearray->list
                              (vlax-variant-value
                                (vla-intersectwith y x acextendnone)
                              ))))))
                 (setq lstc (cons (vlax-vla-object->ename x) lstc))
               )
             ) objl)
        ) lst)
   )
   lstc
 )
;;; Author: Copyrightゥ 2006-2008 Charles Alan Butler 
;;; Contact @  www.TheSwamp.org
(defun break_with (ss2brk ss2brkwith self Gap / cmd intpts lst masterlist ss ssobjs
                  onlockedlayer ssget->vla-list list->3pair GetNewEntities oc
                  get_interpts break_obj GetLastEnt LastEntInDatabase ss2brkwithList
                 )
 ;; ss2brk     selection set to break
 ;; ss2brkwith selection set to use as break points
 ;; self       when true will allow an object to break itself
 ;;            note that plined will break at each vertex
 ;;
 ;; return list of enames of new objects  
 (vl-load-com)  
 (princ "\nCalculating Break Points, Please Wait.\n")
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;                S U B   F U N C T I O N S                      
;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 ;;  return T if entity is on a locked layer
 (defun onlockedlayer (ename / entlst)
   (setq entlst (tblsearch "LAYER" (cdr (assoc 8 (entget ename)))))
   (= 4 (logand 4 (cdr (assoc 70 entlst))))
 )

 ;;  return a list of objects from a selection set
;|  (defun ssget->vla-list (ss)
   (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss ))))
 )|;
 (defun ssget->vla-list (ss / i ename allobj) ; this is faster, changed in ver 1.7
      (setq i -1)
      (while (setq  ename (ssname ss (setq i (1+ i))))
        (setq allobj (cons (vlax-ename->vla-object ename) allobj))
      )
      allobj
 )

 ;;  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)
 )

;;=====================================
;;  return a list of intersect points  
;;=====================================
(defun get_interpts (obj1 obj2 / iplist)
 (if (not (vl-catch-all-error-p
            (setq iplist (vl-catch-all-apply
                           'vlax-safearray->list
                           (list
                             (vlax-variant-value
                               (vla-intersectwith obj1 obj2 acextendnone)
                             ))))))
   iplist
 )
)

;;========================================
;;  Break entity at break points in list  
;;========================================
(defun break_obj (ent brkptlst BrkGap / brkobjlst en enttype maxparam closedobj
                 minparam obj obj2break p1param p2param brkpt2 dlst idx brkptS
                 brkptE brkpt result GapFlg result ignore dist tmppt
                 #ofpts 2gap enddist lastent obj2break stdist
                )
 (or BrkGap (setq BrkGap 0.0)) ; default to 0
 (setq BrkGap (/ BrkGap 2.0)) ; if Gap use 1/2 per side of break point

 (setq obj2break ent
       brkobjlst (list ent)
       enttype   (cdr (assoc 0 (entget ent)))
       GapFlg    (not (zerop BrkGap)) ; gap > 0
       closedobj (vlax-curve-isclosed obj2break)
 )
 ;; when zero gap no need to break at end points
 (if (zerop Brkgap)
   (setq spt (vlax-curve-getstartpoint ent)
         ept (vlax-curve-getendpoint ent)
         brkptlst (vl-remove-if '(lambda(x) (or (< (distance x spt) 0.0001)
                                                (< (distance x ept) 0.0001)))
                                brkptlst)
   )
 )
 (if brkptlst
   (progn
 ;;  sort break points based on the distance along the break object
 ;;  get distance to break point, catch error if pt is off end
 ;; ver 2.0 fix - added COND to fix break point is at the end of a
 ;; line which is not a valid break but does no harm
 (setq brkptlst (mapcar '(lambda(x) (list x (vlax-curve-getdistatparam obj2break
                                              ;; ver 2.0 fix
                                              (cond ((vlax-curve-getparamatpoint obj2break x))
                                                  ((vlax-curve-getparamatpoint obj2break
                                                    (vlax-curve-getclosestpointto obj2break x))))))
                           ) brkptlst))
 ;; sort primary list on distance
 (setq brkptlst (vl-sort brkptlst '(lambda (a1 a2) (< (cadr a1) (cadr a2)))))

 (if GapFlg ; gap > 0
   ;; Brkptlst starts as the break point and then a list of pairs of points
   ;;  is creates as the break points
   (progn
     ;;  create a list of list of break points
     ;;  ((idx# stpoint distance)(idx# endpoint distance)...)
     (setq idx 0)
     (foreach brkpt brkptlst

       ;; ----------------------------------------------------------
       ;;  create start break point, then create end break point    
       ;;  ((idx# startpoint distance)(idx# endpoint distance)...)  
       ;; ----------------------------------------------------------
       (setq dist (cadr brkpt)) ; distance to center of gap
       ;;  subtract gap to get start point of break gap
       (cond
         ((and (minusp (setq stDist (- dist BrkGap))) closedobj )
          (setq stdist (+ (vlax-curve-getdistatparam obj2break
                            (vlax-curve-getendparam obj2break)) stDist))
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                        (vlax-curve-getparamatdist obj2break stDist))
                                 stDist) dlst))
          )
         ((minusp stDist) ; off start of object so get startpoint
          (setq dlst (cons (list idx (vlax-curve-getstartpoint obj2break) 0.0) dlst))
          )
         (t
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                        (vlax-curve-getparamatdist obj2break stDist))
                                 stDist) dlst))
         )
       )
       ;;  add gap to get end point of break gap
       (cond
         ((and (> (setq stDist (+ dist BrkGap))
                  (setq endDist (vlax-curve-getdistatparam obj2break
                                    (vlax-curve-getendparam obj2break)))) closedobj )
          (setq stdist (- stDist endDist))
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                        (vlax-curve-getparamatdist obj2break stDist))
                                 stDist) dlst))
          )
         ((> stDist endDist) ; off end of object so get endpoint
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                       (vlax-curve-getendparam obj2break))
                                 endDist) dlst))
          )
         (t
          (setq dlst (cons (list idx
                                 (vlax-curve-getpointatparam obj2break
                                        (vlax-curve-getparamatdist obj2break stDist))
                                 stDist) dlst))
         )
       )
       ;; -------------------------------------------------------
       (setq idx (1+ IDX))
     ) ; foreach brkpt brkptlst


     (setq dlst (reverse dlst))
     ;;  remove the points of the gap segments that overlap
     (setq idx -1
           2gap (* BrkGap 2)
           #ofPts (length Brkptlst)
     )
     (while (<= (setq idx (1+ idx)) #ofPts)
       (cond
         ((null result) ; 1st time through
          (setq result (list (car dlst)) ; get first start point
                result (cons (nth (1+(* idx 2)) dlst) result))
         )
         ((= idx #ofPts) ; last pass, check for wrap
          (if (and closedobj (> #ofPts 1)
                   (<= (+(- (vlax-curve-getdistatparam obj2break
                           (vlax-curve-getendparam obj2break))
                         (cadr (last BrkPtLst))) (cadar BrkPtLst)) 2Gap))
            (progn
              (if (zerop (rem (length result) 2))
                (setq result (cdr result)) ; remove the last end point
              )
              ;;  ignore previous endpoint and present start point
              (setq result (cons (cadr (reverse result)) result) ; get last end point
                    result (cdr (reverse result))
                    result (reverse (cdr result)))
            )
          )
         )
         ;; Break Gap Overlaps
         ((< (cadr (nth idx Brkptlst)) (+ (cadr (nth (1- idx) Brkptlst)) 2Gap))
          (if (zerop (rem (length result) 2))
            (setq result (cdr result)) ; remove the last end point
          )
          ;;  ignore previous endpoint and present start point
          (setq result (cons (nth (1+(* idx 2)) dlst) result)) ; get present end point
          )
         ;; Break Gap does Not Overlap previous point 
         (t
          (setq result (cons (nth (* idx 2) dlst) result)) ; get this start point
          (setq result (cons (nth (1+(* idx 2)) dlst) result)) ; get this end point
         )
       ) ; end cond stmt
     ) ; while

     (setq dlst     (reverse result)
           brkptlst nil)
     (while dlst ; grab the points only
       (setq brkptlst (cons (list (cadar dlst)(cadadr dlst)) brkptlst)
             dlst   (cddr dlst))
     )
   )
 )
 ;;   -----------------------------------------------------

 ;; (if (equal  a ent) (princ)) ; debug CAB  -------------

 (foreach brkpt (reverse brkptlst)
   (if GapFlg ; gap > 0
     (setq brkptS (car brkpt)
           brkptE (cadr brkpt))
     (setq brkptS (car brkpt)
           brkptE brkptS)
   )
   ;;  get last entity created via break in case multiple breaks
   (if brkobjlst
     (progn
       (setq tmppt brkptS) ; use only one of the pair of breakpoints
       ;;  if pt not on object x, switch objects
       (if (not (numberp (vl-catch-all-apply
                           'vlax-curve-getdistatpoint (list obj2break tmppt))))
         (progn ; find the one that pt is on
           (setq idx (length brkobjlst))
           (while (and (not (minusp (setq idx (1- idx))))
                       (setq obj (nth idx brkobjlst))
                       (if (numberp (vl-catch-all-apply
                                      'vlax-curve-getdistatpoint (list obj tmppt)))
                         (null (setq obj2break obj)) ; switch objects, null causes exit
                         t
                       )
                  )
           )
         )
       )
     )
   )

   (setq closedobj (vlax-curve-isclosed obj2break))
   (if GapFlg ; gap > 0
     (if closedobj
       (progn ; need to break a closed object
         (setq brkpt2 (vlax-curve-getPointAtDist obj2break
                    (- (vlax-curve-getDistAtPoint obj2break brkptE) 0.00001)))
         (command "._break" obj2break "_non" (trans brkpt2 0 1)
                  "_non" (trans brkptE 0 1))
         (and (= "CIRCLE" enttype) (setq enttype "ARC"))
         (setq BrkptE brkpt2)
       )
     )

     (if (and closedobj 
              (not (setq brkptE (vlax-curve-getPointAtDist obj2break
                      (+ (vlax-curve-getdistatparam obj2break
                           ;;(vlax-curve-getparamatpoint obj2break brkpts)) 0.00001))))
                           ;; ver 2.0 fix
                           (cond ((vlax-curve-getparamatpoint obj2break brkpts))
                                 ((vlax-curve-getparamatpoint obj2break
                                     (vlax-curve-getclosestpointto obj2break brkpts))))) 0.00001)))))
       (setq brkptE (vlax-curve-getPointAtDist obj2break
                      (- (vlax-curve-getdistatparam obj2break
                           ;;(vlax-curve-getparamatpoint obj2break brkpts)) 0.00001)))
                           ;; ver 2.0 fix
                           (cond ((vlax-curve-getparamatpoint obj2break brkpts))
                                 ((vlax-curve-getparamatpoint obj2break
                                     (vlax-curve-getclosestpointto obj2break brkpts))))) 0.00001)))
      )
   ) ; endif

   ;; (if (null brkptE) (princ)) ; debug

   (setq LastEnt (GetLastEnt))
   (command "._break" obj2break "_non" (trans brkptS 0 1) "_non" (trans brkptE 0 1))
   (and *BrkVerbose* (princ (setq *brkcnt* (1+ *brkcnt*))) (princ "\r"))
   (and (= "CIRCLE" enttype) (setq enttype "ARC"))
   (if (and (not closedobj) ; new object was created
            (not (equal LastEnt (entlast))))
       (setq brkobjlst (cons (entlast) brkobjlst))
   )
 )
 )
 ) ; endif brkptlst

) ; defun break_obj

;;====================================
;;  CAB - get last entity in datatbase
(defun GetLastEnt ( / ename result )
 (if (setq result (entlast))
   (while (setq ename (entnext result))
     (setq result ename)
   )
 )
 result
)
;;===================================
;;  CAB - return a list of new enames
(defun GetNewEntities (ename / new)
 (cond
   ((null ename) (alert "Ename nil"))
   ((eq 'ENAME (type ename))
     (while (setq ename (entnext ename))
       (if (entget ename) (setq new (cons ename new)))
     )
   )
   ((alert "Ename wrong type."))
 )
 new
)

 ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ;;         S T A R T  S U B R O U T I N E   H E R E              
 ;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   (setq LastEntInDatabase (GetLastEnt))
   (if (and ss2brk ss2brkwith)
   (progn
     (setq oc 0
           ss2brkwithList (ssget->vla-list ss2brkwith))
     (if (> (* (sslength ss2brk)(length ss2brkwithList)) 5000)
       (setq *BrkVerbose* t)
     )
     (and *BrkVerbose*
          (princ (strcat "Objects to be Checked: "
           (itoa (* (sslength ss2brk)(length ss2brkwithList))) "\n")))
     ;;  CREATE a list of entity & it's break points
     (foreach obj (ssget->vla-list ss2brk) ; check each object in ss2brk
       (if (not (onlockedlayer (vlax-vla-object->ename obj)))
         (progn
           (setq lst nil)
           ;; check for break pts with other objects in ss2brkwith
           (foreach intobj  ss2brkwithList
             (if (and (or self (not (equal obj intobj)))
                      (setq intpts (get_interpts obj intobj))
                 )
               (setq lst (append (list->3pair intpts) lst)) ; entity w/ break points
             )
             (and *BrkVerbose* (princ (strcat "Objects Checked: " (itoa (setq oc (1+ oc))) "\r")))
           )
           (if lst
             (setq masterlist (cons (cons (vlax-vla-object->ename obj) lst) masterlist))
           )
         )
       )
     )    
     (and *BrkVerbose* (princ "\nBreaking Objects.\n"))
     (setq *brkcnt* 0) ; break counter
     ;;  masterlist = ((ent brkpts)(ent brkpts)...)
     (if masterlist
       (foreach obj2brk masterlist
         (break_obj (car obj2brk) (cdr obj2brk) Gap)
       )
     )
     )
 )
;;==============================================================
  (and (zerop *brkcnt*) (princ "\nNone to be broken."))
  (setq *BrkVerbose* nil)
 (GetNewEntities LastEntInDatabase) ; return list of enames of new objects
)

 

xin lỗi chú Gia_Bach . cháu thấy lisp chú rất là hay cháu có thể nhờ lại chú và mọi người sửa lisp thành lựa chọn nhiều đối tượng cắt ( chứ ko phải 1 hay 2 đối tượng ) được không ạ 

Cháu cảm ơn

 

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Tạo một tài khoản hoặc đăng nhập để nhận xét

Bạn cần phải là một thành viên để lại một bình luận

Tạo tài khoản

Đăng ký một tài khoản mới trong cộng đồng của chúng tôi. Điều đó dễ mà.

Đăng ký tài khoản mới

Đăng nhập

Bạn có sẵn sàng để tạo một tài khoản ? Đăng nhập tại đây.

Đăng nhập ngay

×