pythonocc基础使用:1.读取/写入brep,iges,step,stl文件

总目录 >> PythonOCC入门进阶到实战(目前已更新入门篇、基础篇和进阶篇)


由作者实现的国产云端CAD www.yuntucad.com

对于cad系统的开发,读入外界的文件很重要。
目前OCC开发者已经支持快速读入brep,igs,stp,stl格式了(但是这部分的资源目前还未同步到0.18.1版本),具体使用方法见下

1.读取/写入 brep 格式文件

brep作为opencascade官方推出的格式,内核对其解析会更快。
参考:https://www.opencascade.com/doc/occt-7.3.0/overview/html/occt_user_guides__brep_wp.html

##brep作为occ原生的格式,加载速度快,需要特别注意。
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Display.SimpleGui import init_display
from OCC.Core.BRepTools import breptools_Read,breptools_Write
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.BRep import BRep_Builder


# 建造一个正方体,并写入brep
box_shp = BRepPrimAPI_MakeBox(10, 20, 20).Shape()

breptools_Write(box_shp,'box.brep')
print("已经成功写入brep")

#读入一个brep
read_box = TopoDS_Shape()
builder = BRep_Builder()
breptools_Read(read_box, 'box.brep', builder)



display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(read_box, update=True)
start_display()

2.读取/写入 iges,step,stl格式文件

from OCC.Display.SimpleGui import init_display
from OCC.Extend.DataExchange import read_iges_file,read_step_file,read_stl_file
shapes=read_iges_file(fileName1)
#shapes=read_step_file(fileName1)
#shapes=read_stl_file(fileName1)
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(shapes, update=True)

如果你懒得去找官方写的资源,我下面也会展示read_iges_file,read_step_file,read_stl_file函数是如何定义的:
read_iges_file函数

def read_iges_file(filename, return_as_shapes=False, verbosity=False):
    """ read the IGES file and returns a compound
    filename: the file path
    return_as_shapes: optional, False by default. If True returns a list of shapes,
                      else returns a single compound
    verbosity: optionl, False by default.
    """

    assert os.path.isfile(filename)

    iges_reader = IGESControl_Reader()
    status = iges_reader.ReadFile(filename)

    _shapes = []

    if status == IFSelect_RetDone:  # check status
        if verbosity:
            failsonly = False
            iges_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
            iges_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
        iges_reader.TransferRoots()
        nbr = iges_reader.NbRootsForTransfer()
        for n in range(1, nbr+1):
            nbs = iges_reader.NbShapes()
            if nbs == 0:
                print("At least one shape in IGES cannot be transfered")
            elif nbr == 1 and nbs == 1:
                a_res_shape = iges_reader.Shape(1)
                if a_res_shape.IsNull():
                    print("At least one shape in IGES cannot be transferred")
                else:
                    _shapes.append(a_res_shape)
            else:
                for i in range(1, nbs+1):
                    a_shape = iges_reader.Shape(i)
                    if a_shape.IsNull():
                        print("At least one shape in STEP cannot be transferred")
                    else:
                        _shapes.append(a_shape)
    # if not return as shapes
    # create a compound and store all shapes
    if not return_as_shapes:
        builder = BRep_Builder()
        compound = TopoDS_Compound()
        builder.MakeCompound(compound)
        for s in _shapes:
            builder.Add(compound, s)
        _shapes = compound
    return _shapes

read_step_file函数

def read_step_file(filename, return_as_shapes=False, verbosity=False):
    """ read the STEP file and returns a compound
    filename: the file path
    return_as_shapes: optional, False by default. If True returns a list of shapes,
                      else returns a single compound
    verbosity: optionl, False by default.
    """
    assert os.path.isfile(filename)

    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(filename)

    if status == IFSelect_RetDone:  # check status
        if verbosity:
            failsonly = False
            step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
            step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
        ok = step_reader.TransferRoot(1)
        _nbs = step_reader.NbShapes()
        shape_to_return = step_reader.Shape(1)  # a compound
        assert not shape_to_return.IsNull()
    else:
        raise AssertionError("Error: can't read file.")
    if return_as_shapes:
        shape_to_return = TopologyExplorer(shape_to_return).solids()

    return shape_to_return

read_stl_file函数

def read_stl_file(filename):
    """ opens a stl file, reads the content, and returns a BRep topods_shape object
    """
    assert os.path.isfile(filename)

    stl_reader = StlAPI_Reader()
    the_shape = TopoDS_Shape()
    stl_reader.Read(the_shape, filename)

    assert not the_shape.IsNull()

    return the_shape

你可能感兴趣的:(pythonocc,opencascade,pythonocc,python,cad开发)