1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3.  
  4. ##Copyright 2009-2015 Thomas Paviot (tpaviot@gmail.com)
  5. ##
  6. ##This file is part of pythonOCC.
  7. ##
  8. ##pythonOCC is free software: you can redistribute it and/or modify
  9. ##it under the terms of the GNU Lesser General Public License as published by
  10. ##the Free Software Foundation, either version 3 of the License, or
  11. ##(at your option) any later version.
  12. ##
  13. ##pythonOCC is distributed in the hope that it will be useful,
  14. ##but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ##GNU Lesser General Public License for more details.
  17. ##
  18. ##You should have received a copy of the GNU Lesser General Public License
  19. ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
  20.  
  21. import math
  22.  
  23. from OCC.gp import gp_Pnt, gp_OX, gp_Vec, gp_Trsf, gp_DZ, gp_Ax2, gp_Ax3, gp_Pnt2d, gp_Dir2d, gp_Ax2d
  24. from OCC.GC import GC_MakeArcOfCircle, GC_MakeSegment
  25. from OCC.GCE2d import GCE2d_MakeSegment
  26. from OCC.Geom import Geom_Plane, Geom_CylindricalSurface, Handle_Geom_Plane, Handle_Geom_Surface
  27. from OCC.Geom2d import Geom2d_Ellipse, Geom2d_TrimmedCurve, Handle_Geom2d_Ellipse, Handle_Geom2d_Curve
  28. from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire, BRepBuilderAPI_MakeFace, \
  29. BRepBuilderAPI_Transform
  30. from OCC.BRepPrimAPI import BRepPrimAPI_MakePrism, BRepPrimAPI_MakeCylinder
  31. from OCC.BRepFilletAPI import BRepFilletAPI_MakeFillet
  32. from OCC.BRepAlgoAPI import BRepAlgoAPI_Fuse
  33. from OCC.BRepOffsetAPI import BRepOffsetAPI_MakeThickSolid, BRepOffsetAPI_ThruSections
  34. from OCC.BRepLib import breplib
  35. from OCC.BRep import BRep_Tool_Surface, BRep_Builder
  36. from OCC.TopoDS import topods, TopoDS_Edge, TopoDS_Compound
  37. from OCC.TopExp import TopExp_Explorer
  38. from OCC.TopAbs import TopAbs_EDGE, TopAbs_FACE
  39. from OCC.TopTools import TopTools_ListOfShape
  40. from OCC.Display.SimpleGui import *
  41.  
  42. def face_is_plane(face):
  43. """
  44. 判断 TopoDS_Shape 是不是 plane
  45. """
  46. hs = BRep_Tool_Surface(face)
  47. downcast_result = Handle_Geom_Plane.DownCast(hs)
  48. # 不能通过Handle_Geom_Plane转换的不是平面
  49. return not downcast_result.IsNull()
  50.  
  51. def geom_plane_from_face(aFace):
  52. """
  53. 通过 planar surface 得到 geometric plane
  54. """
  55. return Handle_Geom_Plane.DownCast(BRep_Tool_Surface(aFace)).GetObject()
  56.  
  57. height = 70
  58. width = 50
  59. thickness = 30
  60.  
  61. if 1:
  62. # 准备用来创建 瓶身 的点
  63. aPnt1 = gp_Pnt(-width / 2.0, 0, 0)
  64. aPnt2 = gp_Pnt(-width / 2.0, -thickness / 4.0, 0)
  65. aPnt3 = gp_Pnt(0, -thickness / 2.0, 0)
  66. aPnt4 = gp_Pnt(width / 2.0, -thickness / 4.0, 0)
  67. aPnt5 = gp_Pnt(width / 2.0, 0, 0)
  68.  
  69. # 瓶底断面线段和圆弧
  70. aArcOfCircle = GC_MakeArcOfCircle(aPnt2, aPnt3, aPnt4)
  71. aSegment1 = GC_MakeSegment(aPnt1, aPnt2)
  72. aSegment2 = GC_MakeSegment(aPnt4, aPnt5)
  73.  
  74. # 除了下面的方法, 也可以不用上面的segment,而通过点直接创建线段edges
  75. aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
  76. aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
  77. aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
  78.  
  79. # 通过edges 创建 wire
  80. aWire = BRepBuilderAPI_MakeWire(aEdge1.Edge(), aEdge2.Edge(), aEdge3.Edge())
  81.  
  82. # 得到X轴的便捷方式
  83. xAxis = gp_OX()
  84.  
  85. # 设置镜像轴
  86. aTrsf = gp_Trsf()
  87. aTrsf.SetMirror(xAxis)
  88.  
  89. # 应用镜像变换
  90. aBRespTrsf = BRepBuilderAPI_Transform(aWire.Wire(), aTrsf)
  91.  
  92. # 得到镜像 shape
  93. aMirroredShape = aBRespTrsf.Shape()
  94.  
  95. # 通过通用shape得到 wire
  96. aMirroredWire = topods.Wire(aMirroredShape)
  97.  
  98. # 组合两个wire
  99. mkWire = BRepBuilderAPI_MakeWire()
  100. mkWire.Add(aWire.Wire())
  101. mkWire.Add(aMirroredWire)
  102. myWireProfile = mkWire.Wire()
  103.  
  104. # 创建用于体拉伸 sweep 的面
  105. myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)
  106.  
  107. # 我们把面沿Z轴拉伸到指定高度
  108. aPrismVec = gp_Vec(0, 0, height)
  109. myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face(), aPrismVec)
  110.  
  111. # 通过explorer对所有边加倒角
  112. mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape())
  113. anEdgeExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_EDGE)
  114.  
  115. while anEdgeExplorer.More():
  116. anEdge = topods.Edge(anEdgeExplorer.Current())
  117. mkFillet.Add(thickness / 12.0, anEdge)
  118.  
  119. anEdgeExplorer.Next()
  120.  
  121. myBody = mkFillet
  122.  
  123. # 创建瓶颈
  124. neckLocation = gp_Pnt(0, 0, height)
  125. neckAxis = gp_DZ()
  126. neckAx2 = gp_Ax2(neckLocation, neckAxis)
  127.  
  128. myNeckRadius = thickness / 4.0
  129. myNeckHeight = height / 10.0
  130.  
  131. mkCylinder = BRepPrimAPI_MakeCylinder(neckAx2, myNeckRadius, myNeckHeight)
  132.  
  133. myBody = BRepAlgoAPI_Fuse(myBody.Shape(), mkCylinder.Shape())
  134.  
  135. # 下面找到Z最高的面, 并将它移除
  136. faceToRemove = None
  137. zMax = -1
  138.  
  139. # 遍历所有的面, 查找Z最高的面
  140. aFaceExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_FACE)
  141. while aFaceExplorer.More():
  142. aFace = topods.Face(aFaceExplorer.Current())
  143.  
  144. if face_is_plane(aFace):
  145. aPlane = geom_plane_from_face(aFace)
  146.  
  147. aPnt = aPlane.Location()
  148. aZ = aPnt.Z()
  149. if aZ > zMax:
  150. zMax = aZ
  151. faceToRemove = aFace
  152.  
  153. aFaceExplorer.Next()
  154.  
  155. facesToRemove = TopTools_ListOfShape()
  156. facesToRemove.Append(faceToRemove)
  157.  
  158. myBody = BRepOffsetAPI_MakeThickSolid(myBody.Shape(), facesToRemove, -thickness / 50.0, 0.001)
  159.  
  160. # 建立需要创建螺纹的瓶颈表面
  161. neckAx2_Ax3 = gp_Ax3(neckLocation, gp_DZ())
  162. aCyl1 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 0.99)
  163. aCyl2 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 1.05)
  164.  
  165. # 建立创建螺纹的曲线
  166. aPnt = gp_Pnt2d(2.0 * math.pi, myNeckHeight / 2.0)
  167. aDir = gp_Dir2d(2.0 * math.pi, myNeckHeight / 4.0)
  168. anAx2d = gp_Ax2d(aPnt, aDir)
  169.  
  170. aMajor = 2.0 * math.pi
  171. aMinor = myNeckHeight / 5.0
  172.  
  173. anEllipse1 = Geom2d_Ellipse(anAx2d, aMajor, aMinor)
  174. anEllipse2 = Geom2d_Ellipse(anAx2d, aMajor, aMinor / 8.0)
  175.  
  176. anArc1 = Geom2d_TrimmedCurve(Handle_Geom2d_Ellipse(anEllipse1), 0, math.pi)
  177. anArc2 = Geom2d_TrimmedCurve(Handle_Geom2d_Ellipse(anEllipse2), 0, math.pi)
  178.  
  179. anEllipsePnt1 = anEllipse1.Value(0)
  180. anEllipsePnt2 = anEllipse1.Value(math.pi)
  181.  
  182. aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2)
  183.  
  184. # 构建用于螺纹的边和环
  185. anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(Handle_Geom2d_Curve(anArc1), Handle_Geom_Surface(aCyl1))
  186. anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment.Value(), Handle_Geom_Surface(aCyl1))
  187. anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(Handle_Geom2d_Curve(anArc2), Handle_Geom_Surface(aCyl2))
  188. anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value(), Handle_Geom_Surface(aCyl2))
  189.  
  190. threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1.Edge(), anEdge2OnSurf1.Edge())
  191. threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2.Edge(), anEdge2OnSurf2.Edge())
  192.  
  193. # 计算边和环的三维表现
  194. breplib.BuildCurves3d(threadingWire1.Shape())
  195. breplib.BuildCurves3d(threadingWire2.Shape())
  196.  
  197. # 创建螺纹的表面
  198. aTool = BRepOffsetAPI_ThruSections(True)
  199. aTool.AddWire(threadingWire1.Wire())
  200. aTool.AddWire(threadingWire2.Wire())
  201. aTool.CheckCompatibility(False)
  202. myThreading = aTool.Shape()
  203.  
  204. # 构建组合结果
  205. aRes = TopoDS_Compound()
  206. aBuilder = BRep_Builder()
  207. aBuilder.MakeCompound(aRes)
  208. aBuilder.Add(aRes, myBody.Shape())
  209. aBuilder.Add(aRes, myThreading)
  210.  
  211. display, start_display, add_menu, add_function_to_menu = init_display()
  212. display.DisplayColoredShape(aRes)
  213.  
  214. start_display()

