Example #1
0
def topo2topotype(occtopology):
    """
    This function converts the original OCCtopology of the given topology. e.g. an OCCcompound that is originally an OCCface etc.
 
    Parameters
    ----------
    occtopology : OCCtopology 
        The OCCtopology to be converted. 
        OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex 
        
    Returns
    -------
    original topology : OCCtopology
        The original OCCtopology of the input.
    """
    shapetype = occtopology.ShapeType()
    if shapetype == TopAbs_COMPOUND:#compound
        orig_topo = topods_Compound(occtopology)
    if shapetype == TopAbs_COMPSOLID:#compsolid
        orig_topo = topods_CompSolid(occtopology)
    if shapetype == TopAbs_SOLID:#solid
        orig_topo = topods_Solid(occtopology)
    if shapetype == TopAbs_SHELL:#shell
        orig_topo = topods_Shell(occtopology)
    if shapetype == TopAbs_FACE:#face
        orig_topo = topods_Face(occtopology)
    if shapetype == TopAbs_WIRE:#wire
        orig_topo = topods_Wire(occtopology)
    if shapetype == TopAbs_EDGE:#edge
        orig_topo = topods_Edge(occtopology)
    if shapetype == TopAbs_VERTEX:#vertex
        orig_topo = topods_Vertex(occtopology)
    return orig_topo
Example #2
0
def topo_explorer(occtopo2explore, topotype2find):
    """
    This function explores and fetches the specified topological type from the given OCCtopology.
    e.g. find a list of OCCfaces in an OCCcompound.
 
    Parameters
    ----------
    occtopo2explore : OCCtopology 
        The OCCtopology to be explored.
        OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex 
        
    topotype2find : str 
        The string describing the topology to find. 
        The strings can be e.g. "compound", "compsolid", "solid", "shell", "face", "wire", "edge", "vertex". 
        
    Returns
    -------
    list of topology : list of OCCtopology
        The list of OCCtopology found in the specified OCCtopology.
    """
    geom_list = []
    if topotype2find == "compound":
        shapetype2find_topABS = TopAbs_COMPOUND
    if topotype2find == "compsolid":
        shapetype2find_topABS = TopAbs_COMPSOLID
    if topotype2find == "solid":
        shapetype2find_topABS = TopAbs_SOLID
    if topotype2find == "shell":
        shapetype2find_topABS = TopAbs_SHELL
    if topotype2find == "face":
        shapetype2find_topABS = TopAbs_FACE
    if topotype2find == "wire":
        shapetype2find_topABS = TopAbs_WIRE
    if topotype2find == "edge":
        shapetype2find_topABS = TopAbs_EDGE
    if topotype2find == "vertex":
        shapetype2find_topABS = TopAbs_VERTEX
        
    ex = TopExp_Explorer(occtopo2explore, shapetype2find_topABS)
    while ex.More():
        if shapetype2find_topABS == 0:
            geom = topods_Compound(ex.Current())
        if shapetype2find_topABS == 1:
            geom = topods_CompSolid(ex.Current())
        if shapetype2find_topABS == 2:
            geom = topods_Solid(ex.Current())
        if shapetype2find_topABS == 3:
            geom = topods_Shell(ex.Current())
        if shapetype2find_topABS == 4:
            geom = topods_Face(ex.Current())
        if shapetype2find_topABS == 5:
            geom = topods_Wire(ex.Current())
        if shapetype2find_topABS == 6:
            geom = topods_Edge(ex.Current())
        if shapetype2find_topABS == 7:
            geom = topods_Vertex(ex.Current())
        geom_list.append(geom)
        ex.Next()
    return geom_list
