Face Dimensions
-
I am having problems getting the length and breadth of a rectangular face in SketchUp. I am using this code:` def height
fheight = 0
bb = self.boundsif bb.depth != 0 fheight = bb.depth else fheight = bb.height end return fheight.to_m end # Method to return the width of a face in m def width fwidth = 0 bb = self.bounds if bb.depth != 0 fwidth = Math.sqrt((bb.width)**2 + (bb.height)**2) else fwidth = bb.width end return fwidth.to_m end`
But I am not getting the correct dimensions of the red rectangular face in attached screenshots.
-
You're using bounds to get dimensions - BoundingBox objects are always aligned the the world axis. What you want to do is investigate the faces edges and their lengths. Sketchup::Edge got a length method that returns the dimension for the edge.
btw
@unknownuser said:
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># Method to return the width of a face in m<br /></span><span class="syntaxdefault">def width<br /> fwidth </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">0<br /> bb </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">bounds<br /></span>
You're extending the base Sketchup::Face class? If you do, beware that if you public the plugin you might run into naming conflict if some other plugin use the same name. Using the base classes has the same risks of naming conflict as making methods in the global namespace.
-
Same component, different bbox. The bbox is subject to the world axis at the time the component is created.
-
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># Returns nil if the face is not a parallelogram.<br /># Returns an array [width, height] on success.<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_rectangle_size</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">edges </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">outer_loop</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edges<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil unless edges</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">size </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">4<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil unless edges</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">edges</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">length<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil unless edges</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">edges</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">length<br /> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">edges</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">edges</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">end<br /></span>
Untested - but I think it should work.
-
As requested per PM:
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># Returns nil if the face is not a circle.<br /># Returns the radius on success.<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_circle_radius</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">edges </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">outer_loop</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edges<br /> edge </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">edges</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /> return </span><span class="syntaxdefault">nil unless edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?( </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ArcCurve </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">radius<br />end<br /></span>
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># Returns nil if the face is not a semi circle.<br /># (ArcCurve and one edge connecting each curve end)<br /># Returns the radius on success.<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_semicircle_radius</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">edges </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">outer_loop</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edges<br /> curve_edges </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">edges</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select </span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">| </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?( </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ArcCurve </span><span class="syntaxkeyword">) }<br /> </span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">curve_edges</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">first</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil unless curve_edges</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">all</span><span class="syntaxkeyword">? { |</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">| </span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault">edge </span><span class="syntaxkeyword">= (</span><span class="syntaxdefault">edges </span><span class="syntaxkeyword">- </span><span class="syntaxdefault">curve_edges</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">first<br /> </span><span class="syntaxcomment"># At this point you have all the data;<br /> # curve refers to the ArcCurve of the semicircle<br /> # edge refers to the Edge connecting the ArcCurve's ends<br /></span><span class="syntaxdefault">end<br /></span>
Advertisement