How to create a component contains the all entities of active model?

I want to create a component that contains all entities of active model.

Just like the manual operation in Sketchup:

  1. Select all things in the file
  2. Right click of mouse
  3. Select create component
  4. Give a name for the component defination, other settings are default
  5. Click Ok.

Appreciate for any suggestion or code segment. Thanks.

a .skp model is a component, when imported into another model, so you do a save_copy, with a new filename…

model.save_copy(filename)

john

3 Likes

… and some extra candy …

# Select all things in the file
model = Sketchup.active_model
# Create component, Give a name, other settings are default
comps_path = Sketchup.find_support_file("Components")
model.name= "ComponentName"
model.description= "Some text describing the component."
filename = "#{model.name}.skp"
filepath = File.join(comps_path,filename)
model.save_copy(filepath)
# Clear out stuff so we don't get copies of resources
model.entities.clear!
model.definitions.purge_unused
model.layers.purge_unused
model.materials.purge_unused
# Load the new component definition
cdef = model.definitions.load(filepath)
# Add an instance of the new definition at the ORIGIN:
model.entities.add_instance(cdef,IDENTITY)

:bulb:

2 Likes

What a candy, so sweet!
Thanks man, it works well.

Ps: a little mistake about the spelling of ‘xxx.purge_unused’, missing an ‘u’…

Ah … fixed that. Glad it worked as now ya know I didn’t test it. :wink:

Hey, Dan.

Another problem, the above code is working well for all sketchup file. However, for the large .skp file, such as 800Mb…it takes about 40 mins…So, is there a way I could optimize that?

Thanks~

Well … yes.

The first step it to purge all unused stuff.

model.definitions.purge_unused
model.layers.purge_unused
model.materials.purge_unused

Secondly, set the model’s name and description that’ll be displayed in the Components panel …

model.name= "Some Component Name"
model.description= "Some text that describes the component."

EDIT: You can also do this manually from the File panel of the Model Info dialog.
(There are also the glue, opening, shadow and face camera controls for components on this panel.)

image

Then, save the model.

Lastly, as said above …

Open the save location and copy it to your %AppData% “Components” folder (or subfolder), or some other location if you’ve already set up “local components collection(s)”.

To get quickly to your user components: Window (menu) > Preferences (dialog) > Files (panel)
and click the browse to folder button …

image


And BTW, … inserting 800MB components into other models is crazy. This’ll slow down modeling considerably.