def replace_splitted_faces(builder, shape, old_face, splitted_faces):
    """
    Try to create new shape that does not include old_face, but it
    includes instead splitted_faces
    """

    old_points = points_of_face(old_face)

    sewing = BRepBuilderAPI_Sewing(0.01, True, True, True, False)
    sewing.SetFloatingEdgesMode(True)

    # Get list of all faces in shape
    primitives = brep_explorer.shape_disassembly(shape)    

    obsolete_face_found = False

    # This loop tries to add original not-splitted faces to sewing
    for face_shape in primitives[4].values():
        new_points = points_of_face(face_shape)
        # Quick Python trick using sets: all points in new_points
        # has to be included in old_points.
        # Note: may be, it is not 100% reliable, but for all uses cases it just works.
        old_set = set(old_points)
        new_set = set(new_points)
        if not old_set <= new_set:
            old_face = topods_Face(face_shape)
            sewing.Add(old_face)
        else:
            obsolete_face_found = True

    # When no face for replacement was found, then
    # return origin unchanged shape
    if obsolete_face_found is not True:
        return shape

    # This loop adds splitted faces to sewing
    for face_shape in splitted_faces:
        old_face = topods_Face(face_shape)
        sewing.Add(old_face)

    # Sew all faces together
    sewing.Perform()
    sewing_shape = sewing.SewedShape()

    # When there is hole in sewing, then following function will be terminated with Error.
    # Do not use try-except to solve this problems! Fill hole(s) with face(s).
    shell = topods_Shell(sewing_shape)

    # Make solid from shell and return result
    make_solid = BRepBuilderAPI_MakeSolid()
    make_solid.Add(shell)
    solid = make_solid.Solid()
    builder.MakeSolid(solid)
    builder.Add(solid, shell)
    return solid
Example #4
0
def shape2shapetype(occ_shape):
    shapetype = occ_shape.ShapeType()
    if shapetype == TopAbs_COMPOUND:  #compound
        orig_topo = topods_Compound(occ_shape)
    if shapetype == TopAbs_COMPSOLID:  #compsolid
        orig_topo = topods_CompSolid(occ_shape)
    if shapetype == TopAbs_SOLID:  #solid
        orig_topo = topods_Solid(occ_shape)
    if shapetype == TopAbs_SHELL:  #shell
        orig_topo = topods_Shell(occ_shape)
    if shapetype == TopAbs_FACE:  #face
        orig_topo = topods_Face(occ_shape)
    if shapetype == TopAbs_WIRE:  #wire
        orig_topo = topods_Wire(occ_shape)
    if shapetype == TopAbs_EDGE:  #edge
        orig_topo = topods_Edge(occ_shape)
    if shapetype == TopAbs_VERTEX:  #vertex
        orig_topo = topods_Vertex(occ_shape)
    return orig_topo
Example #5
0
def geom_explorer(geom2explore, shapetype2find):
    geom_list = []
    if shapetype2find == "compound":
        shapetype2find_topABS = TopAbs_COMPOUND
    if shapetype2find == "compsolid":
        shapetype2find_topABS = TopAbs_COMPSOLID
    if shapetype2find == "solid":
        shapetype2find_topABS = TopAbs_SOLID
    if shapetype2find == "shell":
        shapetype2find_topABS = TopAbs_SHELL
    if shapetype2find == "face":
        shapetype2find_topABS = TopAbs_FACE
    if shapetype2find == "wire":
        shapetype2find_topABS = TopAbs_WIRE
    if shapetype2find == "edge":
        shapetype2find_topABS = TopAbs_EDGE
    if shapetype2find == "vertex":
        shapetype2find_topABS = TopAbs_VERTEX

    ex = TopExp_Explorer(geom2explore, shapetype2find_topABS)
    while ex.More():
        if shapetype2find_topABS == 0:
            geom = topods_Compound(ex.Current())
        if shapetype2find_topABS == 1:
            geom = topods_CompSolid(ex.Current())
        if shapetype2find_topABS == 2:
            geom = topods_Solid(ex.Current())
        if shapetype2find_topABS == 3:
            geom = topods_Shell(ex.Current())
        if shapetype2find_topABS == 4:
            geom = topods_Face(ex.Current())
        if shapetype2find_topABS == 5:
            geom = topods_Wire(ex.Current())
        if shapetype2find_topABS == 6:
            geom = topods_Edge(ex.Current())
        if shapetype2find_topABS == 7:
            geom = topods_Vertex(ex.Current())
        geom_list.append(geom)
        ex.Next()
    return geom_list
