Example #1
0
 def number_solids(self, shape):
     count = 0
     e = TopExp_Explorer(shape, TopAbs_SOLID)
     while e.More():
         count += 1
         e.Next()
     return count
Example #2
0
def _get_topo(shape, topo):
    explorer = TopExp_Explorer(shape, topo)
    hashes = {}
    while explorer.More():
        item = explorer.Current()
        hash = item.HashCode(HASH_CODE_MAX)
        if hashes.get(hash) is None:
            hashes[hash] = True
            yield downcast(item)
        explorer.Next()
Example #3
0
def _get_topo(shape, topo):
    explorer = TopExp_Explorer(shape, topo)
    hashes = {}
    while explorer.More():
        item = explorer.Current()
        hash_value = item.HashCode(MAX_HASH_KEY)
        if hashes.get(hash_value) is None:
            hashes[hash_value] = True
            yield downcast(item)
        explorer.Next()
Example #4
0
def _objects(shape, shape_type):
    HASH_CODE_MAX = 2147483647
    out = {}  # using dict to prevent duplicates

    explorer = TopExp_Explorer(shape, shape_type)

    while explorer.More():
        item = explorer.Current()
        out[item.HashCode(HASH_CODE_MAX)] = downcast(item)
        explorer.Next()

    return list(out.values())
Example #5
0
def tessellate(shape, tolerance: float, angularTolerance: float = 0.1):

    # Remove previous mesh data
    BRepTools.Clean_s(shape)

    triangulated = BRepTools.Triangulation_s(shape, tolerance)
    if not triangulated:
        # this will add mesh data to the shape and prevent calculating an exact bounding box after this call
        cleanup = True
        BRepMesh_IncrementalMesh(shape, tolerance, True, angularTolerance)

    vertices = []
    triangles = []
    normals = []

    offset = 0

    explorer = TopExp_Explorer(shape, TopAbs_FACE)

    for face in get_faces(shape):
        loc = TopLoc_Location()
        poly = BRep_Tool.Triangulation_s(face, loc)
        Trsf = loc.Transformation()

        reverse = face.Orientation() == TopAbs_Orientation.TopAbs_REVERSED
        internal = face.Orientation() == TopAbs_Orientation.TopAbs_INTERNAL

        # add vertices
        vertices += [(v.X(), v.Y(), v.Z())
                     for v in (v.Transformed(Trsf) for v in poly.Nodes())]

        # add triangles
        triangles += [(
            t.Value(1) + offset - 1,
            t.Value(3 if reverse else 2) + offset - 1,
            t.Value(2 if reverse else 3) + offset - 1,
        ) for t in poly.Triangles()]

        # add normals
        if poly.HasUVNodes():
            prop = BRepGProp_Face(face)
            uvnodes = poly.UVNodes()
            for uvnode in uvnodes:
                p = gp_Pnt()
                n = gp_Vec()
                prop.Normal(uvnode.X(), uvnode.Y(), p, n)

                if n.SquareMagnitude() > 0:
                    n.Normalize()
                if internal:
                    n.Reverse()

                normals.append((n.X(), n.Y(), n.Z()))

        offset += poly.NbNodes()

    if not triangulated:
        # Remove the mesh data again
        BRepTools.Clean_s(face)

    return (
        np.asarray(vertices, dtype=np.float32),
        np.asarray(triangles, dtype=np.uint32),
        np.asarray(normals, dtype=np.float32),
    )