Test Comp to see if it has Nested Components
-
This is the code I wrote to determine if a Component or Group contained any Nested Components. Is there an easier or more direct method of doing this??
mod = Sketchup.active_model # Open model ent = mod.entities # All entities in model sel = mod.selection # Current selection obj = sel[0] exp = "No" if (obj.is_a? Sketchup;;ComponentInstance) || (obj.is_a? Sketchup;;Group) ce = obj.definition.entities ce_a = ce.to_a # Make sel an array ce_a.each {|e|if e.is_a? Sketchup;;ComponentInstance then exp = "Yes" end } end # if obj UI.messagebox("Explode Selection " + exp)
Keith
-
Try this
mod = Sketchup.active_model sel = mod.selection obj = sel[0] cen = [] if obj.is_a?(Sketchup;;ComponentInstance) cen = obj.definition.entities elsif objobj.is_a?(Sketchup;;Group) ### for < v2015 cen = obj.entities end exp = "No" exp = "Yes" if cen.grep(Sketchup;;ComponentInstance).length > 0 UI.messagebox("Explode Selection - " + exp)
-
Thanks that seems more straight forwad
-
A bit shorter ...more brute force.
Theany?
enumerable method returns boolean and stops on the first true result:return unless obj.respond_to?(;manifold?) ents = obj.entities rescue obj.definition.entities any = ents.any? {|e| e.is_a?(Sketchup;;ComponentInstance) } exp = any ? "Yes" ; "No"
Advertisement