这里返回总目录>>返回总目录

core_classic_occ_bottle.py

本例从https://github.com/tpaviot/pythonocc-demos搬运而来

运行版本:0.18.2

在其余版本运行不保证正确

先上结果图

代码部分

import math

from OCC.Core.gp import gp_Pnt, gp_OX, gp_Vec, gp_Trsf, gp_DZ, gp_Ax2, gp_Ax3, gp_Pnt2d, gp_Dir2d, gp_Ax2d

from OCC.Core.GC import GC_MakeArcOfCircle, GC_MakeSegment

from OCC.Core.GCE2d import GCE2d_MakeSegment

from OCC.Core.Geom import Geom_Plane, Geom_CylindricalSurface

from OCC.Core.Geom2d import Geom2d_Ellipse, Geom2d_TrimmedCurve

from OCC.Core.BRepBuilderAPI import (BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire,

BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform)

from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakePrism, BRepPrimAPI_MakeCylinder

from OCC.Core.BRepFilletAPI import BRepFilletAPI_MakeFillet

from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Fuse

from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_MakeThickSolid, BRepOffsetAPI_ThruSections

from OCC.Core.BRepLib import breplib

from OCC.Core.BRep import BRep_Tool_Surface, BRep_Builder

from OCC.Core.TopoDS import topods, TopoDS_Compound

from OCC.Core.TopExp import TopExp_Explorer

from OCC.Core.TopAbs import TopAbs_EDGE, TopAbs_FACE

from OCC.Core.TopTools import TopTools_ListOfShape

def face_is_plane(face):

"""

Returns True if the TopoDS_Shape is a plane, False otherwise

"""

hs = BRep_Tool_Surface(face)

downcast_result = Geom_Plane.DownCast(hs)

# The handle is null if downcast failed or is not possible, that is to say the face is not a plane

if downcast_result is None:

return False

else:

return True

def geom_plane_from_face(aFace):

"""

Returns the geometric plane entity from a planar surface

"""

return Geom_Plane.DownCast(BRep_Tool_Surface(aFace))

height = 70

width = 50

thickness = 30

print("creating bottle")

# The points we'll use to create the profile of the bottle's body

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)

# Could also construct the line edges directly using the points instead of the resulting line

aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())

aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())

aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())

# Create a wire out of the edges

aWire = BRepBuilderAPI_MakeWire(aEdge1.Edge(), aEdge2.Edge(), aEdge3.Edge())

# Quick way to specify the X axis

xAxis = gp_OX()

# Set up the mirror

aTrsf = gp_Trsf()

aTrsf.SetMirror(xAxis)

# Apply the mirror transformation

aBRespTrsf = BRepBuilderAPI_Transform(aWire.Wire(), aTrsf)

# Get the mirrored shape back out of the transformation and convert back to a wire

aMirroredShape = aBRespTrsf.Shape()

# A wire instead of a generic shape now

aMirroredWire = topods.Wire(aMirroredShape)

# Combine the two constituent wires

mkWire = BRepBuilderAPI_MakeWire()

mkWire.Add(aWire.Wire())

mkWire.Add(aMirroredWire)

myWireProfile = mkWire.Wire()

# The face that we'll sweep to make the prism

myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)

# We want to sweep the face along the Z axis to the height

aPrismVec = gp_Vec(0, 0, height)

myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face(), aPrismVec)

# Add fillets to all edges through the explorer

mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape())

anEdgeExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_EDGE)

while anEdgeExplorer.More():

anEdge = topods.Edge(anEdgeExplorer.Current())

mkFillet.Add(thickness / 12.0, anEdge)

anEdgeExplorer.Next()

myBody = mkFillet

# Create the neck of the bottle

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())

# Our goal is to find the highest Z face and remove it

faceToRemove = None

zMax = -1

# We have to work our way through all the faces to find the highest Z face so we can remove it for the shell

aFaceExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_FACE)

while aFaceExplorer.More():

