ok i have my script (almost working)
def cinchange
#--initialize--
model=Sketchup.active_model
ss=model.selection.first
UI.messagebox('selection OK')
########################
#--Change the instance--
########################
#--Get component definition of instance--
compdef=ss.definition.name
UI.messagebox('definition OK')
#--Write component def. to instance dictionary
ss.set_attribute 'xdid_dict', 'compdef', compdef
UI.messagebox('attribute OK')
#--Make instance unique (creates new comp. definition)--
ss.make_unique
UI.messagebox('unique OK')
#--Edit the instance (make all entities red)--
compdef2=ss.definition
ent=compdef2.entities
ent.each {|i|
if i.typename=="Face"
i.material="red"
i.back_material="red"
end
}
UI.messagebox('rood OK')
UI.messagebox('total succes')
end
def cinrestore
#########################
#--Restore the instance--
#########################
#--Get current component definition of instance--
model=Sketchup.active_model
ss=model.selection.first
UI.messagebox('selection OK')
#--Get the component definition defined in the attrib. dict.--
orig_compdef=ss.get_attribute 'xdid_dict', 'compdef'
UI.messagebox(orig_compdef)
#--If no comp. definition set in the attrib. dict., do nothing--
#--If comp. def. set in attrib. dict. does not equal current comp. def., set the component definition from the attrib. dict. back--
current_compdef=ss.definition.name
UI.messagebox(current_compdef)
if orig_compdef!= current_compdef
UI.messagebox('niet gelijk')
ss.definition=orig_compdef
end
The only problem left is how to set the definition of the instance back to the original one based upon the variable orig_compdef which is the name of the definition.
I have saved the original 'name' of the component definition in the attrib. dictionary cause appaerently it is not possible to save the definition itself.
Can someone help me on this? --> SOLVED
Also, could you check if it is possible to make this script 'better'?
Thx