pythonOCC版 瓶子代码的更多相关文章

  1. 编译opengl编程指南第八版示例代码通过

    最近在编译opengl编程指南第八版的示例代码,如下 #include <iostream> #include "vgl.h" #include "LoadS ...

  2. 数据结构(c语言版)代码

    第1章  绪论       文档中源码及测试数据存放目录:数据结构\▲课本算法实现\▲01 绪论  概述        第一章作为绪论,主要介绍了数据结构与算法中的一些基本概念和术语.对于这些概念术语 ...

  3. 学会使用Hdlbits网页版Verilog代码仿真验证平台

    给大家推荐一款网页版的 Verilog代码编辑仿真验证平台,这个平台是国外的一家开源FPGA学习网站,通过“https://hdlbits.01xz.net/wiki/Main_Page” 地址链接进 ...

  4. Python系列教程-详细版 | 图文+代码,快速搞定Python编程(附全套速查表)

    作者:韩信子@ShowMeAI 教程地址:http://showmeai.tech/article-detail/python-tutorial 声明:版权所有,转载请联系平台与作者并注明出处 引言 ...

  5. 正则表达式学习笔记(附:Java版示例代码)

    具体学习推荐:正则表达式30分钟入门教程 .         除换行符以外的任意字符\w      word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s        space,空白符 ...

  6. JAVA版Kafka代码及配置解释

    伟大的程序员版权所有,转载请注明:http://www.lenggirl.com/bigdata/java-kafka.html.html 一.JAVA代码 kafka是吞吐量巨大的一个消息系统,它是 ...

  7. 铭飞MCMS内容管理系统完整开源版J2EE代码

    当前版本:4.6.0铭飞MS官网:http://ms.mingsoft.net官网同时提供一键运行版本下载,请步移官网....QQ交流群号1:221335098很多人说铭飞MCMS是大天朝国唯一完整开 ...

  8. 【伪一周小结(没错我一周就做了这么点微小的工作)】HDOJ-1241 Oil Deposits 初次AC粗糙版对比代码框架重构版

    2016 11月最后一周 这一周复习了一下目前大概了解的唯一算法--深度优先搜索算法(DFS).关于各种细节的处理还是极为不熟练,根据题意判断是否还原标记也无法轻松得出结论.不得不说,距离一个准ACM ...

  9. 2048聚合版开源代码,cocos2d-js编写,基于CocosEditor开发工具,可运行Android,ios,html5等

    1. [代码][JavaScript]代码         /** * @GameName : * 2048 * * @DevelopTool: * Cocos2d-x Editor (CocosEd ...

随机推荐

  1. 【原创】redhat5安装oracle10g

    安装缺失的包: 用 root 用户身份运行以下命令: rpm -q gcc make binutils openmotif setarch compat-db compat-gcc compat-gc ...

  2. dubbo之泛化实现

    实现泛化调用 泛化接口调用方式主要用于客户端没有 API 接口及模型类元的情况,参数及返回值中的所有 POJO 均用 Map 表示,通常用于框架集成,比如:实现一个通用的服务测试框架,可通过 Gene ...

  3. POJ 3041 - 最大二分匹配

    这道题实现起来还是比较简单的,但是理解起来可能有点困难. 我最开始想到的是贪心法,每次消灭当前小行星最多的一行或一列.然而WA了.Discuss区里已经有高人给出反例. 下面给出正确的解法 我们把行和 ...

  4. C# 检测dll的新版本号方法

    FileVersionInfo info = FileVersionInfo.GetVersionInfo(YourFileNameHere);string version = info.FileMa ...

  5. [Jxoi2012]奇怪的道路 题解(From luoguBlog)

    题面 状压好题 1<= n <= 30, 0 <= m <= 30, 1 <= K <= 8 这美妙的范围非状压莫属 理所当然地,0和1代表度的奇偶 dp[i][j ...

  6. React Native未来导航者:react-navigation 使用详解

    该库包含三类组件: (1)StackNavigator:用来跳转页面和传递参数 (2)TabNavigator:类似底部导航栏,用来在同一屏幕下切换不同界面 (3)DrawerNavigator:侧滑 ...

  7. Oracle经验积累

    Oracle 常用Funtion ----行转列 开始---- select u_id, wmsys.wm_concat(goods) goods_sum from shopping group by ...

  8. transparent

    transparent属性用来指定全透明色彩

  9. [转载]ext4文件系统的delalloc选项造成单次写延迟增加的分析

    转载http://www.cnblogs.com/cobbliu/p/5603472.html 最近我们的服务进程遇到kill -15后处于Z的状态,变为了僵尸进程,经过/proc/{thread_i ...

  10. USACO 4.1 Fence Loops

    Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...