Creating faces from points or edges fails
-
Hi!
I´m trying to create a face consisting of four edges. Therefore I create the four (connected) edges with the enitites.add_edge method and store the resulting edge-objects in an array. When I try to draw a face by entities.add_face with the array containing the four edges I receive the following error:
Error; #<ArgumentError; wrong number of values in array>
which is strange, because this is what´s in the Array:
[[#<Sketchup;;Edge;0xb7c1ba8>], [#<Sketchup;;Edge;0xb7c189c>], [#<Sketchup;;Edge;0xb7c1590>], [#<Sketchup;;Edge;0xb7c1284>]]
Then I tried to draw the face using all the Point3d-objects by collecting them in an Array while drawing the four edges. When I try to draw the face with the Array of points as an argument I get the error that there are duplicate points in the Array. I tried to remove the duplicate points with the uniq-method, but that doesn´t work. I assume, uniq only works with primitive-datatypes and not with points since the array contains only referneces?
So my three questions are:
-
Why cant the face be drawn with the Array of edges as an argument?
-
Is there an easy way/plugin to delete duplicate points from an array?
-
Is there a better approach to achieve my goal of drawing a face with four given edges?
Here is my so far code:
<span class="syntaxdefault"></span><span class="syntaxkeyword">require </span><span class="syntaxstring">'sketchup.rb'<br /><br /></span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">send_action </span><span class="syntaxstring">"showRubyPanel;"<br /><br /></span><span class="syntaxdefault">model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />entities </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities<br /><br /><br />UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"PlugIns"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Bauteile"</span><span class="syntaxkeyword">) {<br /> <br /> </span><span class="syntaxdefault">dialog </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">WebDialog</span><span class="syntaxkeyword">.new(</span><span class="syntaxstring">"Bauteile"</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">false</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"SketchUp"</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">460</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">600</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">100</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">100</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">false</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_file </span><span class="syntaxstring">"C;/Program Files (x86)/Google/Google SketchUp 8/Plugins/bauteile/frontend_bauteilerstellung.html"<br /> <br /> </span><span class="syntaxdefault">testString </span><span class="syntaxkeyword">=</span><span class="syntaxstring">"0p0p0ae10p0p0k10p0p0ae10p10p0k10p10p0ae0p10p0k0p10p0ae0p0p0"<br /> </span><span class="syntaxdefault">kantenStrings </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">testString</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">split</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"k"</span><span class="syntaxkeyword">)<br /> <br /> <br /> </span><span class="syntaxdefault">kantenArray </span><span class="syntaxkeyword">= Array.new<br /> </span><span class="syntaxdefault">punktArray </span><span class="syntaxkeyword">= Array.new<br /> <br /> </span><span class="syntaxdefault">kantenStrings</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">do |</span><span class="syntaxdefault">k</span><span class="syntaxkeyword">|<br /> </span><span class="syntaxdefault">punkteString </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">k</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">split</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"ae"</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">anfangsPunktString </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">punkteString</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">split</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"p"</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">endPunktString </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">punkteString</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">split</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"p"</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">anfangsPunkt </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">anfangsPunktString</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_i</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">anfangsPunktString</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_i</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">anfangsPunktString</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_i</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">endPunkt </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">endPunktString</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_i</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">endPunktString</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_i</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">endPunktString</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_i</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">punktArray </span><span class="syntaxkeyword"><< </span><span class="syntaxdefault">anfangsPunkt<br /> punktArray </span><span class="syntaxkeyword"><< </span><span class="syntaxdefault">endPunkt<br /> kante </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_edges anfangsPunkt</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">endPunkt<br /> kantenArray </span><span class="syntaxkeyword"><< </span><span class="syntaxdefault">kante<br /> end<br /> <br /> puts kantenArray</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inspect<br /> puts punktArray</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inspect<br /> punktArray </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">punktArray</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">uniq<br /> puts punktArray</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inspect<br /> <br /> flaeche </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_face kantenArray<br /> </span><span class="syntaxcomment">#flaeche = entities.add_face punktArray<br /> <br /> </span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_action_callback</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"zeichne_flaeche"</span><span class="syntaxkeyword">) {|</span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">params</span><span class="syntaxkeyword">|<br /> }<br /> </span><span class="syntaxdefault">dialog</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">show<br /></span><span class="syntaxkeyword">} </span><span class="syntaxdefault"></span>
"testString" is just a...well testString which simulates the data I will later receive from the webDialog. It´s then parsed into edges. That works, since I can see a square in Sketchup.
"kantenArray" is the Array containing the four edges
"punktArray" is the Array containing eight Points, with four of them having the same coordinates -
-
I just found out the answer to question number 1:
since "add_edges" returns an Array of edge (also only 1 edge ist created), only the first (and only) value of the return value must be put into the Array of edges:
wrong:
kante = entities.add_edges anfangsPunkt, endPunkt kantenArray << kante
correct:
kante = entities.add_edges anfangsPunkt, endPunkt kantenArray << kante.first
-
Or, since you only add one edge at a time - use
add_line
instead.kante = entities.add_line( anfangsPunkt, endPunkt ) kantenArray << kante
-
However, I recommend you collect the point first, then when you have them all create all the edges in bulk with
add_edges
- it has much better performance. -
Also be aware of Ruby's expand/contract operator
*
If the method only takes a list of values, you can expand an array into an argument list like:
some_method( *kantenArray )
-
Thanks for your advice, I denfinateley will keep that in mind...or at least try to...
In case I follow your tip with collecting all points first and then create all edges with one single add_edges-command I would still have the problem of the duplicate points. That could quite easily be solved by an own "uniq"-method for Point3d-objects, but then the order of the points in that array could became disarranged, depending on which duplicte entry will be deleted. So I´m afraid my square could easily end up as an sandclock-shaped drawing.
Since I have to draw about 20 faces at most, I will try the edge-by-edge method first.
-
@tafkab76 said:
That could quite easily be solved by an own "uniq"-method for Point3d-objects
Actually - no.
#uniq
will not compareGeom::Point3d
against their positions - but rather if they are the same object instance.
Advertisement