def bezier_surfaces(filename=None, output=None):
    """
    Generate and display bspline surface
    """
    ret = False
    display.EraseAll()
    array1 = TColgp_Array2OfPnt(1, 4, 1, 4)

    array1.SetValue(1, 1, gp_Pnt(1, 1, -1))
    array1.SetValue(1, 2, gp_Pnt(2, 1, 0))
    array1.SetValue(1, 3, gp_Pnt(3, 1, -1))
    array1.SetValue(1, 4, gp_Pnt(4, 1, 0))
    array1.SetValue(2, 1, gp_Pnt(1, 2, 3))
    array1.SetValue(2, 2, gp_Pnt(2, 2, 5))
    array1.SetValue(2, 3, gp_Pnt(3, 2, 2))
    array1.SetValue(2, 4, gp_Pnt(4, 2, 3))
    array1.SetValue(3, 1, gp_Pnt(1, 3, 2))
    array1.SetValue(3, 2, gp_Pnt(2, 3, 1))
    array1.SetValue(3, 3, gp_Pnt(3, 3, 0))
    array1.SetValue(3, 4, gp_Pnt(4, 3, 1))
    array1.SetValue(4, 1, gp_Pnt(1, 4, 0))
    array1.SetValue(4, 2, gp_Pnt(2, 4, -1))
    array1.SetValue(4, 3, gp_Pnt(3, 4, 0))
    array1.SetValue(4, 4, gp_Pnt(4, 4, -1))

    BZ1 = Geom_BezierSurface(array1)

    bezierarray = TColGeom_Array2OfBezierSurface(1, 1, 1, 1)
    bezierarray.SetValue(1, 1, BZ1.GetHandle())
    
    BB = GeomConvert_CompBezierSurfacesToBSplineSurface(bezierarray)
    if BB.IsDone():
        poles = BB.Poles().GetObject().Array2()
        print(type(poles))
        uknots = BB.UKnots().GetObject().Array1()
        print(type(uknots))
        vknots = BB.VKnots().GetObject().Array1()
        umult = BB.UMultiplicities().GetObject().Array1()
        vmult = BB.VMultiplicities().GetObject().Array1()
        udeg = BB.UDegree()
        vdeg = BB.VDegree()

        BSPLSURF = Geom_BSplineSurface( poles, uknots, vknots, umult, vmult, udeg, vdeg, 0, 0 )
        BSPLSURF.Translate(gp_Vec(0,0,2))

        if filename is not None:
            error = 1e-6
            face = BRepBuilderAPI_MakeFace(BSPLSURF.GetHandle(), error).Shape()
            breptools_Write(face, filename)
            # Meshing using GMSH
            if output is not None:
                ret = call([GMSH_BIN, "-2", "-format", "stl", "-o", output, filename])

    if ret is not False:
        stl_reader = StlAPI_Reader()
        fan_shp = TopoDS_Shape()
        stl_reader.Read(fan_shp, output)

        display.DisplayShape(fan_shp, update=True)
        start_display()
Esempio n. 2
0
def writeBRep(filename, shapeList):
    aRes = TopoDS_Compound()
    aBuilder = BRep_Builder()
    aBuilder.MakeCompound(aRes)
    for shape in shapeList:
        aBuilder.Add(aRes, shape)
    breptools_Write(aRes, filename)
Esempio n. 3
0
def mesh_shape(a_topods_shape):
    """ Takes a BRep filename (extension .brep) and returns
    a topods_shp ready to be displayed
    """
    # dump the geometry to a brep file
    breptools_Write(a_topods_shape, "shape.brep")

    # create the gmesh file
    gmsh_geo_file_content = """SetFactory("OpenCASCADE");

    Mesh.CharacteristicLengthMin = 1;
    Mesh.CharacteristicLengthMax = 5;

    a() = ShapeFromFile("shape.brep");
    """
    gmsh_geo_file = open("shape.geo", "w")
    gmsh_geo_file.write(gmsh_geo_file_content)
    gmsh_geo_file.close()

    # call gmsh
    gmsh_success = os.system("gmsh shape.geo -2 -o shape.stl -format stl")
    # load the stl file
    if gmsh_success != 0 and os.path.isfile("shape.stl") :
        return read_stl_file("shape.stl")
    else:
        print("Be sure gmsh is in your PATH")
        sys.exit()
def write_result(compound, filename):
    """
    Write result compound to BREP file
    """
    if filename is not None:
        print('Writing compound to file:', filename)
        breptools_Write(compound, filename)
def save_bspline_surface_brep(bspline_surface, file_name):
    """
    This function tries to save bspline surface to BREP file
    """
    error = 1e-6
    face = BRepBuilderAPI_MakeFace(bspline_surface.GetHandle(), error).Shape()
    breptools_Write(face, file_name)
