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
27973 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 21 of 53

 

Hi,

 

Thanks for your prompt response, the Lisp is working fine. But I need to convert Polyline to circle as per attached DWG file. Can you please help me on this?

---
Regards,
Shashanka
Message 22 of 53


@shashankabehera wrote:

....

Thanks for your prompt response, the Lisp is working fine. But I need to convert Polyline to circle as per attached DWG file. Can you please help me on this?


Those Polylines are not closed, and many [but not all] have one or more locations [not the same one(s) in each Polyline] with a whole pile of vertices [9 or so] in the same place, so they have a lot of zero-length segments.  Also, they're not quite regular [draw Lines across opposite vertices, and you'll see that they don't intersect in the center, and if you take your Units precision to 3 decimal places and grab all the Lines, you'll see that they're not the same length].  So they fail several of Pg2C's tests and don't "qualify" as regular polygons.

 

I suggest you set running Endpoint [only] object snap, and draw Circles with the 3Point option.  That could be automated something like this:

 

(setq pl (car (entsel "\nSelect at-least-25-sided Polyline to convert to Circle: ")))
(if (> (cdr (assoc 90 (entget pl))) 24)
  (progn
    (command "_.circle" "_3P"
      (vlax-curve-getPointAtParam pl 0)
      (vlax-curve-getPointAtParam pl 12)
      (vlax-curve-getPointAtParam pl 24)
    )
    (entdel pl)
  )
)

 

That does only one at a time, but it could be put into something that would allow selecting more than one.  But you'd have to be careful about what you select, because while it will decline to process your outline [too few vertices], it can't filter out any other Polylines with enough segments, since it has no criteria by which to judge whether they're approximately circular.

 

I briefly tried taking Pg2C and eliminating the checks on whether a polygon is closed and whether its segments are all the same length, but it didn't seem to work -- there are too many things about it that depend on the polygon being regular.  For instance, it finds the center using the point opposite the start, which will truly be opposite if it's a regular polygon, but having zero-length segments throws that off, so even if it calculates something, the resulting Circle won't be in the right place.

Kent Cooper, AIA
Message 23 of 53
Kent1Cooper
in reply to: Kent1Cooper


@Kent1Cooper wrote:

I had occasion to want to convert a Circle to a circular Polyline .... and while I was at it, ... to go the other way -- convert a circular Polyline (Donut) into a Circle. ....


Here's a further upgraded version of CirclePolylineSwap.lsp.  Two main improvements:

 

1.  Instead of converting only one thing at a time, both commands now let you pick as many objects as you want, and convert all qualifying objects in one running of the command.  (In P2C, if any of the selected Polylines have global non-zero width, it asks once about the Inside/Center/Outside positioning of the resulting Circle, and applies the same choice to all such Polylines in the selection.)

 

2.  P2C now uses a different method to check whether a Polyline is circular and should be converted to a Circle.  It no longer requires Polylines such as Donut makes, restricted to two equal half-circle arc segments.  It will convert any truly circular Polyline, no matter how many segments it has, and whether or not they're of equal included angles, as long as they don't backtrack over themselves.

Kent Cooper, AIA
Message 24 of 53
Edwin
in reply to: Kent1Cooper

Thanks buddy!You're an ace.

Message 25 of 53
zasanil
in reply to: Kent1Cooper

Hi Kent,

This is a really great lisp that you wrote. While using the "P2C" function on one of my drawings with multiple objects selected, I noticed that only one of the objects translated is the correct size. I would run the command, select multiple wide polylines of all the same diameter/length/width, then select inside (or outside), and they all translated as if I selected centerline except for one. It would do this with however many I selected. If I only select 1 it seems to work as I picked. I don't know if there is a newer version of this lisp that fixes it, but I wanted to see if there was a work-around.

Thanks!

Dan Nicholson C.I.D.
PCB Design Engineer
Message 26 of 53
Kent1Cooper
in reply to: zasanil


@zasanil wrote:

.... While using the "P2C" function on one of my drawings with multiple objects selected, I noticed that only one of the objects translated is the correct size. I would run the command, select multiple wide polylines of all the same diameter/length/width, then select inside (or outside), and they all translated as if I selected centerline except for one. ....


Very interesting -- it does the same for me, so I must not have tried that kind of a situation when testing it.  Looking over the code, nothing jumped out at me as a cause, but I'll certainly try to figure that out.

