1
1
from . import Shape , Workplane , Assembly , Sketch , Compound , Color , Vector , Location
2
2
from .occ_impl .assembly import _loc2vtk , toVTK
3
3
4
- from typing import Union , Any , List , Tuple , Iterable , cast
4
+ from typing import Union , Any , List , Tuple , Iterable , cast , Optional
5
5
6
6
from typish import instance_of
7
7
18
18
vtkPolyDataMapper ,
19
19
vtkAssembly ,
20
20
vtkRenderWindow ,
21
+ vtkWindowToImageFilter ,
21
22
)
22
23
from vtkmodules .vtkCommonCore import vtkPoints
23
24
from vtkmodules .vtkCommonDataModel import vtkCellArray , vtkPolyData
24
25
from vtkmodules .vtkCommonColor import vtkNamedColors
26
+ from vtkmodules .vtkIOImage import vtkPNGWriter
25
27
26
28
27
29
DEFAULT_COLOR = [1 , 0.8 , 0 , 1 ]
@@ -150,10 +152,22 @@ def show(
150
152
edges : bool = False ,
151
153
specular : bool = True ,
152
154
title : str = "CQ viewer" ,
155
+ screenshot : Optional [str ] = None ,
156
+ interact : bool = True ,
157
+ zoom : float = 1.0 ,
158
+ roll : float = - 35 ,
159
+ elevation : float = - 45 ,
160
+ width : Union [int , float ] = 0.5 ,
161
+ height : Union [int , float ] = 0.5 ,
162
+ trihedron : bool = True ,
163
+ bgcolor : tuple [float , float , float ] = (1 , 1 , 1 ),
164
+ gradient : bool = True ,
165
+ xpos : Union [int , float ] = 0 ,
166
+ ypos : Union [int , float ] = 0 ,
153
167
** kwrags : Any ,
154
168
):
155
169
"""
156
- Show CQ objects using VTK.
170
+ Show CQ objects using VTK. This functions optionally allows to make screenshots.
157
171
"""
158
172
159
173
# split objects
@@ -171,6 +185,11 @@ def show(
171
185
172
186
# VTK window boilerplate
173
187
win = vtkRenderWindow ()
188
+
189
+ # Render off-screen when not interacting
190
+ if not interact :
191
+ win .SetOffScreenRendering (1 )
192
+
174
193
win .SetWindowName (title )
175
194
win .AddRenderer (renderer )
176
195
@@ -208,26 +227,30 @@ def show(
208
227
axes .GetZAxisCaptionActor2D ().GetCaptionTextProperty ().ShallowCopy (tp )
209
228
210
229
# add to an orientation widget
211
- orient_widget = vtkOrientationMarkerWidget ()
212
- orient_widget .SetOrientationMarker (axes )
213
- orient_widget .SetViewport (0.9 , 0.0 , 1.0 , 0.2 )
214
- orient_widget .SetZoom (1.1 )
215
- orient_widget .SetInteractor (inter )
216
- orient_widget .EnabledOn ()
217
- orient_widget .InteractiveOff ()
230
+ if trihedron :
231
+ orient_widget = vtkOrientationMarkerWidget ()
232
+ orient_widget .SetOrientationMarker (axes )
233
+ orient_widget .SetViewport (0.9 , 0.0 , 1.0 , 0.2 )
234
+ orient_widget .SetZoom (1.1 )
235
+ orient_widget .SetInteractor (inter )
236
+ orient_widget .EnabledOn ()
237
+ orient_widget .InteractiveOff ()
218
238
219
239
# use gradient background
220
- renderer .SetBackground (1 , 1 , 1 )
221
- renderer .GradientBackgroundOn ()
240
+ renderer .SetBackground (* bgcolor )
241
+
242
+ if gradient :
243
+ renderer .GradientBackgroundOn ()
222
244
223
245
# use FXXAA
224
246
renderer .UseFXAAOn ()
225
247
226
248
# set camera
227
249
camera = renderer .GetActiveCamera ()
228
- camera .Roll (- 35 )
229
- camera .Elevation (- 45 )
250
+ camera .Roll (roll )
251
+ camera .Elevation (elevation )
230
252
renderer .ResetCamera ()
253
+ camera .Zoom (zoom )
231
254
232
255
# add pts and locs
233
256
renderer .AddActor (pts )
@@ -241,12 +264,36 @@ def show(
241
264
inter .Initialize ()
242
265
243
266
w , h = win .GetScreenSize ()
244
- win .SetSize (w // 2 , h // 2 )
245
- win .SetPosition (- 10 , 0 )
267
+ win .SetSize (
268
+ int (w * width ) if isinstance (width , float ) else width ,
269
+ int (h * height ) if isinstance (height , float ) else height ,
270
+ ) # is height, width specified as float assume it is relative
271
+
272
+ # set position
273
+ win .SetPosition (
274
+ int (w * xpos ) if isinstance (xpos , float ) else xpos ,
275
+ int (h * ypos ) if isinstance (ypos , float ) else ypos ,
276
+ )
246
277
247
278
# show and return
248
279
win .Render ()
249
- inter .Start ()
280
+
281
+ # make a screenshot
282
+ if screenshot :
283
+ win2image = vtkWindowToImageFilter ()
284
+ win2image .SetInput (win )
285
+ win2image .SetInputBufferTypeToRGB ()
286
+ win2image .ReadFrontBufferOff ()
287
+ win2image .Update ()
288
+
289
+ writer = vtkPNGWriter ()
290
+ writer .SetFileName (screenshot )
291
+ writer .SetInputConnection (win2image .GetOutputPort ())
292
+ writer .Write ()
293
+
294
+ # start interaction
295
+ if interact :
296
+ inter .Start ()
250
297
251
298
252
299
# alias
0 commit comments