-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path08.4.TrimSurf.py
271 lines (242 loc) · 7.91 KB
/
08.4.TrimSurf.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
#!/usr/bin/env python
###
# Copyright (c) 2002-2007 Systems in Motion
#
# Permission to use, copy, modify, and distribute this coin.Software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE coin.SoFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS coin.SoFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATcoin.SoEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS coin.SoFTWARE.
#
###
# This is an example from the Inventor Mentor Programming Guide,
# chapter 8, example 4.
#
# This example creates and displays a Bezier Surface
# with trim curves. The surface is identical to the
# surface in example 08.3.BezSurf. The coin.SoNurbsProfile
# class is used to define the trims.
#
# One trim curve makes a counter-clockwise square around
# the whole surface. Two other trim curves are combined
# end to end to cut a hole from the surface. The outside
# trim curve and the first inside trim curve are both
# order 2 curves and are therefore sets of straight lines
# in parameter space. The second inside curve is a
# Bezier curve.
#
####################################################################
# Modified to be compatible with FreeCAD #
# #
# Author : Mariwan Jalal mariwan.jalal@gmail.com #
####################################################################
import os
import sys
import FreeCAD as App
import FreeCADGui as Gui
import pivy.coin as coin
floorData = """#Inventor V2.0 ascii
Separator {
SpotLight {
cutOffAngle 0.9
dropOffRate 0.2
location 6 12 2
direction 0 -1 0
}
ShapeHints {
faceType UNKNOWN_FACE_TYPE
}
Texture2Transform {
# rotation 1.57
scaleFactor 8 8
}
Texture2 {
filename oak.rgb
}
NormalBinding {
value PER_PART
}
Material { diffuseColor 1 1 1 specularColor 1 1 1 shininess 0.4 }
DEF FloorPanel Separator {
DEF FloorStrip Separator {
DEF FloorBoard Separator {
Normal { vector 0 1 0 }
TextureCoordinate2 {
point [ 0 0, 0.5 0, 0.5 2, 0.5 4, 0.5 6,
0.5 8, 0 8, 0 6, 0 4, 0 2 ] }
Coordinate3 {
point [ 0 0 0, .5 0 0, .5 0 -2, .5 0 -4, .5 0 -6,
.5 0 -8, 0 0 -8, 0 0 -6, 0 0 -4, 0 0 -2, ]
}
FaceSet { numVertices 10 }
BaseColor { rgb 0.3 0.1 0.0 }
Translation { translation 0.125 0 -0.333 }
Cylinder { parts TOP radius 0.04167 height 0.002 }
Translation { translation 0.25 0 0 }
Cylinder { parts TOP radius 0.04167 height 0.002 }
Translation { translation 0 0 -7.333 }
Cylinder { parts TOP radius 0.04167 height 0.002 }
Translation { translation -0.25 0 0 }
Cylinder { parts TOP radius 0.04167 height 0.002 }
}
Translation { translation 0 0 8.03 }
USE FloorBoard
Translation { translation 0 0 8.04 }
USE FloorBoard
}
Translation { translation 0.53 0 -0.87 }
USE FloorStrip
Translation { translation 0.53 0 -2.3 }
USE FloorStrip
Translation { translation 0.53 0 1.3 }
USE FloorStrip
Translation { translation 0.53 0 1.1 }
USE FloorStrip
Translation { translation 0.53 0 -0.87 }
USE FloorStrip
Translation { translation 0.53 0 1.7 }
USE FloorStrip
Translation { translation 0.53 0 -0.5 }
USE FloorStrip
}
Translation { translation 4.24 0 0 }
USE FloorPanel
Translation { translation 4.24 0 0 }
USE FloorPanel
}"""
############################################################
# CODE FOR The Inventor Mentor STARTS HERE
# The array of trim coordinates
tpts = (
(0.0, 0.0),
(1.0, 0.0),
(1.0, 1.0),
(0.0, 1.0),
(0.2, 0.2),
(0.2, 0.7),
(0.9, 0.7),
(0.9, 0.2),
(0.7, 0.0),
(0.4, 0.8))
# The 16 coordinates defining the Bezier surface.
pts = (
(-4.5, -2.0, 8.0),
(-2.0, 1.0, 8.0),
(2.0, -3.0, 6.0),
(5.0, -1.0, 8.0),
(-3.0, 3.0, 4.0),
(0.0, -1.0, 4.0),
(1.0, -1.0, 4.0),
(3.0, 2.0, 4.0),
(-5.0, -2.0, -2.0),
(-2.0, -4.0, -2.0),
(2.0, -1.0, -2.0),
(5.0, 0.0, -2.0),
(-4.5, 2.0, -6.0),
(-2.0, -4.0, -5.0),
(2.0, 3.0, -5.0),
(4.5, -2.0, -6.0))
# The 3 knot vectors for the 3 trim curves.
tknots1 = (0, 0, 1, 2, 3, 4, 4)
tknots2 = (0, 0, 1, 2, 3, 3)
tknots3 = (0, 0, 0, 0, 1, 1, 1, 1)
# The Bezier knot vector for the surface.
# This knot vector is used in both the U and
# V directions.
knots = (0, 0, 0, 0, 1, 1, 1, 1)
# Create the nodes needed for the Bezier patch
# and its trim curves.
def makeSurface():
surfSep = coin.SoSeparator()
# Define the Bezier surface including the control
# points, trim curve, and a complexity.
complexity = coin.SoComplexity()
controlPts = coin.SoCoordinate3()
surface = coin.SoNurbsSurface()
complexity.value = 0.7
controlPts.point.setValues(0, 16, pts)
surface.numUControlPoints = 4
surface.numVControlPoints = 4
surface.uKnotVector.setValues(0, 8, knots)
surface.vKnotVector.setValues(0, 8, knots)
surfSep.addChild(complexity)
surfSep.addChild(controlPts)
trimPts = coin.SoProfileCoordinate2()
nTrim1 = coin.SoNurbsProfile()
nTrim2 = coin.SoNurbsProfile()
nTrim3 = coin.SoNurbsProfile()
trimPts.point.setValues(0, 12, tpts)
trimInds = (0, 1, 2, 3, 0)
nTrim1.index.setValues(0, 5, trimInds)
nTrim1.knotVector.setValues(0, 7, tknots1)
trimInds = (4, 5, 6, 7)
nTrim2.linkage = coin.SoProfile.START_NEW
nTrim2.index.setValues(0, 4, trimInds)
nTrim2.knotVector.setValues(0, 6, tknots2)
trimInds = (7, 8, 9, 4)
nTrim3.linkage = coin.SoProfile.ADD_TO_CURRENT
nTrim3.index.setValues(0, 4, trimInds)
nTrim3.knotVector.setValues(0, 8, tknots3)
surfSep.addChild(trimPts)
surfSep.addChild(nTrim1)
surfSep.addChild(nTrim2)
surfSep.addChild(nTrim3)
surfSep.addChild(surface)
return surfSep
# CODE FOR The Inventor Mentor ENDS HERE
############################################################
def TrimSurfExec():
root = coin.SoSeparator()
rot = coin.SoRotation()
rot.rotation.setValue(coin.SbRotation(
coin.SbVec3f(0.0, 1.0, 0.0), 22/7/2.0))
root.addChild(rot)
# Create the scene graph for the carpet
carpet = coin.SoSeparator()
surf = makeSurface()
tex = coin.SoTexture2()
tex.filename = "diamondRug.rgb"
carpet.addChild(tex)
carpet.addChild(surf)
root.addChild(carpet)
# Create the scene graph for the floor
floor = coin.SoSeparator()
xlate = coin.SoTranslation()
scale = coin.SoScale()
input = coin.SoInput()
input.setBuffer(floorData)
result = coin.SoDB.readAll(input)
xlate.translation = (-12.0, -5.0, -5.0)
scale.scaleFactor = (2.0, 1.0, 2.0)
floor.addChild(xlate)
floor.addChild(scale)
floor.addChild(result)
root.addChild(floor)
# Create the scene graph for the carpet's shadow
shadow = coin.SoSeparator()
shmdl = coin.SoLightModel()
shmtl = coin.SoMaterial()
shclr = coin.SoBaseColor()
shxl = coin.SoTranslation()
shscl = coin.SoScale()
shmdl.model = coin.SoLightModel.BASE_COLOR
shclr.rgb = (0.21, 0.15, 0.09)
shmtl.transparency = 0.3
shxl.translation = (0.0, -4.9, 0.0)
shscl.scaleFactor = (1.0, 0.0, 1.0)
shadow.addChild(shmtl)
shadow.addChild(shmdl)
shadow.addChild(shclr)
shadow.addChild(shxl)
shadow.addChild(shscl)
shadow.addChild(surf)
root.addChild(shadow)
view = Gui.ActiveDocument.ActiveView
sg = view.getSceneGraph()
sg.addChild(root)