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

    Best way to get all Face's in model?

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

        @soldatino said:

        But a question... this code works really slow, I'd like to find a better way....

        There are two reasons your code is slow:

        1. You're comparing strings a lot. Don't use .typename to check for entity type. It's very slow. ( See more performance tips here: http://forums.sketchucation.com/viewtopic.php?f=180&t=25305#p217180 )
          Instead, test for its class: e.is_a?( Sketchup::Group )

        2. Exploding is very slow in SU. ( and if your script should fail at some point and halt - you're left with a broken model. )

        I have a plugin that transfer the group/component materials onto the faces with default materials - here's an extract from that plugin:

        <span class="syntaxdefault"><br />  def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instance_materials_to_faces<br />    model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />    model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Instance Material to Faces'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> true</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instance_material_to_faces</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> nil </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br />  end<br />  <br />  def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instance_material_to_faces</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> entities</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> material </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    for e in entities<br />      if TT</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Instance</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault"> e </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">        temp_material </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> material<br />        d </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> TT</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Instance</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definition</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> e </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">        self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instance_material_to_faces</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"> temp_material </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Recursive call<br /></span><span class="syntaxdefault">        e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br />      elsif 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">)<br /></span><span class="syntaxdefault">        e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> material if e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault">        e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">back_material </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> material if e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">back_material</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault">      end<br />    end<br />  end<br /></span>
        

        Note that I have to custom methods here, not explained:
        TT::Instance.is?( e ) returns true if the entity is a Group or ComponentInstance
        TT::Instance.definition( e ) return the definition of a Group or ComponentInstance

        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

          Thanks for your responce!
          Yes, teorically the strings compare is ever slow ... but here the difference is irrelevant more than in other languages, I checked a large file, and the part of the scanning and explode sub is the 99.99% of the working code of my parser.

          if entity.is_a?(Sketchup::ComponentInstance)
          and groups ...
          start 13:49:42 -> end 14:08:26

          if entity.typename=="ComponentInstance"
          and groups ..
          start 14:11:30 -> end 14:29:07

          In the second reason (explode is slow insted of get from material list) you are right 100%, but it is a bit complicated for me at this time (I am new about skup database).... but next time I must to improve at...
          I was hoping that there are something like these easy :
          group_parent.face .... group_parent.group ...
          but the only parent seems to be the entire list of materials...
          Thanks again, I apologize for my bad english.

          r3nDer tools

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

            @soldatino said:

            Yes, teorically the strings compare is ever slow ... but here the difference is irrelevant more than in other languages, I checked a large file, and the part of the scanning and explode sub is the 99.99% of the working code of my parser.

            It's a significant difference - check out the test data in this thread: http://forums.sketchucation.com/viewtopic.php?f=323&t=19576&st=0&sk=t&sd=a&start=15#p166698

            @unknownuser said:

            is_a? - 16.297c
            kind_of? - 16.141c
            class - 18.703c
            typename - 88.703c

            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

              @thomthom said:

              ............
              2. Exploding is very slow in SU. ( and if your script should fail at some point and halt - you're left with a broken model. )

              ok! I changed my way and it seems work alot faster! (for my plugin converter 2 POV-Ray script).
              note: At this time I get only the color of the parent object(s) and not texture...

               def pov_find_faces(entities,tform,layername)
                entities.each do |entity|
                  if entity.is_a?(Sketchup;;Face)
                    pov_write_face(entity, tform)
                  elsif entity.is_a?(Sketchup;;Group)
                    if entity.material
                      if entity.material.color
                        $levelcolor[$level]=entity.material.color
                      else
                        $levelcolor[$level]=$levelcolor[$level-1]
                      end
                      if entity.material.alpha
                        $levelalpha[$level]=entity.material.alpha.to_s
                      else
                        $levelalpha[$level]=$levelalpha[$level-1]
                      end
                    else
                      $levelcolor[$level]=$levelcolor[$level-1]
                      $levelalpha[$level]=$levelalpha[$level-1]
                    end
                    $level+=1
                    pov_find_faces(entity.entities,tform * entity.transformation,entity.name)
                  elsif entity.is_a?(Sketchup;;ComponentInstance)
                    if entity.material
                      if entity.material.color
                        $levelcolor[$level]=entity.material.color
                      else
                        $levelcolor[$level]=$levelcolor[$level-1]
                      end
                      if entity.material.alpha
                        $levelalpha[$level]=entity.material.alpha.to_s
                      else
                        $levelalpha[$level]=$levelalpha[$level-1]
                      end
                    else
                      $levelcolor[$level]=$levelcolor[$level-1]
                      $levelalpha[$level]=$levelalpha[$level-1]
                    end
                    $level+=1
                    pov_find_faces(entity.definition.entities,tform * entity.transformation,entity.name)
                  end  
                end
                $level=$level-1
                #UI.messagebox($level.to_s)
              end
              
              

              r3nDer tools

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

              Advertisement