sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Best way to get all Face's in model?

    Scheduled Pinned Locked Moved Developers' Forum
    25 Posts 5 Posters 1.9k 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.
    • thomthomT Offline
      thomthom
      last edited by

      
      model = Sketchup.active_model
      faces = []
      faces.concat( model.entities.select{|e| e.is_a?( Sketchup;;Face ) }
      model.definitions.each { |d|
        next if d.image?
        faces.concat( d.entities.select{|e| e.is_a?( Sketchup;;Face ) }
      }
      
      

      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
        draftomatic
        last edited by

        Thanks thomthom!

        1 Reply Last reply Reply Quote 0
        • E Offline
          exvion
          last edited by

          thomthom, I run your code on Sketchup 7.1:

          (eval);508; compile error
          (eval);505; syntax error, unexpected tIDENTIFIER, expecting ')'
          model.definitions.each { |d|
               ^
          (eval);508; syntax error, unexpected '}', expecting ')'<<< Process finished.
          
          
          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            Ther was a typo [a missing ')'...]

                model = Sketchup.active_model
                faces = []
                faces.concat( model.entities.select{|e| e.is_a?( Sketchup;;Face ) }
                model.definitions.each { |d|
                  next if d.image?
                  faces.concat( d.entities.select{|e| e.is_a?( Sketchup;;Face ) } )
                }
            

            TIG

            1 Reply Last reply Reply Quote 0
            • E Offline
              exvion
              last edited by

              This code work:

               model = Sketchup.active_model
                  faces = []
                  faces.concat( model.entities.select{|e| e.is_a?( Sketchup;;Face ) }    )
                  model.definitions.each { |d|
                    next if d.image?
                    faces.concat( d.entities.select{|e| e.is_a?( Sketchup;;Face ) } )
                  }
              
              1 Reply Last reply Reply Quote 0
              • D Offline
                draftomatic
                last edited by

                @exvion said:

                This code work:

                 model = Sketchup.active_model
                >     faces = []
                >     faces.concat( model.entities.select{|e| e.is_a?( Sketchup;;Face ) }    )
                >     model.definitions.each { |d|
                >       next if d.image?
                >       faces.concat( d.entities.select{|e| e.is_a?( Sketchup;;Face ) } )
                >     }
                

                It doesn't seem to be working for me. I put it as a method called get_face_array() inside a class called Tester:
                face_count_diff.JPG

                This is a 68.2MB model.

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

                  faces = [] faces.concat( model.entities.select{|e| e.is_a?( Sketchup::Face ) } )

                  Could be simplified to:

                  faces = model.entities.select{|e| e.is_a?( Sketchup::Face ) }

                  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

                    @draftomatic said:

                    It doesn't seem to be working for me. I put it as a method called get_face_array() inside a class called Tester:

                    It doesn't return the correct number because the code doesn't take into account instances.

                    <span class="syntaxdefault">def number_of_faces<br />  model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />  faces </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}.</span><span class="syntaxdefault">length<br />  model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">    next if d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">image</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault">    next if d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">count_instances </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> 0<br />    faces </span><span class="syntaxkeyword">+=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">count_instances </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">end</span>
                    

                    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

                      Or - even easier if you can settle with SU7.1 or higher:

                      model.number_faces

                      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

                        Oh, wait - you actually need arrays of all the face entities?

                        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
                          draftomatic
                          last edited by

                          @thomthom said:

                          Oh, wait - you actually need arrays of all the face entities?

                          Well, I really just needed to iterate through all the Faces in the model... the array thing is just useful for testing.

                          Bah, I should've realized the instances issue... So your code in the 2nd post isn't quite right 😃

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

                            @draftomatic said:

                            Bah, I should've realized the instances issue... So your code in the 2nd post isn't quite right 😃

                            It depends what you do with it...
                            If you need to count the faces you need to take into account all instances.

                            But if you need to just iterate over all faces you only need to process model.entities and the entities of each definition, even if a definition has multiple instances, a face in that definition is the same in each instance. It just exist in multiple locations.

                            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
                              draftomatic
                              last edited by

                              @thomthom said:

                              @draftomatic said:

                              Bah, I should've realized the instances issue... So your code in the 2nd post isn't quite right 😃

                              It depends what you do with it...
                              If you need to count the faces you need to take into account all instances.

                              But if you need to just iterate over all faces you only need to process model.entities and the entities of each definition, even if a definition has multiple instances, a face in that definition is the same in each instance. It just exist in multiple locations.

                              As you saw a few minutes ago, I'm writing an exporter, and it doesn't keep Component information. I need every Face.

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

                                Closer, but still not working:

                                face_count_diff2.JPG

                                Here's the get_face_count() method I'm using:

                                  def get_face_count()
                                    count = 0
                                    @model.entities.each { |e|
                                      if (e.is_a?(Sketchup;;Face))
                                        count +=1
                                      end
                                    }
                                    
                                    @model.definitions.each { |d|
                                      next if d.image?
                                      next if d.count_instances == 0
                                      count += d.entities.select { |e|
                                        e.is_a?(Sketchup;;Face)
                                      }.length * d.count_instances
                                    }
                                    return count
                                  end
                                
                                1 Reply Last reply Reply Quote 0
                                • thomthomT Offline
                                  thomthom
                                  last edited by

                                  hm... instances inside other instance might throw the count of...

                                  I think you need to traverse the model in a tree like structure

                                  <span class="syntaxdefault"><br />def&nbsp;process_model<br />&nbsp;&nbsp;walk_faces</span><span class="syntaxkeyword">(&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model&nbsp;</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /><br />def&nbsp;walk_faces</span><span class="syntaxkeyword">(&nbsp;</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">transformation&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.new&nbsp;)<br />&nbsp;&nbsp;</span><span class="syntaxdefault">faces&nbsp;</span><span class="syntaxkeyword">=&nbsp;[]<br />&nbsp;&nbsp;for&nbsp;</span><span class="syntaxdefault">e&nbsp;in&nbsp;entities<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">if&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face&nbsp;</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">faces&nbsp;</span><span class="syntaxkeyword"><<&nbsp;</span><span class="syntaxdefault">e<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">elseif&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group&nbsp;</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">faces</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">concat</span><span class="syntaxkeyword">(&nbsp;</span><span class="syntaxdefault">walk_faces</span><span class="syntaxkeyword">(&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">transformation&nbsp;</span><span class="syntaxkeyword">*&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation&nbsp;</span><span class="syntaxkeyword">)&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;elseif&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance&nbsp;</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">faces</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">concat</span><span class="syntaxkeyword">(&nbsp;</span><span class="syntaxdefault">walk_faces</span><span class="syntaxkeyword">(&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definition</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">transformation&nbsp;</span><span class="syntaxkeyword">*&nbsp;</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation&nbsp;</span><span class="syntaxkeyword">)&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">end<br />&nbsp;&nbsp;end<br />&nbsp;&nbsp;faces<br />end<br /></span>
                                  

                                  (Untested - but something like this)

                                  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
                                    draftomatic
                                    last edited by

                                    😍 😍 😍 😍

                                    face_count_diff_works.JPG

                                    Your code had a few lil typos; this one works:

                                      def process_model()
                                        return walk_faces( Sketchup.active_model.entities )
                                      end
                                    
                                      def walk_faces( entities, transformation = Geom;;Transformation.new )
                                        faces = []
                                        entities.each { |e|
                                          if e.is_a?( Sketchup;;Face )
                                            faces << e
                                          elsif e.is_a?( Sketchup;;Group )
                                            faces.concat( walk_faces( e.entities, transformation * e.transformation ) )
                                          elsif e.is_a?( Sketchup;;ComponentInstance )
                                            faces.concat( walk_faces( e.definition.entities, transformation * e.transformation ) )
                                          end
                                        }
                                        return faces
                                      end
                                    

                                    Thanks Tom, you're the best!

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

                                      There we go - got there in the end! 😄

                                      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
                                        draftomatic
                                        last edited by

                                        One more question - I see you're messing with a transformation - if I ask a nested instance for, say, it's Face's vertices locations (assuming it's just a rectangle or something simple), will it give me the (globally) correct coordinates, or something relative to the Component's origin?

                                        I guess ComponentDefinitions have entities, so I can ask for their locations. But ComponentInstances are transformed somewhere... I don't have much experience with transformations...

                                        EDIT: Ah nevermind, I see now that you're carrying the transformation through each nesting of an instance. Gotcha.

                                        Thanks again Tom

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

                                          😉

                                          Had a feeling the transformation would be needed.

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

                                          1 Reply Last reply Reply Quote 0
                                          • S Offline
                                            soldatino
                                            last edited by

                                            I had the same problem, but I need to get the "material" of each face.
                                            So I had a lot of problems because the "material" is hereditary if there is (or not) in the groups of faces (or nested groups). So it is really difficult to get the tree of the materials of all the faces.
                                            So I wrote this inelegant recursive sub, and it works fine for me.

                                            def pov_ex()
                                               model=Sketchup.active_model 
                                               entities=model.entities
                                               entities.each do |entity|
                                                    if entity.typename=="ComponentInstance"
                                                        entity.explode
                                                        pov_ex()
                                                    elsif entity.typename=="Group"
                                                        entity.explode
                                                        pov_ex()
                                                    end
                                               end
                                            end
                                            

                                            at the end, after material's extraction, I wrote a "model.abort_operation" so all the entities return to original state and are not corrupted by explode.
                                            But a question... this code works really slow, I'd like to find a better way....

                                            r3nDer tools

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

                                            Advertisement