def make_box(builder, points):
    """
    Make box from 8 points
    """
    array1 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array1.SetValue(1, 1, points[0])
    array1.SetValue(1, 2, points[1])
    array1.SetValue(2, 1, points[3])
    array1.SetValue(2, 2, points[2])
    bspl_surf1 = create_bspline_surface(array1)

    array2 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array2.SetValue(1, 1, points[0])
    array2.SetValue(1, 2, points[4])
    array2.SetValue(2, 1, points[3])
    array2.SetValue(2, 2, points[7])
    bspl_surf2 = create_bspline_surface(array2)

    array3 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array3.SetValue(1, 1, points[0])
    array3.SetValue(1, 2, points[4])
    array3.SetValue(2, 1, points[1])
    array3.SetValue(2, 2, points[5])
    bspl_surf3 = create_bspline_surface(array3)

    array4 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array4.SetValue(1, 1, points[1])
    array4.SetValue(1, 2, points[5])
    array4.SetValue(2, 1, points[2])
    array4.SetValue(2, 2, points[6])
    bspl_surf4 = create_bspline_surface(array4)

    array5 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array5.SetValue(1, 1, points[2])
    array5.SetValue(1, 2, points[6])
    array5.SetValue(2, 1, points[3])
    array5.SetValue(2, 2, points[7])
    bspl_surf5 = create_bspline_surface(array5)

    array6 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array6.SetValue(1, 1, points[4])
    array6.SetValue(1, 2, points[5])
    array6.SetValue(2, 1, points[7])
    array6.SetValue(2, 2, points[6])
    bspl_surf6 = create_bspline_surface(array6)

    error = 1e-6
    face1 = BRepBuilderAPI_MakeFace(bspl_surf1.GetHandle(), error).Shape()
    face2 = BRepBuilderAPI_MakeFace(bspl_surf2.GetHandle(), error).Shape()
    face3 = BRepBuilderAPI_MakeFace(bspl_surf3.GetHandle(), error).Shape()
    face4 = BRepBuilderAPI_MakeFace(bspl_surf4.GetHandle(), error).Shape()
    face5 = BRepBuilderAPI_MakeFace(bspl_surf5.GetHandle(), error).Shape()
    face6 = BRepBuilderAPI_MakeFace(bspl_surf6.GetHandle(), error).Shape()

    sewing = BRepBuilderAPI_Sewing(0.01, True, True, True, False)
    sewing.SetFloatingEdgesMode(True)

    sewing.Add(face1)
    sewing.Add(face2)
    sewing.Add(face3)
    sewing.Add(face4)
    sewing.Add(face5)
    sewing.Add(face6)

    sewing.Perform()

    sewing_shape = sewing.SewedShape()

    shell = topods_Shell(sewing_shape)

    make_solid = BRepBuilderAPI_MakeSolid()
    make_solid.Add(shell)

    solid = make_solid.Solid()

    builder.MakeSolid(solid)
    builder.Add(solid, shell)

    return solid
Example #7
0
    def write_shape(self, l_shells, filename, tol):
        """
        Method to recreate a TopoDS_Shape associated to a geometric shape
        after the modification of points of each Face. It
        returns a TopoDS_Shape (Shape).

        :param l_shells: the list of shells after initial parsing
        :param filename: the output filename
        :param tol: tolerance on the surface creation after modification
        :return: None

        """
        self.outfile = filename
        # global compound containing multiple shells
        global_compound_builder = BRep_Builder()
        global_comp = TopoDS_Compound()
        global_compound_builder.MakeCompound(global_comp)

        if self.check_topo == 0:
            # cycle on shells (multiple objects)
            shape_shells_explorer = TopExp_Explorer(
                self.shape.Oriented(TopAbs_FORWARD), TopAbs_SHELL)
            ishell = 0

            while shape_shells_explorer.More():
                per_shell = topods_Shell(shape_shells_explorer.Current())
                # a local compound containing a shell
                compound_builder = BRep_Builder()
                comp = TopoDS_Compound()
                compound_builder.MakeCompound(comp)

                # cycle on faces
                faces_explorer = TopExp_Explorer(
                    per_shell.Oriented(TopAbs_FORWARD), TopAbs_FACE)
                iface = 0
                while faces_explorer.More():
                    topoface = topods.Face(faces_explorer.Current())
                    newface = self.write_face(l_shells[ishell][iface][0],
                                              l_shells[ishell][iface][1],
                                              topoface, tol)

                    # add face to compound
                    compound_builder.Add(comp, newface)
                    iface += 1
                    faces_explorer.Next()

                new_shell = self.combine_faces(comp, 0.01)
                itype = TopoDS_Shape.ShapeType(new_shell)
                # add the new shell to the global compound
                global_compound_builder.Add(global_comp, new_shell)

                # TODO
                #print("Shell {0} of type {1} Processed ".format(ishell, itype))
                #print "=============================================="

                ishell += 1
                shape_shells_explorer.Next()

        else:
            # cycle on faces
            # a local compound containing a shell
            compound_builder = BRep_Builder()
            comp = TopoDS_Compound()
            compound_builder.MakeCompound(comp)

            # cycle on faces
            faces_explorer = TopExp_Explorer(
                self.shape.Oriented(TopAbs_FORWARD), TopAbs_FACE)
            iface = 0
            while faces_explorer.More():
                topoface = topods.Face(faces_explorer.Current())
                newface = self.write_face(l_shells[0][iface][0],
                                          l_shells[0][iface][1], topoface, tol)

                # add face to compound
                compound_builder.Add(comp, newface)
                iface += 1
                faces_explorer.Next()

            new_shell = self.combine_faces(comp, 0.01)
            itype = TopoDS_Shape.ShapeType(new_shell)
            # add the new shell to the global compound
            global_compound_builder.Add(global_comp, new_shell)

            # TODO print to logging
            # print("Shell {0} of type {1} Processed ".format(0, itype))
            # print "=============================================="

        self.write_shape_to_file(global_comp, self.outfile)
