pythonocc基礎使用:1.讀入iges,step,stl檔案
阿新 • • 發佈:2019-01-04
已經更新的入門指導
PythonOCC入門指導:1.建立pythonocc虛擬環境
PythonOCC入門指導:2.執行一個簡單例項
PythonOCC入門指導:3.建立屬於自己的主介面及對話方塊及安裝qtdesigner
pythonocc基礎使用:1.讀入iges,step,stl檔案
pythonocc基礎使用:2.提取曲線上的點位資訊或者曲面上的點位資訊
對於cad系統的開發,讀入外界的檔案很重要。
目前OCC開發者已經支援快速讀入igs,stp,stl格式了(但是這部分的資源目前還未同步到0.18.1版本),具體使用方法見下
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