Ruby script of bezier curves need edit
-
I operate a CNC machine and use SketchUp-Pro as the primary CAD for the machine. I want the bezier curve ruby script to make curves with more than 20 segments. The CNC machine will cut the curve in a smooth contour so long as the segment length is quite small but with this script draws always with 20 segments.
I have tried to edit the ruby script, changing all references to the number 10 or the number 20 to a higher number re-saved and ran it, with no effect.
What needs changed in the original ruby script to make this happen?I appreciate assistance in this matter.
Below is the raw ruby script code as downloaded from http://www.sketchup.com/download/rubyscripts.html: (under the heading bezier.rb)
<span class="syntaxdefault"></span><span class="syntaxcomment"># Copyright 2004-2005, @Last Software, Inc.<br /><br /># This software is provided as an example of using the Ruby interface<br /># to SketchUp.<br /><br /># Permission to use, copy, modify, and distribute this software for <br /># any purpose and without fee is hereby granted, provided that the above<br /># copyright notice appear in all copies.<br /><br /># THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR<br /># IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED<br /># WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.<br />#-----------------------------------------------------------------------------<br /># Name ; Bezier Curve Tool 1.0<br /># Description ; A tool to create Bezier curves.<br /># Menu Item ; Draw->Bezier Curves<br /># Context Menu; Edit Bezier Curve<br /># Usage ; Select 4 points-<br /># ; 1. Start point of the curve<br /># ; 2. Endpoint of the curve<br /># ; 3. Second control point. It determines the tangency at the start<br /># ; 4. Next to last control point. It determines the tangency at the end<br /># Date ; 8/26/2004<br /># Type ; Tool<br />#-----------------------------------------------------------------------------<br /><br /># Ruby implementation of Bezier curves<br /></span><span class="syntaxkeyword">require </span><span class="syntaxstring">'sketchup.rb'<br /><br /></span><span class="syntaxdefault">module Bezier<br /><br /></span><span class="syntaxcomment"># Evaluate a Bezier curve at a parameter.<br /># The curve is defined by an array of its control points.<br /># The parameter ranges from 0 to 1<br /># This is based on the technique described in "CAGD A Practical Guide, 4th Editoin"<br /># by Gerald Farin. page 60<br /><br /></span><span class="syntaxdefault">def Bezier</span><span class="syntaxkeyword">.eval(</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">t</span><span class="syntaxkeyword">)<br /><br /> </span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">- </span><span class="syntaxdefault">1<br /> </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">< </span><span class="syntaxdefault">1<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil<br /> end<br /> <br /> t1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">1.0 </span><span class="syntaxkeyword">- </span><span class="syntaxdefault">t<br /> fact </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">1.0<br /> n_choose_i </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">1<br /><br /> x </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">x </span><span class="syntaxkeyword">* </span><span class="syntaxdefault">t1<br /> y </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">y </span><span class="syntaxkeyword">* </span><span class="syntaxdefault">t1<br /> z </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">z </span><span class="syntaxkeyword">* </span><span class="syntaxdefault">t1<br /> <br /> </span><span class="syntaxkeyword">for </span><span class="syntaxdefault">i in 1.</span><span class="syntaxkeyword">..</span><span class="syntaxdefault">degree<br /> fact </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">fact</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">t<br /> n_choose_i </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">n_choose_i</span><span class="syntaxkeyword">*(</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">)/</span><span class="syntaxdefault">i<br /> fn </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">fact </span><span class="syntaxkeyword">* </span><span class="syntaxdefault">n_choose_i<br /> x </span><span class="syntaxkeyword">= (</span><span class="syntaxdefault">x </span><span class="syntaxkeyword">+ </span><span class="syntaxdefault">fn</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">) * </span><span class="syntaxdefault">t1<br /> y </span><span class="syntaxkeyword">= (</span><span class="syntaxdefault">y </span><span class="syntaxkeyword">+ </span><span class="syntaxdefault">fn</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">y</span><span class="syntaxkeyword">) * </span><span class="syntaxdefault">t1<br /> z </span><span class="syntaxkeyword">= (</span><span class="syntaxdefault">z </span><span class="syntaxkeyword">+ </span><span class="syntaxdefault">fn</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">z</span><span class="syntaxkeyword">) * </span><span class="syntaxdefault">t1<br /> end<br /><br /> x </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">x </span><span class="syntaxkeyword">+ </span><span class="syntaxdefault">fact</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">t</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">x<br /> y </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">y </span><span class="syntaxkeyword">+ </span><span class="syntaxdefault">fact</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">t</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">y<br /> z </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">z </span><span class="syntaxkeyword">+ </span><span class="syntaxdefault">fact</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">t</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">z<br /><br /> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Point3d</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">z</span><span class="syntaxkeyword">)<br /> <br /></span><span class="syntaxdefault">end </span><span class="syntaxcomment"># method eval<br /><br /># Evaluate the curve at a number of points and return the points in an array<br /></span><span class="syntaxdefault">def Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">points</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">numpts</span><span class="syntaxkeyword">)<br /> <br /> </span><span class="syntaxdefault">curvepts </span><span class="syntaxkeyword">= []<br /> </span><span class="syntaxdefault">dt </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">1.0 </span><span class="syntaxkeyword">/ </span><span class="syntaxdefault">numpts<br /><br /> </span><span class="syntaxcomment"># evaluate the points on the curve<br /> </span><span class="syntaxkeyword">for </span><span class="syntaxdefault">i in 0.</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">numpts<br /> t </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">i </span><span class="syntaxkeyword">* </span><span class="syntaxdefault">dt<br /> curvepts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">] = </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.eval(</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">t</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">end<br /> <br /> curvepts<br />end<br /><br /></span><span class="syntaxcomment"># Create a Bezier curve in SketchUp<br /></span><span class="syntaxdefault">def Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">numseg </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">16</span><span class="syntaxkeyword">)<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 /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation </span><span class="syntaxstring">"Bezier Curve"<br /> <br /> </span><span class="syntaxdefault">curvepts </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">points</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">numseg</span><span class="syntaxkeyword">)<br /> <br /> </span><span class="syntaxcomment"># create the curve<br /> </span><span class="syntaxdefault">edges </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_curve</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">curvepts</span><span class="syntaxkeyword">);<br /> </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br /> edges<br /> <br />end<br /><br /></span><span class="syntaxcomment">#-----------------------------------------------------------------------------<br /># Define the tool class for creating Bezier curves<br /><br /></span><span class="syntaxkeyword">class </span><span class="syntaxdefault">BezierTool<br /><br />def initialize</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">3</span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">degree<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">< </span><span class="syntaxdefault">1 </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox </span><span class="syntaxstring">"Minimum degree is 1"<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">1<br /> elsif</span><span class="syntaxkeyword">( @</span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">> </span><span class="syntaxdefault">20 </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox </span><span class="syntaxstring">"Maximum degree is 20"<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">20<br /> end<br /> </span><span class="syntaxcomment"># TODO; I should probably adjust the number of segments used for<br /> # display and creating the curve based on the the degree and/or the<br /> # maximum curvature.<br /></span><span class="syntaxdefault">end<br /><br />def reset<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts </span><span class="syntaxkeyword">= []<br /> @</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">0<br /> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">set_status_text </span><span class="syntaxstring">"Click for start point"<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">drawn </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">false<br />end<br /><br />def activate<br /> </span><span class="syntaxcomment"># There are up to 4 input points that we keep track of<br /> # @ip1 is the start point of the curve<br /> # @ip2 is the endpoint of the curve<br /> # @ip3 is the second control point. It determines the tangency at the start<br /> # @ip4 is the next to last control point. It determines the tangency at the end<br /> # @ip5 is an internal input point<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">InputPoint</span><span class="syntaxkeyword">.new<br /> @</span><span class="syntaxdefault">ip2 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">InputPoint</span><span class="syntaxkeyword">.new<br /> @</span><span class="syntaxdefault">ip3 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">InputPoint</span><span class="syntaxkeyword">.new<br /> @</span><span class="syntaxdefault">ip4 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">InputPoint</span><span class="syntaxkeyword">.new<br /> @</span><span class="syntaxdefault">ip5 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">InputPoint</span><span class="syntaxkeyword">.new<br /> </span><span class="syntaxcomment"># @ip is a temporary input point used to get other positions<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">InputPoint</span><span class="syntaxkeyword">.new<br /> </span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">reset<br /> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">set_status_text </span><span class="syntaxstring">"Degree"</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">SB_VCB_LABEL<br /> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">set_status_text </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">SB_VCB_VALUE<br />end<br /><br />def deactivate</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate </span><span class="syntaxkeyword">if @</span><span class="syntaxdefault">drawn<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip2 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip3 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip4 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip5 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br />end<br /><br />def onMouseMove</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">flags</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> case @</span><span class="syntaxdefault">state<br /> when 0 </span><span class="syntaxcomment"># getting the first end point<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick view</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">? && @</span><span class="syntaxdefault">ip </span><span class="syntaxkeyword">!= @</span><span class="syntaxdefault">ip1 </span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">ip1</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy</span><span class="syntaxkeyword">! @</span><span class="syntaxdefault">ip<br /> view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate<br /> end<br /> when 1 </span><span class="syntaxcomment"># getting the second end point<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick view</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">ip1<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">? && @</span><span class="syntaxdefault">ip </span><span class="syntaxkeyword">!= @</span><span class="syntaxdefault">ip2 </span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">ip2</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy</span><span class="syntaxkeyword">! @</span><span class="syntaxdefault">ip<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">ip2</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate<br /> end<br /> when 2 </span><span class="syntaxcomment"># the second control point - tangency at start<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick view</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">ip1<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">? && @</span><span class="syntaxdefault">ip </span><span class="syntaxkeyword">!= @</span><span class="syntaxdefault">ip3 </span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">ip3</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy</span><span class="syntaxkeyword">! @</span><span class="syntaxdefault">ip<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">ip3</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate<br /> end<br /> when </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">degree </span><span class="syntaxcomment"># the next to last point = tangency at end<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick view</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">ip2<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">? && @</span><span class="syntaxdefault">ip </span><span class="syntaxkeyword">!= @</span><span class="syntaxdefault">ip4 </span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">ip4</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy</span><span class="syntaxkeyword">! @</span><span class="syntaxdefault">ip<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[@</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">ip4</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate<br /> end<br /> when 3.</span><span class="syntaxkeyword">.@</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1 </span><span class="syntaxcomment"># internal points - if degree > 3<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick view</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">? && @</span><span class="syntaxdefault">ip </span><span class="syntaxkeyword">!= @</span><span class="syntaxdefault">ip5 </span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">ip5</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">copy</span><span class="syntaxkeyword">! @</span><span class="syntaxdefault">ip<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[@</span><span class="syntaxdefault">state</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">ip5</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate<br /> end<br /> end<br /> view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">tooltip </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">tooltip </span><span class="syntaxkeyword">if @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault">end<br /><br />def create_curve<br /> curve </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">20<br /> </span><span class="syntaxcomment"># see if this fills in any new faces<br /> </span><span class="syntaxkeyword">if( </span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">edge1 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">edge1</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">find_faces<br /> <br /> </span><span class="syntaxcomment"># Attach an attribute to the curve with the array of points<br /> </span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">edge1</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve<br /> </span><span class="syntaxkeyword">if( </span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_attribute </span><span class="syntaxstring">"skp"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"crvtype"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"Bezier"<br /> </span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_attribute </span><span class="syntaxstring">"skp"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"crvpts"</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts<br /> end<br /> <br /> end<br /> self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">reset<br />end<br /><br />def onLButtonDown</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">flags</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># TODO; Use the two point form of the input point finder to get the new points.<br /> # I need a way to generate an ip at a given position from code.<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick view</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">? )<br /> case @</span><span class="syntaxdefault">state<br /> when 0<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">set_status_text </span><span class="syntaxstring">"Click for end point"<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">1<br /> when </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">degree<br /> self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">create_curve<br /> when 1<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">2<br /> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">set_status_text </span><span class="syntaxstring">"Click for point 2"<br /> </span><span class="syntaxdefault">when 2.</span><span class="syntaxkeyword">..@</span><span class="syntaxdefault">degree<br /> nextstate </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">state</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">1<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">nextstate</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[@</span><span class="syntaxdefault">state</span><span class="syntaxkeyword">]<br /> @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[@</span><span class="syntaxdefault">state</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nextstate<br /> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">set_status_text </span><span class="syntaxstring">"Click for point #{@state}"<br /> </span><span class="syntaxdefault">end<br /> end<br />end<br /><br />def onCancel</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">flag</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate </span><span class="syntaxkeyword">if @</span><span class="syntaxdefault">drawn<br /> reset<br />end<br /><br />def onUserText</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">text</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># get the degree from the text<br /> </span><span class="syntaxdefault">newdegree </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">text</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_i<br /> </span><span class="syntaxkeyword">if( </span><span class="syntaxdefault">newdegree </span><span class="syntaxkeyword">> </span><span class="syntaxdefault">0 </span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">newdegree<br /> self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">create_curve </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">> @</span><span class="syntaxdefault">degree </span><span class="syntaxkeyword">)<br /> else<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">beep<br /> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">set_status_text </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">SB_VCB_VALUE<br /> end<br />end<br /><br />def getExtents<br /> bb </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">BoundingBox</span><span class="syntaxkeyword">.new<br /> if( @</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">0 </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># We are getting the first point<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">? && @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">display</span><span class="syntaxkeyword">? )<br /> </span><span class="syntaxdefault">bb</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> end<br /> </span><span class="syntaxkeyword">else<br /> </span><span class="syntaxdefault">bb</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts<br /> end<br /> bb<br />end<br /><br />def draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /><br /> </span><span class="syntaxcomment"># Show the current input point<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">? && @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">display</span><span class="syntaxkeyword">? )<br /> @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">drawn </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">true<br /> end<br /><br /> </span><span class="syntaxcomment"># show the curve<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">1 </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># just draw a line from the start to the end point<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_color_from_line</span><span class="syntaxkeyword">(@</span><span class="syntaxdefault">ip1</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">ip2</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">drawn </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">true<br /> elsif</span><span class="syntaxkeyword">( @</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">> </span><span class="syntaxdefault">1 </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># draw the curve<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">drawing_color </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"black"<br /> </span><span class="syntaxdefault">curvepts </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">points</span><span class="syntaxkeyword">(@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">12</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">curvepts</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># draw the control polygon<br /> # determine the colos for the first and last segments from the input points<br /> </span><span class="syntaxkeyword">case @</span><span class="syntaxdefault">state<br /> when 2<br /> view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_color_from_line</span><span class="syntaxkeyword">(@</span><span class="syntaxdefault">ip1</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">ip3</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">], @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">])<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">drawing_color </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"gray"<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1.</span><span class="syntaxkeyword">.-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">])<br /> </span><span class="syntaxdefault">when </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">degree<br /> view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">drawing_color </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"gray"<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0.</span><span class="syntaxkeyword">.-</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">])<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_color_from_line</span><span class="syntaxkeyword">(@</span><span class="syntaxdefault">ip2</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">ip4</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[@</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">], @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[@</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">])<br /> else<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">drawing_color </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"gray"<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">end<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">drawn </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">true<br /> end<br />end<br /><br />end </span><span class="syntaxcomment"># class BezierTool<br /><br />#-----------------------------------------------------------------------------<br /># Define the tool class for editing Bezier curves<br /><br /></span><span class="syntaxkeyword">class </span><span class="syntaxdefault">EditBezierTool<br /><br />def activate<br /><br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">0<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">drawn </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">false<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">selection </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pt_to_move </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br /> <br /> </span><span class="syntaxcomment"># Make sure that there is really a Bezier curve selected<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selected_curve<br /> </span><span class="syntaxkeyword">if( </span><span class="syntaxdefault">not </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">curve </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool nil<br /> </span><span class="syntaxkeyword">return<br /> </span><span class="syntaxdefault">end<br /> <br /> </span><span class="syntaxcomment"># Get the control points<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute </span><span class="syntaxstring">"skp"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"crvpts"<br /> </span><span class="syntaxkeyword">if( </span><span class="syntaxdefault">not </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">beep<br /> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool nil<br /> </span><span class="syntaxkeyword">return<br /> </span><span class="syntaxdefault">end<br /> <br /> </span><span class="syntaxcomment"># Get the curve points from the vertices<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">vertices </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">vertices<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">crvpts </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">vertices</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">collect </span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">| </span><span class="syntaxdefault">v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position</span><span class="syntaxkeyword">}<br /> @</span><span class="syntaxdefault">numseg </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">vertices</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">- </span><span class="syntaxdefault">1<br /> <br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">InputPoint</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxdefault">end<br /><br />def deactivate</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate </span><span class="syntaxkeyword">if @</span><span class="syntaxdefault">drawn<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br />end<br /><br />def resume</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">drawn </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">false<br />end<br /><br />def pick_point_to_move</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">old_pt_to_move </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">pt_to_move<br /> ph </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick_helper x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">selection </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">ph</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick_segment </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">selection </span><span class="syntaxkeyword">)<br /> if( @</span><span class="syntaxdefault">selection </span><span class="syntaxkeyword">< </span><span class="syntaxdefault">0 </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># We got a point on a segment. Compute the point closest<br /> # to the pick ray.<br /> </span><span class="syntaxdefault">pickray </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pickray x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y<br /> i </span><span class="syntaxkeyword">= -@</span><span class="syntaxdefault">selection<br /> segment </span><span class="syntaxkeyword">= [@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">], @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">]]<br /> </span><span class="syntaxdefault">result </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">closest_points segment</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">pickray<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pt_to_move </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">result</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /> else<br /> </span><span class="syntaxcomment"># we got a control point<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pt_to_move </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[@</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">end<br /> </span><span class="syntaxkeyword">else<br /> @</span><span class="syntaxdefault">pt_to_move </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">nil<br /> end<br /> old_pt_to_move </span><span class="syntaxkeyword">!= @</span><span class="syntaxdefault">pt_to_move<br />end<br /><br />def onLButtonDown</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">flags</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># Select the segment or control point to move<br /> </span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick_point_to_move x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">1 </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">selection </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /><br />def onLButtonUp</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">flags</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> return if </span><span class="syntaxdefault">not </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">1<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">0<br /> <br /> </span><span class="syntaxcomment"># Update the actual curve. Move the vertices on the curve<br /> # to the new curve points<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">vertices</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">!= @</span><span class="syntaxdefault">crvpts</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox </span><span class="syntaxstring">"Count of curve points is wrong!"<br /> </span><span class="syntaxkeyword">return<br /> </span><span class="syntaxdefault">end<br /><br /> model </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">vertices</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">model<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation </span><span class="syntaxstring">"Edit Bezier Curve"<br /><br /> </span><span class="syntaxcomment"># Move the vertices<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">move_vertices </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">crvpts<br /> <br /> </span><span class="syntaxcomment"># Update the control points stored with the curve<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_attribute </span><span class="syntaxstring">"skp"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"crvpts"</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts<br /> <br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br />end<br /><br />def onMouseMove</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">flags</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># Make sure that the control polygon is shown<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">not </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">drawn<br /> <br /> </span><span class="syntaxcomment"># Move the selected point if state = 1<br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">1 </span><span class="syntaxkeyword">&& @</span><span class="syntaxdefault">selection </span><span class="syntaxkeyword">)<br /> @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick view</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y<br /> </span><span class="syntaxkeyword">return if </span><span class="syntaxdefault">not </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">?<br /> if( @</span><span class="syntaxdefault">selection </span><span class="syntaxkeyword">>= </span><span class="syntaxdefault">0 </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># Moving a control point<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pt_to_move </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[@</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">] = @</span><span class="syntaxdefault">pt_to_move<br /> </span><span class="syntaxkeyword">else<br /> </span><span class="syntaxcomment"># moving a segment<br /> </span><span class="syntaxdefault">pt </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">ip</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">position<br /> vec </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">pt </span><span class="syntaxkeyword">- @</span><span class="syntaxdefault">pt_to_move<br /> i </span><span class="syntaxkeyword">= -@</span><span class="syntaxdefault">selection<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">offset</span><span class="syntaxkeyword">! </span><span class="syntaxdefault">vec<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">offset</span><span class="syntaxkeyword">! </span><span class="syntaxdefault">vec<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pt_to_move </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">pt<br /> end<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">crvpts </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">points</span><span class="syntaxkeyword">(@</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">numseg</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate<br /> </span><span class="syntaxkeyword">else </span><span class="syntaxcomment"># state != 1<br /> # See if we can select something to move<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">invalidate </span><span class="syntaxkeyword">if( </span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pick_point_to_move</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">y</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">) )<br /> </span><span class="syntaxdefault">end<br />end<br /><br />def getMenu</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Done"</span><span class="syntaxkeyword">) {</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool nil</span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">end<br /><br />def getExtents<br /> bb </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">BoundingBox</span><span class="syntaxkeyword">.new<br /> </span><span class="syntaxdefault">bb</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">pts<br /> bb<br />end<br /><br />def draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># Draw the control polygon<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">drawing_color </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"gray"<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">pts</span><span class="syntaxkeyword">)<br /> <br /> if( @</span><span class="syntaxdefault">pt_to_move </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw_points</span><span class="syntaxkeyword">(@</span><span class="syntaxdefault">pt_to_move</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">10</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">1</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"red"</span><span class="syntaxkeyword">);<br /> </span><span class="syntaxdefault">end<br /> <br /> </span><span class="syntaxkeyword">if( @</span><span class="syntaxdefault">state </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">1 </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">drawing_color </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"black"<br /> </span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">draw</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">GL_LINE_STRIP</span><span class="syntaxkeyword">, @</span><span class="syntaxdefault">crvpts</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">end<br /> <br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">drawn </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">true<br />end<br /><br />end </span><span class="syntaxcomment"># class EditBezierTool<br /><br />#-----------------------------------------------------------------------------<br /><br /># Function to test to see if the selection set contains only a Bezier curve<br /># Returns the curve if there is one or else nil<br /></span><span class="syntaxdefault">def Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selected_curve<br /> ss </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">not ss</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_curve</span><span class="syntaxkeyword">?<br /> </span><span class="syntaxdefault">edge </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">ss</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">first<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">not edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">kind_of</span><span class="syntaxkeyword">? </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Edge<br /> curve </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">curve<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">not curve<br /> </span><span class="syntaxkeyword">return </span><span class="syntaxdefault">nil </span><span class="syntaxkeyword">if </span><span class="syntaxdefault">curve</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"skp"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"crvtype"</span><span class="syntaxkeyword">) != </span><span class="syntaxstring">"Bezier"<br /> </span><span class="syntaxdefault">curve<br />end<br /><br /></span><span class="syntaxcomment"># Edit a selected Bezier curve<br /></span><span class="syntaxdefault">def Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edit_curve<br /> curve </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selected_curve<br /> </span><span class="syntaxkeyword">if( </span><span class="syntaxdefault">not curve </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">beep<br /> </span><span class="syntaxkeyword">return<br /> </span><span class="syntaxdefault">end<br /> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool EditBezierTool</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxdefault">end<br /><br /></span><span class="syntaxcomment"># Select the Bezier curve tool<br /></span><span class="syntaxdefault">def Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">tool</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool BezierTool</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">degree</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /><br /></span><span class="syntaxcomment"># Add a menu choice for creating bezier curves<br /></span><span class="syntaxkeyword">if( </span><span class="syntaxdefault">not file_loaded</span><span class="syntaxkeyword">?(</span><span class="syntaxstring">"bezier.rb"</span><span class="syntaxkeyword">) )<br /> </span><span class="syntaxdefault">add_separator_to_menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Draw"</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Draw"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Bezier Curves"</span><span class="syntaxkeyword">) { </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">tool </span><span class="syntaxkeyword">}<br /><br /> </span><span class="syntaxcomment"># Add a context menu handler to let you edit a Bezier curve<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_context_menu_handler </span><span class="syntaxkeyword">do |</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">|<br /> if( </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selected_curve </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_separator<br /> menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Edit Bezier Curve"</span><span class="syntaxkeyword">) { </span><span class="syntaxdefault">Bezier</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">edit_curve </span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault">end<br /> end<br /><br />end<br /><br />end </span><span class="syntaxcomment"># module Bezier<br /></span><span class="syntaxdefault">file_loaded</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"bezier.rb"</span><span class="syntaxkeyword">) </span><span class="syntaxdefault"></span>
-
Have you tried Fredo's bezier and spline plugin?
http://sketchucation.com/forums/viewtopic.php?t=13563Offer lots more control and options.
-
Simple ... referring to the line numbers in the post above:
Change line 86 from:
def Bezier.curve(pts, numseg = 16)
to:
def Bezier.curve(pts, numseg = 100)
(Use whatever default number of segments you want.)
THEN remove the comma and explicit second parameter (
20
) from the where that method is called in the tool, on (or about) line 196, ie:
curve = Bezier.curve @pts, 20
becomes:
curve = Bezier.curve @pts
-
Awesome Dan!
Thanks, I'll try it.
I'll read up on the Newbie's guide as well. -
I'll post some cut tests shortly, but here is the lovely curve:
Advertisement