-
Notifications
You must be signed in to change notification settings - Fork 9
/
bottle.py
318 lines (265 loc) · 11.2 KB
/
bottle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
#
# Adapted from 'Classic OCC Bootle Demo'
#
# This file is part of cadViewer.
# The latest version of this file can be found at:
# //https://github.com/dblanding/cadviewer
#
# cadViewer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# cadViewer 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# if not, write to the Free Software Foundation, Inc.
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
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_CylindricalSurface
from OCC.Core.Geom2d import Geom2d_Ellipse, Geom2d_TrimmedCurve
from OCC.Core.BRepBuilderAPI import (BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire,
BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform,
BRepBuilderAPI_MakeVertex)
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_Builder
from OCC.Core.GeomAbs import GeomAbs_Plane
from OCC.Core.BRepAdaptor import BRepAdaptor_Surface
from OCC.Core.TopoDS import topods, topods_Wire, TopoDS_Compound
from OCC.Core.TopAbs import TopAbs_EDGE, TopAbs_FACE
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopTools import TopTools_ListOfShape
#####################
# #
# Bottle Demo: #
# #
#####################
def face_is_plane(face):
"""
Returns True if the TopoDS_Shape is a plane, False otherwise
"""
surf = BRepAdaptor_Surface(face, True)
surf_type = surf.GetType()
return surf_type == GeomAbs_Plane
def geom_plane_from_face(aFace):
"""
Returns the geometric plane entity from a planar surface
"""
return BRepAdaptor_Surface(aFace, True).Plane()
# Bottle Dimensions...
width = 50
height = 70
thickness = 30
def makeBottle(): # complete bottle
startBottle(complete=True)
def startBottle(complete=False):
"""Build the classic OCC Bottle.
complete=False: minus the neck fillet, shelling & threads.
complete=True: including shelling & threads."""
partName = "Bottle-start"
# 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.Shape()
# 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, mkCylinder.Shape())
if not complete: # quit here
#uid = win.getNewPartUID(myBody.Shape(), name=partName)
#win.redraw()
return
partName = "Bottle-complete"
# 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
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
aRes = TopoDS_Compound()
aBuilder = BRep_Builder()
aBuilder.MakeCompound(aRes)
aBuilder.Add(aRes, myBody.Shape())
aBuilder.Add(aRes, myThreading)
#uid = win.getNewPartUID(aRes, name=partName)
#win.redraw()
# Make Bottle step by step...
def makePoints():
global aPnt1, aPnt2, aPnt3, aPnt4, aPnt5
aPnt1 = gp_Pnt(-width / 2., 0, 0)
aPnt2 = gp_Pnt(-width / 2., -thickness / 4., 0)
aPnt3 = gp_Pnt(0, -thickness / 2., 0)
aPnt4 = gp_Pnt(width / 2., -thickness / 4., 0)
aPnt5 = gp_Pnt(width / 2., 0, 0)
# points aren't visible on screen
# make vertices in order to see them
V1 = BRepBuilderAPI_MakeVertex(aPnt1)
V2 = BRepBuilderAPI_MakeVertex(aPnt2)
V3 = BRepBuilderAPI_MakeVertex(aPnt3)
V4 = BRepBuilderAPI_MakeVertex(aPnt4)
V5 = BRepBuilderAPI_MakeVertex(aPnt5)
# add dummy vertex above bottle just to set view size
V6 = BRepBuilderAPI_MakeVertex(gp_Pnt(0, 0, height * 1.1))
return (V1, V2, V3, V4, V5, V6)
def makeLines():
global aEdge1, aEdge2, aEdge3
# Make type 'Geom_TrimmedCurve' from type 'gp_Pnt'
aArcOfCircle = GC_MakeArcOfCircle(aPnt2, aPnt3, aPnt4)
aSegment1 = GC_MakeSegment(aPnt1, aPnt2)
aSegment2 = GC_MakeSegment(aPnt4, aPnt5)
# Make type 'TopoDS_Edge' from type 'Geom_TrimmedCurve'
aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value())
aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
return (aEdge1.Edge(), aEdge2.Edge(), aEdge3.Edge())
def makeHalfWire():
global aWire
# Make type 'TopoDS_Wire' from type 'TopoDS_Edge'
aWire = BRepBuilderAPI_MakeWire(aEdge1.Edge(),
aEdge2.Edge(),
aEdge3.Edge()).Wire()
return aWire
def makeWholeWire():
global myWireProfile
xAxis = gp_OX()
# Set up the mirror
aTrsf = gp_Trsf()
aTrsf.SetMirror(xAxis)
# Apply the mirror transform
aBRepTrsf = BRepBuilderAPI_Transform(aWire, aTrsf)
# Convert mirrored shape to a wire
aMirroredShape = aBRepTrsf.Shape()
aMirroredWire = topods_Wire(aMirroredShape)
# Combine the two wires
mkWire = BRepBuilderAPI_MakeWire()
mkWire.Add(aWire)
mkWire.Add(aMirroredWire)
myWireProfile = mkWire.Wire()
return myWireProfile
def makeFace():
global myFaceProfile
myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile)
if myFaceProfile.IsDone():
bottomFace = myFaceProfile.Face()
return bottomFace
def makeBody():
aPrismVec = gp_Vec(0, 0, height)
myBody = BRepPrimAPI_MakePrism(myFaceProfile.Shape(),
aPrismVec).Shape()
return myBody
def addNeck():
neckLocation = gp_Pnt(0, 0, height)
neckNormal = gp_DZ()
neckAx2 = gp_Ax2(neckLocation, neckNormal)
myNeckRadius = thickness / 4.
myNeckHeight = height / 10.
MKCylinder = BRepPrimAPI_MakeCylinder(neckAx2,
myNeckRadius,
myNeckHeight)
myNeck = MKCylinder.Shape()
return myNeck