How do you detect a Polygon?
-
I added a feature request on this topic.
-
Ok,
a quick and dirty way is to useSketchup.active_model.selection.count
if it returns 24 (default segments' number in a circle) it is a circle
if it returns anything else it is a polygon
This way does not work if you have 24 segments polygons or if you have changed the circle default segments number.
I am still trying to figure out a better way.
-
Circles can be converted to Polygons - so that won't do either. Detecting circle is easy - checking the start and end angle if it's 360degrees or more. (SU weirdness - circles some times comes out with an total angle of 720 degrees...)
Personally I often change the default segment count. And I also get lots of circles from DWGs.
-
My new method
ArcCurve.is_loop?
tells you if it's an open ended Arc, or looped as a Polygon or Circle, ==no_loose_ends.
The other methodsArcCurve.is_circle?
andArcCurve.is_polygon?
return true if it is...
A clunky fix but it works... -
FYI: the Google response to this:
@unknownuser said:
Hi Thomas,
I’ve traced this through the code, and I’m sad to report that there is nothing exposed in the API that allows you to (quickly) tell the difference between circles and polys. Internally, we store a flag so that says which tool created the curve object. I’ve logged a feature request to expose this in the API.
Until then, the only thing I can suggest is to make a hidden copy of the curve, extrude it, then see if the edges of the resulting faces are smooth. I haven’t tried this yet to see if it works successfully though.
Matt
-
FYI: since SU 7.1M1 you can detect polygons using
Curve.is_polygon?
TIG: that code snippet of yours will now conflict with the new API method.
-
@thomthom said:
FYI: since SU 7.1M1 you can detect polygons using
Curve.is_polygon?
TIG: that code snippet of yours will now conflict with the new API method.
I know BUT not pre v7.1M1... using needing to detect a Polygon need both - with aversion test to load ?
-
I use this:
Sketchup::Curve.method_defined?(:is_polygon?)
But this is why I prefer not to extend the native classes. If someone has implemented the old code - it will still conflict unless that code is updated.
Instead I make methods where I also send the object I want to test.
def MyLib.is_polygon?(curve)
Completely future-safe. -
Here's my three methods
(I've improved .is_loop? to trap any of the arc's vertices that have other edges that are not forming part of that arc !)...ArcCurve.is_loop?
returns true=Circle or Polygon: false=Arc
ArcCurve.is_circle?
returns true=Circle: false=Arc or Polygon
ArcCurve.is_polygon?
returns true=Polygon: false=Arc or Circle*** THIS Method is built-in from SUp v7.1M1v1.2 has test to see if v7.1M1 built-in method if not makes is_polygon?
Other methods also trapped so as not to overwrite built-in methods...***Usage:
if not edge.curve.is_loop? then... ### it's an Arc...
They work, but are a clunky temporary fix...***
-
Updated all of the tests so only load is not built in methods... http://forums.sketchucation.com/viewtopic.php?p=188743#p188743
Advertisement