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

    Triangulate triangles ?

    Scheduled Pinned Locked Moved Plugins
    41 Posts 8 Posters 8.2k 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.
    • renderizaR Offline
      renderiza
      last edited by

      It works! 😍

      http://s9.postimg.org/3oou2krkf/yay.jpg

      Here is the code...

      
      model = Sketchup.active_model
      ents = model.active_entities
      sel = model.selection
      faces = sel.grep(Sketchup;;Face)
      
      model.start_operation('csplit')
        faces.each do |face|
          v = face.vertices
      
          cx = v[0].position.x + v[1].position.x + v[2].position.x
          cy = v[0].position.y + v[1].position.y + v[2].position.y   
          cz = v[0].position.z + v[1].position.z + v[2].position.z 
      
          px = cx / 3
          py = cy / 3 
          pz = cz / 3 
      
          v.each do |vertex|
            p1 = [px, py, pz]
            p2 = vertex
            line = ents.add_line p1,p2
          end
        end
        sel.clear
      model.commit_operation
      

      Note: At the moment the above code works with triangles only.

      For squares we can use the code bellow...

      
      model = Sketchup.active_model
      ents = model.active_entities
      sel = model.selection
      faces = sel.grep(Sketchup;;Face)
      
      model.start_operation('csplit')
        faces.each do |face|
          v = face.vertices
      
          cx = v[0].position.x + v[1].position.x + v[2].position.x + v[3].position.x
          cy = v[0].position.y + v[1].position.y + v[2].position.y + v[3].position.y  
          cz = v[0].position.z + v[1].position.z + v[2].position.z + v[3].position.z 
      
          px = cx / 4
          py = cy / 4 
          pz = cz / 4 
      
          v.each do |vertex|
            p1 = [px, py, pz]
            p2 = vertex
            line = ents.add_line p1,p2
          end
        end
        sel.clear
      model.commit_operation
      

      [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

      1 Reply Last reply Reply Quote 0
      • renderizaR Offline
        renderiza
        last edited by

        The following code will work on all geometry types like triangle, square, ect...

        model = Sketchup.active_model
        ents = model.active_entities
        sel = model.selection
        faces = sel.grep(Sketchup;;Face)
        @addx = []
        @addy = []
        @addz = []
        
        model.start_operation('csplit')
          faces.each do |face|
            @addx.clear
            @addy.clear
            @addz.clear
        
            v = face.vertices
            num = v.length
        
            v.each_with_index {|vert,i|
              @addx << " + v[#{i}].position.x"
              @addy << " + v[#{i}].position.y"
              @addz << " + v[#{i}].position.z"
            }
        
            ax = @addx * ","
            sumx = ax.delete ","
        
            ay = @addy * ","
            sumy = ay.delete ","
        
            az = @addz * ","
            sumz = az.delete ","
        
            cx = eval("#{sumx}")
            cy = eval("#{sumy}")
            cz = eval("#{sumz}")
        
            px = cx / num
            py = cy / num
            pz = cz / num
        
            v.each do |vertex|
              p1 = [px, py, pz]
              p2 = vertex
              line = ents.add_line p1,p2
            end
        
          end
          sel.clear
        model.commit_operation
        
        

        Note: To test code above you can copy & paste it to one of this ruby consoles...
        http://renderizastudio.blogspot.com/2013/06/top-ruby-consoles-for-sketchup.html

        [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

        1 Reply Last reply Reply Quote 0
        • renderizaR Offline
          renderiza
          last edited by

          With the help of Dan Rathbun the code was improved! Because string manipulation in Ruby can be slow he figured a way to do it without strings.

          Here is the new code...

            def csplit()
              #
              model = Sketchup.active_model
              ents = model.active_entities
              sel = model.selection
              faces = sel.grep(Sketchup;;Face)
              #
              model.start_operation('csplit')
                #
                faces.each do |face|
                  #
                  sum = Geom;;Point3d.new(0,0,0)
                  #
                  verts = face.vertices
                  n = verts.size.to_f
                  # use API Geom;;Point3d.+ instance method;
                  verts.each {|v| sum += ORIGIN.vector_to(v.position) }
                  #
                  avg = Geom;;Point3d.new( sum.x/n, sum.y/n, sum.z/n )
                  #
                  verts.each {|v| ents.add_line( avg, v ) }
                  #
                end # each face
                #
              model.commit_operation
              #
            rescue => e
              #
              puts "Error <#{e.class.name}; #{e.message} >"
              puts e.backtrace if $VERBOSE
              #
              model.abort_operation
              #
            else # when no errors;
              #
              sel.clear
              #
            end # csplit()
          
            csplit()
          
          

          If you have the time make sure to thank Dan...Cheers!

          [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

          1 Reply Last reply Reply Quote 0
          • pilouP Offline
            pilou
            last edited by

            Seems very cool! πŸ‘
            Waiting the definitive little plugin more easy than a copy past inside the ruby console! πŸ˜‰

            And you will your name in the index Plugin automatic list by Jim! 😎

            Frenchy Pilou
            Is beautiful that please without concept!
            My Little site :)

            1 Reply Last reply Reply Quote 0
            • renderizaR Offline
              renderiza
              last edited by

              Ok I will start working on that ASAP!!!

              By the way you like the name "FaceSplit"?

              [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

              1 Reply Last reply Reply Quote 0
              • pilouP Offline
                pilou
                last edited by

                "Bermuda's Triangles' Gravity" will be also funny! πŸ˜‰

                https://encrypted-tbn0.gstatic.com/images?q=tbn;ANd9GcQVDrKcLEC7T4VaspbQcQc_3tRmnEFfnCvFR7lAKksR_Dcd6PSkIQ

                Frenchy Pilou
                Is beautiful that please without concept!
                My Little site :)

                1 Reply Last reply Reply Quote 0
                • renderizaR Offline
                  renderiza
                  last edited by

                  I am glad you liked "FaceSplit"! 🀣

                  [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

                  1 Reply Last reply Reply Quote 0
                  • renderizaR Offline
                    renderiza
                    last edited by

                    Here is the logo...

                    Big:

                    http://s8.postimg.org/f4mis2579/Face_Split.png

                    Small:

                    http://s14.postimg.org/4n72xxfrh/Face_Split54.png

                    [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

                    1 Reply Last reply Reply Quote 0
                    • renderizaR Offline
                      renderiza
                      last edited by

                      Here is the plugin main page... http://sketchucation.com/forums/viewtopic.php?p=495689#p495689

                      I used FaceSplit as name but if you guys come up with better name that is short & catchy then it will be easy to change.'

                      Cheers! πŸ‘

                      [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

                      1 Reply Last reply Reply Quote 0
                      • pilouP Offline
                        pilou
                        last edited by

                        Bravo!
                        Seems fabulous after this studious working weekend! 😎

                        Frenchy Pilou
                        Is beautiful that please without concept!
                        My Little site :)

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

                        Advertisement