Remove window definitions from Sketchup 2017 components


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

Is there a way to bypass this error?

Thank you in advance for your help!

Notice the qualifiers ‘seems to’ and ‘appears to’

# 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

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| == 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
  "#{count - list.size} definition(s) removed"
1 Like

Well done sWilliams, :clap:
I would have had a hard time making the link between my problem and the commit_operation!

Thanks dezmo for your example.
If I’m not mistaken, we can do the same job as your example by avoiding the “purge_unused”!

def remove_def( name )
  mod = Sketchup.active_model
  mod.start_operation("Remove component definition: #{name}", true)
  mod.definitions.each{ |d| d.entities.clear! if }

Using the method clear! instances including the definition name “window” will be deleted along with their definition in the component window.

1 Like