00 pythonocc下载安装
一、简介
pythonOCC也就是opencascade的python封装版本,是由tpaviot制作并发行的。这里是tpaviot制作者Github的主页官方api函数网页查询(需翻墙):https://cdn.rawgit.com/tpaviot/pythonocc-core/804f7f3/doc/apidoc/0.18.1/#indices-and-tables ,同时可以对照着 https://www.opencascade.com/doc/occt-6.9.1/refman/html/index.html OCCT的api进行查看pythonocc简介:pythonOCC是python语言构架的 3D CAD/CAE/PLM开发框架,它提供了如下功能: 复杂曲面的操作,信息转换(STEP,IGES,STL格式),用户界面可视化(基于wxpython库或者qt库),jupyter nootbook生成等。
能够提供如下操作:2D和3D几何建模工具包让我们能够对任何类型的对象进行建模:
创建基本体,如棱柱、圆柱、圆锥、圆环面布尔操作(交、并、补)倒角、倒圆角与草图操作平移、抽壳、打孔和扫略建模计算属性,如表面面积,体积,重心和曲率进行投影、插补和近似操作可视化模块能够让你管理模型的显示,以及操作视图。分为六大模块
二、下载安装
1、所需材料
anacondaAnaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。 使用anaconda配置环境,则可以免去相当多的手动配置烦恼。可以在这里进行下载安装包:anaconda下载地址
2、安装
在anaconda prompt运行
conda create-npythonocc-cconda-forge-ctpaviot-cdlr-sc-cpythonocc-cocepythonocc-core==0.18.1python=3.5
按道理说这里等待安装完毕就ok了,但是我这老机子就是装不上。
尝试安装一下版本
conda create-npythonocc-cconda-forge-ctpaviot-cdlr-sc-cpythonocc-cocepythonocc-core==0.17.2python=3.5
安装ok
3、版本
上面安装了0.17.2的版本,奈何网上的教程都是0.18.x版本的了。无奈只有去下安装包来升级。
去别的电脑上下载了0.18.1版本的压缩包,最新的0.18.2硬是找不到。。。
https://conda.anaconda.org/oce/win-64
https://conda.anaconda.org/pythonocc/win-64
使用conda升级
conda install--use-localyour-pkg-name
在github拿个例子来运行一下
#!/usr/bin/env python
# -*- coding:utf-8 -*-
##Copyright 2009-2015 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
importmath
fromOCC.gpimportgp_Pnt,gp_OX,gp_Vec,gp_Trsf,gp_DZ,gp_Ax2,gp_Ax3,gp_Pnt2d,gp_Dir2d,gp_Ax2d
fromOCC.GCimportGC_MakeArcOfCircle,GC_MakeSegment
fromOCC.GCE2dimportGCE2d_MakeSegment
fromOCC.GeomimportGeom_Plane,Geom_CylindricalSurface,Handle_Geom_Plane,Handle_Geom_Surface
fromOCC.Geom2dimportGeom2d_Ellipse,Geom2d_TrimmedCurve,Handle_Geom2d_Ellipse,Handle_Geom2d_Curve
fromOCC.BRepBuilderAPIimportBRepBuilderAPI_MakeEdge,BRepBuilderAPI_MakeWire,BRepBuilderAPI_MakeFace,\
BRepBuilderAPI_Transform
fromOCC.BRepPrimAPIimportBRepPrimAPI_MakePrism,BRepPrimAPI_MakeCylinder
fromOCC.BRepFilletAPIimportBRepFilletAPI_MakeFillet
fromOCC.BRepAlgoAPIimportBRepAlgoAPI_Fuse
fromOCC.BRepOffsetAPIimportBRepOffsetAPI_MakeThickSolid,BRepOffsetAPI_ThruSections
fromOCC.BRepLibimportbreplib
fromOCC.BRepimportBRep_Tool_Surface,BRep_Builder
fromOCC.TopoDSimporttopods,TopoDS_Edge,TopoDS_Compound
fromOCC.TopExpimportTopExp_Explorer
fromOCC.TopAbsimportTopAbs_EDGE,TopAbs_FACE
fromOCC.TopToolsimportTopTools_ListOfShape
fromOCC.Display.SimpleGuiimport*
fromOCC.Extend.DataExchangeimportwrite_step_file
defface_is_plane(face):
"""
判断 TopoDS_Shape 是不是 plane
"""
hs=BRep_Tool_Surface(face)
downcast_result=Handle_Geom_Plane.DownCast(hs)
# 不能通过Handle_Geom_Plane转换的不是平面
returnnotdowncast_result.IsNull()
defgeom_plane_from_face(aFace):
"""
通过 planar surface 得到 geometric plane
"""
returnHandle_Geom_Plane.DownCast(BRep_Tool_Surface(aFace)).GetObject()
height=70
width=50
thickness=30
if1:
# 准备用来创建 瓶身 的点
aPnt1=gp_Pnt(-width/2.0,0,0)
aPnt2=gp_Pnt(-width/2.0,-thickness/4.0,0)
aPnt3=gp_Pnt(0,-thickness/2.0,0)
aPnt4=gp_Pnt(width/2.0,-thickness/4.0,0)
aPnt5=gp_Pnt(width/2.0,0,0)
# 瓶底断面线段和圆弧
aArcOfCircle=GC_MakeArcOfCircle(aPnt2,aPnt3,aPnt4)
aSegment1=GC_MakeSegment(aPnt1,aPnt2)
aSegment2=GC_MakeSegment(aPnt4,aPnt5)
# 除了下面的方法, 也可以不用上面的segment,而通过点直接创建线段edges
aEdge1=BRepBuilderAPI_MakeEdge(aSegment1.Value())
aEdge2=BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
aEdge3=BRepBuilderAPI_MakeEdge(aSegment2.Value())
# 通过edges 创建 wire
aWire=BRepBuilderAPI_MakeWire(aEdge1.Edge(),aEdge2.Edge(),aEdge3.Edge())
# 得到X轴的便捷方式
xAxis=gp_OX()
# 设置镜像轴
aTrsf=gp_Trsf()
aTrsf.SetMirror(xAxis)
# 应用镜像变换
aBRespTrsf=BRepBuilderAPI_Transform(aWire.Wire(),aTrsf)
# 得到镜像 shape
aMirroredShape=aBRespTrsf.Shape()
# 通过通用shape得到 wire
aMirroredWire=topods.Wire(aMirroredShape)
# 组合两个wire
mkWire=BRepBuilderAPI_MakeWire()
mkWire.Add(aWire.Wire())
mkWire.Add(aMirroredWire)
myWireProfile=mkWire.Wire()
# 创建用于体拉伸 sweep 的面
myFaceProfile=BRepBuilderAPI_MakeFace(myWireProfile)
# 我们把面沿Z轴拉伸到指定高度
aPrismVec=gp_Vec(0,0,height)
myBody=BRepPrimAPI_MakePrism(myFaceProfile.Face(),aPrismVec)
# 通过explorer对所有边加倒角
mkFillet1=BRepFilletAPI_MakeFillet(myBody.Shape())
anEdgeExplorer=TopExp_Explorer(myBody.Shape(),TopAbs_EDGE)
whileanEdgeExplorer.More():
anEdge=topods.Edge(anEdgeExplorer.Current())
print(anEdge)
mkFillet1.Add(thickness/12.0,anEdge)
anEdgeExplorer.Next()
myBody=mkFillet1
# 创建瓶颈
neckLocation=gp_Pnt(0,0,height)
neckAxis=gp_DZ()
neckAx2=gp_Ax2(neckLocation,neckAxis)
myNeckRadius=thickness/4.0
myNeckHeight=height/10.0
mkCylinder=BRepPrimAPI_MakeCylinder(neckAx2,myNeckRadius,myNeckHeight)
myBody=BRepAlgoAPI_Fuse(myBody.Shape(),mkCylinder.Shape())
# 下面找到Z最高的面, 并将它移除
faceToRemove=None
zMax=-1
# 遍历所有的面, 查找Z最高的面
aFaceExplorer=TopExp_Explorer(myBody.Shape(),TopAbs_FACE)
whileaFaceExplorer.More():
aFace=topods.Face(aFaceExplorer.Current())
ifface_is_plane(aFace):
aPlane=geom_plane_from_face(aFace)
aPnt=aPlane.Location()
aZ=aPnt.Z()
ifaZ>zMax:
zMax=aZ
faceToRemove=aFace
aFaceExplorer.Next()
facesToRemove=TopTools_ListOfShape()
facesToRemove.Append(faceToRemove)
myBody=BRepOffsetAPI_MakeThickSolid(myBody.Shape(),facesToRemove,-thickness/50.0,0.001)
# 建立需要创建螺纹的瓶颈表面
neckAx2_Ax3=gp_Ax3(neckLocation,gp_DZ())
aCyl1=Geom_CylindricalSurface(neckAx2_Ax3,myNeckRadius*0.99)
aCyl2=Geom_CylindricalSurface(neckAx2_Ax3,myNeckRadius*1.05)
# 建立创建螺纹的曲线
aPnt=gp_Pnt2d(2.0*math.pi,myNeckHeight/2.0)
aDir=gp_Dir2d(2.0*math.pi,myNeckHeight/4.0)
anAx2d=gp_Ax2d(aPnt,aDir)
aMajor=2.0*math.pi
aMinor=myNeckHeight/5.0
anEllipse1=Geom2d_Ellipse(anAx2d,aMajor,aMinor)
anEllipse2=Geom2d_Ellipse(anAx2d,aMajor,aMinor/8.0)
anArc1=Geom2d_TrimmedCurve(Handle_Geom2d_Ellipse(anEllipse1),0,math.pi)
anArc2=Geom2d_TrimmedCurve(Handle_Geom2d_Ellipse(anEllipse2),0,math.pi)
anEllipsePnt1=anEllipse1.Value(0)
anEllipsePnt2=anEllipse1.Value(math.pi)
aSegment=GCE2d_MakeSegment(anEllipsePnt1,anEllipsePnt2)
# 构建用于螺纹的边和环
anEdge1OnSurf1=BRepBuilderAPI_MakeEdge(Handle_Geom2d_Curve(anArc1),Handle_Geom_Surface(aCyl1))
anEdge2OnSurf1=BRepBuilderAPI_MakeEdge(aSegment.Value(),Handle_Geom_Surface(aCyl1))
anEdge1OnSurf2=BRepBuilderAPI_MakeEdge(Handle_Geom2d_Curve(anArc2),Handle_Geom_Surface(aCyl2))
anEdge2OnSurf2=BRepBuilderAPI_MakeEdge(aSegment.Value(),Handle_Geom_Surface(aCyl2))
threadingWire1=BRepBuilderAPI_MakeWire(anEdge1OnSurf1.Edge(),anEdge2OnSurf1.Edge())
threadingWire2=BRepBuilderAPI_MakeWire(anEdge1OnSurf2.Edge(),anEdge2OnSurf2.Edge())
# 计算边和环的三维表现
breplib.BuildCurves3d(threadingWire1.Shape())
breplib.BuildCurves3d(threadingWire2.Shape())
# 创建螺纹的表面
aTool=BRepOffsetAPI_ThruSections(True)
aTool.AddWire(threadingWire1.Wire())
aTool.AddWire(threadingWire2.Wire())
aTool.CheckCompatibility(False)
myThreading=aTool.Shape()
myBody=BRepAlgoAPI_Fuse(myBody.Shape(),myThreading)
write_step_file(myBody.Shape(),'aaa.stp')
# # 构建组合结果
# aRes = TopoDS_Compound()
# aBuilder = BRep_Builder()
# aBuilder.MakeCompound(aRes)
# aBuilder.Add(aRes, myBody.Shape())
# aBuilder.Add(aRes, myThreading)
# myBody = BRepAlgoAPI_Fuse(myBody.Shape(), myThreading)
# from OCC.Extend.DataExchange import write_step_file
# write_step_file(myBody, 'pipe/fuse.stp')
display,start_display,add_menu,add_function_to_menu=init_display()
display.DisplayColoredShape(mkFillet1.Shape())
start_display()
运行报错,无Core、Extend包
0.18.1版本的就是没有Core包,直接删除掉Core发现没报错
Extend包确实没有,这里需要去github上下载下来
https://github.com/tpaviot/pythonocc-core/tree/master/src
然后将Extend包复制到anaconda虚拟环境下的OCC包里,我的路径是这样的
D:\anaconda3\envs\pythonocc\Lib\site-packages\OCC
再次运行代码,示例瓶子绘制成功。