Merging two or more faces
-
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)
-
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 doface = faces[0]
NOTE: if you want to do something to a face like assign a material to it BEFORE usinggroup.explode
, in case when exploded it too merges with other existing geometry outside of the group context... -
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... -
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.
-
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
enddef 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
-
Works for me.
-
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.
-
@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.
Advertisement