Package Pmv :: Module vectfieldCommands
[hide private]
[frames] | no frames]

Source Code for Module Pmv.vectfieldCommands

  1  ############################################################################# 
  2  # 
  3  # Author: Alexandre T. GILLET 
  4  # 
  5  # TSRI 2003 
  6  # 
  7  ############################################################################# 
  8  # 
  9  # 
 10  # $Header: /opt/cvs/python/packages/share1.5/Pmv/vectfieldCommands.py,v 1.11 2006/09/28 21:31:49 vareille Exp $ 
 11  # 
 12  # $Id: vectfieldCommands.py,v 1.11 2006/09/28 21:31:49 vareille Exp $ 
 13  # 
 14  # 
 15  # 
 16  # 
 17  from ViewerFramework.VFCommand import CommandGUI 
 18  from Pmv.mvCommand import MVCommand 
 19  from mglutil.gui.InputForm.Tk.gui import InputFormDescr,InputForm 
 20  from  mglutil.gui.BasicWidgets.Tk.thumbwheel import ThumbWheel 
 21  from mglutil.gui.BasicWidgets.Tk.customizedWidgets import ListChooser, ExtendedSliderWidget 
 22  from mglutil.util.callback import CallBackFunction 
 23  import Tkinter,Pmw,Numeric 
 24  import os,sys,types,string,time,math 
 25  from DejaVu.Spheres import Spheres 
 26  from opengltk.OpenGL import GL 
 27  import Numeric 
 28  from DejaVu.colorTool import RedWhiteBlueRamp, Map 
 29  from DejaVu.Arrows import Arrows 
 30   
 31  from ViewerFramework.VFGUI import dirChoose 
