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

    How can I determine entity level?

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 5 Posters 369 Views 5 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.
    • N Offline
      NewOne
      last edited by

      @jim said:

      Model.active_path should do it. (If the Component is selected as shown.)

      level = Sketchup.active_model.active_path.nitems
      

      If not, I think you need to check the parent and compare that to see if it is the model. Repeat until you get to the model level.

      Something like this (not tried)

      level = 0
      > while entity.parent != active_model
      >   entity = entity.parent
      >   level += 1
      > end
      

      As I can see, both methods work just if my component is selected. In fact, what I want is simple: to make a list with all components/groups that have not inside them (and also are not included in other components or groups). I'd like to avoid .each method because I'm afraid that could take long time to iterate trough all entities in a complex model.

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        The second should work for you.

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • D Offline
          david.
          last edited by

          I don't know of a built in way of doing this. For my purposes, I recursively iterate through each top level group and component while maintaining a list of records containing the level spec and other relevant data for each child entity. Classes are perfect for this BTW. I use tests of the form:

          entity.instance_of?( Sketchup;;Group ) ...
          entity.instance_of?( Sketchup;;ComponentInstance )
          

          It would be nice if the Sketchup Ruby API supported a built in utility to do this.

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

            If you want to find components that do not contain other components then inspect the model's definitions...

            
            what_I_want=[]
            Sketchup.active_model.definitions.each{|d|
              components=false
              d.entities.each{|e|
                if e.class==Sketchup;;ComponentInstance
                  components=true
                  break
                end#if
              }
              what_I_want<<d if not components
            }
            if what_I_want[0]
              puts what_I_want
            end#if
            
            

            This returns & 'puts' an array of the definitions containing components...
            You can also expand it for Groups...

            TIG

            1 Reply Last reply Reply Quote 0
            • N Offline
              NewOne
              last edited by

              Many thanks TIG! Just one thing I can't figure out how to do it: I want to apply this jut to that definitions which are used in model, but I don't want to purge unused definitions. Unfortunately, active_model gives just active entities. Everything is inside a group or component is not accesible.

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                Iterate all model.definitions - for each definition iterate through its .instances array.

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • N Offline
                  NewOne
                  last edited by

                  @thomthom said:

                  Iterate all model.definitions - for each definition iterate through its .instances array.

                  AND?? This is what TIG's idea does... πŸ˜’ I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
                  TIG's script reports me ALL definitions.

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    david.
                    last edited by

                    @newone said:

                    @thomthom said:

                    Iterate all model.definitions - for each definition iterate through its .instances array.

                    AND?? This is what TIG's idea does... πŸ˜’ I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
                    TIG's script reports me ALL definitions.

                    At the top level, you have to iterate through the model or selection entities, not the definitions. For each compound entity found, you then have to iterate through its child entities. There are 2 different ways of doing that depending on whether a compound entity is a group or component.

                    1 Reply Last reply Reply Quote 0
                    • J Offline
                      Jim
                      last edited by

                      definitions have .instances.

                      Hi

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        @newone said:

                        @thomthom said:

                        Iterate all model.definitions - for each definition iterate through its .instances array.

                        AND?? This is what TIG's idea does... πŸ˜’ I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
                        TIG's script reports me ALL definitions.

                        Unused definitions doesn't have any instances. So when you iterate over all the definitions you check for those that have instances. No need to iterate the entire model.

                        
                        Sketchup.active_model.definitions.each { |d|
                          if !d.group? && !d.image? && d.count_instances
                            # Perform your actions to the definition here.
                            # Note the check for group and image definitions - they are also included in the definition list.
                          end
                        }
                        
                        

                        Thomas Thomassen β€” SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • thomthomT Offline
                          thomthom
                          last edited by

                          @david. said:

                          @newone said:

                          @thomthom said:

                          Iterate all model.definitions - for each definition iterate through its .instances array.

                          AND?? This is what TIG's idea does... πŸ˜’ I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
                          TIG's script reports me ALL definitions.

                          At the top level, you have to iterate through the model or selection entities, not the definitions. For each compound entity found, you then have to iterate through its child entities. There are 2 different ways of doing that depending on whether a compound entity is a group or component.

                          Unless you need to know it's parents, or location in the model entity tree, there is no need to parse the whole model. To iterate only used definitions, the definition list is all you need.

                          Thomas Thomassen β€” SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • D Offline
                            david.
                            last edited by

                            @thomthom said:

                            @david. said:

                            @newone said:

                            @thomthom said:

                            Iterate all model.definitions - for each definition iterate through its .instances array.

                            AND?? This is what TIG's idea does... πŸ˜’ I want to make difference between definitions that are used in model and definitions that are not used in model. (let's say you make a component, then delete it. It remains in Component Browser, but you don't have it in model anymore).
                            TIG's script reports me ALL definitions.

                            At the top level, you have to iterate through the model or selection entities, not the definitions. For each compound entity found, you then have to iterate through its child entities. There are 2 different ways of doing that depending on whether a compound entity is a group or component.

                            Unless you need to know it's parents, or location in the model entity tree, there is no need to parse the whole model. To iterate only used definitions, the definition list is all you need.

                            If you re-read to OP, that is what I understood that he wants...

                            @newone said:

                            Hy, I was playing a little with Entity.parent and model methods and I can't figure out how can I determine the "depth" of an entity. If I have a component (or group) that is encapsulated deep in other component, how can I find how deep (in numbers from 0 to n, where 0 means the component is topmost)

                            1 Reply Last reply Reply Quote 0
                            • N Offline
                              NewOne
                              last edited by

                              Thanks thomthom, sorry... I was tired whed I read your post and I didn't understood exactly what you mean. But, Jim opened my eyes. β˜€
                              Finally this is how I did it:

                              model = Sketchup.active_model
                              all_defs = model.definitions
                              in_model = 0
                              out_model = 0
                              t1 = Time.new
                              
                              all_defs.each do |defn|
                              	count = defn.count_instances
                              		if count == 0
                              			out_model += 1
                              		else
                              			in_model += 1
                              		end
                              end
                              t2 = Time.new
                              dt = t2 - t1
                              puts 'time for completion; ' + dt.to_s
                              puts 'all definitions; ' + all_defs.length.to_s
                              puts 'in model; ' + in_model.to_s
                              puts 'out_model; ' + out_model.to_s
                              

                              I was surprised that with 1600 component definitions , time for completion was 0.0

                              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