stepfile_compare / app /utils /geometry_utils.py
xryv's picture
v1
fd4e6a2
Raw History Blame Contribute Delete
1.03 kB
from OCC.Core.BRep import BRep_Tool
from OCC.Core.BRepExtrema import BRepExtrema_DistShapeShape
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_VERTEX, TopAbs_EDGE
from OCC.Core.gp import gp_Pnt
def compute_centroid(shape):
"""Calculate the centroid of a shape."""
extrema = BRepExtrema_DistShapeShape(shape, shape)
extrema.Perform()
if extrema.IsDone():
p1 = extrema.PointOnShape1(1)
return {"x": p1.X(), "y": p1.Y(), "z": p1.Z()}
return {"x": 0, "y": 0, "z": 0}
def count_features(shape):
"""Count specific geometric features like vertices and edges."""
vertex_count = 0
edge_count = 0
# Count vertices
explorer = TopExp_Explorer(shape, TopAbs_VERTEX)
while explorer.More():
vertex_count += 1
explorer.Next()
# Count edges
explorer = TopExp_Explorer(shape, TopAbs_EDGE)
while explorer.More():
edge_count += 1
explorer.Next()
return {"vertices": vertex_count, "edges": edge_count}