总目录 >> 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 compoundfilename: the file pathreturn_as_shapes: optional, False by default. If True returns a list of shapes,else returns a single compoundverbosity: optionl, False by default."""assert os.path.isfile(filename)iges_reader = IGESControl_Reader()status = iges_reader.ReadFile(filename)_shapes = []if status == IFSelect_RetDone:  # check statusif verbosity:failsonly = Falseiges_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 shapesif not return_as_shapes:builder = BRep_Builder()compound = TopoDS_Compound()builder.MakeCompound(compound)for s in _shapes:builder.Add(compound, s)_shapes = compoundreturn _shapes

read_step_file函数

def read_step_file(filename, return_as_shapes=False, verbosity=False):""" read the STEP file and returns a compoundfilename: the file pathreturn_as_shapes: optional, False by default. If True returns a list of shapes,else returns a single compoundverbosity: optionl, False by default."""assert os.path.isfile(filename)step_reader = STEPControl_Reader()status = step_reader.ReadFile(filename)if status == IFSelect_RetDone:  # check statusif verbosity:failsonly = Falsestep_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 compoundassert 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基础使用:1.读取/写入brep,iges,step,stl文件相关推荐

  1. java压缩文件读取_用Java读取/写入压缩和非压缩文件

    java压缩文件读取 这篇文章的主要原因是尝试不要重复自己( DRY ),因为通常,我会遇到递归的需求,即读写压缩的和非压缩的文件(主要是JSON和CSV). 首先让我们看看如何读取文本文件. 注意我 ...

  2. 用Java读取/写入压缩和非压缩文件

    这篇文章的主要原因是尝试不重复自己( DRY ),因为通常,我会遇到读写压缩和非压缩文件(主要是JSON和CSV)的递归需求. 首先让我们看看如何读取文本文件. 注意我正在使用(相对较小的)文本文件, ...

  3. Python文件(一):文件类型、文件的打开,读取写入,关闭、文件备份、文件和文件夹的操作

    一.文件 文件是存储在存储器上的一组数据序列,可以包含任何数据内容. 文件是数据的抽象和集合. 二.文件类型 文本文件:长字符串 二进制是信息按照非字符但有特定格式形成的文件,文件内部数据的组织格式与 ...

  4. STEP、IGES、STL各类3D模型转换为适用Web的glb,gltf格式并压缩

    STEP.IGES.STL各类3D模型转换为适用Web的glb,gltf格式并压缩 格式介绍 STEP和IGES格式 STL格式 GLTF格式 输出GLTF格式的思路 环境安装 实现转换(方法1) p ...

  5. C#基础 字符串读取/写入文本文件 代码示例

    C#基础 字符串读取/写入文本文件 代码示例 写入文本文件: 1 class Program 2 { 3 static void Main(String[] args) 4 { 5 //写入strin ...

  6. pandas 读取写入保存文件

    文章目录 csv 读取 写入 excel 读取数据 写入数据 csv import pandas as pd 读取 df1 = pd.read_csv(r'shares\中原证券(601375)\lr ...

  7. php csv文件的读取,写入,输出下载操作详解

    2019独角兽企业重金招聘Python工程师标准>>> php对csv文件的读取,写入,输出下载操作. 代码: <?php $file = fopen('text.csv',' ...

  8. API读取写入 ini文件内容的方法函数详解

    ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息.ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Ke ...

  9. python关闭读写的所有的文件-Python文件操作:文件的打开关闭读取写入

    Python文件操作:文件的打开关闭读取写入 一.文件的打开关闭 Python能以文本和二进制两种方式处理文件,本文主要讨论在Python3中文本文件的操作. 文件操作都分为以下几个步骤: 1.打开文 ...

最新文章

  1. Ubuntu 系统禁止或者改变中文简体切换繁体,方便使用AS全局搜索
  2. Microsoft Sql Server Management studio与visual studio 建立连接数据库
  3. system文件_解压MIUI 10 升级包 system.new.dat.br
  4. python继承问题_深入浅析python继承问题
  5. (MathType)公式编号(1)和(2a)(2b)混编
  6. 《啊哈算法》知识点总结
  7. 在mysql中 创建视图需要使用_语句_在MySQL中创建视图的X种方式
  8. JPA+QueryDSL
  9. 拓展练习--find查找、打包压缩、服务器、磁盘挂载
  10. 图解css3:核心技术与案例实战. 导读
  11. (ISC)2官方中国CISSP授权培训服务提供商正式启动
  12. 《神奇的数学》读后感_数学王国_奇妙的数学王国读后感10篇
  13. java控制分屏_java实现arcgis地图分屏(双图)
  14. C# 参考 cool edit 样式, 绘制音频波形图
  15. 发那科机器人请关闭电源_发那科FANUC机器人报警处理(中文)
  16. 精灵鼠从入口到出口的最少减少速度
  17. Java毕设项目自行车在线租赁管理系统2021(java+VUE+Mybatis+Maven+Mysql)
  18. c语言字母意义,C语言 字符串前加L的意义 如:L“A”
  19. block、多线程与GCD总结
  20. python qq机器人开发 利用Python读取QQ消息

热门文章

  1. JimuReport积木报表 v1.5.8版本发布—免费的数据可视化报表
  2. Linux 安装OpenGL
  3. Python学习——面向对象(一)
  4. 我的西数硬盘信息(蓝德盒装正品)
  5. java深圳博思得,博思得
  6. 【ESXi8.0安装及硬件直通】
  7. 一生等待,不如半世相拥
  8. 仙人掌之歌——金秋十月(1)
  9. Jrebel激活服务
  10. EDM邮件营销真的落伍了吗?