How to export components of each definition file (for import Lumion as nodes)?

As title, I plan to go through a list of definition, get the components of each definition and save them to separate files.
But I couldn’t find a way to open a new model, copy the entities and save that model.

My current solution is to select the instances of definitions, then invert the selection to delete other, save to new file and undo. But that will not work with large data, and contain too much unpurged data.

def_list = model.definitions["tree-1"]                
Sketchup.active_model.start_operation("Export to new file", true)

selection.add def_list.instances
selection.invert

selection.to_a.each { |entity|
  entity.erase!
}

So what is the correct way to do this?

Class: Sketchup::ComponentDefinition # save_as (file_path) ⇒ Boolean— SketchUp Ruby API Documentation

I tried that, but it only save the component definition in co-ordinate 0,0,0. Is there anything I’m missing?

What I need is to save all the components with their location, all the instances of that component definition.

I see…

:bulb:Something like this came to my mind

def export_instances(definition)
  model = Sketchup.active_model
  entities = model.active_entities
  instances = definition.instances

  #note : All instnaces below must have a common parent
  group = entities.add_group(instances)

  def_to_export = group.definition
  success = def_to_export.save_as( "c:/export.skp" )
  #clean up:
  group.explode
end

export_instances( Sketchup.active_model.definitions["tree-1"] )
2 Likes

This coding problem usually requires that the model hierarchy be walked.

2 Likes

Thank you all.

It’s work perfect.

2 Likes