Kent Cooper, AIA
Message 27 of 53
Kent1Cooper
in reply to: zasanil


@zasanil wrote:

.... While using the "P2C" function on one of my drawings with multiple objects selected, I noticed that only one of the objects translated is the correct size. I would run the command, select multiple wide polylines of all the same diameter/length/width, then select inside (or outside), and they all translated as if I selected centerline except for one. ....


Found it!  I had something being checked inside an (and) function inside an (if) function, that should have been out as its own separate (if) check [I can spell out why it caused that problem if you really want to know the nitty-gritty].  I've corrected that, and while I was in there, I changed the Undo begin/end and Undo end in the *error* handler to be acceptable in 2015 [no longer allowing (command) functions in *error* without extra shenanigans], and tidied up a few other little things.  Thanks for finding that shortcoming so I could fix it.

Kent Cooper, AIA
Message 28 of 53
zasanil
in reply to: Kent1Cooper

That is much better! This will be a good timesaver.

Thanks Kent!

Dan Nicholson C.I.D.
PCB Design Engineer
Message 29 of 53
Edwin
in reply to: Kent1Cooper

So much appreciated Kent.

 

You're an ace-clipart-ace_of_hearts.jpg!!!!!!!!!!!!!!!!!!

 

Thanks,

Ed

Message 30 of 53
Edwin
in reply to: Kent1Cooper

Hi Kent,

 

It works well although takes effect to only 1 object selection while option asks "select objects:"

 

There must be a way to boost the file to perform global selection.

 

I am dealing with tree canopy  drawing by the way.

 

Also, you may be able to know how to "convert points to circles" with known diameter attributes, also takes effect on global selection.

 

I'll be grateful.

 

 

Cheers and more power,

Ed 

Message 31 of 53
Kent1Cooper
in reply to: Edwin


@EDWIN wrote:

.... 

It works well although takes effect to only 1 object selection while option asks "select objects:"

 

There must be a way to boost the file to perform global selection.

....

Also, you may be able to know how to "convert points to circles" with known diameter attributes, also takes effect on global selection.

....


I don't understand.  You can select as many objects as you want at once, not only 1, with individual picking and/or windowing, crossing, fences, removing/adding and all the other selection options, and the commands convert all of them that are of the right characteristics in one shot.  Or by "global selection" do you mean that you would like the routines to find and select all potential objects for you, so you don't need to select anything?  That could be done with (ssget "_X" ...), but I'd be reluctant to make it that way.  For example, in P2C, there's no way to filter in (ssget) for the characteristics that make a Polyline circular, so you would need to have it find all Polylines of any type and any configuration, and then step through all of them to check each one for circularity.  The way I draw, there would be far more Polylines that it couldn't work with than those that it could, but it would need to look at them all anyway.  And what if there are some in different spaces?

 

Making Circles at all Points would be comparatively easy, if you want all the Circles in the same conversion command to be of the same radius.  Again, would you want such a routine to find all Points in the drawing and make a Circle around every one, or do it to those in a User selection?  Would you want it to keep the Points or remove them?

Kent Cooper, AIA
Message 32 of 53
Edwin
in reply to: Kent1Cooper

You're most helpful and prompt Kent.

 

  • "C2p ansd P2C"

I am still finding the routine converts only 1 among the selected circles (by windowing, crossing, fences, removing/adding and all the other selection options,etc...);similarly with the P2C command.

 

  • "Making Circles at all Points "

These points have northing/easting coordinates and various diameters or radii as available attributes (from Excel) to represent tree canopies.

YES, it will be awesome to have a routine to find all Points within a User selection in the drawing and make a Circle around and keep the points.

 

 

Gratefully yours,

Ed

 

 

 

 

 

 

 

 

Message 33 of 53
Kent1Cooper
in reply to: Edwin


@EDWIN wrote:

....

I am still finding the routine converts only 1 among the selected circles ...; similarly with the P2C command.

....

"Making Circles at all Points "

These points have northing/easting coordinates and various diameters or radii as available attributes (from Excel)....

.... 


Both commands work for multiple objects for me, in both AutoCAD 2004 and 2015 [the two versions I have access to].  There must be some other setting on your computer that's not standard, or something else wrong.  Do they allow you to select more than one, but convert only one of them, or do they allow you to select only one?

 

On the Circles from Points, are you talking about Point entities, or Blocks [suggested by your use of the word "attributes"], or maybe is there nothing already drawn but only the information in Excel?  I would not be able to help with relating to an Excel file, but I imagine the people over in the Civil Forums would.

