sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Module variables and nested class

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 5 Posters 363 Views 5 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.
    • renderizaR Offline
      renderiza
      last edited by

      Maybe try something like this...

      
      require 'sketchup.rb'
      
      module JBB_MyModule
      
         class JBB_LP_AppObserver < Sketchup;;AppObserver
      
            @@model = Sketchup.active_model
            @@layers = @@model.layers
            @@customVar = #something
      
            def onNewModel(newModel)  #or onOpenModel
               @@model = newModel
               @@layers = newModel.layers
               @@customVar = #some other thing
            end#def
            
            ##########
            # Modify #
            ##########
            if !file_loaded?(__FILE__) #then
               @@jpp_test = UI.menu("Plugins").add_submenu("TEST")
          
               @@jpp_test.add_item('Test') {
                  Sketchup.active_model.select_tool JBB_MyModule;;JBB_LP_AppObserver.new
               }
      
               # Add toolbars
               jpp_test_tb = UI;;Toolbar.new "Test"
               jpp_test_cmd = UI;;Command.new("Test") {
                  Sketchup.active_model.select_tool JBB_MyModule;;JBB_LP_AppObserver.new
               }
      
               # icons toolbar
               jpp_test_cmd.small_icon = "img/jpp_test_1_16.png"
               jpp_test_cmd.large_icon = "img/jpp_test_1_24.png"
               jpp_test_cmd.tooltip = "Test"
               jpp_test_cmd.status_bar_text = "Test"
               jpp_test_cmd.menu_text = "Test"
               jpp_test_tb = jpp_test_tb.add_item jpp_test_cmd
               jpp_test_tb.show
      
            end
      
         end#class
      
      end#module
      
      

      [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

      1 Reply Last reply Reply Quote 0
      • A Offline
        Aerilius
        last edited by

        You can use constants for this: Constants are looked up through all parent classes/modules until the first matching is found.

        
        module JBB_MyModule
        
            self;;MODEL = Sketchup.active_model 
            # self;; makes sure the constant is defined in this module
            # Otherwise we would modify a "global" MODEL constant if someone had defined one.
        
            class JBB_LP_AppObserver < Sketchup;;AppObserver
        
                def onNewModel(newModel)
                    MODEL = newModel  # or JBB_MyModule;;MODEL
                end#def
        
            end#class
        
        end#module
        
        
        1 Reply Last reply Reply Quote 0
        • jiminy-billy-bobJ Offline
          jiminy-billy-bob
          last edited by

          Thanks guys !

          I used constants as Aerilius pointed, but had to use .const_set() for this method to work.

          πŸ˜„

          25% off Skatter for SketchUcation Premium Members

          1 Reply Last reply Reply Quote 0
          • tt_suT Offline
            tt_su
            last edited by

            Use attr, attr_reader, attr_writer, attr_accessor to create methods to access the module/class's instance variables.

            The class << self syntax is needed for modules (class attributes). If you define instance attributes that is not needed.

            <span class="syntaxdefault"><br /></span><span class="syntaxkeyword">require&nbsp;</span><span class="syntaxstring">'sketchup.rb'<br /><br /></span><span class="syntaxdefault">module&nbsp;JBB_MyModule<br /><br />&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">model&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">layers&nbsp;</span><span class="syntaxkeyword">=&nbsp;@</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers<br />&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">customVar&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxcomment">#something<br /><br />&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">class&nbsp;<<&nbsp;</span><span class="syntaxdefault">self<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr_accessor&nbsp;</span><span class="syntaxkeyword">;</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">,&nbsp;;</span><span class="syntaxdefault">layer</span><span class="syntaxkeyword">,&nbsp;;</span><span class="syntaxdefault">customVar<br />&nbsp;&nbsp;&nbsp;end<br /><br />&nbsp;&nbsp;&nbsp;</span><span class="syntaxcomment">#&nbsp;Some&nbsp;code&nbsp;using&nbsp;these&nbsp;@variables<br /><br />&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">class&nbsp;</span><span class="syntaxdefault">JBB_LP_AppObserver&nbsp;</span><span class="syntaxkeyword"><&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">AppObserver<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;onNewModel</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">newModel</span><span class="syntaxkeyword">)&nbsp;&nbsp;</span><span class="syntaxcomment">#or&nbsp;onOpenModel<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">JBB_MyModule</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">model&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">newModel<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JBB_MyModule</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">newModel</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JBB_MyModule</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">customVar&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxcomment">#some&nbsp;other&nbsp;thing<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">end</span><span class="syntaxcomment">#def<br /><br />&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">end</span><span class="syntaxcomment">#class<br /><br /></span><span class="syntaxdefault">end</span><span class="syntaxcomment">#module<br />&nbsp;</span><span class="syntaxdefault"></span>
            
            1 Reply Last reply Reply Quote 0
            • tt_suT Offline
              tt_su
              last edited by

              Alternatively, in the case of observers, I usually define methods in the root module which I call from the observers - defining minimum amount of code in the observer class itself.

              1 Reply Last reply Reply Quote 0
              • jiminy-billy-bobJ Offline
                jiminy-billy-bob
                last edited by

                Thanks a lot Thomas ! That was exactly what I was looking for.

                @tt_su said:

                Alternatively, in the case of observers, I usually define methods in the root module which I call from the observers - defining minimum amount of code in the observer class itself.

                For what ? Less bugsplats/erros ?

                25% off Skatter for SketchUcation Premium Members

                1 Reply Last reply Reply Quote 0
                • tt_suT Offline
                  tt_su
                  last edited by

                  Just because observers doesn't have access to the instance variables of the parent module. All I need observers for are tell when when things change - so I prefer to keep the logic that modifies stuff in the parent module along with the rest of it. It's an organization thing.

                  1 Reply Last reply Reply Quote 0
                  • A Offline
                    Anton_S
                    last edited by

                    Is there any advantages or disadvantages of instance variables over the class variables in the module?

                    I know a few advantages for using instance vars:

                    1. no extra @ symbol
                    2. attr_... functions that give simpler organizations

                    I just wanted to know if there is any difference when it comes to using them in the module context.

                    1 Reply Last reply Reply Quote 0
                    • tt_suT Offline
                      tt_su
                      last edited by

                      I don't think class variables in modules has any use.

                      In general people (when you search the net for Ruby) will advice against class variables if you can avoid them due to that they are troublesome when you create subclasses.

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        Anton_S
                        last edited by

                        Thank you ThomThom πŸ‘

                        Moving to instance variables πŸ‘Ώ

                        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