2D (heavy) Polyline to LW Polyline

2D (heavy) Polyline to LW Polyline

Browning_Zed
Advocate Advocate
1,204 Views
7 Replies
Message 1 of 8

2D (heavy) Polyline to LW Polyline

Browning_Zed
Advocate
Advocate

Hello everyone,
I found some code that converts 2D Polyline to LW Polyline. It works great, but it is based on the Entmakex function. I would like to transform polylines using the ActiveX method with function AddLightWeightPolyline.
Below I post two routines.
The first converts 2D Polyline to LW Polyline using the Entmakex method. The second routine converts Circle to LW Polyline, which is based on the AddLightWeightPolyline function and is shown as an example. Thanks in advance for any help.

 

2D (heavy) Polyline to LW Polyline (Entmakex based)

(defun c:OldStyle2LwPolyline ( / pl plst xdata vtx vlst elst)
  (setq	pl (car (entsel))
  plst  (entget pl '("*"))
	xdata (assoc -3 plst)
	vtx   (entnext pl)
  )
  (while (= (cdr (assoc 0 (setq vlst (entget vtx)))) "VERTEX")
    (if	(zerop (logand (cdr (assoc 70 vlst)) 16))
      (setq elst (cons (vl-remove-if-not
			 (function
			   (lambda (x)
			     (member (car x) '(10 40 41 42))
			   )
			 )
			 vlst
		       )
		       elst
		 )
      )
    )
    (setq vtx (entnext vtx))
  )
  (if (setq new
	     (entmakex
	       (append
		 (list
		   '(0 . "LWPOLYLINE")
		   '(100 . "AcDbEntity")
		   (assoc 410 plst)
		   (assoc 8 plst)
		   (cond
		     ((assoc 39 plst))
		     (T '(39 . 0))
		   )
		   '(100 . "AcDbPolyline")
		   (cons 90 (length elst))
		   (cons 70 (logand 129 (cdr (assoc 70 plst))))
		   (cons 38 (last (caar elst)))
		   (assoc 210 plst)
		 )
		 (apply 'append (reverse elst))
		 (if xdata
		   (list xdata)
		 )
	       )
	     )
      )
    (entdel pl)
  )
  new
)

 

Circle to LW Polyline (ActiveX based)

(defun c:2p ( / ent spc obj)
  (setq ent (car (entsel))
        spc (vlax-get-property (vla-get-activedocument (vlax-get-acad-object)) (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))
        obj (vlax-invoke spc 'addlightweightpolyline (apply 'append (list (reverse (cdr (reverse (vlax-curve-getstartpoint ent)))) (reverse (cdr (reverse (vlax-curve-getpointatparam ent pi)))))))
  )
  (vlax-put-property obj 'closed :vlax-true)
  (foreach x '(0 1) (vla-setbulge obj x -1.0))
  (vla-delete (vlax-ename->vla-object ent))
)

 

0 Likes
1,205 Views
7 Replies
Replies (7)
Message 2 of 8

pbejse
Mentor
Mentor

@Browning_Zed wrote:

Hello everyone,
I found some code that converts 2D Polyline to LW Polyline. It works great, but it is based on the Entmakex function. I would like to transform polylines using the ActiveX method with function AddLightWeightPolyline.
...


Good time to start learning Visual LISP & Polylines 

Start reading. 🙂

 

Message 3 of 8

Browning_Zed
Advocate
Advocate

Thanks for the link, but that doesn't answer my question. Because first I need to get a list of coordinates characterizing a 2D Polyline (which is essentially a curve), and only then pass this list of coordinates to the AddLightWeightPolyline function.😶

0 Likes
Message 4 of 8

hak_vz
Advisor
Advisor

Ok. Let start learning process.

Check older codes provided to you or pasted to this forum. In my codes you can find functions named take , pointlist2d, pointlist 3d, various vlax-put vlax-get vla- combinations.

Check system variable "plinetype",  (setq eo vlax-ename->vla-object (car(entsel)), (vlax-dump-object eo T)

Try to create three lines of code that would select pline entity, convert it to visual lisp object, extract coordinates from that entity, and convert it to point list. You can work with arrays or but you don't have to (check differences between vla-get-xxxxx and vla-get object 'property.

P.S.

Mixing autolisp and visual-lisp commands is not a sin, synergy makes them better .

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 8

Browning_Zed
Advocate
Advocate

OK. I try, but I don't get what I want:

(defun take (amount lst / ret)(repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
(defun pointlist2d (lst / ret) (while lst (setq	ret (cons (take 2 lst) ret) lst (cddr lst))) (reverse ret))
(pointlist2d (vlax-get (vlax-ename->vla-object (car (entsel))) 'Coordinates))
Message 6 of 8

hak_vz
Advisor
Advisor

That is the way to go.

Task is not as simple as it looks (actually it is but you have to take steps)

I've changed system variable PLINETYPE to 0 and created "heavy or old style" polyline with three vertexes. After selection and conversion to vl object if we run vlax-get coordinates we may receive something like

 

 

(vlax-get eo 'Coordinates)
(100.0 100.0 0.0 200.0 200.0 0.0 300.0 300.0 0.0)

 

 

List length is 9 and coordinates of vertexes are in 3d so function pointlist2d has to be elevated to pointlist 3d to accept z coordinate. Same principle apply to 3d polylines that use same principle. Heavy polylines were simplified to lightweight by removing z coordinate and adding property "elevation".

So you have to change function pointlist2d. Follow up with code continuation.

 

Digression:

Best way to learn visual lisp is to dig deep into object properties and methods so function vlax-object-dump is your best friend. Instead of searching always try to explore object. Create sample entity and work on it. You can copy dump result to separate text file and use it as reminder.

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 7 of 8

Browning_Zed
Advocate
Advocate

The situation here is not as simple as it seems. If the Spline option in the PEDIT is used to smooth the 2D Polyline, then there will not be enough points obtained with (vlax-get eo 'Coordinates). In this case, it will be necessary to calculate additional points on the curve. And then transform the polyline into LW. In addition, all segments of the created LW polyline should become arcs.

 

And thank you for the clarification about vlax-object-dump, I really appreciate it.

0 Likes
Message 8 of 8

hak_vz
Advisor
Advisor

@Browning_Zed wrote:

The situation here is not as simple as it seems. If the Spline option in the PEDIT is used to smooth the 2D Polyline, then there will not be enough points obtained with (vlax-get eo 'Coordinates).


If you have SPLINE object it provide fitpoints property and this are points we are looking for.

 

For splined lwpoly using vlax-get coordinates only provide so called control points. There are different methods to do that. Splined lwpoly is mo more lwpoly object but ACDBPOLYLINE. To  collect fitpoints one would have to use function vlax-curve-getpointatparam and section it to small increment steps. Make it your learning project when you become fluent in using visual lisp. That's all from me for today. Have a nice day!

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes