Author Topic: Break Polyline at Given Point  (Read 2844 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 494
Break Polyline at Given Point
« on: April 19, 2014, 12:38:34 PM »
Dear All

I want to break a polyline into two polylines at a given point with zero gap.

My questions :-

1.) How to check whether a point lies on a polyline or not ?

2.) Apart from method of (Command "BREAK".........) is there any way to break it using VL- or VLA- functions ? This is because I want to get entity names of both the polylines (after split).


mailmaverick

  • Bull Frog
  • Posts: 494
Re: Break Polyline at Given Point
« Reply #1 on: April 19, 2014, 12:47:04 PM »
For my first question, I got following approach :-

(setq newpt (vlax-curve-getClosestPointTo Polyline GivenPoint))

(if (<= (distance newpt GivenPoint) FUZZ)
(princ "Point Lies on Polyline")
(princ "Point does not lie on Polyline")
)

Is there any faster approach to this ?

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Break Polyline at Given Point
« Reply #2 on: April 19, 2014, 01:35:07 PM »
1.) Another method to check if the point lies on curve entity is to check for existence of parameter value, or distance value with (vlax-curve-getparamatpoint curve GivenPoint) or (vlax-curve-getdistatpoint curve GivenPoint)... "curve" variable is 'ENAME of requested polyline...

2.) You can try to retrieve DXF data of list that is before newly added vertex when you use (vla-addvertex) method and construct new polyline with (entmakex) and also retrieve DXF data of list that is after newly added vertex and construct second polyline also using (entmakex) function with which you can also get their 'ENAMEs at the same time...
« Last Edit: April 19, 2014, 01:42:48 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

mailmaverick

  • Bull Frog
  • Posts: 494
Re: Break Polyline at Given Point
« Reply #3 on: April 19, 2014, 02:56:43 PM »
Dear Marko

Thanks for your reply.

Can u post code for your 2 nd reply.

