Hello,
I have made a rather straightforward Sketchup Ruby extension to “groupify” contents in Sketchup (i.e. convert from Components to Groups). As such this also makes Dynamic Components into “normal” components without attributes and options.
I can recursively run this on any component and it works just fine.
The idea was to also have the option to select one or more components manually, and run the script on the selection. And here is where I have run into a strange problem. If I select a single component instance everything works, but if I select multiple components the outer loop breaks (only one component from the selection is actually converted). And the outer loop breaks because of a necessary statement in the conversion function.
Actually, If the intst.erase! statement is commented out then everything in the selection is handled, but this is - of course - no option.
The principle is really simple, but for my life I cannot see why the loop breaks.
convert_component_to_group(inst)
{
...
inst.erase! # <--- This breaks outer loop??? How come?
...
}
sel.each { |inst|
convert_component_to_group(inst)
}
In the actual code below I have left out the recursive part to focus on the issue at hand.
I would be grateful if someone could point me to the reason why the outer loop breaks for
multiple selected items (component instances). I realize that I am no Ruby expert, so forgive me if my code looks simplistic.
module JoNoS_Extensions
module JoNoS_Groupify
#----------
def self.convert_component_to_group(inst)
if !(inst.is_a? Sketchup::ComponentInstance)
return false
end
puts "Converting component #{inst} to group "
ents = inst.parent.entities
grp = ents.add_group()
tmp = grp.entities.add_instance(inst.definition, inst.transformation)
tmp.make_unique # Is this necessary???
grp.layer = inst.layer
grp.material = inst.material
grp.locked = inst.locked?
grp.hidden = inst.hidden?
inst.erase! # <--- This breaks outer loop??? How come?
tmp.explode
return true
end
#----------
def self.xtraverse_top(ent, is_recursive)
if is_recursive
# xtraverse(ent)
end
self.convert_component_to_group(ent)
end
#----------
def self.groupify_main()
# Default code, use or delete...
mdl = Sketchup.active_model # Open model
ents = mdl.entities # All entities in model
sel = mdl.selection # Current selection
puts ""
is_recursive = false
top_ents = sel
if top_ents.count > 0
# Define everything as one undo operation
mdl.start_operation "Groupify"
top_ents.each { |top_ent|
puts("Top node #{top_ent.inspect} of type #{top_ent.typename}" )
self.xtraverse_top(top_ent, is_recursive)
}
mdl.commit_operation
end
nil
end
end #module JoNoS_Groupify
end #module JoNoS_Extensions
# Only for testing, comment out otherwise
JoNoS_Extensions::JoNoS_Groupify::groupify_main()