1
2 """ Wrapper class for the paraview servermanager
3
4 Sets up the servermanager to be used with OpenFOAM-Data. Especially makes sure that
5 the plugins for the OpenFOAM-Data are loaded"""
6
7 from math import sqrt
8
9 from paraview import servermanager
10 from PyFoam.Paraview import version
11 from PyFoam.FoamInformation import foamVersion
12
13 if version()>=(3,6):
14 from paraview.simple import LoadPlugin
15 from paraview import simple
16
17 from os import environ,path
18 from platform import uname
19
20 from PyFoam.Error import error,warning
21
23 """Wrapper class for the servermanager
24
25 Load the plugins and build a connection"""
26
27 - def __init__(self,requiredReader="PV3FoamReader"):
28 """Sets up the Servermanager in such a way that it is usable
29 with OpenFOAM-data.
30 @param requiredReader: Reader that is needed. If not found, try to load plugins"""
31
32 try:
33 self.con=self.module().Connect()
34 except RuntimeError:
35
36 self.con=None
37
38 dyExt="so"
39 if uname()[0]=="Darwin":
40 dyExt="dylib"
41 elif uname()[0]=="Windows":
42 dyExt="DLL"
43
44 if requiredReader in dir(simple) and not "OpenFOAMReader":
45 warning("Reader",requiredReader,"already present. No plugins loaded")
46 return
47
48 if requiredReader=="PV3FoamReader":
49 if uname()[0]=="Darwin":
50 import ctypes
51
52 lib=ctypes.CDLL(path.join(environ["FOAM_LIBBIN"],"libOpenFOAM.dylib"),mode=ctypes.RTLD_GLOBAL)
53
54 print lib
55 elif uname()[0]=="Linux":
56 try:
57 import ctypes
58 dirs=[environ["FOAM_LIBBIN"]]+environ["PV_PLUGIN_PATH"].split(":")
59 lib=None
60 for d in dirs:
61 try:
62 lib=ctypes.CDLL(path.join(d,"libPV3FoamReader.so"),mode=ctypes.RTLD_GLOBAL)
63 break
64 except OSError:
65 pass
66 if not lib:
67 warning("Could not find libPV3FoamReader.so in",dirs)
68 except ImportError:
69 error("The Workaround for Linux-Systems won't work because there is no ctypes library")
70
71 plug1="libPV3FoamReader."+dyExt
72 if foamVersion()>=(1,7):
73 plug1=None
74
75 plug2="libPV3FoamReader_SM."+dyExt
76
77 loaded=False
78 for p in environ["PV_PLUGIN_PATH"].split(":"):
79 if path.exists(path.join(p,plug2)):
80 if version()>=(3,6):
81 LoadPlugin(path.join(p,plug2),ns=globals())
82 try:
83 if plug1:
84 LoadPlugin(path.join(p,plug1),ns=globals())
85 pass
86 except NameError:
87 print dir(self.module())
88 pass
89 else:
90 if plug1:
91 servermanager.LoadPlugin(path.join(p,plug1))
92 servermanager.LoadPlugin(path.join(p,plug2))
93 loaded=True
94 break
95
96 if not loaded:
97 error("The plugin",plug2,"was not found in the PV_PLUGIN_PATH",environ["PV_PLUGIN_PATH"])
98 if not "PV3FoamReader" in dir(servermanager.sources):
99 error("The plugin was not properly loaded. PV3FoamReader not found in the list of sources")
100 elif requiredReader=="OpenFOAMReader":
101 if "ParaView_DIR" in environ:
102 hasPlug=False
103 for d in ["plugins","Plugins"]:
104 plug=path.join(environ["ParaView_DIR"],"bin",d,"libPOpenFOAMReaderPlugin."+dyExt)
105 if path.exists(plug):
106 LoadPlugin(plug)
107 hasPlug=True
108 break
109 if not hasPlug:
110 warning("Can't find expected plugin 'libPOpenFOAMReaderPlugin' assuming that correct reader is compiled in. Wish me luck")
111 else:
112 warning("Can't plugin without ParaView_DIR-variable. Continuing without")
113 else:
114 warning("Loading of plugins for reader",requiredReader,"not implemented")
115
117 """Delegate Attributes to the servermanager-module"""
118
119 return getattr(servermanager,attr)
120
122 """Delegate Attributes to the servermanager-module"""
123
124 return setattr(servermanager,attr,val)
125
127 """Return the actual module (for developing)"""
128 return servermanager
129
131 """Make sure that everything gets thrown out. Doesn't work"""
132
133 for v in servermanager.GetRenderViews():
134 del v
135 if self.con:
136 self.module().Disconnect(self.con)
137 self.con=None
138
139