Jump to content

Recursive Break Polylines at Points


j2lstaples

Recommended Posts

Suppose you have a list called ptlist that consists of points (i.e. (x, y, z) coordinates), and a polyline entity called pline.

Not assuming that all these points are on the pline, can you recursively try to (command "break" pline) and subsequent resulting entities.

I'm trying out the logic of this exercise, but I can't seem to correct my logic.

Currently I have my code looking like this.

 

;; pline - polyline entity 
;; pt - [LIST] (e.g. ((x1, y1, z1) (x3, y3, z3) (x2, y2, z3)))
(setq plinelist nil)
(foreach pt (MA:ptlist ss)	;; generates my list of points from a function that takes a selection set
	(setq plinelist (append pline plinelist)) 	;; appends the polyline list with current pline (could be original, could be subsequent)
    											;; ensures the last on the list is the original pline 
	(if (command "break" pline pt pt)			;; test to see if polyline can break with first point
		(setq pline (entlast))	;; subsequent resulting pline to be added on plinelist on next iteration
		(progn		;; else if polyline can't break at pt 
			(if (command "break" (last plinelist) pt pt)	;; tests the last pline
				(setq p1 (car plinelist)	;; placeholder of previous pline entity
					  pline (entlast))		;; new pline to append in the next iteration
				(if	(command "break p1 pt pt)	;; else ;;  p1 = nil empty if the first plinelist length is 1
					(setq pline (entlast))
				)
			)
		)
	)
)

 

I know there's a simpler way of doing this but I can't seem to find it. If you fellas can help, that would be a great learning experience. 

Link to comment
Share on other sites

Just a maybe go the other way use (ssget pt) to see if an object is nearby the pt, then break, this way a single pline becomes 2 then 3 then 4 etc.

 

So in example code you have 3 points to look for a nearby object.

 

 

Link to comment
Share on other sites

This can break LWPOLYLINE with points or inserts of block

(vl-load-com)
(defun add_vtx (obj add_pt ent_name / sw ew nw bulg)
  (vla-GetWidth obj (fix add_pt) 'sw 'ew)
  (vla-addVertex
    obj
    (1+ (fix add_pt))
    (vlax-make-variant
      (vlax-safearray-fill
        (vlax-make-safearray vlax-vbdouble (cons 0 1))
          (list
            (car (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
            (cadr (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
          )
      )
    )
  )
  (setq
    nw
    (*
      (/
        (- ew sw)
        (- (vlax-curve-getdistatparam obj (1+ (fix add_pt))) (vlax-curve-getdistatparam obj (fix add_pt)))
      )
      (- (vlax-curve-getdistatparam obj add_pt) (vlax-curve-getdistatparam obj (fix add_pt)))
    )
    bulg (atan (vla-GetBulge obj (fix add_pt)))
  )
  (vla-SetBulge obj
    (fix add_pt)
    (/
      (sin (* 4 bulg (- add_pt (fix add_pt)) 0.25))
      (cos (* 4 bulg (- add_pt (fix add_pt)) 0.25))
    )
  )
  (vla-SetBulge obj
    (1+ (fix add_pt))
    (/
      (sin (* 4 bulg (- (1+ (fix add_pt)) add_pt) 0.25))
      (cos (* 4 bulg (- (1+ (fix add_pt)) add_pt) 0.25))
    )
  )
  (vla-SetWidth obj
    (fix add_pt)
    sw
    (+ nw sw)
  )
  (vla-SetWidth obj
    (1+ (fix add_pt))
    (+ nw sw)
    ew
  )
  (vla-update obj)
)
(defun c:Break_Poly@point ( / js AcDoc Space js_b nb p lst_pt ename obj pt_brk l_tst lst_brk lst_sort dxf_obj dxf_43 dxf_38 dxf_39 dxf_10 dxf_40 dxf_41 dxf_42 dxf_39 dxf_210 lst_tmp where count ltmp)
  (princ "\nSelect polyline")
  (while (not (setq js (ssget "_+.:E:S" '((0 . "LWPOLYLINE"))))))
  (setq
    AcDoc (vla-get-ActiveDocument (vlax-get-acad-object))
    Space
    (if (= 1 (getvar "CVPORT"))
      (vla-get-PaperSpace AcDoc)
      (vla-get-ModelSpace AcDoc)
    )
  )
  (initget "Point Insert")
  (setq typ_ent (getkword "\nBreak at [Point/Insert] off blocks? <Point>: "))
  (if (not typ_ent) (setq typ_ent "Point"))
  (princ (strcat "\nSelect " typ_ent))
  (while (not (setq js_b (ssget (list (cons 0 (strcase typ_ent)))))))
  (vla-startundomark AcDoc)
  (repeat (setq nb (sslength js_b))
    (setq p (cdr (assoc 10 (entget (ssname js_b (setq nb (1- nb)))))))
    (if (not (member p lst_pt))
      (setq lst_pt (cons p lst_pt))
    )
  )
  (setq
    ename (ssname js 0)
    obj (vlax-ename->vla-object ename)
    dxf_10 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget ename)))
  )
  (foreach el (mapcar '(lambda (x) (trans x 0 ename)) lst_pt)
    (setq
      pt_brk (trans (vlax-curve-getClosestPointTo ename (trans el ename 0) nil) 0 ename)
      l_tst (vl-member-if '(lambda (x) (and (equal (car x) (car pt_brk) 1E-08) (equal (cadr x) (cadr pt_brk) 1E-08))) dxf_10)
    )
    (if
      (and
        (not (eq (length l_tst) 1))
        (not (eq (length l_tst) (length dxf_10)))
      )
      (progn
        (setq lst_brk (cons pt_brk lst_brk))
        (if (zerop (length l_tst))
          (add_vtx obj (vlax-curve-getparamatpoint ename (trans pt_brk ename 0)) ename)
        )
      )
    )
  )
  (setq
    lst_sort (mapcar '(lambda (x) (list (vlax-curve-GetDistAtPoint ename (trans x ename 0)) (list (car x) (cadr x)))) lst_brk)
    lst_brk (reverse (mapcar 'cadr (mapcar '(lambda (x) (assoc x lst_sort)) (vl-sort (mapcar 'car lst_sort) '<))))
    dxf_obj (entget ename)
  )
  (if (cdr (assoc 43 dxf_obj))
    (setq dxf_43 (cdr (assoc 43 dxf_obj)))
    (setq dxf_43 0.0)
  )
  (if (cdr (assoc 38 dxf_obj))
    (setq dxf_38 (cdr (assoc 38 dxf_obj)))
    (setq dxf_38 0.0)
  )
  (if (cdr (assoc 39 dxf_obj))
    (setq dxf_39 (cdr (assoc 39 dxf_obj)))
    (setq dxf_39 0.0)
  )
  (setq
    dxf_10 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) dxf_obj))
    dxf_40 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 40)) dxf_obj))
    dxf_41 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 41)) dxf_obj))
    dxf_42 (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 42)) dxf_obj))
    dxf_210 (cdr (assoc 210 dxf_obj))
  )
  (if (not (zerop (boole 1 (cdr (assoc 70 dxf_obj)) 1)))
    (setq
      dxf_10 (append dxf_10 (list (car dxf_10)))
      dxf_40 (append dxf_40 (list (car dxf_40)))
      dxf_41 (append dxf_41 (list (car dxf_41)))
      dxf_42 (append dxf_42 (list (car dxf_42)))
    )
  )
  (repeat (1+ (length lst_brk))
    (setq
      ltmp nil
      lst_tmp (vl-member-if '(lambda (x) (and (equal (car x) (caar lst_brk) 1E-08) (equal (cadr x) (cadar lst_brk) 1E-08))) dxf_10)
      where (if lst_tmp (vl-position (car lst_tmp) dxf_10) 0)
    )
    (repeat (setq count (- (length dxf_10) where))
      (setq ltmp (cons (mapcar '(lambda (x y) (cons y (nth where x))) (list dxf_10 dxf_40 dxf_41 dxf_42) (list 10 40 41 42)) ltmp))
      (setq where (1+ where))
    )
    (entmake
      (append
        (list
          (cons 0 "LWPOLYLINE")
          (cons 100 "AcDbEntity")
          (assoc 67 dxf_obj)
          (assoc 410 dxf_obj)
          (assoc 8 dxf_obj)
          (if (assoc 62 dxf_obj) (assoc 62 dxf_obj) (cons 62 256))
          (if (assoc 6 dxf_obj) (assoc 6 dxf_obj) (cons 6 "BYLAYER"))
          (if (assoc 370 dxf_obj) (assoc 370 dxf_obj) (cons 370 -1))
          (cons 100 "AcDbPolyline")
          (cons 90 (length ltmp))
          (cons 70 (boole 1 (cdr (assoc 70 dxf_obj)) 128))
          (cons 38 dxf_38)
          (cons 39 dxf_39)
        )
        (apply 'append (reverse ltmp))
        (list (cons 210 dxf_210))
      )
    )
    (repeat (1- count)
      (setq
        dxf_10 (reverse (cdr (reverse dxf_10)))
        dxf_40 (reverse (cdr (reverse dxf_40)))
        dxf_41 (reverse (cdr (reverse dxf_41)))
        dxf_42 (reverse (cdr (reverse dxf_42)))
      )
    )
    (setq lst_brk (cdr lst_brk) ltmp nil)
  )
  (entdel ename)
  (vla-endundomark AcDoc)
  (prin1)
)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...