pyNastran  0.5.0
pyNastran BDF Reader/Writer, OP2 Parser, and GUI
wgsReader.py
Go to the documentation of this file.
00001 ## GNU Lesser General Public License
00002 ## 
00003 ## Program pyNastran - a python interface to NASTRAN files
00004 ## Copyright (C) 2011-2012  Steven Doyle, Al Danial
00005 ## 
00006 ## Authors and copyright holders of pyNastran
00007 ## Steven Doyle <mesheb82@gmail.com>
00008 ## Al Danial    <al.danial@gmail.com>
00009 ## 
00010 ## This file is part of pyNastran.
00011 ## 
00012 ## pyNastran is free software: you can redistribute it and/or modify
00013 ## it under the terms of the GNU Lesser General Public License as published by
00014 ## the Free Software Foundation, either version 3 of the License, or
00015 ## (at your option) any later version.
00016 ## 
00017 ## pyNastran is distributed in the hope that it will be useful,
00018 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 ## GNU General Public License for more details.
00021 ## 
00022 ## You should have received a copy of the GNU Lesser General Public License
00023 ## along with pyNastran.  If not, see <http://www.gnu.org/licenses/>.
00024 ## 
00025 import copy
00026 from math import sin,cos
00027 from numpy import array,radians,dot,zeros
00028 
00029 class LaWGS_Panel(object):
00030     """
00031     @param rotate
00032     @param translate
00033     @param scale
00034     """
00035     def __init__(self,key,header,group):
00036         #print "key=%s \nheader=|%s|" %(key,header)   # ,iSymG
00037         (ID,nline,npnt,isyml,rx,ry,rz,tx,ty,tz,xScale,yScale,zScale,iSymG) = header.strip().split()
00038         print "ID=%s name=%s imax=%s jmax=%s" %(ID,key,nline,npnt)
00039         print "Rotate    = <%s,%s,%s>" %(rx,ry,rz)
00040         print "Translate = <%s,%s,%s>" %(tx,ty,rz)
00041         print "Scale     = <%s,%s,%s>" %(xScale,yScale,zScale)
00042 
00043         self.name = key
00044         self.rotate    = array([rx,ry,rz],'d')
00045         self.translate = array([tx,ty,tz],'d')
00046         self.scale     = array([xScale,yScale,zScale],'d')
00047         npnt  = int(npnt)
00048         nline = int(nline)
00049 
00050         self.nCols = npnt
00051         self.nRows = nline
00052         nGroupLines = npnt//2 # self.nCols
00053         isSingleLine = False
00054         if npnt%2==1: # self.nCols
00055             isSingleLine = True
00056             #nGroupLines+=1
00057             
00058 
00059         #X = array((nRows,nCols))
00060         #Y = array((nRows,nCols))
00061         #Z = array((nRows,nCols))
00062         points = []
00063 
00064         irow = 0
00065         #print "*****",group[-1]
00066         for iline in xrange(self.nRows):
00067             for row in xrange(nGroupLines):
00068                 line = group[irow]
00069                 #print " line = ",line
00070                 (x1,y1,z1,x2,y2,z2) = line.strip().split()
00071                 points.append( array([x1,y1,z1],'d') )
00072                 points.append( array([x2,y2,z2],'d') )
00073                 #print "points[%s]=%s" %(i,points[i])
00074                 #print "points[%s]=%s" %(i+1,points[i+1])
00075                 #print "---"
00076                 irow+=1
00077 
00078             if isSingleLine:
00079                 line = group[irow]
00080                 #print "*line = ",line
00081                 (x1,y1,z1) = line.strip().split()
00082                 points.append( array([x1,y1,z1],'d') )
00083                 irow+=1
00084             ###
00085         #for i,point in enumerate(points):
00086             #print "point[%s] = %s" %(i,point)
00087 
00088         n = 0
00089         #for n,point in enumerate(points):
00090         Points = []
00091         for i in xrange(self.nRows):
00092             points2 = []
00093             for j in xrange(self.nCols):
00094                 points2.append(points[n])
00095                 #jj = n%self.nCols
00096                 #ii = n/self.nCols
00097                 #print "n=%-2s i=%-2s j=%-2s ii=%-2s jj=%-2s" %(n,i,j,ii,jj)
00098                 n+=1
00099             ###
00100             Points.append(points2)
00101             #print "len(points[%s]) = %s" %(j,len(points2))
00102         #asdf
00103         ###
00104         self.points = Points
00105         #print "len(self.points) = %s" %(len(self.points))
00106     
00107     
00108     def buildRotationMatrix(self,r):
00109         """
00110         Form the rotation matrix used for geometrical transformations
00111         Taken from NASA TM 85767 defining LaWGS.
00112         """
00113         # rotation angles, degrees
00114         #r = radians([self.phi,self.theta,self.psi])
00115         #print "self.rotate = ",self.rotate
00116         r = [radians(ri) for ri in self.rotate]
00117         cPhi   = cos(r[0])
00118         sPhi   = sin(r[0])
00119         cTheta = cos(r[1])
00120         sTheta = sin(r[1])
00121         cPsi   = cos(r[2])
00122         sPsi   = sin(r[2])
00123 
00124         rot = zeros((3,3),'d')
00125         #print rot
00126         rot[0,0] =  cTheta*cPsi
00127         rot[1,0] =  cTheta*sPsi
00128         rot[2,0] = -sTheta
00129 
00130         rot[0,1] = -sPsi*cPhi + sTheta*cPsi*sPhi
00131         rot[1,1] =  cPsi*cPhi + sTheta*sPsi*sPhi
00132         rot[2,1] =  cTheta*sPhi
00133 
00134         rot[0,2] =  sPsi*sPhi + sTheta*cPsi*cPhi
00135         rot[1,2] = -cPsi*sPhi + sTheta*sPsi*cPhi
00136         rot[2,2] =  cTheta*cPhi
00137 
00138         return rot
00139 
00140     def updatePoints(self):
00141         rot = self.buildRotationMatrix(self.rotate)
00142         scale = self.scale
00143         translate = self.translate
00144         
00145         points = self.points
00146         Points2 = copy.deepcopy(points)
00147         print "size(points) = (%s,%s)\n" %(len(points),len(points[0]))
00148         for i in xrange(self.nRows):
00149             #points2 = []
00150             for j in xrange(self.nCols):
00151                 Points2[i][j] = scale*(dot(rot,points[i][j]) + translate)
00152             ###
00153         ###
00154         self.Points = Points2
00155 
00156     def getPoints(self):
00157         Points = []
00158         for i in xrange(self.nRows):
00159             #points2 = []
00160             for j in xrange(self.nCols):
00161                 Points.append( self.Points[i][j] )
00162             ###
00163         ###
00164         return Points,len(Points)
00165 
00166     def getElements(self,pointI):
00167         n = (self.nRows-1)*(self.nCols-1)
00168         #print "name=%s n=%s nout=%s" %(self.name,n)
00169         elements = []
00170 
00171         for i in xrange(self.nRows-1):
00172             for j in xrange(self.nCols-1):
00173                 elements.append(self.getElement(pointI,i,j))
00174         assert n==len(elements)
00175         return elements,n
00176 
00177     def getElement(self,pointI,j,i):
00178         p1 = (j  )*self.nCols+(i  )+pointI
00179         p2 = (j  )*self.nCols+(i+1)+pointI
00180         p3 = (j+1)*self.nCols+(i+1)+pointI
00181         p4 = (j+1)*self.nCols+(i  )+pointI
00182         return [p1,p2,p3,p4]
00183     
00184     def writeAsPlot3D(self,f):
00185         X = []
00186         Y = []
00187         Z = []
00188         for i in xrange(self.nRows):
00189             #points2 = []
00190             for j in xrange(self.nCols):
00191                 (x,y,z) = self.Points[i][j]
00192                 X.append(x)
00193                 Y.append(y)
00194                 Z.append(z)
00195             ###
00196         ###
00197 
00198         msg = ''
00199         for x in X:
00200             msg += '%s ' %(x)
00201         f.write(msg+'\n')
00202 
00203         msg = ''
00204         for y in Y:
00205             msg += '%s ' %(y)
00206         f.write(msg+'\n')
00207 
00208         msg = ''
00209         for z in Z:
00210             msg += '%s ' %(z)
00211         f.write(msg+'\n')
00212 
00213 class LaWGS(object):
00214     modelType = 'LaWGS'
00215     def __init__(self,filename='tmx1242.wgs'):
00216         self.filename = filename
00217     
00218     def readLaWGS(self):
00219         lawgs = open(self.filename,'r')
00220         lines = lawgs.readlines()
00221         
00222         nlines = len(lines)
00223         groups = {}
00224         name = ''
00225         group = []
00226         header = ''
00227         
00228         i=1
00229         while i<nlines:
00230             line = lines[i].rstrip()
00231             if line.strip()=='':
00232                 #print "found blank line, breaking..."
00233                 break
00234             #print line
00235             if "'" in line:
00236                 #print "if"
00237                 groups[name] = [header,group]
00238                 name = line.strip("' \t\r\n")
00239                 header = lines[i+1].rstrip()
00240                 group = []
00241                 i+=1
00242             else:
00243                 group.append(line)
00244             i+=1
00245         ###
00246         groups[name] = [header,group]
00247         
00248         del groups['']
00249         self.panels = {}
00250         for key,headerGroup in sorted(groups.iteritems()):
00251             header,group = headerGroup
00252             #if key=='BODY':
00253             if 1:
00254                 panel = LaWGS_Panel(key,header,group)
00255                 panel.updatePoints()
00256                 self.panels[key] = panel
00257         ###
00258 
00259     def getPointsElements(self):
00260         points = []
00261         elements = []
00262         pointI=0
00263         for (name,panel) in sorted(self.panels.iteritems()):
00264             (pointsI,pointi) = panel.getPoints()
00265             (elementsI,n) = panel.getElements(pointI)
00266             #print "elementsI = ",elementsI
00267             points += pointsI
00268             elements += elementsI
00269             pointI += pointi
00270             #print "name=%s len(AllElements)=%s len(allPoints)=%s" %(name,len(elements),len(points))
00271         ###
00272         #for point in points:
00273             #print point
00274         return points,elements
00275 
00276     def writeAsPlot3D(self,p3dname):
00277         f = open(p3dname,'wb')
00278 
00279         f.write('%s\n' %(len(self.panels)))
00280         for (name,panel) in sorted(self.panels.iteritems()):
00281             f.write('%s %s 1\n' %(panel.nRows,panel.nCols))
00282 
00283         for (name,panel) in sorted(self.panels.iteritems()):
00284             panel.writeAsPlot3D(f)
00285 
00286 if __name__=='__main__':
00287     lawgs = LaWGS('tmx1242.wgs')
00288     lawgs.readLaWGS()
 All Classes Namespaces Files Functions Variables