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

Source Code for Module Pmv.writeMsmsAsCommands

  1  ############################################################################# 
  2  # 
  3  # Author: Michel F. SANNER 
  4  # 
  5  # Copyright: M. Sanner TSRI 2000 
  6  # 
  7  ############################################################################# 
  8   
  9  # 
 10  # $Header: /opt/cvs/python/packages/share1.5/Pmv/writeMsmsAsCommands.py,v 1.6 2006/12/27 18:27:00 sargis Exp $ 
 11  #  
 12  # $Id: writeMsmsAsCommands.py,v 1.6 2006/12/27 18:27:00 sargis Exp $ 
 13  # 
 14  # 
 15   
 16  import types 
 17  from MolKit.tree import TreeNode, TreeNodeSet 
 18  from MolKit.molecule import Atom 
 19  from opengltk.OpenGL import GL 
 20  from geomutils.geomalgorithms import  TriangleNormals 
 21  from ViewerFramework.VFCommand import CommandGUI 
 22  from Pmv.mvCommand import MVCommand 
 23   
24 -class WriteSTL(MVCommand):
25
26 - def onAddCmdToViewer(self):
27 if not self.vf.commands.has_key('msmsMol'): 28 self.vf.loadCommand('msmsCommands', 'msmsMol', 'Pmv', 29 topCommand=0)
30
31 - def writeSTLCATrace(self, mol, filename):
32 """Write ca trace of the selected molecules in the STL format""" 33 34 if not hasattr(mol.geomContainer, 'catrace'): #ca trace not computed 35 self.vf.buildCATrace.guiCallback() 36 37 # get the extrude catrace object for that molecule 38 caExtrude = mol.geomContainer.geoms['catrace'].extrusion 39 40 # get the vertices and faces 41 vertices = caExtrude.vertices 42 faces = caExtrude.faces 43 f = Numeric.concatenate((faces[:,-2:], faces[:,:1]), 1) 44 F = Numeric.concatenate((faces[:,:3], f), 0) 45 #n = GL.glTriangleNormals(vertices, F, 'PER_VERTEX') 46 n = TriangleNormals(vertices, F, 'PER_VERTEX') 47 48 49 # compute the face normals 50 51 fn = caExtrude.glQuadNormals(self,vertices, faces) 52 #fn = GL.glTriangleNormals(vf[:,:3], fa[:,:3]) 53 54 f = open(filename, 'w') 55 f.write("solid NoName\n") 56 for i in range(len(F)): 57 face = F[i] 58 f.write(" facet normal %f %f %f\n" % tuple(fn[i])) 59 f.write(" outer loop\n") 60 for f in face: 61 f.write(" vertex %f %f %f\n" % tuple(vf[f][:3])) 62 f.write(" endloop\n") 63 f.write(" endfacet\n") 64 f.write("end solid\n") 65 f.close()
66
67 - def writeSTLsurface(self, mol, filename):
68 """Write the MSMS surface corresponding to all selected molecules 69 in the STL format""" 70 71 if not hasattr(mol.geomContainer, 'msms'): #surface not computed 72 self.vf.msmsMol.guiCallback() 73 74 # get the msms surface object for that molecule 75 srf = mol.geomContainer.msms 76 77 # get the vertices and faces 78 vf, vi, fa = srf.getTriangles() 79 80 # compute the face normals 81 #fn = GL.glTriangleNormals(vf[:,:3], fa[:,:3]) 82 fn = TriangleNormals(vf[:,:3], fa[:,:3]) 83 f = open(filename, 'w') 84 f.write("solid NoName\n") 85 for i in range(len(fa)): 86 face = fa[i] 87 f.write(" facet normal %f %f %f\n" % tuple(fn[i])) 88 f.write(" outer loop\n") 89 f.write(" vertex %f %f %f\n" % tuple(vf[face[0]][:3])) 90 f.write(" vertex %f %f %f\n" % tuple(vf[face[1]][:3])) 91 f.write(" vertex %f %f %f\n" % tuple(vf[face[2]][:3])) 92 f.write(" endloop\n") 93 f.write(" endfacet\n") 94 f.write("end solid\n") 95 f.close()
96 97
98 - def doit(self, nodes, filename):
99 self.log(nodes, filename) 100 # oldcursor = self.vf.GUI.setCursor('watch') 101 102 if type(nodes)==types.StringType: 103 nodes = self.vf.Mols.NodesFromName(nodes) 104 105 assert isinstance(nodes,TreeNode) or isinstance(nodes,TreeNodeSet) 106 if isinstance(nodes,TreeNode): 107 nodes = TreeNodeSet([nodes]) 108 109 if len(nodes)==0: return 110 atoms = nodes.findType( Atom ) 111 molecules = atoms.top.uniq() 112 113 for mol in molecules: 114 self.writeSTLsurface(mol, '%s.%s.stl'%(filename, mol.name))
115 116 # self.vf.GUI.setCursor(oldcursor) 117 118
119 - def __call__(self, atoms, filename):
120 """Write STL file ( molecules, filename )""" 121 self.doit(atoms, filename)
122 123
124 - def guiCallback(self):
125 newfile = self.vf.askFileSave(types = [('STL files', '*.stl'),], 126 title = 'Select STL files:') 127 print 'filename: ', newfile 128 if newfile != None: 129 self.doit(self.vf.getSelection(), newfile)
130 131 writeSTLGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 132 'menuButtonName':'File', 133 'menuEntryLabel':'Write STL'} 134 135 WriteSTLGUI = CommandGUI() 136 WriteSTLGUI.addMenuCommand('menuRoot', 'File', 'Write STL') 137
138 -class WriteColoredM(MVCommand):
139
140 - def onAddCmdToViewer(self):
141 if not self.vf.commands.has_key('msmsMol'): 142 self.vf.loadCommand('msmsCommands', 'msmsMol', 'Pmv', 143 topCommand = 0)
144
145 - def writeCMsurface(self, mol, filename):
146 """Write the MSMS surface corresponding to all selected molecules 147 in the .m format with rbg color for each vertex""" 148 149 if not hasattr(mol.geomContainer, 'msms'): #surface not computed 150 self.vf.msmsMol.guiCallback() 151 152 # get the msms surface object for that molecule 153 srf = mol.geomContainer.msms 154 155 # get the vertices and faces 156 vf, vi, fa = srf.getTriangles() 157 158 # get the colors 159 col = mol.geomContainer.geoms['msms'].materials[1028].prop[0] 160 assert len(col) == len(vf) 161 162 f = open(filename, 'w') 163 for i in xrange(len(vf)): 164 f.write("vertex %d %f %f %f %f %f %f\n" % (i+1, vf[i][0],vf[i][1],vf[i][2],col[i][0],col[i][1],col[i][2]) ) 165 166 for i in xrange(len(fa)): 167 face = fa[i] 168 f.write("face %d %d %d %d\n" % (i+1, fa[i][0]+1, fa[i][1]+1, fa[i][2]+1)) 169 f.close()
170 171
172 - def doit(self, nodes, filename):
173 self.log(nodes, filename) 174 175 if type(nodes)==types.StringType: 176 nodes = self.vf.Mols.NodesFromName(nodes) 177 178 assert isinstance(nodes,TreeNode) or isinstance(nodes,TreeNodeSet) 179 if isinstance(nodes,TreeNode): 180 nodes = TreeNodeSet([nodes]) 181 182 if len(nodes)==0: return 183 atoms = nodes.findType( Atom ) 184 molecules = atoms.top.uniq() 185 186 for mol in molecules: 187 self.writeCMsurface(mol, '%s.%s.m'%(filename, mol.name))
188 189
190 - def __call__(self, nodes, filename, **kw):
191 """None <- writeCM(self, nodes, filename, **kw) 192 Write colored .m files 193 nodes : current selection, or string representing nodes 194 filename: filename 195 """ 196 apply(self.doitWrapper, (nodes, filename), kw)
197
198 - def guiCallback(self):
199 newfile = self.vf.askFileSave(types = [('.m Files', '*.m'),], 200 title = "Select .m file:") 201 if newfile != None: 202 apply(self.doitWrapper, (self.vf.getSelection(), newfile), {})
203 204 writeColoredMGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 205 'menuButtonName':'File', 206 'menuEntryLabel':'Write ColoredM'} 207 208 WriteColoredMGUI = CommandGUI() 209 WriteColoredMGUI.addMenuCommand('menuRoot', 'File', 'Write ColoredM') 210 211 commandList = [ 212 #{'name':'writeSTL', 'cmd':WriteSTL(), 'gui':WriteSTLGUI }, 213 {'name':'writeCM', 'cmd':WriteColoredM(), 'gui':WriteColoredMGUI }, 214 ] 215
216 -def initModule(viewer):
217 218 for dict in commandList: 219 viewer.addCommand( dict['cmd'], dict['name'], dict['gui'])
220