def main(file_name=None):
    """
    Main function for testing
    """

    # Definition of Bezier patches using indexes and coordinates of control points
    patch1 = {
        (1, 1): (1.0, 1.0, 0.0), (1, 2): (2.0, 1.0, 0.0), (1, 3): (3.0, 1.0, 0.0),
        (2, 1): (1.0, 2.0, 0.0), (2, 2): (2.0, 2.0, 0.0), (2, 3): (3.0, 2.0, 0.0),
        (3, 1): (1.0, 3.0, 0.0), (3, 2): (2.0, 3.0, 0.0), (3, 3): (3.0, 3.0, 0.0)
    }
    patch2 = {
        (1, 1): (3.0, 1.0, 0.0), (1, 2): (4.0, 1.0, 0.0), (1, 3): (5.0, 1.0, 0.0),
        (2, 1): (3.0, 2.0, 0.0), (2, 2): (4.0, 2.0, 0.0), (2, 3): (5.0, 2.0, 0.0),
        (3, 1): (3.0, 3.0, 0.0), (3, 2): (4.0, 3.0, 0.0), (3, 3): (5.0, 3.0, 0.0)
    }
    patch3 = {
        (1, 1): (1.0, 3.0, 0.0), (1, 2): (2.0, 3.0, 0.0), (1, 3): (3.0, 3.0, 0.0),
        (2, 1): (1.0, 4.0, 0.0), (2, 2): (2.0, 4.0, 0.0), (2, 3): (3.0, 4.0, 0.0),
        (3, 1): (1.0, 5.0, 1.0), (3, 2): (2.0, 5.0, 1.0), (3, 3): (3.0, 5.0, 0.0)
    }
    patch4 = {
        (1, 1): (3.0, 3.0, 0.0), (1, 2): (4.0, 3.0, 0.0), (1, 3): (5.0, 3.0, 0.0),
        (2, 1): (3.0, 4.0, 0.0), (2, 2): (4.0, 4.0, 0.0), (2, 3): (5.0, 4.0, 0.0),
        (3, 1): (3.0, 5.0, 0.0), (3, 2): (4.0, 5.0, 1.0), (3, 3): (5.0, 5.0, 1.0)
    }
    patches = {}
    patches[(1, 1)] = patch1
    patches[(1, 2)] = patch2
    patches[(2, 1)] = patch3
    patches[(2, 2)] = patch4

    bspline_surface = create_bspline_surfaces(patches)

    error = 1e-6

    tmp1_patches = {}
    tmp1_patches[(1,1)] = patch1
    tmp1_bs = create_bspline_surfaces(tmp1_patches)
    tmp1_face = BRepBuilderAPI_MakeFace(tmp1_bs.GetHandle(), error).Shape()

    tmp2_patches = {}
    tmp2_patches[(1,1)] = patch2
    tmp2_bs = create_bspline_surfaces(tmp2_patches)
    tmp2_face = BRepBuilderAPI_MakeFace(tmp2_bs.GetHandle(), error).Shape()

    border_curve = BRepAlgoAPI_Common(tmp1_face, tmp2_face).Shape()

    face = BRepBuilderAPI_MakeFace(bspline_surface.GetHandle(), error).Shape()

    mold = BRepAlgoAPI_Fuse(face, border_curve).Shape()

    breptools_Write(mold, file_name)

    draw_bspline_surface(bspline_surface)
def main2(filename):
    display.EraseAll()
    display.Context.RemoveAll()

    # With common edges
    S3 = BRepPrimAPI_MakeBox(gp_Pnt(0., 0., 0.), gp_Pnt(100., 100., 50.)).Shape()
    S4 = BRepPrimAPI_MakeBox(gp_Pnt(0., 0., 50.), gp_Pnt(100., 100., 100.)).Shape()

    faces_S3 = get_faces(S3)
    faces_S4 = get_faces(S4)

    # tagging allows to visually find the right faces to glue
    tag_faces(faces_S3, "BLUE", "s3")
    tag_faces(faces_S4, "GREEN", "s4")

    F3, F4 = faces_S3[5], faces_S4[4]

    glue2 = BRepFeat_Gluer(S4, S3)
    glue2.Bind(F4, F3)
    glue2.Build()
    shape = glue2.Shape()

    # move the glued shape, such to be able to inspect input and output
    # of glueing operation
    trsf = gp_Trsf()
    trsf.SetTranslation(gp_Vec(750, 0, 0))
    shape.Move(TopLoc_Location(trsf))

    common_edges = LocOpe_FindEdges(F4, F3)
    common_edges.InitIterator()

    n = 0
    while common_edges.More():
        edge_from = common_edges.EdgeFrom()
        edge_to = common_edges.EdgeTo()

        tag_edge(edge_from, "edge_{0}_from".format(n))
        tag_edge(edge_to, "edge_{0}_to".format(n))

        glue2.Bind(edge_from, edge_to)
        common_edges.Next()
        n += 1

    tag_faces(get_faces(shape), "BLACK", "")

    if filename is not None:
         breptools_Write(shape, filename)

    # render glued shape
    display.DisplayShape(shape)
    display.FitAll()
    start_display()
