sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Merging two or more faces

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 4 Posters 1.8k Views 4 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
      simsimzone
      last edited by

      Dear guys, I have two questions:
      1- when I add faces using ent.add_face, and supposing their geometry are intersected,then they won't intersect. I'v noticed this by drawing intersected lines too, seems I'm missing some kind of lastly operation.

      2- suppose question 1 is unsolved, how to merge these two faces together to become one face instead.

      ` ent = Sketchup.active_model.entities
      face1 = ent.add_face([0, 0, 0], [20, 0, 0], [20, 20, 0], [0, 20, 0])
      face2 = ent.add_face([5, -5, 0], [10, -5, 0], [10, 25, 0], [5, 25, 0])`
      

      I want to call some function say:
      face3 = merge(face1, face2)

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

        Place your new stuff inside a group - albeit momentarily.

        ents = Sketchup.active_model.active_entities
        group = ents.add_group()
        gents = group.entities
        gents.add_face([0, 0, 0], [20, 0, 0], [20, 20, 0], [0, 20, 0])
        gents.add_face([5, -5, 0], [10, -5, 0], [10, 25, 0], [5, 25, 0])
        

        Now intersect.

        tr = Geom;;Transformation.new()
        (gents.grep(Sketchup;;Face).length).times{gents.intersect_with(false, tr, gents, tr, false, gents.to_a)}
        

        Now the two [or more] faces will have intersected.

        However. your issue will be finding the new set of faces etc...
        If you want to merge into just the one face then use:

        gents.grep(Sketchup;;Edge).each{|e| e.erase! if e.faces.length > 1}
        

        To then find the face[s] use...

        faces = gents.grep(Sketchup;;Face)
        

        You now have an array of the combined face**s**.
        There might be more that one face after the intersection, e.g. if the faces do NOT intersect !!
        But if they do face = faces[0]
        NOTE: if you want to do something to a face like assign a material to it BEFORE using group.explode, in case when exploded it too merges with other existing geometry outside of the group context... πŸ€“

        TIG

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

          Dear Mr. TIG:
          I'v already tried this, but everytime I run the script, it shows me a different result, sometimes right and sometimes wrong intersection.
          I really don't know why...

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

            I'v tried this: πŸ˜‰

            ` model = Sketchup.active_model
            ent = model.entities
            sel = model.selection
            g1 = ent.add_group()
            g2 = g1.entities.add_group()
            face1 = g2.entities.add_face(
            	[0, 0, 0], [20, 0, 0], [20, 20, 0], [0, 20, 0])
            face2 = g2.entities.add_face(
            	[5, -5, 0], [10, -5, 0], [10, 25, 0], [5, 25, 0])
            g2.explode()
            g1.entities.grep(Sketchup::Edge).each do |e|
            	e.erase! if e.faces.length > 1
            end
            g1.explode()`
            

            Here I put my faces in a group which is also inside another group, then exploded the inner group which did the trick and then did my logic to remove the unwanted edges, then arbitrarily exploded the outer group.

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

              dears, I came up with simple method to merge faces:

              ` #f1,f2 are the input faces
              #returns the final merged face
              def union_faces (f1,f2)
              #return nil if !f1.coplanar_to?(f2)
              ent = Sketchup.active_model.entities
              g2 = ent.add_group([f1, f2])
              g1 = ent.add_group(g2)
              g2.explode()
              g1.entities.grep(Sketchup::Edge).each do |e|
              e.erase! if e.valid? && e.faces.length > 1
              end
              faces = g1.entities.grep(Sketchup::Face)
              ret = faces.first
              g1.explode()
              return ret
              end

              def test
              model = Sketchup.active_model
              ent = model.entities
              sel = model.selection
              face1 = ent.add_face([0, 0, 0], [1000, 0, 0], [1000, 200, 0], [0, 200, 0])
              face2 = ent.add_face([1000, 0, 0], [1200, 0, 0], [1200, 1000, 0], [1000, 1000, 0])
              union1 = union_faces(face1, face2)
              face3 = ent.add_face([0, 1000, 0], [1200, 1000, 0], [1200, 1200, 0], [0, 1200, 0])
              union2 = union_faces(union1, face3)
              face4 = ent.add_face([0, 200, 0], [200, 200, 0], [200, 1000, 0], [0, 1000, 0])
              union3 = union_faces(union2, face4)
              face5 = ent.add_face([1500, -1000, 0], [1700, -1000, 0], [1700, 2000, 0], [1500, 2000, 0])
              union4 = union_faces(union3, face5)
              sel.add(union4)
              end
              
              test()`
              

              I think Group.explode will heal and intersect with the parent.

              Please check my method and reply.. I didn't sleep last night 😲

              1 Reply Last reply Reply Quote 0
              • sdmitchS Offline
                sdmitch
                last edited by

                Works for me.

                Nothing is worthless, it can always be used as a bad example.

                http://sdmitch.blogspot.com/

                1 Reply Last reply Reply Quote 0
                • tt_suT Offline
                  tt_su
                  last edited by

                  Grouping and exploding will be very slow. And add_group with existing entities has been prone to erros and crashes.

                  In my CleanUp extension I merge coplanar faces and all I do is erase the shared edges between coplanar faces. When you do one of the faces is erased and one remains. So if you need a handle to the new face just check them after you erase the edges.

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

                    @tt_su said:

                    Grouping and exploding will be very slow. And add_group with existing entities has been prone to erros and crashes.

                    But Mr.tt-su, when I use entities.intersect_with it doesn't work, can you explain why?
                    After all I'm using grouping just once because my plugin I'm creating right now is dealing with just two faces, not like the sample I gave.

                    [EDIT]
                    I'v tried your plugin and it seems it works only for a ready intersected in the model faces,
                    if I have two faces say from my sample face1 and face2, then your plugin can't work on them unless they are intersected in the model first, if you draw two simple intersected rectangles in sketchup and say selected them, then you plugin can work, hope you get my point.

                    My problem till now is that I have two intersected faces but they are not intersected with the model and I want to merge them.

                    one option gave by TIG to use entities.intersect_with method but failed with me, I don't know exactly the arguments of this function.

                    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