sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Namespace and Tool Class Question

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 3 Posters 352 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      ktkoh
      last edited by

      I have started working on my wood joint tools script to wrap it in my namespace and this is what I came up with so far. The attached code that demonstrates only the Dowell part of my tools. This works, however I would like some comments on the method.
      I retained the old directory structure for the actual tool K2WS_Tools\K2_ToolsDowelJoint.rb so that the new version would overwrite the old version for users that have downloaded the old version.

      Keith

      #This Program Loads the Individual K2_Tools when their Menu is accessed!
      require 'K2WS/k2ws_module.rb' #-NameSpace Module
      
      module K²WS;;K2_ToolsLoader
      
        class << self
      
          #ToolBars are only created when Menu Item is selected
          
          
          
          def dowel_ToolBar	
            if  @tlb5== nil
              itcmd51 = UI;;Command.new("Dowel Joint") {dowelJoint}
              itcmd51.tooltip = "Complete Dowel Joint"
              itcmd51.small_icon = File.join(File.dirname(__FILE__),"K2_ToolsSupportFiles","DowelJoint_small.png")
              itcmd51.large_icon = File.join(File.dirname(__FILE__),"K2_ToolsSupportFiles","DowelJoint_large.png")
              itcmd52 = UI;;Command.new("Dowels 1 Comp") {dowel1Comp}
              itcmd52.small_icon = File.join(File.dirname(__FILE__),"K2_ToolsSupportFiles","Dowel1Comp_small.png")
              itcmd52.large_icon = File.join(File.dirname(__FILE__),"K2_ToolsSupportFiles","Dowel1Comp_large.png")
              itcmd52.tooltip = "Dowel 1 Comp"
              @tlb5 = UI;;Toolbar.new("Dowels")
              @tlb5.add_item(itcmd51)
              @tlb5.add_item(itcmd52)
              @tlb5.show
            else
              #state = @tlb5.get_last_state
              #UI.messagebox("State of Dowel ToolBar " + state.to_s)
              @tlb5.show
            end
          end
      
          def dowelJoint
            require  'K2WS_Tools\K2_ToolsDowelJoint.rb'
            Sketchup.active_model.select_tool K2_Tools_Dowel_Joint.new  
          end
          
          
          def dowel1Comp
            require  'K2WS_Tools\K2_ToolsDowelJoint.rb'
            Sketchup.active_model.select_tool K2_Tools_Dowel_Holes.new
            
          end
          
          
          
          
        end # proxy class
        
        #Run Once
      	unless file_loaded?(File.basename(__FILE__))
      		
          # Access the main Tools menu
          tools_menu = UI.menu "Tools"
          # Add a separator and a submenu
          tools_menu.add_separator
          sub_menu = tools_menu.add_submenu("KK_Tools")
          # Add items to the submenu
      		
          it1= sub_menu.add_item("Dowels"){dowel_ToolBar}
          
          
        file_loaded(File.basename(__FILE__))  
           
        end #End Run Once 
      
       
       end #module K²WS;;K2_ToolsLoader
      
      1 Reply Last reply Reply Quote 0
      • A Offline
        Aerilius
        last edited by

        Looks good so far. I haven't seen anyone using ² in a module name (but if it works, though Unicode characters like ᚠᚢᚦᚨᚱᚲ are not supported in our version of Ruby in SketchUp, otherwise my plugins would be full of them).

        @unknownuser said:

        'K2WS_Tools\K2_ToolsDowelJoint.rb'
        

        Ruby internally uses forward slashes that are common on all unix and most non-unix systems. To deal with file paths, you let Ruby handle the delimiters and use the File methods like File.join("K2WS_Tools", "K2_ToolsDowelJoint.rb"). Also it would be best if your plugin works independent of the installation path. Therefore define a constant in your main ruby file (or loader) in your namespace
        PLUGIN_ROOT = File.dirname(__FILE__) unless defined(self::PLUGIN_ROOT)
        and reference all files relative to that.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          @aerilius said:

          Looks good so far. I haven't seen anyone using ² in a module name (but if it works, though Unicode characters like ᚠᚢᚦᚨᚱᚲ are not supported in our version of Ruby in SketchUp, otherwise my plugins would be full of them).

          ² also exists in the ANSI set - so I could be a simple one byte character is he used that encoding.

          @aerilius said:

          Looks good so far. I haven't seen anyone using ² in a module name (but if it works, though Unicode characters like ᚠᚢᚦᚨᚱᚲ are not supported in our version of Ruby in SketchUp, otherwise my plugins would be full of them).

          I copied the example from this page: http://www.oreillynet.com/ruby/blog/2007/10/fun_with_unicode_1.html

          <span class="syntaxdefault"><br />module&nbsp;Kernel<br />&nbsp;&nbsp;&nbsp;alias&nbsp;λ&nbsp;proc<br /><br />&nbsp;&nbsp;&nbsp;def&nbsp;∑</span><span class="syntaxkeyword">(*</span><span class="syntaxdefault">args</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">sum&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{&nbsp;|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|&nbsp;</span><span class="syntaxdefault">sum&nbsp;</span><span class="syntaxkeyword">+=&nbsp;</span><span class="syntaxdefault">e&nbsp;</span><span class="syntaxkeyword">}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">sum<br />&nbsp;&nbsp;&nbsp;end<br /><br />&nbsp;&nbsp;&nbsp;def&nbsp;√</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">root</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">Math</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">sqrt</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">root</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">end<br />end<br /></span>
          

          And it worked:
          ` # A real lambda
          λ { puts ‘Hello’ }.call => ‘Hello’

          Sigma - sum of all elements

          ∑(1,2,3) => 6

          Square root

          √ 49 => 7.0`

          You can also do this:

          <span class="syntaxdefault"><br />module&nbsp;MyTest<br />&nbsp;&nbsp;def&nbsp;self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">ᚠᚢᚦᚨᚱᚲ<br />&nbsp;&nbsp;&nbsp;&nbsp;puts&nbsp;</span><span class="syntaxstring">'world'<br />&nbsp;&nbsp;</span><span class="syntaxdefault">end<br />end<br /></span>
          

          But not this:

          <span class="syntaxdefault"><br />module&nbsp;ᚠᚢᚦᚨᚱᚲ<br />&nbsp;&nbsp;def&nbsp;self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">hello<br />&nbsp;&nbsp;&nbsp;&nbsp;puts&nbsp;</span><span class="syntaxstring">'world'<br />&nbsp;&nbsp;</span><span class="syntaxdefault">end<br />end<br /></span>
          

          Return an error:
          Error: #<SyntaxError: (eval):516: compile error (eval):516: class/module name must be CONSTANT module ᚠᚢᚦᚨᚱᚲ; def self.hello; puts 'world'; end; end ^> (eval):516

          That seem to be related to ᚠ not being recognised as a capital letter.

          Because this works:

          <span class="syntaxdefault"><br />module&nbsp;Xᚠᚢᚦᚨᚱᚲ</span><span class="syntaxkeyword">;&nbsp;</span><span class="syntaxdefault">end<br /></span>
          

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement