jimy-byerley / pymadcad

Simple yet powerful CAD (Computer Aided Design) library, written with Python.
https://madcad.netlify.app/
GNU Lesser General Public License v3.0
208 stars 16 forks source link

Boolean Operation cut_mesh #24

Closed mia0120 closed 2 years ago

mia0120 commented 2 years ago

I need a rectangular flat surface with a hole. First I create a rectangular surface with a flatsurface command and a disk with Circle command. Then using cut_mesh, I try to empty first rectangular surface. But cut_mesh command turns the part to a tuple object. How can I create this flat surface? image

jimy-byerley commented 2 years ago

That's because cut_mesh role is only to cuts the mesh faces, but not to remove the faces inside the holes (you can see the result below)

result, frontier = cut_mesh(rectangle, cylinder)

image

Solution with a boolean operation

pierce is the boolean operation that does what you want

rectangle = flatsurface(wire([   # rectangular surface from a rectangular outline Wire
    vec3(-1,-1,0), 
    vec3(1,-1,0), 
    vec3(1,1,0),
    vec3(-1,1,0),
    ]))
cylinder = extrusion(Z, Circle((O,Z), 0.5))   # note that the cylinder face normals are oriented outside (the surface color is bright) so the faces to remove are inside the cylinder

result = pierce(rectangle, cylinder)
show([result], options={'display_wire':True})

image

Solution with contours

However, the boolean operations are quite expensive in term of performance for any mesh, and to use is for simple planar operations like this can be avoided in this case. You can directly use flatsurface with the appropriate Web

rectangle = wire([   # rectangular Wire
        vec3(-1,-1,0), 
        vec3(1,-1,0), 
        vec3(1,1,0),
        vec3(-1,1,0),
        ]) .close()
circle = Circle((O,-Z), 0.5)   # note that the circle axis is flipped (Z is negative) to ensure that the contour winding is consistent. This is the equivalent of the face normals in the previous solution

result = flatsurface(web([   # generate a surface by triangulating the given loops
    rectangle,
    circle,
    ]))
show([result], options={'display_wire':True})

This will give the exact same result

Hope this help !

jimy-byerley commented 2 years ago

Nonetheless I agree that the boolean operation docs are lacking nice screenshots to explain this ;)