def solid_compound(filename=None):
    """
    Generate and display solid object created from bspline surface.
    """

    # Create Builder first
    builder = BRep_Builder()

    # Base box
    points_1 = [
        gp_Pnt(0.0, 0.0, 0.0),
        gp_Pnt(1.0, 0.0, 0.0),
        gp_Pnt(1.0, 1.0, 0.0),
        gp_Pnt(0.0, 1.0, 0.0),
        gp_Pnt(0.0, 0.0, 1.0),
        gp_Pnt(1.0, 0.0, 1.0),
        gp_Pnt(1.0, 1.0, 1.0),
        gp_Pnt(0.0, 1.0, 1.0)
    ]
    
    solid_box1 = make_box(builder, points_1)
  
    compound = TopoDS_Compound()
    builder.MakeCompound(compound)
    builder.Add(compound, solid_box1)

    base_primitives = brep_explorer.shape_disassembly(solid_box1)

    # Get some testing elements
    face = base_primitives[4].values()[0]
    wire = wires_of_face(face)[0]
    edges = edges_of_wire(wire)
    for ed_id, edge in enumerate(edges):
        print('edge', ed_id, edge.Orientation())
        verts = verts_of_edge(edge)
        for vert in verts:
            coo = coords_from_vert(vert)
            print(coo, vert.Orientation())

    print('Final compound')
    stat = brep_explorer.create_shape_stat(compound)
    brep_explorer.print_stat(stat)

    if filename is not None:
         breptools_Write(compound, filename)

    display, start_display, add_menu, add_function_to_menu = init_display()
    display.EraseAll()

    display.DisplayShape(solid_box1, color='red', update=True)

    start_display()
def bezier_surfaces(filename=None):
    """
    Generate and display bspline surface
    """
    display.EraseAll()
    array1 = TColgp_Array2OfPnt(1, 4, 1, 4)

    array1.SetValue(1, 1, gp_Pnt(1, 1, -1))
    array1.SetValue(1, 2, gp_Pnt(2, 1, 0))
    array1.SetValue(1, 3, gp_Pnt(3, 1, -1))
    array1.SetValue(1, 4, gp_Pnt(4, 1, 0))
    array1.SetValue(2, 1, gp_Pnt(1, 2, 3))
    array1.SetValue(2, 2, gp_Pnt(2, 2, 5))
    array1.SetValue(2, 3, gp_Pnt(3, 2, 2))
    array1.SetValue(2, 4, gp_Pnt(4, 2, 3))
    array1.SetValue(3, 1, gp_Pnt(1, 3, 2))
    array1.SetValue(3, 2, gp_Pnt(2, 3, 1))
    array1.SetValue(3, 3, gp_Pnt(3, 3, 0))
    array1.SetValue(3, 4, gp_Pnt(4, 3, 1))
    array1.SetValue(4, 1, gp_Pnt(1, 4, 0))
    array1.SetValue(4, 2, gp_Pnt(2, 4, -1))
    array1.SetValue(4, 3, gp_Pnt(3, 4, 0))
    array1.SetValue(4, 4, gp_Pnt(4, 4, -1))

    BZ1 = Geom_BezierSurface(array1)

    bezierarray = TColGeom_Array2OfBezierSurface(1, 1, 1, 1)
    bezierarray.SetValue(1, 1, BZ1.GetHandle())
    
    BB = GeomConvert_CompBezierSurfacesToBSplineSurface(bezierarray)
    if BB.IsDone():
        poles = BB.Poles().GetObject().Array2()
        print(type(poles))
        uknots = BB.UKnots().GetObject().Array1()
        print(type(uknots))
        vknots = BB.VKnots().GetObject().Array1()
        umult = BB.UMultiplicities().GetObject().Array1()
        vmult = BB.VMultiplicities().GetObject().Array1()
        udeg = BB.UDegree()
        vdeg = BB.VDegree()

        BSPLSURF = Geom_BSplineSurface( poles, uknots, vknots, umult, vmult, udeg, vdeg, 0, 0 )
        BSPLSURF.Translate(gp_Vec(0,0,2))

        if filename is not None:
            error = 1e-6
            face = BRepBuilderAPI_MakeFace(BSPLSURF.GetHandle(), error).Shape()
            breptools_Write(face, filename)

    display.DisplayShape(BSPLSURF.GetHandle(), update=True)
    start_display()
Esempio n. 10
0
def write_brep(shape, filename):
    """
    Writes a TopoDS_Shape to a brep file
    :param shape: The shape given as a PythonOCC TopoDS_Shape
    :param filename: Filename to be written
    """
    if not breptools_Write(shape, filename):
        raise RuntimeError("Cannot write brep file: " + filename)
Esempio n. 11
0
def write_brep(occtopology, brep_filepath):
    """
    This function writes a 3D model into brep format.
 
    Parameters
    ----------
    occtopology : OCCtopology
        Geometries to be written into STL.
        OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex 
        
    brep_filepath : str
        The file path of the brep file. 
        
    Returns
    -------
    None : None
        The geometries are written to a brep file.
    """
    from OCC.BRepTools import breptools_Write
    breptools_Write(occtopology, brep_filepath)
Esempio n. 12
0
def write_brep(occtopology, brep_filepath):
    """
    This function writes a 3D model into brep format.
 
    Parameters
    ----------
    occtopology : OCCtopology
        Geometries to be written into STL.
        OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex 
        
    brep_filepath : str
        The file path of the brep file. 
        
    Returns
    -------
    None : None
        The geometries are written to a brep file.
    """       
    from OCC.BRepTools import breptools_Write
    breptools_Write(occtopology, brep_filepath)
Esempio n. 13
0
def main(filename):
    display.EraseAll()
    display.Context.RemoveAll()
    # Without common edges
    S1 = BRepPrimAPI_MakeBox(gp_Pnt(0., 0., 0.), gp_Pnt(100., 100., 50.)).Shape()
    facesA = get_faces(S1)
    # tag_faces(facesA, "BLUE", "facesA")

    # the face to glue
    F1 = facesA[5]

    S2 = BRepPrimAPI_MakeBox(gp_Pnt(0., 0., 50.), gp_Pnt(100., 100., 100.)).Shape()
    facesB = get_faces(S2)

    # tag_faces(facesB, "GREEN", "facesB")

    # the face to glue of the opposite shape
    F2 = facesB[4]

    # perform glueing operation
    glue1 = BRepFeat_Gluer(S2, S1)
    glue1.Bind(F2, F1)
    shape = glue1.Shape()

    display.SetModeHLR()

    # move the glued shape, such to be able to inspect input and output
    # of glueing operation
    trsf = gp_Trsf()
    trsf.SetTranslation(gp_Vec(500, 0, 0))
    shape.Move(TopLoc_Location(trsf))

    tag_faces(get_faces(shape), "BLACK", "")

    if filename is not None:
         breptools_Write(shape, filename)

    # render glued shape
    display.DisplayShape(shape)
    display.FitAll()
    start_display()
Esempio n. 14
0
def draw_lineandvertices(event=None):
    Line_1 = TopoDS_Shape()
    Vertex_1 = TopoDS_Shape()
    Vertex_2 = TopoDS_Shape()
    builder = BRep_Builder()
    builder1 = BRep_Builder()
    compound = TopoDS_Compound()
    builder1.MakeCompound(compound)
    breptools_Read(Line_1, 'Line_1.brep', builder)
    breptools_Read(Vertex_1, 'Vertex_1.brep', builder)
    breptools_Read(Vertex_2, 'Vertex_2.brep', builder)
    builder1.Add(compound,Line_1)
    builder1.Add(compound,Vertex_1)
    builder1.Add(compound,Vertex_2)

    #display.DisplayShape({Line_1, Vertex_1, Vertex_2}, update=True)
    #display.DisplayShape(Group_1, update=True)
    display.DisplayShape(compound, update=True)
    breptools_Write(compound, 'compound.brep')

    display.FitAll()
def fuse_patches(filename=None):
    """
    """
    display.EraseAll()

    coo_range = [[0.0, 0.0], [2.0, 2.0]]
    num_x = 20
    num_y = 20

    list_of_bspline_surfs = generate_list_of_bsplines(coo_range, num_x, num_y)

    # display.DisplayShape(sewing.SewedShape(), update=True)

    array3 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array3.SetValue(1, 1, gp_Pnt(0.25, 0.25, -0.5))
    array3.SetValue(1, 2, gp_Pnt(0.25, 0.25,  0.5))
    array3.SetValue(2, 1, gp_Pnt(1.75, 1.75, -0.5))
    array3.SetValue(2, 2, gp_Pnt(1.75, 1.75,  0.5))

    bspl_surf3 = create_bspline_surface(array3)
    error = 1e-6
    face3 = BRepBuilderAPI_MakeFace(bspl_surf3.GetHandle(), error).Shape()
    # display.DisplayShape(bspl_surf2.GetHandle(), update=True)

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

    for bspline_surf in list_of_bspline_surfs:
        face1 = BRepBuilderAPI_MakeFace(bspline_surf.GetHandle(), error).Shape()
        fuse = BRepAlgoAPI_Fuse(face1, face3)
        mold = fuse.Shape()
        sewing.Add(mold)
    sewing.Perform()

    if filename is not None:
        breptools_Write(mold, filename)

    display.DisplayShape(mold, update=True)

    start_display()
def fuse_sewing(filename=None):
    """
    Try to do fuse between sewings and bspline surface
    """
    display.EraseAll()

    coo_range = [[0.0, 0.0], [2.0, 2.0]]
    num_x = 20
    num_y = 20

    list_of_bspline_surfs = generate_list_of_bsplines(coo_range, num_x, num_y)

    print(list_of_bspline_surfs)

    sewing = make_sewing_of_bspline_surfs(list_of_bspline_surfs)

    # display.DisplayShape(sewing.SewedShape(), update=True)

    array3 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array3.SetValue(1, 1, gp_Pnt(0.25, 0.25, -0.5))
    array3.SetValue(1, 2, gp_Pnt(0.25, 0.25,  0.5))
    array3.SetValue(2, 1, gp_Pnt(1.75, 1.75, -0.5))
    array3.SetValue(2, 2, gp_Pnt(1.75, 1.75,  0.5))

    bspl_surf3 = create_bspline_surface(array3)

    # display.DisplayShape(bspl_surf2.GetHandle(), update=True)

    error = 1e-6
    face3 = BRepBuilderAPI_MakeFace(bspl_surf3.GetHandle(), error).Shape()
    fuse = BRepAlgoAPI_Fuse(sewing.SewedShape(), face3)
    mold = fuse.Shape()

    if filename is not None:
        breptools_Write(mold, filename)

    display.DisplayShape(mold, update=True)

    start_display()
def split_shape(event=None):
    """
    Example of spliting shape
    """
    display, start_display, add_menu, add_function_to_menu = init_display()

    S = BRepPrimAPI_MakeBox(gp_Pnt(-100, -60, -80), 150, 200, 170).Shape()
    asect = BRepAlgoAPI_Section(S, gp_Pln(1, 2, 1, -15), False)
    asect.ComputePCurveOn1(True)
    asect.Approximation(True)
    asect.Build()
    R = asect.Shape()

    asplit = BRepFeat_SplitShape(S)

    wire1 = BRepBuilderAPI_MakeWire()

    i = 0
    for edg in Topo(R).edges():
        print(edg)
        face = TopoDS_Face()
        if asect.HasAncestorFaceOn1(edg, face):
            print('Adding edge on face and wire: ', face, i)
            asplit.Add(edg, face)
            # Add edge to wire
            if i > 0:
                wire1.Add(edg)
        i += 1
    asplit.Build()
    wire1.Build()

    display.EraseAll()
    display.DisplayShape(asplit.Shape())
    display.DisplayShape(wire1.Shape(), color='blue')
    # display.FitAll()

    start_display()

    breptools_Write(asplit.Shape(), "split1.brep")
def solid_compound(filename=None):
    """
    Generate and display solid object created from bspline surface.
    """
    display.EraseAll()

    # Create Builder first
    builder = BRep_Builder()

    points_1 = [
        gp_Pnt(0.0, 0.0, 0.0),
        gp_Pnt(1.0, 0.0, 0.0),
        gp_Pnt(1.0, 1.0, 0.0),
        gp_Pnt(0.0, 1.0, 0.0),
        gp_Pnt(0.0, 0.0, 1.0),
        gp_Pnt(1.0, 0.0, 1.0),
        gp_Pnt(1.0, 1.0, 1.0),
        gp_Pnt(0.0, 1.0, 1.0)
    ]
    solid_box, shape = make_box(builder, points_1)

    if solid_box is not None:
        compound = TopoDS_Compound()
        builder.MakeCompound(compound)

        builder.Add(compound, solid_box)

        print('Final compound')
        stat = brep_explorer.create_shape_stat(compound)
        brep_explorer.print_stat(stat)

        if filename is not None:
             breptools_Write(compound, filename)

    display.DisplayShape(shape, color='red', update=True)

    start_display()
Esempio n. 19
0
def write_2_stl_gmsh(occtopology,
                     stl_filepath,
                     mesh_dim=2,
                     min_length=1,
                     max_length=5,
                     gmsh_location="C:\\gmsh\\gmsh.exe"):
    """
    This function mesh an occtopology using gmsh http://gmsh.info/.
 
    Parameters
    ----------
    occtopology : OCCtopology
        Geometries to be written into STL.
        OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex 
        
    stl_filepath : str
        The file path of the STL file. 
        
    mesh_dim : int, optional
        Perform 1D, 2D or 3D mesh generation.
        
    min_length : float, optional
        The minimum edge length of the mesh.
    
    max_length : float, optional
        The max edge length of the mesh.
        
    gmsh_location : str
        The location where the gmsh program is located. We assumed it is isntalled in C:\\gmsh\\gmsh.exe
        
    Returns
    -------
    None : None
        The geometries are meshed and written to a stl file.
    """
    # dump the geometry to a brep file
    from OCC.BRepTools import breptools_Write
    import subprocess

    parent_path = os.path.abspath(os.path.join(stl_filepath, os.pardir))
    filename = os.path.splitext(stl_filepath)[0]
    # create the brep file
    brep_file = os.path.join(parent_path, filename + ".brep")
    breptools_Write(occtopology, brep_file)

    # create the gmesh file
    geo_file = os.path.join(parent_path, "shape.geo")
    set_factory_str = 'SetFactory("OpenCASCADE");\n'
    mesh_min_length = 'Mesh.CharacteristicLengthMin = ' + str(
        min_length) + ';\n'
    mesh_max_length = 'Mesh.CharacteristicLengthMax = ' + str(
        max_length) + ';\n'
    brep_file_str = 'a() = ShapeFromFile("' + brep_file + '");'
    gmsh_geo_file_content = set_factory_str + mesh_min_length + mesh_max_length + brep_file_str
    gmsh_geo_file = open(geo_file, "w")
    gmsh_geo_file.write(gmsh_geo_file_content)
    gmsh_geo_file.close()

    # call gmsh
    cwd = os.getcwd()
    gmsh_dir = os.path.abspath(os.path.join(gmsh_location, os.pardir))
    os.chdir(gmsh_dir)

    command = "gmsh " + geo_file + " -" + str(
        mesh_dim) + " -algo del3d -o " + stl_filepath + " -format stl"
    print command
    process = subprocess.call(command)

    # load the stl file
    if process == 0 and os.path.isfile(stl_filepath):
        print "WROTE TO:", stl_filepath
        os.chdir(cwd)
    else:
        print "Be sure gmsh is in your PATH"
