I am working in a plug-in that requires exploding all group and components in the model. I wonder if it is possible to know before exploding the model, how many explode operations will be performed.
I have been using a code for that as follows:
defs = Sketchup.active_model.definitions
count = 0
defs.each do |d|
d.instances.each do |e|
next unless (e.layer.visible?)
if (e.is_a? Sketchup::Group)
count += 1
elsif (e.is_a? Sketchup::ComponentInstance)
count += 1
end # end if Group/ComponentInstance
end # end loop on instances
end # end loop on definitions
but it turns out that for some models this counting is less than the number of explode operations performed. For example the attached model contains nested groups/components, with the code above I count 57 but I can see the amount of explode operations is 70.
I did not checked the file, and there is no information how are you explode …
but perhaps because:
"The #make_unique method is used to force a group to have a unique definition. If the group is already unique in the model, nothing happens.
Copying a group in SketchUp will create a group that shares the same definition. SketchUp implicitly makes group unique when edited from the GUI, and from a user point of view groups could be thought of as always being unique. To honor this behaviour, call this method before editing a group through the API."
I didn’t use the API to construct the model, I did manually in sketchup and it contains copies of groups (so that might be the problem). In order to explode a model this code is used
defs = Sketchup.active_model.definitions
therearegroupsleft = true
explodedNo = 0
while therearegroupsleft
therearegroupsleft = false
defs.each do |d|
d.instances.each do |e|
next unless (e.layer.visible?)
if (e.is_a? Sketchup::Group)
e.explode
explodedNo += 1
therearegroupsleft = true
elsif (e.is_a? Sketchup::ComponentInstance)
e.explode
explodedNo += 1
therearegroupsleft = true
end # end if Group/ComponentInstance
end # end loop in instances
end # end loop in definitions
end # end while therearegroupsleft loop
Here’s my snippet for flattening the model structire.
def self.instance?(entity)
[Sketchup::Group, Sketchup::ComponentInstance].include?(entity.class)
end
def flatten(entities)
loop do
instances = entities.select { |e| instance?(e) }
break if instances.empty?
instances.each(&:explode)
end
end