32 -class loadVect(MVCommand):
33 """ Command to load a Vect field for a molecule.""" 34
35 - def __init__(self):
36 MVCommand.__init__(self)
37
38 - def onAddCmdToViewer(self):
39 if not hasattr(self.vf,'vectfield'): 40 self.vf.vectfield = {}
41
42 - def doit(self,name, folderPath):
43 """name,file""" 44 from glob import glob 45 filelist = glob(folderPath+'/*.vu') 46 47 vi = self.vf.GUI.VIEWER 48 hasVect=0 49 for i in vi.rootObject.AllObjects(): 50 if i.name == name:hasVect=1 51 if hasVect: 52 self.vf.loadVUFile(name,filelist, topCommand = 0) 53 else: 54 self.createGeometries(name) 55 self.vf.loadVUFile(name,filelist, topCommand = 0)
56
57 - def __call__(self, name,folderPath,**kw):
58 """ 59 None <- loadVect(nodes, file,**kw) 60 name: name of the vect obj 61 folderPath : Path to folder of *.vu 62 """ 63 64 if not name: return 65 if not folderPath: return 66 apply(self.doitWrapper, (name,folderPath), kw)
67
68 - def onAddObjectToViewer(self, obj):
69 # private flag to specify whether or not the geometries for the SS 70 # have been created. 71 obj.__hasVectField = 0
72
73 - def createGeometries(self, name):
74 from DejaVu.Geom import Geom 75 t = Geom(name,shape=(0,0), protected=True) 76 self.vf.vectfield[name]=t 77 t.replace = True 78 self.vf.GUI.VIEWER.AddObject(t, redo=0)
79
80 - def buildFormDescr(self,formName):
81 if formName == "enterName": 82 ifd =InputFormDescr(title = "Pick Vect Name") 83 vectNames = self.vf.vectfield.keys() 84 ifd.append({'widgetType':Pmw.ComboBox, 85 'name':'vectName', 86 'required':1, 87 'tooltip': "Please type-in a new name or chose one from the list below\n '_' are not accepted", 88 'wcfg':{'labelpos':'nw', 89 'label_text':'VectField Name: ', 90 'entryfield_validate':self.entryValidate, 91 'scrolledlist_items':vectNames, 92 }, 93 'gridcfg':{'sticky':'we'}}) 94 return ifd
95
96 - def entryValidate(self, text):
97 """ 98 Method to validate the name of the msms surface. This name 99 will be used by other command to build Pmw widget so it can't 100 contain an '_'. 101 """ 102 if '_' in text: 103 return Pmw.ERROR 104 else: 105 return Pmw.OK
106
107 - def guiCallback(self,**kw):
108 val = self.showForm('entrerName') 109 if not val:return 110 vectName = val['vectName'][0] 111 folderPath = dirChoose(self.vf.GUI.ROOT) 112 if folderPath: 113 #print folderPath,type(folderPath) 114 ## get all the *.vu file from folderPath, 115 ## create filelist 116 folderPath =str(folderPath) 117 apply(self.doitWrapper, (vectName,folderPath),kw)
118 119 ## from glob import glob 120 ## filelist = glob(str(folderPath)+'/*.vu') 121 ## if filelist: 122 ## apply(self.doitWrapper, (vectName,filelist),kw) 123 124 125 ## def guiCallback(self,**kw): 126 ## folderPath = dirChoose(self.vf.GUI.ROOT) 127 ## if folderPath: 128 ## print folderPath,type(folderPath) 129 ## ## get all the *.vu file from folderPath, 130 ## ## create filelist 131 ## from glob import glob 132 ## filelist = glob(str(folderPath)+'/*.vu') 133 ## if filelist: 134 ## if self.vf.userpref['expandNodeLogString']['value'] == 0: 135 ## self.nodeLogString = "self.getSelection()" 136 ## apply(self.doitWrapper, (self.vf.getSelection(),filelist),kw) 137 138 139 140 loadVectGUI = CommandGUI() 141 loadVectGUI.addMenuCommand('menuRoot', 'VectField', 'loadVect') 142 143 ####### VU Field ###################
144 -class loadVUFile(MVCommand):
145 """ Command to a load a VU file to display""" 146
147 - def __init__(self):
148 MVCommand.__init__(self) 149 self.fileTypes =[('VU file',"*.vu")] 150 self.fileBrowserTitle = "Read VU File" 151 self.lastDir = "."
152 153
154 - def onAddCmdToViewer(self):
155 if not self.vf.commands.has_key('loadVect'): 156 self.vf.loadCommand('vectfieldCommands', 'loadVect', 'Pmv', 157 topCommand = 0)
158
159 - def buildFormDescr(self,formName):
160 if formName == "Vectfield": 161 ifd =InputFormDescr(title = "Choose Vect Field") 162 vectList = [] 163 for key in self.vf.vectfield.keys(): 164 vectList.append((key,None)) 165 ifd.append({'widgetType':ListChooser, 166 'name':'vectObj', 167 'wcfg':{'title':'Pick grid', 168 'entries':vectList, 169 'mode':'single', 170 'lbwcfg':{'height':4},}, 171 'gridcfg':{'sticky':'wens', 'column':100, 172 'rowspan':10}}) 173 174 return ifd
175
176 - def guiCallback(self,event=None, *args, **kw):
177 178 if len(self.vf.vectfield.keys())==0: 179 t='no vectfield currently loaded' 180 self.warningMsg(t) 181 return 182 183 val = self.showForm('Vectfield') 184 if len(val)>0 and len(val['vectObj'])>0: 185 field = val['vectObj'][0] 186 187 file = self.vf.askFileOpen(types=self.fileTypes, 188 idir = self.lastDir, 189 title=self.fileBrowserTitle) 190 191 if file != None: 192 self.lastDir = os.path.split(file)[0] 193 file =[file] 194 self.vf.tryto(self.doitWrapper,field, file) 195 return
196
197 - def loadFile(self,filename):
198 """ load the vector pts and color from a file .vu """ 199 file = open(filename,'r') 200 val = file.readlines() 201 file.close() 202 # shtovu colroname rgb 203 # 120 bright red 1 .4 .4 204 # 129 orange 1 .5 0 205 # 150 yellow 1 1 0 206 # 240 green .3 1 .3 207 # 300 cyan 0 1 1 208 209 colormap = {120:(1,.4,.4,1),129:(1.,.5,0.,1),150:(1.,1.,0.,1), 210 240:(.3,1.,.3,1.),300:(0.,1.,1.,1.)} 211 212 i = 0 213 vertices =[] 214 faces = [] 215 materials =[] 216 217 for l in val: 218 x1 = (float) (string.split(string.strip(l))[0]) 219 y1 = (float) (string.split(string.strip(l))[1]) 220 z1 = (float) (string.split(string.strip(l))[2]) 221 x2 = (float) (string.split(string.strip(l))[3]) 222 y2 = (float) (string.split(string.strip(l))[4]) 223 z2 = (float) (string.split(string.strip(l))[5]) 224 color = (int) (string.split(string.strip(l))[6]) 225 pts1 = (x1,y1,z1) 226 pts2=(x2,y2,z2) 227 228 vertices.append(pts1) 229 vertices.append(pts2) 230 faces.append((i,i+1)) 231 materials.append(colormap[color]) 232 i = i+2 233 234 return (vertices,faces,materials)
235 236
237 - def doit(self,vectName,filelist):
238 viewer = self.vf.GUI.VIEWER 239 for file in filelist: 240 f = os.path.split(file)[1] 241 name = string.split(f,'.')[0] 242 #field= VectField(name=filename) 243 #field.loadVUfile(filelist) 244 vertices,faces,materials= self.loadFile(file) 245 #obj.Set(vertices=self.vertices,faces=self.faces, 246 # materials=self.materials, tagModified=False) 247 obj = Arrows(name,vertices=vertices,faces=faces, 248 materials=materials,inheritMaterial=0, 249 inheritLineWidth=0, lineWidth=3) 250 251 if self.vf.vectfield.has_key(vectName): 252 vect = self.vf.vectfield[vectName] 253 else: 254 t = 'No vector field of Name,%s found'%vectName 255 self.warningMsg(t) 256 return 257 obj.replace = True 258 viewer.AddObject(obj, parent=vect) 259 viewer.deleteOpenglList() 260 viewer.Redraw()
261 262
263 - def __call__(self, vectName,filelist,**kw):
264 """None <- loadVUFile(vectName, filelist,**kw) 265 vectName: Name of the vectobj 266 filelist :list of file *.vu holding the coordinate 267 a the vector to display 268 """ 269 if not vectName: return 270 if not filelist: return 271 apply(self.doitWrapper, (vectName,filelist), kw)
272 273 loadVUFileGUI = CommandGUI() 274 loadVUFileGUI.addMenuCommand('menuRoot', 'VectField', 'loadVU') 275 276 277 278 commandList = [ 279 280 {'name':'loadVect','cmd':loadVect(),'gui':loadVectGUI}, 281 {'name':'loadVUFile','cmd':loadVUFile(),'gui':loadVUFileGUI}, 282 283 ] 284 285 286
287 -def initModule(vf):
288 289 for dict in commandList: 290 vf.addCommand(dict['cmd'],dict['name'],dict['gui'])
291