aFace = topods.Face(aFaceExplorer.Current())

if face_is_plane(aFace):

aPlane = geom_plane_from_face(aFace)

# We want the highest Z face, so compare this to the previous faces

aPnt = aPlane.Location()

aZ = aPnt.Z()

if aZ > zMax:

zMax = aZ

faceToRemove = aFace

aFaceExplorer.Next()

facesToRemove = TopTools_ListOfShape()

facesToRemove.Append(faceToRemove)

myBody = BRepOffsetAPI_MakeThickSolid(myBody.Shape(), facesToRemove, -thickness / 50.0, 0.001)

# Set up our surfaces for the threading on the neck

neckAx2_Ax3 = gp_Ax3(neckLocation, gp_DZ())

aCyl1 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 0.99)

aCyl2 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 1.05)

# Set up the curves for the threads on the bottle's neck

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 / 10.0

anEllipse1 = Geom2d_Ellipse(anAx2d, aMajor, aMinor)

anEllipse2 = Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4.0)

anArc1 = Geom2d_TrimmedCurve(anEllipse1, 0, math.pi)

anArc2 = Geom2d_TrimmedCurve(anEllipse2, 0, math.pi)

anEllipsePnt1 = anEllipse1.Value(0)

anEllipsePnt2 = anEllipse1.Value(math.pi)

aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2)

# Build edges and wires for threading

anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1, aCyl1)

anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment.Value(), aCyl1)

anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2, aCyl2)

anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value(), aCyl2)

threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1.Edge(), anEdge2OnSurf1.Edge())

threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2.Edge(), anEdge2OnSurf2.Edge())

# Compute the 3D representations of the edges/wires

breplib.BuildCurves3d(threadingWire1.Shape())

breplib.BuildCurves3d(threadingWire2.Shape())

# Create the surfaces of the threading

aTool = BRepOffsetAPI_ThruSections(True)

aTool.AddWire(threadingWire1.Wire())

aTool.AddWire(threadingWire2.Wire())

aTool.CheckCompatibility(False)

myThreading = aTool.Shape()

# Build the resulting compound

bottle = TopoDS_Compound()

aBuilder = BRep_Builder()

aBuilder.MakeCompound(bottle)

aBuilder.Add(bottle, myBody.Shape())

aBuilder.Add(bottle, myThreading)

print("bottle finished")

if __name__ == "__main__":

from OCC.Display.SimpleGui import init_display

display, start_display, add_menu, add_function_to_menu = init_display()

display.DisplayColoredShape(bottle, update=True)

start_display()

本文地址:https://blog.csdn.net/weixin_42755384/article/details/85887102

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

