# Starting with entity check if entity or any of it's children is is_a type
# Add them to entity_instances
# Cache component_definitions in a set to not check them twice
def get_all_instances_of(entity, type, entity_instances, component_definitions)
if entity.is_a?(Sketchup::ComponentInstance)
# check component_definitions cache
if component_definitions.include?(entity)
return
end
component_definitions.insert entity
# check children
entity.definition.entities.each do |child|
get_all_instances_of(child, type, entity_instances, component_definitions)
end
elsif entity.is_a?(Sketchup::Group)
# check children
entity.entities.each do |child|
get_all_instances_of(child, type, entity_instances, component_definitions)
end
end
if entity.is_a? type
entity_instances << entity
end
end
IyBTdGFydGluZyB3aXRoIGVudGl0eSBjaGVjayBpZiBlbnRpdHkgb3IgYW55IG9mIGl0J3MgY2hpbGRyZW4gaXMgaXNfYSB0eXBlCiMgQWRkIHRoZW0gdG8gZW50aXR5X2luc3RhbmNlcwojIENhY2hlIGNvbXBvbmVudF9kZWZpbml0aW9ucyBpbiBhIHNldCB0byBub3QgY2hlY2sgdGhlbSB0d2ljZQpkZWYgZ2V0X2FsbF9pbnN0YW5jZXNfb2YoZW50aXR5LCB0eXBlLCBlbnRpdHlfaW5zdGFuY2VzLCBjb21wb25lbnRfZGVmaW5pdGlvbnMpCiAgICBpZiBlbnRpdHkuaXNfYT8oU2tldGNodXA6OkNvbXBvbmVudEluc3RhbmNlKSAKCQkjIGNoZWNrIGNvbXBvbmVudF9kZWZpbml0aW9ucyBjYWNoZQoJCWlmIGNvbXBvbmVudF9kZWZpbml0aW9ucy5pbmNsdWRlPyhlbnRpdHkpCgkJCXJldHVybgoJCWVuZAoJCWNvbXBvbmVudF9kZWZpbml0aW9ucy5pbnNlcnQgZW50aXR5CgoJCSMgY2hlY2sgY2hpbGRyZW4KCQllbnRpdHkuZGVmaW5pdGlvbi5lbnRpdGllcy5lYWNoIGRvIHxjaGlsZHwKCQkJZ2V0X2FsbF9pbnN0YW5jZXNfb2YoY2hpbGQsIHR5cGUsIGVudGl0eV9pbnN0YW5jZXMsIGNvbXBvbmVudF9kZWZpbml0aW9ucykKCQllbmQ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