Here is my final … it takes a few minutes to delete the groups inside an operation.
EDIT: Actually, made a change. debug
is now a named argument.
(Note SketchUp itself is purging the unused group definitions from the Definitions collection because they no longer have any instances. We have no control over this.)
def delete_truss_gussets(inst_name = 'plates', debug: false)
gussets_deleted = []
groups_deleted = 0
model = Sketchup.active_model
dlist = model.definitions
puts
puts "Begin Truss Gusset Purging"
model.start_operation('Purge Gusset Groups',true)
#
dlist.each do |cdef|
next unless cdef.group? && cdef.instances.size > 0 &&
cdef.instances[0].name == inst_name
papa = cdef.instances[0].parent
if papa.name.start_with?('T') && papa.instances.size > 0 &&
papa.instances[0].layer.name.start_with?('Trusses')
cname = cdef.name
puts " Processing gusset group defintion: \"#{cname}\"" if debug
cdef.instances.each_with_index do |inst,i|
if inst.name == inst_name
puts " Deleting \"#{inst_name}\" instance index #{i}" if debug
gussets_deleted << cname if cdef.instances.size == 1
inst.erase!
groups_deleted += 1
end
end
end
end
puts "Number of gusset group instances deleted: #{groups_deleted}"
puts "Number of gusset group definitions to purge: #{gussets_deleted.size}"
puts
puts " ... please wait, Purging ..."
puts
#
model.commit_operation
#
orphans = []
dlist.each do |cdef|
if !cdef.group? && !cdef.image? && cdef.instances.size == 0
orphans << cdef
end
end
if orphans.size > 0
puts "Number of orphaned definitions that can be purged: #{orphans.size}"
names = orphans.map {|cdef| cdef.name }
puts names.inspect if debug
choice = UI.messagebox("Purge #{orphans.size} Gusset Components ?",MB_YESNO)
if choice == IDYES
model.start_operation('Purge Gusset Components',true)
#
if dlist.respond_to?(:remove)
orphans.each {|cdef| dlist.remove(cdef) }
else
dlist.purge_unused
end
#
model.commit_operation
end
end
puts "Done\n"
end