def solid_compound(filename=None):
    """
    Generate and display solid object created from b-spline surface.
    """

    # Create Builder first
    builder = BRep_Builder()

    # Base box
    points_1 = [
        gp_Pnt(0.0, 0.0, 0.0),
        gp_Pnt(1.0, 0.0, 0.0),
        gp_Pnt(1.0, 1.0, 0.0),
        gp_Pnt(0.0, 1.0, 0.0),
        gp_Pnt(0.0, 0.0, 1.0),
        gp_Pnt(1.0, 0.0, 1.0),
        gp_Pnt(1.0, 1.0, 1.0),
        gp_Pnt(0.0, 1.0, 1.0)
    ]

    # Definition of boxes used for splitting base box
    dx = 0.5
    dy = 0.01
    dz = 0.01
    points_2 = [
        gp_Pnt(0.0+dx, 0.0-dy, 0.0-dz),
        gp_Pnt(1.0+dx, 0.0-dy, 0.0-dz),
        gp_Pnt(1.0+dx, 1.0+dy, 0.0-dz),
        gp_Pnt(0.0+dx, 1.0+dy, 0.0-dz),
        gp_Pnt(0.0+dx, 0.0-dy, 1.0+dz),
        gp_Pnt(1.0+dx, 0.0-dy, 1.0+dz),
        gp_Pnt(1.0+dx, 1.0+dy, 1.0+dz),
        gp_Pnt(0.0+dx, 1.0+dy, 1.0+dz)
    ]
    
    solids = make_two_boxes(builder, points_1, points_2)

    solids, dup_faces = remove_duple_face_shapes(solids[0], (solids[1],))

    # It would be logical to create "compsolid". Composition of solids sharing
    # one common face, but I did find any Python nor C++ example creating such
    # object.
    #
    # Following code does not work, because builder.Add(compsolid, solids[0])
    # raises error:
    # RuntimeError: TopoDS_UnCompatibleShapes
    # ... it also does not accept shells. OCC is simply horrible library!
    #
    # compsolid = TopoDS_CompSolid()
    # builder.MakeCompSolid(compsolid)
    # builder.Add(compsolid, solids[0])
    # builder.Add(compsolid, solids[1])

    compound = TopoDS_Compound()
    builder.MakeCompound(compound)
    # builder.Add(compound, compsolid)
    builder.Add(compound, solids[0])
    builder.Add(compound, solids[1])

    print('Final compound')
    stat = brep_explorer.create_shape_stat(compound)
    brep_explorer.print_stat(stat)

    if filename is not None:
         breptools_Write(compound, filename)

    display, start_display, add_menu, add_function_to_menu = init_display()
    display.EraseAll()

    display.DisplayShape(solids[0], color='red', update=True)
    display.DisplayShape(solids[1], color='blue', update=True)

    start_display()
Esempio n. 21
0
def save(event=None):

    breptools_Write(display)
Esempio n. 22
0
    def exportBrep(self, fileName):
        """
        Export given shape to a BREP file
        """

        return breptools_Write(self.wrapped, fileName)
Esempio n. 23
0
def saveBrep(shape,filename):
    u"""Зберегти форму у форматі BRep"""
    from OCC.BRepTools import breptools_Write # функція PythonOCC для запису BRep
    breptools_Write(shape,filename) # зберегти в PythonOCC у форматі BRep
def bezier_surfaces(filename=None):
    """
    Generate and display solid object created from bspline surface.
    """
    display.EraseAll()

    # Create Builder first
    builder = BRep_Builder()

    point1 = gp_Pnt(0.0, 0.0, 0.0)
    point2 = gp_Pnt(1.0, 0.0, 0.0)
    point3 = gp_Pnt(1.0, 1.0, 0.0)
    point4 = gp_Pnt(0.0, 1.0, 0.0)
    point5 = gp_Pnt(0.0, 0.0, 1.0)
    point6 = gp_Pnt(1.0, 0.0, 1.0)
    point7 = gp_Pnt(1.0, 1.0, 1.0)
    point8 = gp_Pnt(0.0, 1.0, 1.0)

    array1 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array1.SetValue(1, 1, point1)
    array1.SetValue(1, 2, point2)
    array1.SetValue(2, 1, point4)
    array1.SetValue(2, 2, point3)
    bspl_surf1 = create_bspline_surface(array1)

    array2 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array2.SetValue(1, 1, point1)
    array2.SetValue(1, 2, point5)
    array2.SetValue(2, 1, point4)
    array2.SetValue(2, 2, point8)
    bspl_surf2 = create_bspline_surface(array2)

    array3 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array3.SetValue(1, 1, point1)
    array3.SetValue(1, 2, point5)
    array3.SetValue(2, 1, point2)
    array3.SetValue(2, 2, point6)
    bspl_surf3 = create_bspline_surface(array3)

    array4 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array4.SetValue(1, 1, point2)
    array4.SetValue(1, 2, point6)
    array4.SetValue(2, 1, point3)
    array4.SetValue(2, 2, point7)
    bspl_surf4 = create_bspline_surface(array4)

    array5 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array5.SetValue(1, 1, point3)
    array5.SetValue(1, 2, point7)
    array5.SetValue(2, 1, point4)
    array5.SetValue(2, 2, point8)
    bspl_surf5 = create_bspline_surface(array5)

    array6 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array6.SetValue(1, 1, point5)
    array6.SetValue(1, 2, point6)
    array6.SetValue(2, 1, point8)
    array6.SetValue(2, 2, point7)
    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()
    print 'max tolerance:', sewing.MaxTolerance()
    print 'max tolerance:', sewing.Tolerance()
    print 'min tolerance:', sewing.MinTolerance()
    print 'n degenerated shapes:', sewing.NbDegeneratedShapes()
    print 'n deleted faces:', sewing.NbDeletedFaces()
    print 'n free edges:', sewing.NbFreeEdges()
    print 'n multiple edges:', sewing.NbMultipleEdges()

    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)

    compound = TopoDS_Compound()

    builder.MakeCompound(compound)
    builder.Add(compound, solid)

    if filename is not None:
        breptools_Write(compound, filename)

    display.DisplayShape(shell, update=True)

    start_display()