Example #8
0
    def write_shape(self, l_shells, filename, tol):
        """
        Method to recreate a TopoDS_Shape associated to a geometric shape
        after the modification of points of each Face. It
        returns a TopoDS_Shape (Shape).

        :param l_shells: the list of shells after initial parsing
        :param filename: the output filename
        :param tol: tolerance on the surface creation after modification
        :return: None

        """
        self.outfile = filename
        # global compound containing multiple shells
        global_compound_builder = BRep_Builder()
        global_comp = TopoDS_Compound()
        global_compound_builder.MakeCompound(global_comp)

        if self.check_topo == 0:
            # cycle on shells (multiple objects)
            shape_shells_explorer = TopExp_Explorer(
                self.shape.Oriented(TopAbs_FORWARD), TopAbs_SHELL)
            ishell = 0

            while shape_shells_explorer.More():
                per_shell = topods_Shell(shape_shells_explorer.Current())
                # a local compound containing a shell
                compound_builder = BRep_Builder()
                comp = TopoDS_Compound()
                compound_builder.MakeCompound(comp)

                # cycle on faces
                faces_explorer = TopExp_Explorer(
                    per_shell.Oriented(TopAbs_FORWARD), TopAbs_FACE)
                iface = 0
                while faces_explorer.More():
                    topoface = topods.Face(faces_explorer.Current())
                    newface = self.write_face(l_shells[ishell][iface][0],
                                              l_shells[ishell][iface][1],
                                              topoface, tol)

                    # add face to compound
                    compound_builder.Add(comp, newface)
                    iface += 1
                    faces_explorer.Next()

                new_shell = self.combine_faces(comp, 0.01)
                itype = TopoDS_Shape.ShapeType(new_shell)
                # add the new shell to the global compound
                global_compound_builder.Add(global_comp, new_shell)

                print("Shell {0} of type {1} Processed ".format(ishell, itype))
                print "=============================================="

                ishell += 1
                shape_shells_explorer.Next()

        else:
            # cycle on faces
            # a local compound containing a shell
            compound_builder = BRep_Builder()
            comp = TopoDS_Compound()
            compound_builder.MakeCompound(comp)

            # cycle on faces
            faces_explorer = TopExp_Explorer(
                self.shape.Oriented(TopAbs_FORWARD), TopAbs_FACE)
            iface = 0
            while faces_explorer.More():
                topoface = topods.Face(faces_explorer.Current())
                newface = self.write_face(l_shells[0][iface][0],
                                          l_shells[0][iface][1], topoface, tol)

                # add face to compound
                compound_builder.Add(comp, newface)
                iface += 1
                faces_explorer.Next()

            new_shell = self.combine_faces(comp, 0.01)
            itype = TopoDS_Shape.ShapeType(new_shell)
            # add the new shell to the global compound
            global_compound_builder.Add(global_comp, new_shell)

            print("Shell {0} of type {1} Processed ".format(0, itype))
            print "=============================================="

        self.write_shape_to_file(global_comp, self.outfile)