sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    To detect if a component contains a component

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 238 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.
    • G Offline
      glro
      last edited by

      i am wondering if there is a way to detect if a component contains an embedded component
      the idea is:

      if it contains an embedded component, explode it
      if not, don't explode it

      There is a simple way: add some characters at the end of the component name, and check wether these characters exist

      model = Sketchup.active_model # Open model
      entities = model.entities # All entities in model
      	
      entities.to_a
      
      model.start_operation "explose_comp_imbriques"
      
      for e in entities.to_a
            if e.is_a? Sketchup;;ComponentInstance
                str= e.definition.name 
                e.explode if (str.include? "#i" )
            end
      end
      
      Sketchup.active_model.definitions.purge_unused
      	
      model.commit_operation
      

      but it would be better not to be obliged to add special characters

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        This explodes all component-instances that contain another instance AND clears the now unused definitions

        model=Sketchup.active_model
        defns=model.definitions
        model.start_operation('xxx!')
        defns.each{|d|
          next unless d.instances[0]
          nested=false
          d.entities.each{|e|
            if e.is_a?(Sketchup;;ComponentInstance)
              nested=true
              break
            end
          }
          d.instances.each{|i| i.explode } if nested
        }
        defns.each{|d|
          d.entities.clear! unless d.instances[0]
        }
        model.commit_operation
        

        Is it what you really want to do ?
        It is one step undo-able,
        It's only partial coder - it needs to be wrapped in a module/method etc of your choice...
        ` module Giro
        def self.xxx()

        the code goes here

        end#def
        end#moduleUsage: in the Ruby Console: Giro.xxx`
        Change 'xxx' to be whatever suits you...

        TIG

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          Here's another edition (wrapped in method,) that only works upon the active editing context (to help prevent BugSplats!,) tests first for any definitions and lastly filters out images and groups.

          Also adds in a begin ... rescue block.

          @dan rathbun said:

          ...snip... (WIP) found errors

          Actually it may be correct. (No food yet today.)

          Not thoroughly tested.

          def explode_parent_components()
            #
            model = Sketchup.active_model
            context = model.active_entities
            #
            if context.any?{|e| Sketchup;;ComponentInstance === e }
              #
              defns = model.definitions.find_all{|d| !d.image? && !d.group? }
              #
              if defns.any?
                begin
                  #
                  op = 'Explode ONLY Parent Components'
                  #
                  if model.method(;start_operation).arity==1
                    model.start_operation(op)
                  else
                    model.start_operation(op,true) # disable UI on SU 7+
                  end
                    #
                    defns.each{|d|
                      inst = d.instances
                      next if inst.empty?
                      inst.reject!{|i| i.parent != context }
                      inst.each{|i| i.explode }
                    }
                    #
                  model.commit_operation()
                  #
                rescue => e
                  model.abort_operation()
                  UI.messagebox($!.message) if $VERBOSE
                  raise()
                end
                #
              end # if any defns
              #
            end # if any instances in the active context
            #
          end # def
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • G Offline
            glro
            last edited by

            It works perfectly

            thank you!

            1 Reply Last reply Reply Quote 0
            • G Offline
              glro
              last edited by

              @dan rathbun said:

              Here's another edition (wrapped in method,) that only works upon the active editing context (to help prevent BugSplats!,) tests first for any definitions and lastly filters out images and groups.

              thank you for the code

              i tried it, but it doesn't work, at least on my computer

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

              Advertisement