How do you detect a Polygon?
-
How do you find the difference between a circle and a polygon via Ruby?
-
Well if Poly ain't in it's cage then it's....gone.
Sorry, stupid humor, I should not even be lurking in forums I don't understand.
-
A mystery I was thinking about the other day... without success ! Polygons and Circles are all 'Curves' and 'ArcCurves'
A Circle/Arc 'extrudes' with 'smooth' faces - but a Polygon doesn't ? I thought perhaps if you look for an Edge's Curve and if it's an ArcCurve it could then be an Arc, a Circle or a Polygon. If the ArcCurve Curve's Vertices are not looped
loop=true;curve.vertices.each{|v|if v.edges.length==1;loop=false;break;end}
then it's Circle or a Polygon. Then, if edge.smooth? (or .soft? ???) it's circle, else it's a Polygon...
BUT the smoothness of both is the same - false ...
So how does SUp know the difference between a Circle/Arc and a Polygon. 'Welding' Edges into a Curve
entities.add_curve(points)
makes a a curve where smooth?==true. You make a Polygon with
entities.add_ngon(points)
- however, if you have two Edges one in a Curve of a Cirle and the other a Polygon you can't tell which is whicha s their properties seem to be the same ???
- however, if you have two Edges one in a Curve of a Cirle and the other a Polygon you can't tell which is whicha s their properties seem to be the same ???
-
The brute force way would be to check the angle between all the lines is the same and all lines are the same length. theres got to be a better way of doing it, though.
-
@remus said:
The brute force way would be to check the angle between all the lines is the same and all lines are the same length. theres got to be a better way of doing it, though.
BUT that could still be Circle !!!
-
Sorry, i forgot to add that it should also be a curve/arccurve. Beyond that the distinction is pretty arbitrary anyway because of the segmented nature of circles in SU.
-
Ingenuous suggestion
You can't read the "Entity info" by ruby? -
@tig said:
If the ArcCurve Curve's Vertices are not looped
I check the radius of the ArcCurve to work out if it's a Circle/Polygon. Less entity iteration that way.
def self.is_circle?(ent) # (i) A bug in SU makes extruded circles into Arcs with .end_angle of 720 degrees when reopening the file. # Instead of checking for 360 degrees exactly, we check for anything larger as well. A 2D arc # can't have more than 360 degrees. return false unless ent.kind_of?(Sketchup;;Edge) && ent.curve && ent.curve.kind_of?(Sketchup;;ArcCurve) arc_curve = ent.curve # This doesn't work. Maybe due to rounding errors? #return (arc_curve.end_angle - arc_curve.start_angle >= 360.degrees) ? true ; false return ((arc_curve.end_angle - arc_curve.start_angle).radians >= 360) ? true ; false end
@tig said:
So how does SUp know the difference between a Circle/Arc and a Polygon
SU must have more info internally which isn't hooked up to the Ruby.
@remus said:
Sorry, i forgot to add that it should also be a curve/arccurve. Beyond that the distinction is pretty arbitrary anyway because of the segmented nature of circles in SU.
Polygons comes out as an ArcCurve - so still no go.
@unknownuser said:
Ingenuous suggestion
You can't read the "Entity info" by ruby?Nope.
-
That was the only method I could think of as well. But for what I want to use it for (my Selection Toys plugins - that can potentially evaluate millions of entities in some models) it would be too slow.
I guess an evaluation that doesn't require to test-push-pull the polygon/circle is something to ask for in a feature request?
I tried to have a quick look at the private methods - hoping to find something locked up there. Might try to have another look.
Crosswords puzzles are boring. SU Ruby challenges are much more fun!
-
I had hopes for
.typename
to work - but alas. -
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.
Advertisement