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

    Fastest way to select component by definition.name

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 3 Posters 1.8k 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.
    • H Offline
      HPW
      last edited by

      What is the fasted way of getting a component by its definition.name?
      Must I iterate through the entities every time:

      entities=model.entities
        entities.each{|c|
           mylist.push(c) if c.definition.name=="WantedComponentname"
        }
      
      
      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        
        defn = Sketchup.active_model.definitions['WantedComponentname']
        # Returns nil if the definition doesn't exist,
        
        

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

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

          @hpw said:

          What is the fasted way of getting a component by its definition.name?
          Must I iterate through the entities every time:

          entities=model.entities
          >   entities.each{|c|
          >      mylist.push(c) if c.definition.name=="WantedComponentname"
          >   }
          > 
          
          name="WantedComponentName" ### for example
          defn=nil ### empty variable
          Sketchup.active_model.definitions.each{|definition| defn=definition if definition.name==name}
          puts defn ### prints it in ruby console so we see if it works...
          Sketchup.active_model.selection.clear
          Sketchup.active_model.selection.add(defn.instances)
          ### selects all component instances with that name...
          

          TIG

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

            @thomthom said:

            
            > defn = Sketchup.active_model.definitions['WantedComponentname']
            > # Returns nil if the definition doesn't exist,
            > 
            

            Well done - much shorter...
            For some reason I prefer the 'name' to be either a variable [string] or inside "...", though your '...' does work...

            Sketchup.active_model.selection.add((Sketchup.active_model.definitions["WantedComponentName"]).instances)
            ### as a one liner ?
            

            TIG

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

              I prefer to use single quotes out of habit from coding PHP where single quoted strings require less parsing resources as they don't allow variables inside them as double quoted does. (Same thing is for Ruby strings but in the SU environment as oppose to web environment I don't think it matters much.)

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

              1 Reply Last reply Reply Quote 0
              • H Offline
                HPW
                last edited by

                Thanks for both valuable Tips.
                I am making slow progress in learning SU-ruby and importing my Autocad-Models in a half-automatic fashion.
                Just finished a Autolisp-Function to write ruby-scripts to TempDir with attributs for non-editable DC's.
                (I noticed a few problems with DC attributes. Capital letters in field names are not liked by the DC, and also german Umlauts gives problem, they has to be converted to HTML syntax)
                A ruby function restored the block-hierarchy using groups.

                Now I will have a look at setting interact behaviour to this components.
                By the way: An idea to move component axes by ruby-code?

                Regards

                Hans-Peter

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

                  @unknownuser said:

                  By the way: An idea to move component axes by ruby-code?

                  Find my ComponentInstance-add_entities.rb that has some instance/definition transformation stuff in it. It has to add entities to the instance definition without changing its origin - it does it by transforming the contents of an instance back to the origin, making a new definition and, swapping names, then overwriting the old one with new one, and purging the old one...

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • H Offline
                    HPW
                    last edited by

                    Hello TIG,

                    I tried a different approach with your componentinstance.add_entities
                    (Your ComponentInstance-add_entities.rb is loaded)
                    (Instead of creating a new Master-Group I want to make a existing component to the master-component of it's Sub-components)

                    sel_def = Sketchup.active_model.definitions['MyDefName']
                    sel_def.add_entities(MySubCompArray)
                    
                    

                    This throws the error ' undefined method `add_entities' '
                    I think I need the instance.
                    So what's the way back from the definition to the instance?
                    (Still miss the deep understanding of SU data-model) ;-(

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

                      @unknownuser said:

                      sel_def = Sketchup.active_model.definitions['MyDefName']
                      sel_def.add_entities(MySubCompArray)

                      Doesn't work...
                      My thing only adds given entities into an instance - with flags*. If you only have one instance you could use

                      sel_def.instances[0].add_entities(MySubCompArray)
                      

                      If you want to add some entities to a definition itself use...

                      sel_def = Sketchup.active_model.definitions['MyDefName']
                      sel_def.entities.add(MySubCompArray)
                      

                      These new entities need to be where you want them in the component. My instance based method lets you add things to an instance as it is placed and then *either make it unique or update the original definition and all of its instances etc...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • H Offline
                        HPW
                        last edited by

                        Hello TIG,

                        Again many thanks for the hint.

                        sel_def.instances[0].add_entities(MySubCompArray)

                        does the job for me.

                        Now I can use the imported dummy-block from autocad as the hierarchy-master instead of creating a separate new group. Will keep the outliner structure more readable. I also use now Jim's togglewindows.rb to keep the outliner close during execution.

                        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