PythonOCC 3D图形库学习—导入STEP模型
PythonOCC comes with importers/exporters for the most commonly used standard data files format in engineering: STEP, IGES, STL (ascii/binary) and VRML. After the import is successfull, the resulting shape can be handled as a native topology/geometry, i.e. you can use boolean operations, shape fixing, traverse topology etc. PythonOCC中含有导入/导出模块,可以处理STEP,IGES,STL等格式的CAD模型.
STEP file is a CAD file format, usually used to share 3D models between users with different CAD systems. CAD file interchangeability is a huge, huge headache in the field, so it has to be make uniform. Standard ISO 10303 is trying to solve this problem. This standard is informally known as “STEP”, which stands for “Standard for the Exchange of Product model data”. STEP-file (ISO 10303-21) is implementation method of STEP standard that can represent 3D object in Computer-aided design (CAD) and related information.
各种CAD软件一般都使用自己定义的格式存储模型,因此造成数据交换困难,比如在UG里面创建的三维模型不能直接导入到Solidworks中。因此CAD数据交换标准之一的STEP格式被制定出来,使用任何的主流三维设计软件Peo/E、UG、CATIA、Solidworks等都可以直接打开STEP格式的文件(*.step, *.stp)
下面使用PythonOCC导入飞行器STEP格式的三维模型并显示出来(模型下载网址:https://grabcad.com/library):
1 ''' 2 You can translate a STEP file into an OCCT shape in the following steps: 3 1.load the file, 4 2.check file consistency, 5 3.set the translation parameters, 6 4.perform the translation, 7 5.fetch the results. 8 ''' 9 10 import sys 11 from OCC.Display.SimpleGui import init_display 12 from OCC.IFSelect import IFSelect_RetDone,IFSelect_ItemsByEntity 13 14 # Reads STEP files, checks them and translates their contents into Open CASCADE models 15 from OCC.STEPControl import STEPControl_Reader 16 17 18 # Creates a reader object with an empty STEP mode 19 step_reader = STEPControl_Reader() 20 21 # Loads a file and returns the read status 22 status = step_reader.ReadFile('Drone.step') 23 24 # check status 25 if status == IFSelect_RetDone: # RetDone : normal execution with a result 26 # Checking the STEP file 27 # Error messages are displayed if there are invalid or incomplete STEP entities 28 step_reader.PrintCheckLoad(True, IFSelect_ItemsByEntity) 29 30 # Performing the STEP file translation 31 step_reader.TransferRoot() 32 33 # Each successful translation operation outputs one shape 34 # Returns the shape resulting from a translation 35 shape = step_reader.Shape() 36 else: 37 print("Error: can't read file.") 38 sys.exit(0) 39 40 # initializes the display 41 display, start_display, add_menu, add_function_to_menu = init_display() 42 43 # Then the shape is sent to the renderer 44 display.DisplayShape(shape, update=True) 45 46 # enter the gui mainloop 47 start_display()
苏30:
F-22 猛禽:
大疆phantom3:
参考:
http://www.opencascade.com/doc/occt-6.9.1/overview/html/occt__tutorial.html
http://www.opencascade.com/doc/occt-6.9.0/refman/html/class_s_t_e_p_control___reader.html