def bezier_surfaces(filename=None):
    """
    Generate and display bspline surface
    """
    display.EraseAll()

    array1 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array1.SetValue(1, 1, gp_Pnt(0, 0, 0))
    array1.SetValue(1, 2, gp_Pnt(1, 0, 0))
    array1.SetValue(2, 1, gp_Pnt(0, 1, 0))
    array1.SetValue(2, 2, gp_Pnt(1, 1, 0))
    bspl_surf1 = create_bspline_surface(array1)

    array2 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array2.SetValue(1, 1, gp_Pnt(0, 0, 0))
    array2.SetValue(1, 2, gp_Pnt(0, 0, 1))
    array2.SetValue(2, 1, gp_Pnt(0, 1, 0))
    array2.SetValue(2, 2, gp_Pnt(0, 1, 1))
    bspl_surf2 = create_bspline_surface(array2)

    array3 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array3.SetValue(1, 1, gp_Pnt(0, 0, 0))
    array3.SetValue(1, 2, gp_Pnt(0, 0, 1))
    array3.SetValue(2, 1, gp_Pnt(1, 0, 0))
    array3.SetValue(2, 2, gp_Pnt(1, 0, 1))
    bspl_surf3 = create_bspline_surface(array3)

    array4 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array4.SetValue(1, 1, gp_Pnt(1, 0, 0))
    array4.SetValue(1, 2, gp_Pnt(1, 0, 1))
    array4.SetValue(2, 1, gp_Pnt(1, 1, 0))
    array4.SetValue(2, 2, gp_Pnt(1, 1, 1))
    bspl_surf4 = create_bspline_surface(array4)

    array5 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array5.SetValue(1, 1, gp_Pnt(1, 1, 0))
    array5.SetValue(1, 2, gp_Pnt(1, 1, 1))
    array5.SetValue(2, 1, gp_Pnt(0, 1, 0))
    array5.SetValue(2, 2, gp_Pnt(0, 1, 1))
    bspl_surf5 = create_bspline_surface(array5)

    array6 = TColgp_Array2OfPnt(1, 2, 1, 2)
    array6.SetValue(1, 1, gp_Pnt(0, 0, 1))
    array6.SetValue(1, 2, gp_Pnt(1, 0, 1))
    array6.SetValue(2, 1, gp_Pnt(0, 1, 1))
    array6.SetValue(2, 2, gp_Pnt(1, 1, 1))
    bspl_surf6 = create_bspline_surface(array6)

    #display.DisplayShape(bspl_surf1.GetHandle(), update=True)

    array7 = TColgp_Array2OfPnt(1, 3, 1, 3)
    array7.SetValue(1, 1, gp_Pnt(-0.25, -0.25, 0.25))
    array7.SetValue(1, 2, gp_Pnt( 0.50, -0.25, 0.50))
    array7.SetValue(1, 3, gp_Pnt( 1.25, -0.25, 0.25))
    array7.SetValue(2, 1, gp_Pnt(-0.25,  0.50, 0.50))
    array7.SetValue(2, 2, gp_Pnt( 0.50,  0.50, 0.75))
    array7.SetValue(2, 3, gp_Pnt( 1.25,  0.50, 0.50))
    array7.SetValue(3, 1, gp_Pnt(-0.25,  1.25, 0.25))
    array7.SetValue(3, 2, gp_Pnt( 0.50,  1.25, 0.50))
    array7.SetValue(3, 3, gp_Pnt( 1.25,  1.25, 0.25))

    bspl_surf7 = create_bspline_surface(array7)

    #display.DisplayShape(bspl_surf2.GetHandle(), update=True)

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

    face7 = BRepBuilderAPI_MakeFace(bspl_surf7.GetHandle(), error).Shape()
    mold = BRepAlgoAPI_Fuse(temp, face7).Shape()

    if filename is not None:
        breptools_Write(mold, filename)

    display.DisplayShape(mold, update=True)

    start_display()
def save_bspline_surface_brep(bspline_surface, file_name):
    """
    This function tries to save bspline surface to BREP file
    """
    breptools_Write(bspline_surface, file_name)
