As the “remove” method of the “Sketchup::DefinitionList” class is not available before the 2018 release, it is very difficult to remove window definitions from components with SketchUp 2017.
Using the “clear!” of the class “Sketchup::Entities” it is possible to delete instances and entities in the component window provided that the user draws, deletes or imports an “entity” manually after the execution of the code.
Before that, definitions continue to exist in the component window:
mod = Sketchup.active_model
ents = mod.active_entities
ents.grep(Sketchup::ComponentInstance) do |i|
ent = i.definition.entities
ent.clear!
end
# Clearing the entities of a component definition appears to remove
# the component definition from the model's definitions list
#
# note: start_operation and commit_operation are required because
# it is the commit_operation that seems to trigger
# the cleanup of the model's definition list
#
mod = Sketchup.active_model
puts "The model has #{mod.definitions.size} component definitions"
mod.start_operation("remove component")
# Remove the first component found in the active_entities
active_ents = mod.active_entities
inst = active_ents.grep(Sketchup::ComponentInstance)[0]
def_ents = inst.definition.entities
def_ents.clear!
mod.commit_operation
puts "The model now has #{mod.definitions.size} component definitions"
Your code above will find all instances in active drawing context, then clear up (remove) all entities from the collection of entities of definition of instances. Creating an empty definition(s).
Normally if you do this via UI the definition is disappearing from component tray.
As you see @sWilliams example ruby needs to trigger it by wrapping the code to start_operation -commit_operation
There is an other possibility to remove definition from the list, after removing all instances inserted into the model:
The purge_unused method is used to remove the unused component definitions.
(However this will remove all unused definition not only the “window”)
E.g.: let’s assume you have windows, with definition name of “window”:
def remove_def(name)
definitions = Sketchup.active_model.definitions
count = definitions.size
window_def = definitions.find{|d| d.name == name}
return "There is no #{name}" unless window_def
window_instances = window_def.instances.to_a
model = Sketchup.active_model
model.start_operation("Remove component definition: #{name}", true)
window_instances.each(&:erase!) if window_instances.first
list = definitions.purge_unused
model.commit_operation
"#{count - list.size} definition(s) removed"
end
remove_def("window")