Splitting and saving entities in a component/group


#1

I have a skp model of a 3D city. When I load it into sketchup, I can right click it and select edit component. Then, when I right click it I can click edit group. When I do this, I can select individual buildings and save them as seperate models.

I want to do this for a lot of cities, so I need to use the Ruby API/console for this. It seems straightforward but I am a sketchup beginner and have no clue how to do this.

Can anyone help with this?

Thanks a lot!


#2

I was able to save them as sketchup files using the the save_as method of the ComponentDefinition class, but I need to save them as obj files. Is there some way to do so?

Any help greatly appreciated!


#3

The exporters do not provide to the API an option to export a given entity (or array of entities), only the whole model or the current selection (which is very GUI-bound, you cannot keep the user’s selection untouched).

# Backup whatever the user had selected.
selection_before = model.selection.to_a
# Select the entity to export.
model.selection.clear
model.selection.add(component_instance)
# Export selection set only.
options_hash = {
  :triangulated_faces  => true,
  :doublesided_faces   => true,
  :edges               => false,
  :materials_by_layer  => false,
  :texture_maps        => true,
  :selectionset_only   => true,
  :preserve_instancing => true
}
success = model.export(path, options_hash)
# Restore whatever the user had selected.
model.selection.clear
model.selection.add(selection_before)

Take care that altering the selection also triggers selection observers. You can disable GUI updates (e.g. entity info) and improve performance if you wrap it into an operation with the second boolean parameter false.


#4
Slight code correction (since fixed)

There actually is no #selection= instance method of the Sketchup::Model class.

In reality to restore the previous selection set, you just add the “before” entity array back into the singleton Sketchup::Selection instance (after clearing it of temporary entity objects) …

model.selection.add(selection_before)

#5

Thanks, I fixed it. I hadn’t been able to run the code.


#6

Thanks so much Aerilius, this is exactly what I needed. Worked perfectly!