def solid_compound(filename=None):
    """
    Generate and display solid object created from bspline surface.
    """

    # Create Builder first
    builder = BRep_Builder()

    # Base box
    points_1 = [
        gp_Pnt(0.0, 0.0, 0.0),
        gp_Pnt(1.0, 0.0, 0.0),
        gp_Pnt(1.0, 1.0, 0.0),
        gp_Pnt(0.0, 1.0, 0.0),
        gp_Pnt(0.0, 0.0, 1.0),
        gp_Pnt(1.0, 0.0, 1.0),
        gp_Pnt(1.0, 1.0, 1.0),
        gp_Pnt(0.0, 1.0, 1.0)
    ]

    # Definition of shapes used for spliting base box
    dx = 0.5
    dy = 0.01
    dz = 0.01
    points_2 = [
        gp_Pnt(0.6+dx, 0.0-dy, 0.0-dz),
        gp_Pnt(1.0+dx, 0.0-dy, 0.0-dz),
        gp_Pnt(1.0+dx, 1.0+dy, 0.0-dz),
        gp_Pnt(0.6+dx, 1.0+dy, 0.0-dz),
        gp_Pnt(0.6+dx, 0.0-dy, 1.0+dz),
        gp_Pnt(1.0+dx, 0.0-dy, 1.0+dz),
        gp_Pnt(1.0+dx, 1.0+dy, 1.0+dz),
        gp_Pnt(0.0+dx, 1.0+dy, 1.0+dz)
    ]
    
    dx = 1 - dx
    points_3 = [
        gp_Pnt(0.0-dx, 0.0-dy, 0.0-dz),
        gp_Pnt(1.6-dx, 0.0-dy, 0.0-dz),
        gp_Pnt(1.6-dx, 1.0+dy, 0.0-dz),
        gp_Pnt(0.0-dx, 1.0+dy, 0.0-dz),
        gp_Pnt(0.0-dx, 0.0-dy, 1.0+dz),
        gp_Pnt(1.6-dx, 0.0-dy, 1.0+dz),
        gp_Pnt(1.0-dx, 1.0+dy, 1.0+dz),
        gp_Pnt(0.0-dx, 1.0+dy, 1.0+dz)
    ]
    
    solids = make_two_boxes(builder, points_1, points_2, points_3)

    new_solids, dup_faces = remove_duple_face_shapes(solids[0], (solids[1],))

    compound = TopoDS_Compound()
    builder.MakeCompound(compound)
    builder.Add(compound, new_solids[0])
    builder.Add(compound, new_solids[1])

    # builder.Add(compound, comp_solid)

    print('Final compound')
    stat = brep_explorer.create_shape_stat(compound)
    brep_explorer.print_stat(stat)

    if filename is not None:
         breptools_Write(compound, filename)

    display.DisplayShape(new_solids[0], color='red', update=True)
    display.DisplayShape(new_solids[1], color='blue', update=True)

    start_display()
Esempio n. 28
0
def write_2_stl_gmsh(occtopology, stl_filepath, mesh_dim = 2, min_length = 1, max_length = 5,
                     gmsh_location = "C:\\gmsh\\gmsh.exe"):
    """
    This function mesh an occtopology using gmsh http://gmsh.info/.
 
    Parameters
    ----------
    occtopology : OCCtopology
        Geometries to be written into STL.
        OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex 
        
    stl_filepath : str
        The file path of the STL file. 
        
    mesh_dim : int, optional
        Perform 1D, 2D or 3D mesh generation.
        
    min_length : float, optional
        The minimum edge length of the mesh.
    
    max_length : float, optional
        The max edge length of the mesh.
        
    gmsh_location : str
        The location where the gmsh program is located. We assumed it is isntalled in C:\\gmsh\\gmsh.exe
        
    Returns
    -------
    None : None
        The geometries are meshed and written to a stl file.
    """
    # dump the geometry to a brep file
    from OCC.BRepTools import breptools_Write
    import subprocess
    
    parent_path = os.path.abspath(os.path.join(stl_filepath, os.pardir))
    filename = os.path.splitext(stl_filepath)[0]
    # create the brep file
    brep_file = os.path.join(parent_path, filename +".brep")
    breptools_Write(occtopology,brep_file)
    
    # create the gmesh file
    geo_file = os.path.join(parent_path, "shape.geo")
    set_factory_str = 'SetFactory("OpenCASCADE");\n'
    mesh_min_length = 'Mesh.CharacteristicLengthMin = ' + str(min_length)+';\n'
    mesh_max_length = 'Mesh.CharacteristicLengthMax = ' + str(max_length) + ';\n'
    brep_file_str = 'a() = ShapeFromFile("' + brep_file +'");'
    gmsh_geo_file_content = set_factory_str+mesh_min_length+mesh_max_length+brep_file_str
    gmsh_geo_file = open(geo_file, "w")
    gmsh_geo_file.write(gmsh_geo_file_content)
    gmsh_geo_file.close()

    # call gmsh
    cwd = os.getcwd()
    gmsh_dir = os.path.abspath(os.path.join(gmsh_location, os.pardir))
    os.chdir(gmsh_dir)

    command = "gmsh " +  geo_file + " -" + str(mesh_dim) + " -algo del3d -o " + stl_filepath + " -format stl"
    print command
    process = subprocess.call(command)

    
    # load the stl file
    if process == 0 and os.path.isfile(stl_filepath) :
        print "WROTE TO:", stl_filepath
        os.chdir(cwd)
    else:
        print "Be sure gmsh is in your PATH"
    face = OCC.BRepBuilderAPI.BRepBuilderAPI_MakeFace(bspl_surf.GetHandle(), error).Shape()
    sewing.Add(face)
    extrude_surface(sewing, bspl_surf, -1.0)
    sewing.Perform()
    sewing_shape = sewing.SewedShape()
    shell = topods_Shell(sewing_shape)

    # Try to fix normals
    shape_fix = OCC.ShapeFix.ShapeFix_Shell(shell)
    shape_fix.FixFaceOrientation(shell)
    shape_fix.Perform()

    make_solid = BRepBuilderAPI_MakeSolid()
    make_solid.Add(shell)

    solid = make_solid.Solid()

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

    compound = TopoDS_Compound()

    builder.MakeCompound(compound)
    builder.Add(compound, solid)

    from OCC.BRepTools import breptools_Write
    breptools_Write(compound, 'extrude.brep')

    display.DisplayShape(shell, update=True)
    start_display()