Counting explode operations to be performed in a model

Hi,

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.

regards, Carlos
Houses_SU18.skp (69.8 KB)

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."

dezmo, thanks for your reply

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 

I’m assuming that in arriving at your expected count you allowed for the fact that your code skips any groups whose layer is not visible?

This should return the number of times explode needs to be called to completely flatten the model hierarchy.

Sketchup.active_model.definitions.sum { |d| d.count_used_instances }

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
1 Like

slbaumgartner, thanks for your reply

yes I dont want groups/components in invisible layers to be exploded, so I skip them in the counting

ene_su, thanks for your reply It was very helpful :slight_smile:

regards, Carlos