Refer to entities from code
-
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.
-
@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?
-
@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
-
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...
-
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)
-
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")
-
you need to move the
mod = Sketchup.active_model
definitions = mod.definitions.to_astatements inside the def since they are not module variables starting with "@".
-
@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")
-
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
-
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)
Advertisement