How to bring along attributes dictionaries with the instance?

Folks,

I stored some values using
entity.set_attribute dict, attribute, value

we assume this entity is a component instance, so actually it can use the “save as” to make it a standalone skp model - for next round use again.
but I realise that once do this “save as”, when 2nd time import to a new context, the customised dictionary, attributes and values all gone.

need some advice, is there any methods that I can either:
1 build a save_as extension to enforce while saving out individual components to bring along the customised attributes
2 any native solution?

A component instance doesn’t correspond to a model, but a component definition do. If you store the attributes to the definition, they’ll be exported with the model.

2 Likes

Do not assume. Test objects using Ruby core #is_a?() or ducktype using respond_to?() methods.

The #save_as method is a method of the Sketchup::ComponentDefinition class, not of instances.

Ex …

if entity.respond_to?(:save_as)
  # It is a definition, so you can save it.
end

In the GUI, when you context click on an instance and do a Save As, you are saving it’s definition out to a .skp file.

Instead store using …

instance.definition.set_attribute( dict, attribute, value )

* Make sure that your dictionary name is unique and no one else would ever choose the same name.
Ie, I suggest using your company name followed by the extension name. It makes it easier if you use the default saving key. From within your sub-module …

    KEY ||= Module.nesting[0].name.gsub('::','_')
2 Likes

@DanRathbun

is it something like this?

mod = Sketchup.active_model
sel = mod.selection
sel.grep(Sketchup::ComponentInstance).each do |s|

s.definition.set_attribute("dynamic_attributes","a0_type", "1")
 s.definition.set_attribute("dynamic_attributes","_a0_type_label", "a0_type")
 s.definition.set_attribute("dynamic_attributes","_a0_type_formulaunits", "STRING")
 s.definition.set_attribute("dynamic_attributes","_a0_type_access", "LIST")
 s.definition.set_attribute("dynamic_attributes","_a0_type_formlabel", "CABINET_TYPE")
 s.definition.set_attribute("dynamic_attributes","_a0_type_options", "MODULAR=1&SEMI-MODULAR=2")

$dc_observers.get_latest_class.redraw_with_undo(s)
end

Any refinement needed?

Sure it could be “something like this” :wink:

The word “refinement” has a special meaning for Ruby …


If you really mean do I think there are any errors, I do not see any in the posted code (except for incorrect indentation.)


If you mean could it be done better, sure …

mod = Sketchup.active_model
sel = mod.selection
dict = 'dynamic_attributes'
atts = Hash[
  "a0_type", "1",
  "_a0_type_label", "a0_type",
  "_a0_type_formulaunits", "STRING",
  "_a0_type_access", "LIST",
  "_a0_type_formlabel", "CABINET_TYPE",
  "_a0_type_options", "MODULAR=1&SEMI-MODULAR=2"
]

sel.grep(Sketchup::ComponentInstance).each do |s|
  atts.each do |key,value|
    s.definition.set_attribute(dict, key, value)
  end
  $dc_observers.get_latest_class.redraw_with_undo(s)
end

However, why would the definitions for multiple instances all be set the same ?

1 Like

Thanks.

i meant any changes needed. Thanks for letting know about Refinement in ruby

Thanks for correcting me on the indentations

I didn’t get on this. is it using definition multiple times for the single attribute!

Yes I just realized that much appreciate!

Thanks Dan, I just realized that I attached the dict and attr to the instance, should have attach to definition instead… thanks!

1 Like