
Wed, 05/24/2023 - 11:36
Forums:
Hi,
I have a collection of faces that intersect each other. All together they enclose a closed 3D shape (in this example it's a cube). Please see the attatched image for better understanding.
I have 2 questions:
Is there an efficient way to extract a solid of the shape they enclose?
If there was a hole (the shape the faces enclose is not fully closed), is there a way to cap this hole and extract a solid anyways?
Here is the pythonOCC script i wrote to solve the first problem, but i'm affraid it's not very efficient. For second problem i have no idea.
import numpy as np
from OCC.Core.gp import gp_Pnt
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace, BRepBuilderAPI_MakePolygon, BRepBuilderAPI_MakeSolid
from OCC.Extend.TopologyUtils import TopologyExplorer
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Sewing
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Fuse
# function to exclude all faces, that do not form a closed shell
def discardSticikingOutFaces(compound):
innerFaces = []
for face in TopologyExplorer(compound, ignore_orientation=True).faces():
add = True
for edge in TopologyExplorer(face, ignore_orientation=True).edges():
count = 0
for edgeFace in TopologyExplorer(compound, ignore_orientation=True).faces_from_edge(edge):
count += 1
#if any edge of a face touches only 1 face, then this face is not part of a closed shell
if count < 2:
add = False
break
if add:
#if all edges of a face touche at least 2 faces, then this face is part of a closed shell
innerFaces.append(face)
return innerFaces
#Create a list of vertices of faces (6 facees x 5 vertices)
verticesAll = [[[2.0, -1, 2],
[2, -1, -2],
[-2, -1, -2],
[-2, -1, 2],
[2, -1, 2],],
[ [1, 2, 2],
[1, 2, -2],
[1, -2, -2],
[1, -2, 2],
[1, 2, 2],],
[[-2, 1, 2],
[-2, 1, -2],
[2, 1, -2],
[2, 1, 2],
[-2, 1, 2],],
[ [-1, 2, 2],
[-1, 2, -2],
[-1, -2, -2],
[-1, -2, 2],
[-1, 2, 2],],
[[-2, 2, -1],
[-2, -2, -1],
[2, -2, -1],
[2, 2, -1],
[-2, 2, -1],],
[ [-2, 2, 1],
[-2, -2, 1],
[2, -2, 1],
[2, 2, 1],
[-2, 2, 1],]
]
verticesAll = np.array(verticesAll)
#create face objects from vertices
faces = []
for vertices in verticesAll:
# Create a list of gp_Pnt objects from the vertices
pnt_list = [gp_Pnt(*vertex) for vertex in vertices]
# Create a BRepBuilderAPI_MakePolygon object and add the vertices to it
polygon_builder = BRepBuilderAPI_MakePolygon()
for pnt in pnt_list:
polygon_builder.Add(pnt)
# Create a BRepBuilderAPI_MakeFace object and set the polygon as its argument
polygon = polygon_builder.Shape()
face_builder = BRepBuilderAPI_MakeFace(polygon)
# Use the face in the minimal distance calculation as before
face1 = face_builder.Face()
faces.append(face1)
#fuse faces together
fused_shape = faces[0]
for shape in faces[1:]:
fused_shape = BRepAlgoAPI_Fuse(fused_shape, shape).Shape()
#discard faces, that stick out and don't form a closed shell
shellFaces = discardSticikingOutFaces(fused_shape)
#sow the rest of faces together
sewing = BRepBuilderAPI_Sewing()
for face in shellFaces:
sewing.Add(face)
sewing.Perform()
sewed_shape = sewing.SewedShape()
#turn it into solid
solid = BRepBuilderAPI_MakeSolid(sewed_shape)
solid = solid.Solid()
Attachments:
Wed, 05/24/2023 - 22:33
For the 1st problem, use BOPAlgo_MakerVolume.
For the 2nd, nothing unless you have created a cover by yourself.
Fri, 05/26/2023 - 09:57
Thanks! BOPAlgo_MakerVolume worked.
If anyone has any idea, how i could go arround second problem, any suggestions are highly apreciated!