pythonocc view coordinate_pythonOCC例子搬运:4.经典瓶子造型相关推荐

  1. pythonocc_pythonOCC例子搬运:4.经典瓶子造型

    这里返回总目录>>返回总目录 core_classic_occ_bottle.py 本例从https://github.com/tpaviot/pythonocc-demos搬运而来 运行 ...

  2. 信度和效度经典例子_浅析经典目标检测评价指标--mmAP(一)

    大家好,我是旷视科技南京研究院研究员赵博睿,主要研究领域为目标检测.今天和大家聊聊mmAP的那些事- 目标检测是计算机视觉领域的一项基础问题,在许多智能场景的落地应用中目标检测通常都是视觉感知的第一步 ...

  3. 创建指南针View的例子

    在接下来的例子里,你将通过扩展View类创建一个指南针View.它使用传统的指南针上升箭头来指示方向.当完成时,应该和图4-3看起来一样. 指南针是一个UI控件的例子,它需要完全不同的视觉显示,不同于 ...

  4. python实战经典例子_Python入门经典实例

    类和继承 class Base: def __init__(self): self.data = [] def add(self, x): self.data.append(x) def addtwi ...

  5. python编程例子_Python的经典入门书籍有哪些?这5本值得一看

    人工智能时代的来临让Python崭露头角,语法简洁.功能强大的特性更是吸引了很多人学习Python.由于某些条件的限制,有部分人选择自学Python,而需要什么书籍资料成为困扰她们的一大难题.Pyth ...

  6. python递归函数例子_Python递归函数经典案例-汉诺塔问题

    汉诺塔 汉诺塔问题是递归算法学习的一个经典案例,首先来看下汉诺塔问题的相关描述: 汉诺塔问题起源于一个古老的印度传说,大梵天创世时制作了三根金刚石石柱,在第一根柱子上从上往下从小到大摞着64片金盘,婆 ...

  7. 用计算机解决问题的例子,电脑故障经典案例与解决方法20个

    现代生活里电脑已经成为了我们的一部分,无论是日常生活还是在日常工作中,作为使用者的我们,也有必要详细地了解一下我们的电脑,下面就由学习啦小编为大家介绍20个电脑故障经典案例与解决方法,供大家参考和学习 ...

  8. java抽象类例子_java抽象类经典实例分享

    在这之前有给大家详细的介绍过java抽象类,相信很多人应该都比较了解了,那么下面就要继续来给大家分享一个java抽象类的实例. 题目: 不同几何图形的面积计算公式是不一样的,可是,它们具有的特性是一样 ...

  9. 【从零开始】PythonOCC

    目录 1. 三维展示 2. 基本的几何操作 2.1 建立坐标轴 2.2 获取三维物体的包围盒 3. 拓扑操作 3.1 拓扑形状之间的布尔操作 3.2 拓扑形状的数据获取 4. 经典案例--画瓶子 参考 ...

最新文章

  1. RoI Pooling 与 RoI Align 有什么区别?
  2. 使用Wine 1.6.2 在OS X El Capitan下运行Galgame
  3. [王晓刚]深度学习在图像识别中的研究进展与展望(转发)
  4. MT6580启动流程
  5. BIEE连接数据库的方法
  6. 优朋普乐大数据_优朋普乐邵以丁:用大数据全面洞察需求
  7. C++ 高级篇(五)—— 预处理指令
  8. matlab与水库调度,蛙跳算法优化水库调度,全局迭代中最优解未更新
  9. vue调用函数怎么传参_Vue(非)父子组件的传值以及方法调用
  10. Android 屏幕旋转的多种状态
  11. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_2_对象的序列化流_ObjectOutputStream...
  12. matlab拟合曲线教程,【Matlab】matlab如何使用拟合工具?matlab如何拟合曲线?matlab拟合工具cftool如何使用?...
  13. 身神话继续遭受DDOS进攻,也遭受了雷同的陵犯
  14. java随机产生long_java生成随机数
  15. 听NBA现场讲解很爽,但啥都听不懂?快来学学术语!
  16. 国产全志T3+Logos FPGA开发板(4核ARM Cortex-A7)规格书
  17. yolov3/yolov4/yolov5/yolov6/yolov7/lite/fastdet/efficientdet各系列模型开发、项目交付、组合改造创新之—桥梁基建隧道裂痕裂缝检测实战
  18. JQuery.Gantt(甘特图) 开发指南
  19. hive 计算周几_hive返回星期几的方法
  20. docker配置镜像加速

热门文章

  1. Wide Deep模型的理解及实战(Tensorflow)
  2. android ndk neon,Android NDK开发之 NEON使用介绍
  3. HOJ 2550 百步穿杨
  4. 近视眼手术-如何治近视-激光近视手术
  5. 计算机联到同一个局域网,两台电脑连接同一个wifi算是局域网么
  6. 从两家主流报表工具的报jia看报表行业的报jia水深-----常用报表工具对比---主流报表对比
  7. 微信小程序中图片下面出现空白区
  8. 程序员如何保持身心健康?
  9. JAVA计算机毕业设计新疆旅游专列订票系统Mybatis+源码+数据库+lw文档+系统+调试部署
  10. C语言单分支if语句形式,2 单分支if语句