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

    Refer to entities from code

    Scheduled Pinned Locked Moved Developers' Forum
    19 Posts 5 Posters 372 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.
    • D Offline
      dacastror
      last edited by

      I'm reading carefully the lines of code and I'm learning TIG proposes several things, another question arises me, suppose that I assign a different name to each dictionary of each entity and I want to find the entity using the name of the dictionary. How can I modify the condition
      " if i.get_attribute("dacastor 666", "id", nil)==idd "?
      (google translator)

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

        If you have say three different dictionaries you can use something like:
        ` nlist=["dacastor123", "dacastor666", "dacastor999"]###***
        ###... etc
        d.instances.each{|i|
        ###... etc
        nlist.each{|dict|
        if i.get_attribute(dict, "id", nil)==idd
        ###.... etc ### do stuff here
        end
        }

        } ###... etc`

        ***### 'nlist' is an array of possible dictionary names... set up before you start iterating collections of things...
        'dict' refers to each "string" name, inside the 'nlist' [array]...

        Note that the attribute dictionary 'names' AND the 'keys' should not contain 'spaces' or other puncuation - it should start with a-z, then more or 0-9, but you can include an '_'

        TIG

        1 Reply Last reply Reply Quote 0
        • D Offline
          dacastror
          last edited by

          TIG thank you very much, I will try that 😄

          I was trying something, but is a bit more complicated:

          
          idd = "dic1"
          hit = nil
          mod.definitions.each{|d|
             next if d.image?  
             d.instances.each{|i|
                next if i.attribute_dictionaries.to_a.size<1
                dictio = i.attribute_dictionaries.to_a
                if dictio[0].name==idd
                   hit=i           
                   break
                end
             break if hit
             }
          }
          
          1 Reply Last reply Reply Quote 0
          • jolranJ Offline
            jolran
            last edited by

            Hah 😄 , yes thats exactly just what I did.

            I ran into trouble deleting the newly created definition, with something like :
            gp=Sketchup.active_model.active_entities.add_instance(@su_gp.definition, ORIGIN)

            Also since @su_gp (in my case) could be either a group or component I had to put in some ifs and nots as well. With entities.parent for groups just like you described.
            Not a big deal to explode, performance-vise. Feels safer.

            Thanks for reasuring about the group unique thing. Not gonna use it this time though 😄 .

            BTW hope this issue was related to the topic.

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

              @dacastror said:

              ... suppose that I assign a different name to each dictionary of each entity and I want to find the entity using the name of the dictionary. How can I modify the condition
              " if i.get_attribute("dacastor 666", "id", nil)==idd "?
              (google translator)

              if i.attribute_dictionary(idd)

              or

              unless i.attribute_dictionary(idd).nil?

              I'm not here much anymore.

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

                @dacastror said:

                I was trying something, but is a bit more complicated:

                you can simply do:

                def find_inst_by_dict_name(dname)
                  found = nil
                  for d in definitions
                     next if d.image? 
                     found = d.instances.find{|i|
                       i.attribute_dictionary(dname)
                     }
                     break if found
                  end # for
                  return found
                end # def
                

                I'm not here much anymore.

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

                  You/we are starting to conflate the searching for an object that has a named attribute-dictionary attached to it [and which is uniquely named for one object, and is irrespective of any key/value pairs], and then the original code snippets which were about assigning, and then checking for, the 'value' of a specific 'key' in a named attribute-dictionary [where the key and dictionary are commonly named across several objects, and only vary by the key's 'value'].

                  I suggest you step back and decide what it is you are trying to do and how you do it best...

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    dacastror
                    last edited by

                    Thanks Dan, I find it very nice your suggestion, I will give a chance to this

                    Thanks for replying TIG, I really needed to know the two things, I was lost without your help

                    (google translator)

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      dacastror
                      last edited by

                      I did this and I get "error", I couldn't see what is wrong

                      mod = Sketchup.active_model
                      definitions = mod.definitions.to_a
                      
                      def find_entity(dname)
                        found = nil
                        for d in definitions
                           next if d.image? 
                           found = d.instances.find{|i|
                             i.attribute_dictionary(dname)
                           }
                           break if found
                        end # for
                        return found
                      end # def
                      
                      entity = find_entity("dic1")
                      
                      1 Reply Last reply Reply Quote 0
                      • sdmitchS Offline
                        sdmitch
                        last edited by

                        you need to move the

                        mod = Sketchup.active_model
                        definitions = mod.definitions.to_a

                        statements inside the def since they are not module variables starting with "@".

                        Nothing is worthless, it can always be used as a bad example.

                        http://sdmitch.blogspot.com/

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

                          @dacastror said:

                          I did this and I get "error", I couldn't see what is wrong

                          I CAN 😳 It is a stupid mistake on my part.

                          def self.find_entity(dname)
                            found = nil
                            for d in Sketchup.active_model.definitions
                               next if d.image? 
                               found = d.instances.find{|i|
                                 i.attribute_dictionary(dname)
                               }
                               break if found
                            end # for
                            return found
                          end # def
                          
                          
                          entity = find_entity("dic1")
                          

                          I'm not here much anymore.

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

                            Assume you have a "DiegoCastro" folder:

                            And in it a "Lib.rb" file for your library methods:

                            module DiegoCastro
                            
                              module Lib
                            
                                def find_entity(model,dname)
                                  found = nil
                                  for d in model.definitions
                                    next if d.image? 
                                    found = d.instances.find{|i|
                                      i.attribute_dictionary(dname)
                                    }
                                    break if found
                                  end # for
                                  return found
                                end # def
                            
                              end # Lib sub-module
                              
                            end # Author toplevel namespace module
                            
                            

                            Then in SomePlugin you do this... (name it whatever.. "diegotest.rb" etc.)

                            
                            module DiegoCastro
                            
                              module SomePlugin # <--<< change plugin sub-module name as needed
                            
                                class << self # proxy class
                            
                                  require('DiegoCastro/Lib')
                                  include(DiegoCastro;;Lib)
                                
                                  def find_inst_by_dict_name()
                                    if @last_dict.nil?
                                      @last_dict = "dynamic_attributes" # initial value
                                    end
                                    dname = UI.inputbox(['Dictionary Name........; '], [@last_dict], "Find by Dicto Name")
                                    if dname # user did not cancel the inputbox
                                      unless dname.empty? || dname[0].length==0
                                        @last_dict = dname[0]
                                        entity = find_entity(Sketchup.active_model,dname[0])
                                        if entity
                                          UI.messagebox("Found #{entity.inspect}     \nwith dictionary name '#{@last_dict}'     ")
                                          #
                                          return entity
                                          #
                                        else # nothing was found because find returned nil 
                                          UI.messagebox("No entity found with     \n dictionary name '#{@last_dict}'     ")
                                        end
                                      end
                                    end
                                    return nil
                                  end
                                
                                end # proxy class
                                
                                unless file_loaded?(File.basename(__FILE__))
                            
                                  # create menus here;
                                  @@submenu = UI.menu('Plugins').add_submenu('Diego')
                                  @@submenu.add_item('Find entity by Dicto Name') {
                                    find_inst_by_dict_name()
                                  }
                            
                                  file_loaded(File.basename(__FILE__))
                            
                                end
                            
                              end # plugin sub-module
                            
                            end # Auhtor module
                            

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • D Offline
                              dacastror
                              last edited by

                              wow thanks Dan, I will take some time to study this code, worth but me several questions arise, I will try to solve the most I can on my own, thank you very much 😄
                              (google translator)

                              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