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!
    ⚠️ Important | Libfredo 15.6b introduces important bugfixes for Fredo's Extensions Update

    Refer to entities from code

    Scheduled Pinned Locked Moved Developers' Forum
    19 Posts 5 Posters 679 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.
    • 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