Save component definition with attribute dictionaries in new .skp file

I have a model with several component definitions & instances. I want to save a single definition in a new .skp file, so I use the ComponentDefinition::save_as method.

I would like to save some attribute dictionaries of the global model into the .skp file composed only by the component definition. Is there any way to achieve it?

When you save out a component definition as a SKP file, the definition IS the global model for the saved file.

So just go ahead and copy the attribute dictionaries that you wish to copy. There is no premade copy method.

Just use:

mod = Sketchup::active_model
cdef = mod.definitions["My Component"]
dicts_to_copy = ["ThisDictionary","ThatDictionary"]

if mod.attribute_dictionaries
  dicts_to_copy.each do |dictname|
    dict = mod.attribute_dictionaries[dictname]
    if dict
      dictcopy = cdef.attribute_dictionary(dictname,true)
      dict.each_pair {|key,val| dictcopy[key]= val }
    end
  end
end

# save out component here:
cdef.save_as(some_path)
1 Like

Thank you @DanRathbun

Your welcome.

But be aware that when you insert one of these components, it’s dictionaries attached to the file, become dictionaries of the definition, in the new model. They will NOT be automatically recopied up to the model object level.

This topic was automatically closed after 91 days. New replies are no longer allowed.