pyNastran
0.5.0
pyNastran BDF Reader/Writer, OP2 Parser, and GUI
|
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 # pylint: disable=C0103,R0902,R0904,R0914 00026 00027 from __future__ import division, print_function 00028 import sys 00029 import copy 00030 00031 class BDFCard(object): 00032 def __init__(self, card=None, oldCardObj=None, debug=False): 00033 self.debug = debug 00034 if card: 00035 self.card = self._wipeEmptyFields(card) 00036 #self.oldCard = oldCardObj 00037 self.nfields = len(self.card) 00038 else: 00039 self.oldCard = None 00040 self.card = None 00041 self.nfields = None 00042 00043 def Is(self, cardName): 00044 """ 00045 Returns True if the card is of type cardName 00046 @param self the object pointer 00047 @param cardName the cardName to compare against 00048 @retval IsACardName True/False 00049 """ 00050 if self.card.field(0) == cardName.upper(): 00051 return True 00052 return False 00053 00054 def _wipeEmptyFields(self, card): 00055 """ 00056 Removes an trailing Nones from the card. Also converts empty strings to None. 00057 @param self the object pointer 00058 @param card the fields on the card as a list 00059 @retval shortCard the card with no trailing blank fields 00060 """ 00061 cardB = [] 00062 for field in card: 00063 if isinstance(field, str) and field.strip() == '': 00064 field = None 00065 cardB.append(field) 00066 00067 i = 0 00068 iMax = 0 00069 while i < len(card): 00070 if cardB[i] is not None: 00071 iMax = i 00072 i += 1 00073 #print "i=%s iMax=%s"%(i,iMax) 00074 #print "cardC = ",cardB[:iMax+1],'\n' 00075 00076 return cardB[:iMax+1] 00077 00078 def __repr__(self): 00079 """ 00080 prints the card as a list 00081 @param self the object pointer 00082 @retval msg the string representation of the card 00083 """ 00084 return str(self.card) 00085 00086 def nFields(self): 00087 """ 00088 gets how many fields are on the card 00089 @param self the object pointer 00090 @retval nFields the number of fields on the card 00091 """ 00092 return self.nfields 00093 00094 def fields(self, i=0, j=None, defaults=None, debug=False): 00095 """ 00096 gets multiple fields on the card 00097 @param self the object pointer 00098 @param i the ith field on the card (following list notation) 00099 @param j the jth field on the card (None means till the end of the card) 00100 @param defaults the default value for the field (as a list) len(defaults)=i-j-1 00101 @param debug prints out the values at intermediate steps 00102 @retval the values on the ith-jth fields 00103 """ 00104 if defaults is None: 00105 defaults = [] 00106 if j is None: 00107 if self.nfields == None: 00108 return [None] 00109 j = self.nfields 00110 00111 if defaults == []: 00112 defaults = [None]*(j-i+1) 00113 out = [] 00114 00115 d = 0 00116 for n in xrange(i, j): 00117 if debug: 00118 print(" default = %s" %(defaults[d])) 00119 value = self.field(n, defaults[d]) 00120 if debug: 00121 print(" self.field(%s) = %s" %(n, self.field(n))) 00122 out.append(value) 00123 d+=1 00124 ### 00125 return out 00126 00127 def field(self, i, default=None): 00128 """ 00129 gets the ith field on the card 00130 @param self the object pointer 00131 @param i the ith field on the card (following list notation) 00132 @param default the default value for the field 00133 @retval the value on the ith field 00134 """ 00135 if (i < self.nfields and self.card[i] is not None 00136 and self.card[i] is not ''): 00137 return self.card[i] 00138 else: 00139 return default 00140 00141 def replaceExpression(self, fieldNew, fieldOld, 00142 replaceChar='=', replaceChar2=''): 00143 """used for nastran = format""" 00144 fieldNew = fieldNew.replace(replaceChar, str(fieldOld)+replaceChar2) 00145 typeOld = type(fieldOld) 00146 if isinstance(fieldOld, int) or isinstance(fieldOld, float): 00147 fieldNew = typeOld(eval(fieldNew)) 00148 else: 00149 fieldNew = str(fieldNew) 00150 return fieldNew 00151 00152 def isSameName(self): 00153 """used for nastran = format""" 00154 if '=' in self.card[0]: 00155 return True 00156 return False 00157 00158 def applyOldFields(self, cardCount=0): 00159 """used for nastran = format""" 00160 if not self.isSameName(): 00161 return 00162 00163 self.cardCount = cardCount 00164 stop = False 00165 self.cardTextOld = self.card 00166 if self.nfields == 1 and self.oldCard.nfields > 1: 00167 #self.nfields = self.oldCard.nfields 00168 #self.applyOldFields(self,cardCount=1) 00169 cardCount = self.oldCard.cardCount+cardCount 00170 if self.debug: 00171 print("newCount = %s" %(cardCount)) 00172 self.card = copy.deepcopy(self.oldCard.cardTextOld) 00173 self.nfields = len(self.card) 00174 self.oldCard.nfields = len(self.oldCard.card) 00175 00176 if self.debug: 00177 print("oldCard = %s" %(self.oldCard)) 00178 print("selfCard = %s" %(self.card)) 00179 #stop = True 00180 00181 fieldsNew = self.fields() 00182 fieldsOld = self.oldCard.fields() 00183 00184 maxLength = max(self.nFields(), self.oldCard.nFields()) 00185 minLength = min(self.nFields(), self.oldCard.nFields()) 00186 00187 cardBuilt = [fieldsOld[0]] 00188 00189 for i in xrange(1, minLength): 00190 fieldOld = fieldsOld[i] 00191 fieldNew = fieldsNew[i] 00192 00193 a = "|%s|" %(fieldNew) 00194 if '*' in fieldNew: 00195 newChar = '+%s*' %(cardCount+1) 00196 fieldNew = self.replaceExpression(fieldNew, fieldOld, 00197 '*', newChar) 00198 elif '/' in fieldNew: 00199 newChar = '-%s*' %(cardCount+1) 00200 fieldNew = self.replaceExpression(fieldNew, fieldOld, 00201 '/', newChar) 00202 elif '==' == fieldNew: 00203 #break 00204 fieldNew = fieldOld 00205 elif '=' in fieldNew: 00206 if fieldNew == '=': 00207 fieldNew = fieldOld 00208 elif fieldNew == '==': # handle this in the max length section 00209 pass 00210 else: # replace = with str(expression) 00211 fieldNew = self.replaceExpression(fieldNew, fieldOld) 00212 ### 00213 elif '' == fieldNew: 00214 fieldNew = fieldOld 00215 else: 00216 b = "|%s|" %(fieldNew) 00217 c = "|%s|" %(fieldOld) 00218 print("i=%s fieldStart %-10s fieldNew %-10s fieldOld %-10s" %(i, a, b, c)) 00219 raise RuntimeError('unhandled case...') 00220 if self.debug: 00221 b = "|%s|" %(fieldNew) 00222 c = "|%s|" %(fieldOld) 00223 print("i=%s fieldStart %-10s fieldNew %-10s fieldOld %-10s" %(i, a, b, c)) 00224 cardBuilt.append(fieldNew) 00225 i += 1 00226 00227 if maxLength < len(cardBuilt): 00228 # the new card is longer than builtCard 00229 for i in xrange(self.nfields, maxLength): 00230 cardBuilt.append(self.card[i]) 00231 elif len(cardBuilt) < self.oldCard.nfields: 00232 # builtCard is shorter than the old card 00233 for i in xrange(self.nfields, maxLength): 00234 cardBuilt.append(self.oldCard.field(i)) 00235 #else: # same length 00236 #pass 00237 00238 if self.debug: 00239 print("cardBuilt = %s" %(cardBuilt)) 00240 self.card = cardBuilt 00241 self.nfields = len(self.card) 00242 00243 if stop: 00244 sys.exit("stopping in applyOldFields") 00245 00246 def getOldField(self, i): 00247 """used for nastran = format""" 00248 return self.oldCard.field(i) 00249 00250 def wipeEmptyFields(card): 00251 """ 00252 Removes an trailing Nones from the card. 00253 Also converts empty strings to None. 00254 00255 @param self 00256 the object pointer 00257 @param card 00258 the fields on the card as a list 00259 @retval shortCard 00260 the card with no trailing blank fields 00261 """ 00262 cardB = [] 00263 for field in card: 00264 if isinstance(field, str) and field.strip() == '': 00265 field = None 00266 cardB.append(field) 00267 00268 i = 0 00269 iMax = 0 00270 while i < len(card): 00271 if cardB[i] is not None: 00272 iMax = i 00273 i += 1 00274 return cardB[:iMax+1]