Thanks again

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Break Polyline at Given Point
« Reply #4 on: April 19, 2014, 03:05:51 PM »
Here, try this...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:plbr ( / add_vtx osm pl pt flag pldxf ptdxf ptdxfdata ptdxfdatapl1 ptdxfdatapl2 pldxfdata pl1 pl2 )
  2.  
  3.  
  4.   (defun add_vtx ( obj add_pt ent_name / bulg sw ew )
  5.       (vla-GetWidth obj (fix add_pt) 'sw 'ew)
  6.       (vla-addVertex
  7.           obj
  8.           (1+ (fix add_pt))
  9.           (vlax-make-variant
  10.               (vlax-safearray-fill
  11.                   (vlax-make-safearray vlax-vbdouble (cons 0 1))
  12.                       (list
  13.                           (car (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
  14.                           (cadr (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
  15.                       )
  16.               )
  17.           )
  18.       )
  19.       (setq bulg (vla-GetBulge obj (fix add_pt)))
  20.       (vla-SetBulge obj
  21.           (fix add_pt)
  22.           (/
  23.               (sin (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
  24.               (cos (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
  25.           )
  26.       )
  27.       (vla-SetBulge obj
  28.           (1+ (fix add_pt))
  29.           (/
  30.               (sin (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
  31.               (cos (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
  32.           )
  33.       )
  34.       (vla-SetWidth obj (fix add_pt) sw (+ sw (* (- ew sw) (- add_pt (fix add_pt)))))
  35.       (vla-SetWidth obj (1+ (fix add_pt)) (+ sw (* (- ew sw) (- add_pt (fix add_pt)))) ew)
  36.       (vla-update obj)
  37.   )
  38.  
  39.   (setq osm (getvar 'osmode))
  40.   (setvar 'osmode 512)
  41.   (setq pl (car (entsel "\nSelect 2d pline entity")))
  42.   (if (not (wcmatch (cdr (assoc 0 (entget pl))) "*POLYLINE"))
  43.     (progn
  44.       (alert "\nPicked entity isn't polyline - quitting")
  45.       (exit)
  46.     )
  47.     (if (and (not (< 7 (cdr (assoc 70 (entget pl))) 14)) (eq (cdr (assoc 0 (entget pl))) "POLYLINE"))
  48.       (if (< (cdr (assoc 70 (entget pl))) 130)
  49.         (progn
  50.           (setq flag T)
  51.           (command "_.convertpoly" "l" pl "")
  52.         )
  53.         (progn
  54.           (alert "\nYou picked 2d polyline that can't be converted to LWPOLYLINE - quitting")
  55.           (exit)
  56.         )
  57.       )
  58.       (if (not (eq (cdr (assoc 0 (entget pl))) "LWPOLYLINE"))
  59.         (progn
  60.           (alert "\nYou picked 3d polyline - quitting")
  61.           (exit)
  62.         )
  63.       )
  64.     )
  65.   )
  66.   (setq pt (getpoint "\nPick point where you want to break pline"))
  67.   (setq pt (trans pt 1 0))
  68.   (add_vtx (vlax-ename->vla-object pl) (vlax-curve-getparamatpoint pl (vlax-curve-getclosestpointto pl pt)) pl)
  69.   (setq pt (trans pt 0 pl))
  70.   (setq pldxf (entget pl))
  71.   (setq ptdxf (vl-member-if '(lambda ( x ) (equal (list 10 (car pt) (cadr pt)) x 1e-8)) pldxf))
  72.   (setq ptdxfdata (vl-remove nil (list (car ptdxf) (cadr ptdxf) (caddr ptdxf) (cadddr ptdxf) (if (assoc 91 ptdxf) (nth 4 ptdxf)))))
  73.   (setq ptdxfdatapl1 (append (reverse (cdr (member (car ptdxfdata) (reverse (member (assoc 10 pldxf) pldxf))))) ptdxfdata))
  74.   (setq ptdxfdatapl2 (reverse (if (eq (vla-get-closed (vlax-ename->vla-object pl)) :vlax-true) (append (list (assoc 10 pldxf)) (cdr (reverse ptdxf))) (cdr (reverse ptdxf)))))
  75.   (setq pldxfdata (vl-remove-if '(lambda ( x ) (member (car x) '(-1 5 330 90 70 10 40 41 42 91 210))) pldxf))
  76.   (setq pl1 (entmakex (append pldxfdata (list (cons 90 (length (vl-remove-if-not '(lambda ( x ) (= (car x) 10)) ptdxfdatapl1)))) (if (eq (vla-get-linetypegeneration (vlax-ename->vla-object pl)) :vlax-true) (list (cons 70 128)) (list (cons 70 0))) ptdxfdatapl1 (list (assoc 210 pldxf)))))
  77.   (setq pl2 (entmakex (append pldxfdata (list (cons 90 (length (vl-remove-if-not '(lambda ( x ) (= (car x) 10)) ptdxfdatapl2)))) (if (eq (vla-get-linetypegeneration (vlax-ename->vla-object pl)) :vlax-true) (list (cons 70 128)) (list (cons 70 0))) ptdxfdatapl2 (list (assoc 210 pldxf)))))
  78.   (if flag
  79.     (command "_.convertpoly" "h" (ssadd pl1 (ssadd pl2)) "")
  80.   )
  81.   (prompt "\nFirst 2d polyline after break : ") (princ pl1)
  82.   (prompt "\nSecond 2d polyline after break : ") (princ pl2)
  83.   (entdel pl)
  84.   (setvar 'osmode osm)
  85.   (princ)
  86. )
  87.  



edit:kdub -> formatting code=cadlisp-7
« Last Edit: April 20, 2014, 09:41:07 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Break Polyline at Given Point
« Reply #5 on: April 19, 2014, 04:38:50 PM »
After a few modifications, code is finally correct...

You can test it and if you find something bad, please report...

M.R. :wink:
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: Break Polyline at Given Point
« Reply #6 on: April 19, 2014, 04:43:10 PM »
After a few modifications, code is finally correct...

You can test it and if you find something bad, please report...

M.R. :wink:

Marko

Your not making him work very hard for it....

ymg

  • Guest
Re: Break Polyline at Given Point
« Reply #7 on: April 19, 2014, 05:54:42 PM »
You already know the entname you are breaking.

I believe (entlast) will give you the one you are adding
by breaking.

ymg

mailmaverick

  • Bull Frog
  • Posts: 494
Re: Break Polyline at Given Point
« Reply #8 on: April 19, 2014, 08:50:12 PM »
After a few modifications, code is finally correct...

You can test it and if you find something bad, please report...

M.R. :wink:

Marko

Your not making him work very hard for it....


Snownut2, how do you know whether I have worked hard on this or not ?

And Marko, thanks a lot.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Break Polyline at Given Point
« Reply #9 on: April 20, 2014, 06:00:09 AM »
This program may offer some ideas: Object Break - see line 282 onwards.

mailmaverick

  • Bull Frog
  • Posts: 494
Re: Break Polyline at Given Point
« Reply #10 on: April 20, 2014, 08:01:46 AM »
Thanks Marco and Lee.

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Break Polyline at Given Point
« Reply #11 on: April 20, 2014, 08:45:11 AM »
Thanks Marco and Lee.

You're welcome, mailmaverick... My name is Marko Ribar - just a little remark... And I also noticed one mistake in line 70 of my posted code. (now should work correct and for ACAD versions that haven't implemented DXF 91 code like versions that don't have Geometric Constraints...) (Tested on A2009 and worked...)

[EDIT : Also changed subfunction (add_vtx) to include variable start/end widths of 2d polyline]
« Last Edit: April 20, 2014, 09:42:54 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube