How do you detect a Polygon?
-
You would parse down the entities quite quickly ?
all_entities ==1000000
all_edges ==500000
edge.curve ==50000
is ArCurve ==25000
ArcCurve.is_loop? ==10000
that ArcCurve.is_polygon? ==1000???
BUT, it would be MUCH easier if we had access to the internal methods that SUP has to return Polygon etc in the Entity Info Panel !!!
-
Lateral solution [at last !!!]...
which we can now write into three new methods -ArcCurve.is_loop?
[returns true if it's a Circle OR a Polygon; false if it's an Arc] and alsoArcCurve.is_polygon?
[true if it's a Polygon and false if it's a Circle or an Arc], orArcCurve.is_circle?
[true if it's a Circle and false if it's a Polygon or an Arc[/ruby] ...EDIT: added all 'three' methods...
class Sketchup;;ArcCurve def is_loop?()### circle OR polygon self.edges.each{|e| e.vertices.each{|v| if not v.edges[1] return false ### it's an arc break end#if } } return true end#if def is_polygon?() self.edges.each{|e| e.vertices.each{|v| if not v.edges[1] return false ### it's an arc break end#if } } ### if we get here it might be a polygon !!! model=Sketchup.active_model entities=model.active_entities edge=self.edges[0] all_connected=edge.all_connected tgroup=entities.add_group(all_connected) es=edge.start.position ee=edge.end.position group=tgroup.copy tgroup.explode gents=group.entities gedges=[] gents.each{|e|gedges<<e if e.class==Sketchup;;Edge} gedge=nil gedges.each{|e| e.smooth=false e.soft=false gedge=e if (e.start.position==es and e.end.position==ee)or(e.start.position==ee and e.end.position==es)} ### remove all faces so only face and pushpull's remain gents.each{|e|e.erase! if e.class==Sketchup;;Face} gedge.find_faces face=nil gents.each{|e|face=e if e.class==Sketchup;;Face} return false if not face face.pushpull(1.0) edges=[] gents.each{|e|edges<<e if e.class==Sketchup;;Edge} edges.each{|e| if e.smooth? and e.soft? group.erase! if group.valid? return false ### a circle end#if } group.erase! if group.valid? return true ### a polygon end#def def is_circle?() self.edges.each{|e| e.vertices.each{|v| if not v.edges[1] return false ### it's an arc break end#if } } ### if we get here it might be a polygon !!! model=Sketchup.active_model entities=model.active_entities edge=self.edges[0] all_connected=edge.all_connected tgroup=entities.add_group(all_connected) es=edge.start.position ee=edge.end.position group=tgroup.copy tgroup.explode gents=group.entities gedges=[] gents.each{|e|gedges<<e if e.class==Sketchup;;Edge} gedge=nil gedges.each{|e| e.smooth=false e.soft=false gedge=e if (e.start.position==es and e.end.position==ee)or(e.start.position==ee and e.end.position==es)} ### remove all faces so only face and pushpull's remain gents.each{|e|e.erase! if e.class==Sketchup;;Face} gedge.find_faces face=nil gents.each{|e|face=e if e.class==Sketchup;;Face} return false if not face face.pushpull(1.0) edges=[] gents.each{|e|edges<<e if e.class==Sketchup;;Edge} edges.each{|e| if e.smooth? and e.soft? group.erase! if group.valid? return true ### a circle end#if } group.erase! if group.valid? return false ### a polygon end#def end#class
-
Can't just check if 2 adjacent edges are soft? Circles are, polys are not, right?
Guess not.
-
@jim said:
Can't just check if 2 adjacent edges are soft? Circles are, polys are not, right?
Guess not.
You'd think so, so did I and TIG, but that's not the case.
-
-
The Edges in a Curve that's an ArcCurve, that's either a Circle and a Polygon, are identical [neither are smooth nor soft] !!! It's only when you Extrude them in 3D the difference is visually [and Ruby-test-ably] apparent. There is [seems to be?] no Ruby-accessible Method to see if a looped ArcCurve is a Circle or a Polygon... UNLESS you do my elaborate lateral and clunky test by extruding it and testing the extrusion's edges for smooth/soft? ...
-
Another oddity. Select an extruded circle. From the console:
sel[0].soft=true
If you click on the circle where one of the edges now are soft - the circle is now selected as well as the faces connected to it. -
@thomthom said:
@thomthom said:
@jim said:
Can't just check if 2 adjacent edges are soft? Circles are, polys are not, right?
Guess not.
You'd think so, so did I and TIG, but that's not the case.
Thinking of it - it makes sense that they don't as that would render extruded circles invisible.
Smooth/soft edges that are profiles stay visible...
-
Right, it's the connecting edges of an extrusion that are softened, not the perimeter of the polygon/circle.
-
Only the edges that forms the outline of the geometry from the current view.
Look at it from an angle where it doesn't take part of the outline and it's hidden.
-
When you Smooth/Soften the Polygon's Edge its Face merges with the already Smoothed/Softened side Faces in the extrusion to give a single 'Surface' - as reported in the Entity Info Box. This 'Surface' Selects and Highlights as one thing... unless Hidden Geometry is 'on', then the separate Faces that make up that 'Surface' are individually Selectable/Highlighted. Presumably the [assumed]
Face::is_surface?
method that Entity Info uses, simply looks to see if some of the Face's Edges are Smooth/Soft and have extra Faces attached ? -
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...***
Advertisement