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

    All entities in the model?

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 8 Posters 506 Views 8 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.
    • thomthomT Offline
      thomthom
      last edited by

      Wouldn't crawling the entities of .active_model.entities and then the instance definition do the trick? That way you don't have to crawl through nested objects, just run over them once.

      (I'm not sure here, but there is a definition for each group? Or is it just for groups which have copies?)

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

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

        You need to "mine" into the groups and any component instance definitions.

        I have made some "miners" - e.g. ComponentReporter...

        First you pick up loose geometry etc and then compo instances [get that instance's definitions contents] and any groups' contents etc... looping back through each until all are done... grouped groups, groups of compos etc...

        Good luck...

        TIG

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

          Traversing component instances seem unnecessary. You get all component definitions from .active_model.definitions. No point going over the several instances of the same definition. Or is there some gotcha's I've missed?

          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

            Just checked. And it seem to works for groups as well.

            
            def all_entities
            	model = Sketchup.active_model
            	
            	# All loose geometry
            	model.entities.each { |e|
            		yield e
            	}
            	
            	# All group/component geometry geometry
            	model.definitions.each { |definition|
            		definition.entities.each { |e|
            			yield e
            		}
            	}
            end
            
            

            You can then do this to make all entities' material red:

            
            all_entities { |e| e.material = 'red' }
            
            

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

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

              Thomas, what if a definition exists in the DefinitionList but does not have an instance in the model? Do you still want its entities? I guess it depends of what the use of the collection is.

              Hi

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

                Hi all,

                Thomas, thats a nice use of yield inside blocks ... I wasn't aware of that possibility in Ruby!

                And Jim seems to be right, component definitions often exist although not instantiated (e.g. that default man in the corner).

                And, though Matthieu did not specifically asked for that, I can imagine, that he also is interested in the actual transformations (final placement and scale) of every component instance. At least I would be interested in that information.

                But, maybe, the transformation thingy opens a "can of worms"?

                best regards, Georg

                1 Reply Last reply Reply Quote 0
                • M Offline
                  Matt666
                  last edited by

                  Wow very interesting answers!!!
                  So, no native ruby method can return this list...

                  @unknownuser said:

                  Thomas, thats a nice use of yield inside blocks ... I wasn't aware of that possibility in Ruby!
                  +1 !! And I don't understand... 😉 Yield function is very strange... Bref.
                  Thomthom, your function doesn't work if I want to have a list contains all used component instances in the model.

                  Thank you all for these answers!

                  Frenglish at its best !
                  My scripts

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

                    hm... wonder if we can get an extra function to the code block on this forum so they could be expanded to they don't wrap or scroll.

                    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

                      Yes, my code snipped won't give you the entities' relationships to each other. Then you have to travel the tree. I'm working on a plugin at the moment that does that. But group and component transformations gives me headache...

                      If you need a flat list of all entities, and only those used in the model, this should work:

                      
                      def all_entities_in_model
                      	model = Sketchup.active_model
                      	# All loose geometry
                      	model.entities.each { |e|
                      		yield e
                      	}
                      	# All group/component geometry
                      	model.definitions.each { |definition|
                      		if definition.count_instances > 0
                      			definition.entities.each { |e|
                      				yield e
                      			}
                      		end
                      	}
                      end
                      
                      

                      And this method should yield a list of all instances used. (excludes group instances)

                      
                      def all_used_instances
                      	model = Sketchup.active_model
                      	# All used component instances
                      	model.definitions.each { |definition|
                      		if !definition.group? && !definition.image? && definition.count_instances > 0
                      			definition.instances.each { |i|
                      				yield i
                      			}
                      		end
                      	}
                      end
                      
                      

                      Usage:

                      all_used_instances { |i| i.material = 'red' }
                      

                      If you want it as an array:

                      
                      def get_all_used_instances
                      	model = Sketchup.active_model
                      	used_instances = []
                      	# All used component instances
                      	model.definitions.each { |definition|
                      		if !definition.group? && !definition.image? && definition.count_instances > 0
                      			used_instances += definition.instances
                      		end
                      	}
                      	return used_instances
                      end
                      
                      
                      my_array = get_all_used_instances
                      

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

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        azuby
                        last edited by

                        @jim said:

                        Thomas, what if a definition exists in the DefinitionList but does not have an instance in the model? Do you still want its entities? I guess it depends of what the use of the collection is.
                        Use ComponentDefinition#instances:

                        Sketchup.active_model.definitions.each do |d|
                          if inss = d.instances # line is correct!
                            inss.each do |ins|
                              # ...
                            end
                          end
                        end
                        

                        azuby

                        *error initus :: Blocks | CurrentDate | d/Code | extensionmanager | FFlipper | HideEdges | MeasuredArea | ModelHistory | PluginsHelp | PronButton | SAWSO | SCP | SU²CATT

                        Bad English? PM me, correct me. :smile:**

                        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