1 """
2 Old implementation using Tkinter. This is no longer supported.
3 If possible use the Qt-Variant
4 """
5
6 import sys
7
8 from PyFoam.RunDictionary.ParsedBlockMeshDict import ParsedBlockMeshDict
9 from PyFoam.Applications.PyFoamApplication import PyFoamApplication
10 from PyFoam.Error import error,warning
11
12 from PyFoam.ThirdParty.six import print_
13
15 try:
16 global tkinter
17 from PyFoam.ThirdParty.six.moves import tkinter
18 global vtk
19 try:
20 import vtk
21 print_("Using system-VTK")
22 except ImportError:
23 print_("Trying VTK implementation from Paraview")
24 from paraview import vtk
25 global vtkTkRenderWindowInteractor
26 from vtk.tk.vtkTkRenderWindowInteractor import vtkTkRenderWindowInteractor
27 except ImportError:
28 e = sys.exc_info()[1]
29 error("Error while importing modules:",e)
30
33 description="""\
34 Reads the contents of a blockMeshDict-file and displays the vertices
35 as spheres (with numbers). The blocks are sketched by lines. One block
36 can be seceted with a slider. It will be displayed as a green cube
37 with the local directions x1,x2 and x3. Also a patch that is selected
38 by a slider will be sketched by blue squares. This implementation
39 uses Tkinter and is no longer activly developed. Use the QT-version.
40 """
41 PyFoamApplication.__init__(self,
42 description=description,
43 usage="%prog [options] <blockMeshDict>",
44 interspersed=True,
45 nr=1)
46
48 doImports()
49
50 self.renWin = vtk.vtkRenderWindow()
51 self.ren = vtk.vtkRenderer()
52 self.renWin.AddRenderer(self.ren)
53 self.renWin.SetSize(600, 600)
54 self.ren.SetBackground(0.7, 0.7, 0.7)
55 self.ren.ResetCamera()
56 self.cam = self.ren.GetActiveCamera()
57
58 self.axes = vtk.vtkCubeAxesActor2D()
59 self.axes.SetCamera(self.ren.GetActiveCamera())
60
61 self.undefinedActor=vtk.vtkTextActor()
62 self.undefinedActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
63 self.undefinedActor.GetPositionCoordinate().SetValue(0.05,0.2)
64 self.undefinedActor.GetTextProperty().SetColor(1.,0.,0.)
65 self.undefinedActor.SetInput("")
66
67 try:
68 self.readFile()
69 except Exception:
70 e = sys.exc_info()[1]
71 warning("While reading",self.parser.getArgs()[0],"this happened:",e)
72 raise e
73
74 self.ren.ResetCamera()
75
76 self.root = tkinter.Tk()
77 self.root.withdraw()
78
79
80 self.top = tkinter.Toplevel(self.root)
81 self.top.title("blockMesh-Viewer")
82 self.top.protocol("WM_DELETE_WINDOW", self.quit)
83
84
85 self.f1 = tkinter.Frame(self.top)
86 self.f2 = tkinter.Frame(self.top)
87
88 self.f1.pack(side="top", anchor="n", expand=1, fill="both")
89 self.f2.pack(side="bottom", anchor="s", expand="f", fill="x")
90
91
92 self.rw = vtkTkRenderWindowInteractor(self.f1, width=400, height=400, rw=self.renWin)
93 self.rw.pack(expand="t", fill="both")
94
95 self.blockHigh=tkinter.IntVar()
96 self.blockHigh.set(-1)
97
98 self.oldBlock=-1
99 self.blockActor=None
100 self.blockTextActor=None
101
102 self.patchHigh=tkinter.IntVar()
103 self.patchHigh.set(-1)
104
105 self.oldPatch=-1
106 self.patchActor=None
107 self.patchTextActor=vtk.vtkTextActor()
108 self.patchTextActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
109 self.patchTextActor.GetPositionCoordinate().SetValue(0.05,0.1)
110 self.patchTextActor.GetTextProperty().SetColor(0.,0.,0.)
111 self.patchTextActor.SetInput("Patch: <none>")
112
113 self.scroll=tkinter.Scale(self.f2,orient='horizontal',
114 from_=-1,to=len(self.blocks)-1,resolution=1,tickinterval=1,
115 label="Block (-1 is none)",
116 variable=self.blockHigh,command=self.colorBlock)
117
118 self.scroll.pack(side="top", expand="t", fill="x")
119
120 self.scroll2=tkinter.Scale(self.f2,orient='horizontal',
121 from_=-1,to=len(list(self.patches.keys()))-1,resolution=1,tickinterval=1,
122 label="Patch (-1 is none)",
123 variable=self.patchHigh,command=self.colorPatch)
124
125 self.scroll2.pack(side="top", expand="t", fill="x")
126
127 self.f3 = tkinter.Frame(self.f2)
128 self.f3.pack(side="bottom", anchor="s", expand="f", fill="x")
129
130 self.b1 = tkinter.Button(self.f3, text="Quit", command=self.quit)
131 self.b1.pack(side="left", expand="t", fill="x")
132 self.b2 = tkinter.Button(self.f3, text="Reread blockMeshDict", command=self.reread)
133 self.b2.pack(side="left", expand="t", fill="x")
134
135 self.root.update()
136
137 self.iren = self.renWin.GetInteractor()
138 self.istyle = vtk.vtkInteractorStyleSwitch()
139
140 self.iren.SetInteractorStyle(self.istyle)
141 self.istyle.SetCurrentStyleToTrackballCamera()
142
143 self.addProps()
144
145 self.iren.Initialize()
146 self.renWin.Render()
147 self.iren.Start()
148
149 self.root.mainloop()
150
179
181 if not i in self.undefined:
182 self.undefined.append(i)
183
185 self.ren.AddViewProp(self.axes)
186 self.ren.AddActor2D(self.patchTextActor)
187 self.ren.AddActor2D(self.undefinedActor)
188
190 sphere=vtk.vtkSphereSource()
191 sphere.SetRadius(self.vRadius*factor)
192 sphere.SetCenter(coord)
193 mapper=vtk.vtkPolyDataMapper()
194 mapper.SetInputConnection(sphere.GetOutputPort())
195 actor = vtk.vtkActor()
196 actor.SetMapper(mapper)
197 self.ren.AddActor(actor)
198
199 return actor
200
202 coord=self.vertices[index]
203 self.vActors[index]=self.addPoint(coord)
204 text=vtk.vtkVectorText()
205 text.SetText(str(index))
206 tMapper=vtk.vtkPolyDataMapper()
207 tMapper.SetInput(text.GetOutput())
208 tActor = vtk.vtkFollower()
209 tActor.SetMapper(tMapper)
210 tActor.SetScale(2*self.vRadius,2*self.vRadius,2*self.vRadius)
211 tActor.AddPosition(coord[0]+self.vRadius,coord[1]+self.vRadius,coord[2]+self.vRadius)
212 tActor.SetCamera(self.cam)
213 tActor.GetProperty().SetColor(1.0,0.,0.)
214 self.ren.AddActor(tActor)
215
217 try:
218 c1=self.vertices[index1]
219 c2=self.vertices[index2]
220 except:
221 if index1>=len(self.vertices):
222 self.addUndefined(index1)
223 if index2>=len(self.vertices):
224 self.addUndefined(index2)
225 return None
226 line=vtk.vtkLineSource()
227 line.SetPoint1(c1)
228 line.SetPoint2(c2)
229 mapper=vtk.vtkPolyDataMapper()
230 mapper.SetInputConnection(line.GetOutputPort())
231 actor = vtk.vtkActor()
232 actor.SetMapper(mapper)
233 self.ren.AddActor(actor)
234 return actor
235
237 try:
238 c1=self.vertices[index1]
239 c2=self.vertices[index2]
240 except:
241 return None,None
242 line=vtk.vtkLineSource()
243 line.SetPoint1(c1)
244 line.SetPoint2(c2)
245 tube=vtk.vtkTubeFilter()
246 tube.SetRadius(self.vRadius*0.5)
247 tube.SetNumberOfSides(10)
248 tube.SetInput(line.GetOutput())
249 text=vtk.vtkVectorText()
250 text.SetText(label)
251 tMapper=vtk.vtkPolyDataMapper()
252 tMapper.SetInput(text.GetOutput())
253 tActor = vtk.vtkFollower()
254 tActor.SetMapper(tMapper)
255 tActor.SetScale(self.vRadius,self.vRadius,self.vRadius)
256 tActor.AddPosition((c1[0]+c2[0])/2+self.vRadius,(c1[1]+c2[1])/2+self.vRadius,(c1[2]+c2[2])/2+self.vRadius)
257 tActor.SetCamera(self.cam)
258 tActor.GetProperty().SetColor(0.0,0.,0.)
259 return tube.GetOutput(),tActor
260
262 points = vtk.vtkPoints()
263 for i in range(len(lst)):
264 v=lst[i]
265 points.InsertPoint(i,v[0],v[1],v[2])
266 spline=vtk.vtkParametricSpline()
267 spline.SetPoints(points)
268 spline.ClosedOff()
269 splineSource=vtk.vtkParametricFunctionSource()
270 splineSource.SetParametricFunction(spline)
271 mapper=vtk.vtkPolyDataMapper()
272 mapper.SetInputConnection(splineSource.GetOutputPort())
273 actor = vtk.vtkActor()
274 actor.SetMapper(mapper)
275 self.ren.AddActor(actor)
276
287
289 points = vtk.vtkPoints()
290 side = vtk.vtkCellArray()
291 side.InsertNextCell(len(lst))
292 for i in range(len(lst)):
293 try:
294 v=self.vertices[lst[i]]
295 except:
296 self.addUndefined(lst[i])
297 return None
298 points.InsertPoint(i,v[0],v[1],v[2])
299 side.InsertCellPoint(i)
300 result=vtk.vtkPolyData()
301 result.SetPoints(points)
302 result.SetPolys(side)
303
304 return result
305
307 b=self.blocks[index]
308
309 self.addLine(b[ 0],b[ 1])
310 self.addLine(b[ 3],b[ 2])
311 self.addLine(b[ 7],b[ 6])
312 self.addLine(b[ 4],b[ 5])
313 self.addLine(b[ 0],b[ 3])
314 self.addLine(b[ 1],b[ 2])
315 self.addLine(b[ 5],b[ 6])
316 self.addLine(b[ 4],b[ 7])
317 self.addLine(b[ 0],b[ 4])
318 self.addLine(b[ 1],b[ 5])
319 self.addLine(b[ 2],b[ 6])
320 self.addLine(b[ 3],b[ 7])
321
323 append=vtk.vtkAppendPolyData()
324 for a in self.vActors:
325 if a!=None:
326 append.AddInput(a.GetMapper().GetInput())
327 self.axes.SetInput(append.GetOutput())
328
329
330
333
357
359 newBlock=int(value)
360 if self.oldBlock>=0 and self.blockActor!=None:
361 self.ren.RemoveActor(self.blockActor)
362 for ta in self.blockTextActor:
363 self.ren.RemoveActor(ta)
364 self.blockActor=None
365 self.blockTextActor=None
366 if newBlock>=0:
367 append=vtk.vtkAppendPolyData()
368 append2=vtk.vtkAppendPolyData()
369 b=self.blocks[newBlock]
370 append.AddInput(self.makeFace([b[0],b[1],b[2],b[3]]))
371 append.AddInput(self.makeFace([b[4],b[5],b[6],b[7]]))
372 append.AddInput(self.makeFace([b[0],b[1],b[5],b[4]]))
373 append.AddInput(self.makeFace([b[3],b[2],b[6],b[7]]))
374 append.AddInput(self.makeFace([b[0],b[3],b[7],b[4]]))
375 append.AddInput(self.makeFace([b[1],b[2],b[6],b[5]]))
376 d,t1=self.makeDirection(b[0],b[1],"x1")
377 append.AddInput(d)
378 self.ren.AddActor(t1)
379 d,t2=self.makeDirection(b[0],b[3],"x2")
380 append.AddInput(d)
381 self.ren.AddActor(t2)
382 d,t3=self.makeDirection(b[0],b[4],"x3")
383 append.AddInput(d)
384 self.ren.AddActor(t3)
385 self.blockTextActor=(t1,t2,t3)
386 mapper=vtk.vtkPolyDataMapper()
387 mapper.SetInputConnection(append.GetOutputPort())
388 self.blockActor = vtk.vtkActor()
389 self.blockActor.SetMapper(mapper)
390 self.blockActor.GetProperty().SetColor(0.,1.,0.)
391 self.blockActor.GetProperty().SetOpacity(0.3)
392 self.ren.AddActor(self.blockActor)
393
394 self.oldBlock=newBlock
395 self.renWin.Render()
396
398 newPatch=int(value)
399 if self.oldPatch>=0 and self.patchActor!=None:
400 self.ren.RemoveActor(self.patchActor)
401 self.patchActor=None
402 self.patchTextActor.SetInput("Patch: <none>")
403 if newPatch>=0:
404 name=list(self.patches.keys())[newPatch]
405 subs=self.patches[name]
406 append=vtk.vtkAppendPolyData()
407 for s in subs:
408 append.AddInput(self.makeFace(s))
409 mapper=vtk.vtkPolyDataMapper()
410 mapper.SetInputConnection(append.GetOutputPort())
411 self.patchActor = vtk.vtkActor()
412 self.patchActor.SetMapper(mapper)
413 self.patchActor.GetProperty().SetColor(0.,0.,1.)
414 self.patchActor.GetProperty().SetOpacity(0.3)
415 self.ren.AddActor(self.patchActor)
416 self.patchTextActor.SetInput("Patch: "+name)
417
418 self.oldPatch=newPatch
419 self.renWin.Render()
420
421
422