I have read/searched here for a bit and can’t seem to find a way to tell if a component is nested. Hope I am wrong any suggestions?
Thanks
I have read/searched here for a bit and can’t seem to find a way to tell if a component is nested. Hope I am wrong any suggestions?
Thanks
Entity method “parent”
Thanks!
model = Sketchup.active_model
selset = model.selection
selset.each do|dc|
dc_parent = dc.parent
if dc_parent == model
puts 'parent'
else
puts 'child'
end
end
This works if the parent is at the top model level. I realize now that I need it the other way , can I tell if it has children?
Then you should iterate along the component definition entities. If one of them has definition then your component has children.
By the way selection is an array, it’s not neccesary .to_a
Here it is:
model = Sketchup.active_model
selset = model.selection
selset.each do |dc|
cdef = dc.definition
cdef_ents = cdef.entities
cdef_ents.each do |entity|
if entity.respond_to?(:definition)
puts 'has child'
else
puts 'no children'
end
end
end
Thanks for the help!
NO.
The Selection is an Object, and Includes Enumerable
That’s not why you don’t need to copy the current collection to an array…
…but:
Selection
is not an Array
. )to_a
and then use each
on the array, when removing content.Using Enumerable#any?
…
def has_children?(dc)
dc.definition.entities.any? { |entity| entity.respond_to?(:definition) }
end
selset.grep(Sketchup::ComponentInstance) do |dc|
has_children?(dc) ? puts 'has child' : puts 'no children'
end
Or getting parents with children …
def get_parents(selset)
selset.grep(Sketchup::ComponentInstance).select(&:has_children?)
end