Kent Cooper, AIA
Message 34 of 53
Edwin
in reply to: Kent1Cooper

C2P and P2C: Cheers Kent It's working!

 

Circles from points: Point entities are good enough. A point with Block Attributes would be as good.

 

  

 

THANKS heaps and have a good day mate!

Message 35 of 53
zasanil
in reply to: Kent1Cooper

Hi Kent,

Would it be easy to modify this so that it works the same way for polylines with width? I don't mean changing it to a circle, but changing a wide polyline to be either inside, center, or outside by the global width (its constant) of the polyline and deleting the original. I think the way this current version promps for a direction and use the width as the distance is great. I also like how you can select multiple polylines on different layers and it would retain which layer the polyline was on. If theres a lisp that already does this please let me know.

Thanks!

Dan Nicholson C.I.D.
PCB Design Engineer
Message 36 of 53
Kent1Cooper
in reply to: zasanil


@zasanil wrote:

... Would it be easy to modify this so that it works the same way for polylines with width? I don't mean changing it to a circle, but changing a wide polyline to be either inside, center, or outside by the global width (its constant) of the polyline and deleting the original. ....


I assume you are looking for zero-width end results.  For the center option, you can just select as many Polylines as you like, and in the Properties box, collectively give them a global width of 0.  May I also assume that for the inside- or outside-edge options, you might want to use such a thing on Polylines of any shape, not just circular ones?  [That would make it a lot easier, not needing to determine whether they're circular.]  Whether open or closed?

 

There is at least one routine out there [and probably others] that makes zero-width Polylines along both edges of Polylines with constant width -- see, for example, PLWtoOutline.lsp at this location, and some of the earlier permutations on the same thread [e.g. PLWto2.lsp if you don't need the ends of un-closed ones connected].  It could probably be modified easily enough to do one or the other at the User's choice.  [Post 21 there has something to deal with non-constant width, though I haven't tried it.]

Kent Cooper, AIA
Message 37 of 53
zasanil
in reply to: Kent1Cooper

I guess the center one would just be making it zero, hehe. I was looking for something that you could pick either inside or outside and have it be a closed polyline on the same layer it started out, and with the original deleted. This would be for any shape, correct. Basically something like the PolylineToCircle program you have but for any shape. (Ending with closed zero width polylines instead of circles). I'll check out that thread you linked.

Thanks,

Dan Nicholson C.I.D.
PCB Design Engineer
Message 38 of 53
zasanil
in reply to: Kent1Cooper

Kent,

I was trying to incorpoerate your p2c routine into my lisp by hardcoding an outside and inside version and picking a selection set on certain layers (instead of the user input). I've run into a problem though trying to get it to work. When I run the regular p2c - outside command and select the polylines manually, it converts them fine. If I run the hardcoded version, it see the polylines as 0.0 radius and makes them really big. I can't figure out what I'm doing wrong in just hardcoding it and getting it to work right. I've attached the polylines in question and the edited code. Could you please help?

Dan Nicholson C.I.D.
PCB Design Engineer
Message 39 of 53
zasanil
in reply to: zasanil

Never mind. I just realized my mistake..
Sorry!
Dan Nicholson C.I.D.
PCB Design Engineer
Message 40 of 53
Edwin
in reply to: zasanil

Points to Circles with variable diameters or radii

 

Hi Kent/Dan,

 

Much appreciate your efforts on this.

Follwing below is part of Point to Circle routine

defun c:pt2cir ()
(setq sset (ssget "_X" '((0 . "POINT"))) i 0)

(if sset
(repeat (sslength sset)
(setq ent (ssname sset i))
(entmake
(list
(cons 0 "CIRCLE")
(cons 10 (cdr (assoc 10 (entget ent))))
(cons 40 10.0) ;circle size, change the 10.0 to whatever you want
)
)
(entdel ent)
(setq i (1+ i))
)
)
(princ)
)
(prompt "\n TYPE PT2CIR at the command line")

 

which might be helpful to find the missing link. This translates all points to circles with the default 10units radius. The criteria here is to be able translate point/s with corrersponding x,y (easting,northing) coordinates and variable diameters

 

 

By the way, I'm still a novice on developing lisps routine...and trying hard, that is.

 

 

Thanks guys...and cheers!Smiley Happy

 

 

 

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

Post to forums  

Autodesk Design & Make Report

”Boost