Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Circle to Polyline, circular Polyline to Circle

52 REPLIES 52
Reply
Message 1 of 53
Kent1Cooper
28546 Views, 52 Replies

Circle to Polyline, circular Polyline to Circle

I had occasion to want to convert a Circle to a circular Polyline [so I could give it width, but I can imagine other reasons to want to do that].  Since Pedit won't do that conversion with a Circle as it will with a Line or an Arc, I looked for a routine here [assuming I would need to do this again from time to time], that would do the conversion by a simple selection of the Circle, so I didn't have to go through all the steps needed to construct an equivalent Polyline over the Circle and then erase the Circle.  I found some things that were prior to the existence of Lightweight Polylines, and/or that didn't take into account different Coordinate Systems, or a locked Layer, or non-default overrides of color, linetype, etc.

 

So I made one, and while I was at it, made one to go the other way -- convert a circular Polyline (Donut) into a Circle.  [I also found something here to do that, but along with the shortcomings above, it also didn't quite determine whether the selected Polyline is actually circular in Donut-like fashion.]  The Polyline-to-Circle converter works with either heavy or lightweight circular Polylines, and if the selected one has a global non-zero width, the User gets the choice of whether to draw the Circle along the Polyline's center-line or along its inside or outside edge.  It even saves that choice to offer as a default on further use.

 

CirclePolylineSwap.lsp contains two commands:  C2P [= Circle To Polyline] and P2C [= Polyline To Circle].  I hope others will find them useful.

Kent Cooper, AIA
52 REPLIES 52
Message 41 of 53
veco.popovic
in reply to: Kent1Cooper

I have problem with your code. I have a lot of Polylines, and it shows me message like in a attachmetnt.

Message 42 of 53
Kent1Cooper
in reply to: veco.popovic


@veco.popovic wrote:

I have problem with your code. I have a lot of Polylines, and it shows me message like in a attachmetnt.


I suspect the problem is not with the code, but with the Polylines.  It means just what it says.  They need to be closed Polylines, made of arc segments only, and geometrically identical to a circle.  If you post a drawing file, maybe we can tell what the problem is more specifically.

 

EDIT:  And did you get the latest version, in Message 27?  The first version required two-equal-arc-segment Polylines such as Donut makes, but the later versions allow more and/or unequal-swing arc segments, as long as they lie along the equivalent circle.  But the Polyline still has to be closed, and of arc segments only, and can't have coincident vertices, or back-track on itself.

Kent Cooper, AIA
Message 43 of 53
veco.popovic
in reply to: Kent1Cooper

  • Yes,  I tried code from message 27. Please look in drawing in attachment. I hope that you will help me. I am relatively new in AutoCad. 

@Kent1Cooper wrote:

@veco.popovic wrote:

I have problem with your code. I have a lot of Polylines, and it shows me message like in a attachmetnt.


I suspect the problem is not with the code, but with the Polylines.  It means just what it says.  They need to be closed Polylines, made of arc segments only, and geometrically identical to a circle.  If you post a drawing file, maybe we can tell what the problem is more specifically.

 

EDIT:  And did you get the latest version, in Message 27?  The first version required two-equal-arc-segment Polylines such as Donut makes, but the later versions allow more and/or unequal-swing arc segments, as long as they lie along the equivalent circle.  But the Polyline still has to be closed, and of arc segments only, and can't have coincident vertices, or back-track on itself.


 

Message 44 of 53
cadffm
in reply to: veco.popovic

The lispfunction P2C in #27 is for (only) BULGED polylines, your polyline segments are straight,

this is another case and need another solution.

 

 

- Sebastian -
Message 45 of 53
veco.popovic
in reply to: cadffm

Can you explain how to do this? 

Message 46 of 53
cadffm
in reply to: veco.popovic

Yes&No,

I am familiar with the DWG / DXF structure and can create simple programming as required.

It is an answer, i can explain it, but it doesn't help you directly.

 

 

- Sebastian -
Message 47 of 53
hak_vz
in reply to: veco.popovic

@veco.popovic  Try this

 

(defun c:sp2c () (sp2c))
(defun sp2c ( / ss n e eo sp ro cp r len )
	(princ "\nSelect polylines to convert to circles >")
	(setvar 'cmdecho 0)
	(setq ss (ssget '((0 . "*polyline"))))
	(cond 
		((and ss)
			(setq n (-(sslength ss) 1))
			(while (>= n 0)
				(setq e (ssname ss n))
				(setq eo (vlax-ename->vla-object e))
				(setq len  (vlax-get eo 'Length))
				(command "region" e "")
				(setq cp (vlax-get (vlax-ename->vla-object (entlast)) 'Centroid))
				(setq r (/ len (* 2 pi)))
				(entdel (entlast))
				(command "circle" cp r)
				(setq n (- n 1))
			)
		)
	)
	(command "erase" ss "")
	(setvar 'cmdecho 1)
	(princ)
)

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 48 of 53
veco.popovic
in reply to: hak_vz

Thanks a lot! It works. 

Message 49 of 53
Kent1Cooper
in reply to: veco.popovic

Those are regular-enough polygons that PolygonToCircle.lsp with its Pg2C command, attached at Message 20, converts them all to Circles.

Kent Cooper, AIA
Message 50 of 53
ronjonp
in reply to: veco.popovic

Here's another for fun:

 

(defun c:foo (/ ll s ur v)
  ;; RJP » 2020-09-15
  (if (setq s (ssget ":L" '((0 . "*polyline"))))
    (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      (vla-getboundingbox (vlax-ename->vla-object e) 'll 'ur)
      (mapcar 'set '(ll ur) (mapcar 'vlax-safearray->list (list ll ur)))
      (setq v (mapcar 'abs (mapcar '- ll ur)))
      ;; Check if we're mostly square
      (and (equal (car v) (cadr v) 0.1)
	   (entmakex (list '(0 . "circle")
			   (assoc 8 (entget e))
			   (cons 10 (polar ll (angle ll ur) (/ (distance ll ur) 2.)))
			   (cons 40 (/ (car v) 2.))
		     )
	   )
	   (entdel e)
      )
    )
  )
  (princ)
)

 

Message 51 of 53
veco.popovic
in reply to: ronjonp

thanks! It's so quick.

Message 52 of 53
veco.popovic
in reply to: Kent1Cooper

thanks

Message 53 of 53
Kent1Cooper
in reply to: veco.popovic

With polygons of as many segments as yours, it may not make a meaningful difference, but there is a difference in the results of the three commands posted here recently.  They're all useful in their ways, but some people may have a need for a particular kind of result, so they should know what the differences are.  Using the exaggerated example of an octagon [the dashed green in each case], here's what they do:

SP2C-Pg2C-foo.JPG

 

SP2C makes a Circle whose perimeter length is the same as  the source Polyline's, so in the case of a regular polygon source, it goes both inboard and outboard of the original.  Pg2C makes one that goes through the Polyline's vertices, that is, a circumscribed Circle.  foo uses the bounding box  of the source Polyline, so it's affected by orientation -- on the left, it makes an incscribed Circle; on the right [same polygon, rotated], a Circumscribed one [equivalent to Pg2C].

 

Pg2C requires that the source Polyline be an actual regular polygon  [within tolerance], and won't do anything with other shapes.  SP2C will make a Circle from one of any shape, and foo will as long as the bounding box is the same in height and width  [within tolerance]:

SP2C-foo.JPG

 

One last thing:  foo [alone among these three] will even do it with a Polyline that is not closed.

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost