File size: 1,026 Bytes
fd4e6a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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}