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

    Checking for a group?

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 4 Posters 868 Views 4 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.
    • L Offline
      lothcat
      last edited by

      Thanks!

      1 Reply Last reply Reply Quote 0
      • artmusicstudioA Offline
        artmusicstudio
        last edited by

        hello,
        i went thru all snippets,
        but

        how can i check, if a group @NAME
        exists or not in the actve model.

        situation

        i create a group NAME by ruby
        then it either is deleted or not

        whith the next call ruby shall check, if group NAME is there

        if not, create it
        if so, delete and create it new

        my trial:

        
        entities = @NAME.entities 
        entity1 = entities[1]        
        status = entity1.valid?
        
        unless status == true
           model = Sketchup.active_model
           @NAME.erase!
        end
        
        
        

        reports an not existing group, so no check is possible

        tahnx for helping on this

        stan

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          A simple

          group.valid?

          or - assuming the group is named 'xxx'

          group=nil
          Sketchup.active_model.definitions.each{|d|
          next unless d.group?
          if d.instances[0].name=="xxx"
          group=d.instances[0]
          break
          end
          }

          or

          group=nil
          some_entities.grep(Sketchup::Group).each{|g|
          if g.name=="xxx"
          group=g
          break
          end
          }

          TIG

          1 Reply Last reply Reply Quote 0
          • artmusicstudioA Offline
            artmusicstudio
            last edited by

            hi tig and yes,

            
            @group_STAIR.erase! if @group_STAIR.valid?
            
            

            works perfect.

            another advantage: when i generate my group and copy it manually to the desired place
            and then call the ruby again, the original is beeing deleted and the copy remains.
            that is, what i wanted to achieve: when the user is satisfied with the result, he can either make a copy or make a component out of the group.
            next step is done!
            thanx a lot
            stan

            ps: (on the other hand i was not sure in example 2 and 3, why they did not work for me, i still investigate, must be the NAME definition, i think).

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              In Ruby, only constants are ALL_UPPERCASE

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • artmusicstudioA Offline
                artmusicstudio
                last edited by

                hi dan, have to correct it ( was only for me to see it better while working on it), thanx. (and i already study the files, you recomend for newbie's...but it's a lot to learn, of course)

                and i have to correct my previous statement:

                my "checking" code does not work in a new opended file. it again reports a deleted group, while searching for it.

                but, as soon as i create the first group and the activate the checking-line, it works.

                so now a question:

                is there a way to define 1 variable (true or false) ON LOAD ?
                so this variable would only be "true" when sketchup is opened and before my ruby script runs for the first time (i could overwite it then for the active session)?
                then i could run the checking code "unless onload == true " or something like that.

                ragrds stan

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  How is your code 'encapsulated' ?
                  In a module or a class ?
                  In a module use an instance variable @onload=true within the module but outside of any methods will set it as true initially, as the code is first loaded as SketchUp starts up.
                  Later when you run you first method in that module @onload=false ...
                  The @onload value in remembered across instances of that code during that session of SketchUp.
                  For a class [Tool etc] you need a @@onload prefixed class variable to keep it enduring across uses of that class. Again define it initially outside of any class methods but inside the class's code.

                  When you finish processing and exit your method you can might want to reset @onload=true etc ? It depends on what it is used for etc...

                  Because the user could open a new or existing SKP, and that would continue to use your already-loaded/reset code you might want to construe an AppObserver that resets @onload=true whenever such things change ?

                  However, KISS - the simplest way to think about this is to test for group.valid? before trying to do anything to it. It's a basic trap in scripts to trap for anticipated possible failures:
                  do_something() if group.valid?
                  Another way to trap for unexpected failures is like this:
                  begin do_something() rescue do_something_different() end

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    Also ...what we do is use the global methods defined in the file "sketchup.rb"

                    The two we use are named file_loaded() and file_loaded?()

                    require("sketchup.rb")
                    
                    module AMS # <--<< your toplevel namespace
                    
                      module ThisPlugin  # <--<< rename this
                    
                        ### CONSTANTS
                        #
                        THISFILE = "#{Module.nesting[0]};#{File.basename(__FILE__)}"
                    
                        ### MODULE VARS
                        #
                        @@loaded = false unless defined?(@@loaded)
                    
                        ### PROXY CLASS (for this module)
                        #
                        class << self
                    
                          ### METHODS
                          #
                          def loaded?()
                            @@loaded = file_loaded?( THISFILE )
                          end
                    
                        end # proxy class
                    
                    
                        ### define plugin specific classes or sub-modules here
                        #
                        class ThisTool
                          ### define tool class callbacks here
                        end
                    
                    
                        ### FINAL RUN ONCE CODE
                        #
                        unless loaded?
                    
                          # set up dropdown menu here
                          # and/or right-click popup menu
                    
                          # attach any initial observers here
                    
                          # mark this file as loaded
                          file_loaded( THISFILE )
                    
                        end
                    
                      end # module ThisPlugin
                    
                    end # module AMS
                    

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • artmusicstudioA Offline
                      artmusicstudio
                      last edited by

                      @ tig
                      hi tig, thanx,
                      with

                      
                      def delete_model_zf
                      #***************************************************************************
                      @putswitch = 0 
                      	begin
                      	@group_stair.erase!
                      	rescue
                      	end
                      end
                      
                      

                      it worked, as soon as i understood, that rescue can also be NOTHING, so the code runs thru end the method ends

                      @ dan, thanx,
                      this is still higher programming syntax (for my brain), i tried to encapsulate in
                      require("sketchup.rb")
                      MODULE AMS
                      MODULE 01.rb // is it right to put hier 01.rb ??? (the work-name of the ruby)
                      MY CODE
                      END MODULE
                      END AMS,
                      but have errors. have to work it out! but i would like to put the ruby into a proper form for the first release / check. i'll get back to this later.

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        module is a keyword, it must be all lowercase.

                        Module is a class name, it must be title case, if you refer to the class.

                        💭

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          @artmusicstudio said:

                          MODULE 01.rb // is it right to put hier 01.rb ??? (the work-name of the ruby)

                          No you cannot use a Numeric expression as a Class or Module name. They must begin with alpha characters, and be title case.

                          💭

                          I'm not here much anymore.

                          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