Ruby script to output outer face loop?
-
Hi gang,
It has been a couple of years since I started to delve into the sketchup api and ruby.
I am doing a couple of jobs in VB.NET at the moment that could benefit from
converting 2D sketchup geometry into VB.NET code for it's graphicalpath class.Would some kind soul tutor me through creating the script.
I tossed the following together from memory, but I am sure it is way off base.
# Output a VB.NET graphicalpath for a closed face # bounded by 2D lines and arcs to the windows clipboard. require 'sketchup' include Win32 model = Sketchup.active_model face = model.selection[0] if (Face) if Face.is_a? Sketchup;;Face loop = face.outerloop edgeuses = loop.edgeuses sout = "" edgeuses.each do|edgeuse| edge = edgeuse.edge if edge.is_a? Sketchup;;Line line = edge.line s + ConvertToNetLine(line) Elsif edge.is_a? Sketchup;;ArcCurve arc = edge.arccurve s + ConvertToNETArc(arc) end end clipboard.set_data(sout) else # Show 'Select a single Face and try again' end end #--- Functions --- def ConvertToDotNetLine(line) # Convert to VB.NET code graphicpath line definition return "gp.AddLine( + line[0] + "," + line[1] + "," + line[3] + "," + line[4] + ")\n" end def ConvertToDotNetArc(arc) # Convert to VB.NET code graphicpath arc definition x = arc.center[0]-arc.radius # Left of arc circle's bounding box. y = arc.center[y]-arc.radius # Top of arc circle's bounding box. wt = arc.radius * 2 # width of arc circle's bounding box. ht = wt # height of arc circle's bounding box. startang = degrees(arc.start) endang = degrees(arc.stop-arcstart) # Delta of arc angle Return = "gp.AddArc(" + x + "," + y + "," + wt + "," + ht + "," + startang "," + endang + "\n" end def degrees(radians) return radians / 2 * 3.1415 end
-
What is the question here? What is it that you're stuck with?
-
The best way to create a script is to create line by line on the console. This makes sure that every line has been tested.
Here are some things I saw:
-
You probably meant
sout += ConvertToNetLine(line)
-
There is no
Sketchup::Line
, butSketchup::Edge
. -
edge.line has only two elements: [Sketchup::Point3d, Sketchup::Vector3d]
-
Ruby is uses consistent case (case sensitive), everything is lower case except of constants:
E̶l̶s̶i̶f̶ elsif
R̶e̶t̶u̶r̶n̶ ̶=̶ return -
the SketchUp API returns coordinates (and other numbers) as "Length" (of the Length class). Those take their units along into the string, and you would get "AddLine(~2.2cm,…)". So you change the type from Length to Float using
to_f
. -
if edge.is_a? Sketchup::Edge cs = edge.vertices.map{ |v| p = v.position; [p.x.to_f, p.y.to_f] }.flatten s + ConvertToNetLine(*cs)
…
-
-
Thank you gentlemen for the replies.
I really want to get into this conversation, but right now I am slammed
with work.My company just upgraded our MRP system and every app I've ever written that leverages the database it is broken.
I will get back to this question sometime in the near future... perhaps in a week or two.
Regards,
Tom
-
I guess I am confused about straight lines.
From the API documentation I was under the impression that an Edge could be
A line, curve, or arccurve.How do I determine which is which in an if statement.
Won't All three be true for "if edge.is_a? Sketchup::Edge"? -
An
Edge
can be part of anArcCurve
orCurve
.Use
Edge.curve
to check this, it will return the curve instance if it is andnil
if it't not.
http://www.sketchup.com/intl/en/developer/docs/ourdoc/edge.php#curve -
@tt_su said:
An
Edge
can be part of anArcCurve
orCurve
.Use
Edge.curve
to check this, it will return the curve instance if it is andnil
if it't not.
http://www.sketchup.com/intl/en/developer/docs/ourdoc/edge.php#curveThe tricky part for Ruby developers is that ArcCurve and Curve do not appear in the Entities collection. That is, you can't detect that they exist by scanning the Entities. You have to test Edges to find them.
Steve
-
True that - they are meta-entities.
-
Okay I have a test script that iterates over the outer loop of a simple rectangle with
a radius at each corner. What I cannot figure out is how to return just the four lines and four arcs.What I get with the following code is four lines and a hit for every facet in each arc.
What am I doing wrong?
require 'sketchup' model = Sketchup.active_model face = model.selection[0] if (face) loop = face.outer_loop edgeuses = loop.edgeuses edgeuses.each do|edgeuse| edge = edgeuse.edge curve = edge.curve if (curve) if curve.is_a? Sketchup;;ArcCurve UI.messagebox "Edge is an Arc Curve" else UI.messagebox "Edge is a Curve" end else UI.messagebox "Edge is a Line" end end # end edgeuses each end
-
You find the same curve again and again for each Edge associated with it. When you find a curve, you need to use its edges method to get the other Edges in the same Curve and remove them from further consideration. For instance,
require 'sketchup' model = Sketchup.active_model face = model.selection[0] used_edges = [] if (face) loop = face.outer_loop edgeuses = loop.edgeuses edgeuses.each do|edgeuse| edge = edgeuse.edge next if used_edges.index edge curve = edge.curve if (curve) used_edges = used_edges | curve.edges if curve.is_a? Sketchup;;ArcCurve UI.messagebox "Edge is an Arc Curve" else UI.messagebox "Edge is a Curve" end else UI.messagebox "Edge is a Line" end end # end edgeuses each end
-
I think you gentlemen may be giving me too much credit for my knowledge of ruby.
I am a rank beginner and do not get everything your are saying or showing in your snippets.slbaumgartner: your code still displays a arccurve message for all facets of the arc.
How is "next if used_edges.index edge" suppose to remove edges from consideration?tt_su: Your code fails to run at all. "EdgeUses not found"
Would you be so kind as to explain how each line of your snippet works.Regards,
-
Okay, Looked up the 'Uniq' method Now I am even more puzzled.
The following code is a modified version of tt_su's.
Uniq appears to be returning the wrong number of array elements.model = Sketchup.active_model face = model.selection[0] if face count = 0 edges_and_curves = [] edges_filtered = [] loop = face.outer_loop edgeuses = loop.edgeuses edgeuses.each do|edgeuse| count += 1 edge = edgeuse.edge edges_and_curves << edge.curve || edge end edges_filtered = edges_and_curves.uniq UI.messagebox(edges_filtered.length) # Reports 5 Should be 8 UI.messagebox(count) # Correctly reports 52. (4 sides and 4 arcs * 12 facets) Correct. end
-
@emptyvessel said:
I think you gentlemen may be giving me too much credit for my knowledge of ruby.
I am a rank beginner and do not get everything your are saying or showing in your snippets.slbaumgartner: your code still displays a arccurve message for all facets of the arc.
How is "next if used_edges.index edge" suppose to remove edges from consideration?tt_su: Your code fails to run at all. "EdgeUses not found"
Would you be so kind as to explain how each line of your snippet works.Regards,
Hmm. I edited my post after I found errors (typed too fast) - did you try the latest version? I drew a rectangle, changed all the corners to arcs, and ran this and it report four Edges and four ArcCurves... When it completes it dumps a whole Array of stuff on the Console, but that is just because Ruby always returns the last thing it evaluated and I didn't bother to stop it.
If you have a specific example skp, please post it and I'll try to see what went astray.
Also, it looks to me like TT also typed in haste. I don't think his code is right...for instance edgeuses is undefined!
Steve
-
<span class="syntaxdefault"><br />model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />face </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /><br /></span><span class="syntaxdefault">if face<br /> edges_and_curves </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[]<br /></span><span class="syntaxdefault"> face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">outer_loop</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edgeuses</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">edgeuse</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> edge </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> edgeuse</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edge<br /> edges_and_curves </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">||</span><span class="syntaxdefault"> edge<br /> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Because we will have added the Curve entities for each edge segment.<br /></span><span class="syntaxdefault"> edges_and_curves</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">uniq</span><span class="syntaxkeyword">!<br /></span><span class="syntaxdefault">end<br /></span>
-
In deed - I typed in a hurry and made a mess out of it. I made changes in a hurry now as well, but I still haven't checked it properly. Sorry. I'll get back to this next week. Now I need to get to bed as I'm catching a plane early in the morning.
-
Okay I worked on this this weekend and I am ALMOST getting the output I want...
'VB.NET Code output gp.AddLine(0.000,10.000,0.000,2.000) gp.AddArc(0.000,8.000,4.000,4.000,0.000,90.000) gp.AddLine(10.000,12.000,2.000,12.000) gp.AddArc(8.000,8.000,4.000,4.000,0.000,90.000) gp.AddLine(12.000,2.000,12.000,10.000) gp.AddArc(8.000,-0.000,4.000,4.000,0.000,90.000) gp.AddLine(2.000,0.000,10.000,-0.000) gp.AddArc(0.000,0.000,4.000,4.000,0.000,90.000)
The start and end arc angles all appear to be 0 and 90.
The x,y center point is different for each so I know I am getting each arc in the loop.How does this work?
My latest conversion code.
require 'sketchup' # Task; Convert sketchup face outer loop to VB.NET graphicpath code def ConvertToDotNetLine(edge) # Convert to VB.NET code graphicpath line definition startpt = edge.start.position endpt = edge.end.position s = "gp.AddLine(" s += "%.3f" % startpt.x.to_f + "," s += "%.3f" % startpt.y.to_f + "," s += "%.3f" % endpt.x.to_f + "," s += "%.3f" % endpt.y.to_f + ")\n" return s end def ConvertToDotNetArc(arc) # Convert to VB.NET code graphicpath arc definition x = arc.center[0]-arc.radius # Left of arc circle's bounding box. y = arc.center[1]-arc.radius # Top of arc circle's bounding box. wt = arc.radius * 2 # width of arc circle's bounding box. ht = wt # height of arc circle's bounding box. startang = arc.start_angle endang = arc.end_angle s = "gp.AddArc(" s += "%.3f" % x.to_f + "," s += "%.3f" % y.to_f + "," s += "%.3f" % wt.to_f + "," s += "%.3f" % ht.to_f + "," s += "%.3f" % startang.radians + "," s += "%.3f" % endang.radians + ")\n" return s end model = Sketchup.active_model face = model.selection[0] if (face) sout = "" used_edges = [] loop = face.outer_loop edgeuses = loop.edgeuses edgeuses.each do|edgeuse| edge = edgeuse.edge next if used_edges.index edge curve = edge.curve if (curve) used_edges = used_edges | curve.edges if curve.is_a? Sketchup;;ArcCurve sout += ConvertToDotNetArc(curve) # UI.messagebox "Edge is an Arc Curve" else UI.messagebox "Edge is a Curve" end else sout += ConvertToDotNetLine(edge) # UI.messagebox "Edge is a Line" end end # end edgeuses each f = File.open("C;\\aaapath.txt", "w") f.write(sout) f.close UI.messagebox(sout) end
-
Discovered the Angle_Between() method and I am now calculating the start angle.
Things are looking better...
Except now I see that an outer loop in Sketchup is not a true path.
Endpoints of edges are not necessarily coincident with the start point of the next edge.Is there some method or setting that will force this situation?
Otherwise I am looking at recreating every edge and curve in the correct order.This is turning into a much bigger project than I had originally hoped.
-
Use the Loop and EdgeUse objects that a face will give you:
http://www.sketchup.com/intl/en/developer/docs/ourdoc/face.php#loopsThat will let you traverse the edges and vertices of a face in correct order.
-
Thank you Thomas for the reply.
So to verify you are saying that the Loops object returns edges in order, but the outer_loop does not. Correct?
[Edit]
Nope doesn't seem to matter. It is still creating lines flipped around.
(see attachment)
[/Edit]
-
face.outer_loop returns a Loop object. Loop object will return vertices in order and it will return EdgeUse objects in order.
If you have an Edge and want to figure out it's direction in relationship with a face you must use edge.reversed_in?(face).
Advertisement