Deleted component instances in script


#1

Hello. I’ve got big problem with counting deleted elements in my ruby script. Here is below piece of code I use to count components definition and its instance on scene. is_snapping_point_component_definition is just function which checks if function fulfill condition for being treated as interesting component from my point of view.

So basically I’m searching for all component deifnition and its instances which are still valid and count them.

def self.set_snapping_points_visibility(value)
count = 0
count2 = 0
definitions = Sketchup.active_model.definitions.select { |d| is_snapping_point_component_definition?(d) }
definitions = definitions.uniq
puts 'Component count: ’ + definitions.length.to_s
definitions.each { |d| d.instances.each { |i| count2 += 1 if i.valid? } }
puts 'Entity count: ’ + count2.to_s
end

But very strange behaviour is there. If I add item to empty scene everything is fine. But after deleting instances from scene (even purging everything from scene and leaving blank), numbers are still the same as before deletion. First I told it was because I need to check deleted/valid flag and then I’ve added that check. But that didn’t help. However creating new document restore back numbers to 0.

Any ideas? I just think that I don’t understand how Sketchup is storing instances.


#2

Try using …

Sketchup::ComponentDefinition#count_instances

… and …

Sketchup::ComponentDefinition#count_used_instances


#3

@Dan
Thank you for your reply. My description was simplified so in fact here purpose of this script in long run is not to count definitions, that was just for debugging purposes. What I want to do is find every instance which is specific type of component and hide/show it.

I solved it calling

Sketchup.active_model.definitions.purge_unused

before searching for entities.

But why entity.deleted? and entityvalid? flags doesn’t work in that scenario?


#4

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.