• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Test Comp to see if it has Nested Components

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 371 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    ktkoh
    last edited by 29 Apr 2015, 12:00

    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

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 29 Apr 2015, 12:47

      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)
      

      TIG

      1 Reply Last reply Reply Quote 0
      • K Offline
        ktkoh
        last edited by 29 Apr 2015, 15:32

        Thanks that seems more straight forwad

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 29 Apr 2015, 21:14

          A bit shorter ...more brute force.
          The any? 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"
          
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          1 / 1
          • First post
            1/4
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement