1 # VISU VISU_SWIG : binding of C++ implementation and Python
3 # Copyright (C) 2003 CEA/DEN, EDF R&D
12 from time import sleep
18 from omniORB import CORBA
20 from time import sleep
22 #NRI : interface MED dans le Module Engines !!!!
27 myNamingService = None
28 myLifeCycleCORBA = None
29 myNamingService = None
30 myLocalStudyManager = None
36 def Initialize(theORB, theNamingService, theLifeCycleCORBA, theStudyManager, theStudy, theDelay) :
37 global myORB, myNamingService, myLifeCycleCORBA, myLocalStudyManager, myLocalStudy
38 global mySession, myLocalVisu, myDelay
41 myNamingService = theNamingService
42 myLifeCycleCORBA = theLifeCycleCORBA
43 myLocalStudyManager = theStudyManager
44 mySession = myNamingService.Resolve("/Kernel/Session")
45 mySession.GetInterface()
48 myLocalVisu = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "VISU")
49 myLocalStudy = theStudy
50 myLocalVisu.SetCurrentStudy(myLocalStudy)
53 def StrToMap(theString) :
54 aPairList = split(theString,";")
57 for i in range(iEnd) :
58 aPair = split(aPairList[i],"=")
59 aMap[aPair[0]] = aPair[1]
62 def IntToEntity(theInt) :
73 def HasValue(theList, theValue) :
75 return theList.index(theValue) + 1
76 except StandardError, e :
79 def CreateStudyForMed(theVisu, theFileName) :
80 print "CreateStudyForMed ", theFileName,
81 if os.path.isfile(theFileName) :
82 if os.path.splitext(theFileName)[1] == '.med' :
84 aStudyName = os.path.basename(theFileName)
85 aStudyName = os.path.splitext(aStudyName)[0]
86 aStudyName = generateName(aStudyName)
87 print "myLocalStudyManager.NewStudy -", aStudyName, "...",
88 aStudy = myLocalStudyManager.NewStudy(aStudyName)
89 if aStudy is None : raise RuntimeError, "Error"
92 print "CreateStudyForMed - Creating a 3D viewer...",
93 theVisu.SetCurrentStudy(aStudy)
94 myViewManager = theVisu.GetViewManager()
95 if myViewManager is None : raise RuntimeError, "Error - theVisu.GetViewManager"
96 myView = myViewManager.Create3DView()
97 if myView is None : raise RuntimeError, "Error - myViewManager.Create3DView"
101 return [aStudy, myViewManager, myView]
102 raise RuntimeError, "Error: There is no shuch file."
104 def getMedObjectFromStudy(theStudy = myLocalStudy):
106 theStudy = myLocalStudy
107 mySO = theStudy.FindObject("Objet MED")
108 anAttr = mySO.FindAttribute("AttributeIOR")[1]
109 obj = myORB.string_to_object(anAttr.Value())
110 myObj = obj._narrow(SALOME_MED.MED)
113 def getMedSObject(theStudy = myLocalStudy ):
114 return theStudy.FindObject("Objet MED")
116 def getFieldObjectFromStudy(number, subnumber, theStudy = myLocalStudy):
118 theStudy = myLocalStudy
119 mySO = theStudy.FindObject("MEDFIELD")
121 raise Runtime, "getFieldObjectFromStudy mySO is None"
122 mysub = mySO.FindSubObject(number)[1]
124 mysubsub = mysub.FindSubObject(subnumber)[1]
126 Builder = theStudy.NewBuilder()
127 anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
128 obj = myORB.string_to_object(anAttr.Value())
129 myObj = obj._narrow(SALOME_MED.FIELDINT)
131 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
134 print "ERROR: No Field Object stored in this Study"
137 def SObjectToObject(theSObject) :
139 if theSObject is None :
140 print "SObjectToObject : argument is None"
142 res,Attr = theSObject.FindAttribute("AttributeIOR")
143 if (res != 0) and (Attr is not None) :
144 anIOR = Attr._narrow(SALOMEDS.AttributeIOR);
145 aValue = anIOR.Value();
146 if(len(aValue) != 0) :
147 anObj = myORB.string_to_object(aValue);
149 print "SObjectToObject - IOR = ''"
152 def CreatePrsForMed(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt) :
154 global myLifeCycleCORBA
155 print "lcc.FindOrLoadComponent...",
156 aMedComp = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "MED")
157 if aMedComp is None : print "Error"
160 aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
162 print "aMedComp.readStructFileWithFieldType...",
163 aMedComp.readStructFileWithFieldType(theFileName,myLocalStudy._get_Name())
164 aMedSObj = getMedSObject(myLocalStudy)
165 if aMedSObj is None : raise RuntimeError, "Error"
168 print "theVisu.ImportMed...",
169 aResult = theVisu.ImportMed(aMedSObj)
170 if aResult is None : raise RuntimeError, "Error"
172 aVISUObjList.append(aResult)
173 aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
175 aFolderIter = myLocalStudy.NewChildIterator(aMedSObj);
176 while aFolderIter.More() :
177 aFolderSObj = aFolderIter.Value()
179 anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
180 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
181 aFolderName = anAttr.Value()
182 print " ", aFolderName
184 if aFolderName == 'MEDMESH' :
185 aMeshIter = myLocalStudy.NewChildIterator(aFolderSObj);
186 while aMeshIter.More() :
187 aMeshSObj = aMeshIter.Value()
189 anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
190 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
191 aMeshName = anAttr.Value()
194 aSupportIter = myLocalStudy.NewChildIterator(aMeshSObj);
195 while aSupportIter.More() :
196 aSupportSObj = aSupportIter.Value()
198 anAttr = aSupportSObj.FindAttribute("AttributeName")[1]
199 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
200 aSupportName = anAttr.Value()
201 print " ", aSupportName
203 if aFolderName == 'MEDFIELD' :
204 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
205 while aFieldIter.More() :
206 aFieldSObj = aFieldIter.Value()
208 anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
209 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
210 aFieldName = anAttr.Value()
211 print " ", aFieldName
213 print "theVisu.ImportMed...",
214 aResult = theVisu.ImportMed(aFieldSObj)
215 if aResult is None : raise RuntimeError, "Error"
217 aVISUObjList.append(aResult)
218 aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
220 aTimeStampIter = myLocalStudy.NewChildIterator(aFieldSObj);
221 if aTimeStampIter.More() :
222 aTimeStampIter.Next()
223 while aTimeStampIter.More() :
224 aTimeStampSObj = aTimeStampIter.Value()
225 aTimeStampIter.Next()
226 anAttr = aTimeStampSObj.FindAttribute("AttributeName")[1]
227 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
228 aTimeStampName = anAttr.Value()
229 print " ", aTimeStampName
231 print "theVisu.ImportMed...",
232 aMedField = SObjectToObject(aTimeStampSObj)
233 aResult = theVisu.ImportMedFiled(aMedField)
234 if aResult is None : raise RuntimeError, "Error"
236 aVISUObjList.append(aResult)
237 aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
241 # except Exception, e:
244 def CreatePrsForFile(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt) :
246 aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
248 print "theVisu.ImportMed..."
249 aResult = theVisu.ImportFile(theFileName)
250 if aResult is None : raise RuntimeError, "Error"
253 aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
257 # except Exception, e:
260 def CreatePrsForResult(theVisu, theResult, theView, thePrsTypeList, thePictureDir, thePictureExt) :
262 print "CreatePrsForResult - myLocalStudy.FindObjectIOR...",
263 myLocalStudy = theVisu.GetCurrentStudy()
264 aSObj = myLocalStudy.FindObjectIOR(theResult.GetID())
265 if aSObj is None : raise RuntimeError, "Error"
267 aMeshIter = myLocalStudy.NewChildIterator(aSObj);
268 while aMeshIter.More() :
269 aMeshSObj = aMeshIter.Value()
271 anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
273 aMeshSObj = aMeshIter.Value()
275 anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
276 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
277 aMeshName = anAttr.Value()
280 aFolderIter = myLocalStudy.NewChildIterator(aMeshSObj);
281 while aFolderIter.More() :
282 aFolderSObj = aFolderIter.Value()
284 anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
285 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
286 aFolderName = anAttr.Value()
287 print " ", aFolderName
289 if aFolderName == 'Families' :
290 anEntityIter = myLocalStudy.NewChildIterator(aFolderSObj);
291 while anEntityIter.More() :
292 anEntitySObj = anEntityIter.Value()
294 anAttr = anEntitySObj.FindAttribute("AttributeName")[1]
295 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
296 anEntityName = anAttr.Value()
299 if anEntityName == 'onNodes' :
302 if anEntityName == 'onEdges' :
305 if anEntityName == 'onFaces' :
308 if anEntityName == 'onCells' :
312 if HasValue(thePrsTypeList,VISU.TMESH) :
313 print " ", anEntityName,
314 aMesh = theVisu.MeshOnEntity(theResult,aMeshName,anEntity)
315 if aMesh is None : print "Error"
317 theView.DisplayOnly(aMesh)
319 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "." + thePictureExt
320 aPictureName = re.sub("\s+","_", aPictureName);
321 theView.SavePicture(aPictureName)
322 aVISUObjList.append(aMesh)
324 anFamilyIter = myLocalStudy.NewChildIterator(anEntitySObj);
325 while anFamilyIter.More() :
326 aFamilySObj = anFamilyIter.Value()
328 anAttr = aFamilySObj.FindAttribute("AttributeName")[1]
329 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
330 anFamilyName = anAttr.Value()
331 if HasValue(thePrsTypeList,VISU.TMESH) :
332 print " ", anFamilyName,
333 aMesh = theVisu.FamilyMeshOnEntity(theResult,aMeshName,anEntity,anFamilyName)
334 if aMesh is None : print "Error"
336 theView.DisplayOnly(aMesh)
338 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + anFamilyName + "." + thePictureExt
339 aPictureName = re.sub("\s+","_", aPictureName);
340 theView.SavePicture(aPictureName)
341 aVISUObjList.append(aMesh)
343 if aFolderName == 'Groups' :
344 aGroupIter = myLocalStudy.NewChildIterator(aFolderSObj);
345 while aGroupIter.More() :
346 aGroupSObj = aGroupIter.Value()
348 anAttr = aGroupSObj.FindAttribute("AttributeName")[1]
349 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
350 aGroupName = anAttr.Value()
351 if HasValue(thePrsTypeList,VISU.TMESH) :
352 print " ", aGroupName,
353 aMesh = theVisu.GroupMesh(theResult,aMeshName,aGroupName)
354 if aMesh is None : print "Error"
356 theView.DisplayOnly(aMesh)
358 aPictureName = thePictureDir + aMeshName + "_" + aGroupName + "." + thePictureExt
359 aPictureName = re.sub("\s+","_", aPictureName);
360 theView.SavePicture(aPictureName)
361 aVISUObjList.append(aMesh)
363 if aFolderName == 'Fields' :
364 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
365 while aFieldIter.More() :
366 aFieldSObj = aFieldIter.Value()
368 anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
369 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
370 aFieldName = anAttr.Value()
371 print " ", aFieldName
373 anAttr = aFieldSObj.FindAttribute("AttributeComment")[1]
374 anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
375 aFieldComment = anAttr.Value()
376 aMap = StrToMap(aFieldComment)
378 aTimeStampIter = myLocalStudy.NewChildIterator(aFieldSObj);
379 if aTimeStampIter.More() :
380 aTimeStampIter.Next()
381 while aTimeStampIter.More() :
382 aTimeStampSObj = aTimeStampIter.Value()
383 aTimeStampIter.Next()
384 anAttr = aTimeStampSObj.FindAttribute("AttributeName")[1]
385 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
386 aTimeStampName = anAttr.Value()
387 print " ", aTimeStampName
389 anAttr = aTimeStampSObj.FindAttribute("AttributeComment")[1]
390 anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
391 aTimeStampComment = anAttr.Value()
392 aMap = StrToMap(aTimeStampComment)
393 aMeshName = aMap["myMeshName"]
394 aFieldName = aMap["myFieldName"]
395 aTimeStampId = atoi(aMap["myTimeStampId"])
396 anEntity = IntToEntity(aMap["myEntityId"])
397 anEntityId = str(aMap["myEntityId"]);
398 #print aMeshName, aFieldName, anEntity, aTimeStampId
399 if HasValue(thePrsTypeList,VISU.TSCALARMAP) :
400 print " Creating ScalarMapOnField",
401 aPrsObj = theVisu.ScalarMapOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
402 if aPrsObj is None : print "Error"
404 theView.DisplayOnly(aPrsObj)
406 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSCALARMAP." + thePictureExt
407 aPictureName = re.sub("\s+","_", aPictureName);
408 theView.SavePicture(aPictureName)
409 aVISUObjList.append(aPrsObj)
411 if HasValue(thePrsTypeList,VISU.TISOSURFACE) :
412 print " Creating IsoSurfacesOnField",
413 aPrsObj = theVisu.IsoSurfacesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
414 if aPrsObj is None : print "Error"
416 theView.DisplayOnly(aPrsObj)
418 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TISOSURFACE." + thePictureExt
419 aPictureName = re.sub("\s+","_", aPictureName);
420 theView.SavePicture(aPictureName)
421 aVISUObjList.append(aPrsObj)
423 if HasValue(thePrsTypeList,VISU.TCUTPLANES) :
424 print " Creating CutPlanesOnField",
425 aPrsObj = theVisu.CutPlanesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
426 if aPrsObj is None : print "Error"
428 aPrsObj.SetOrientationType(VISU.CutPlanes.ZX)
429 theView.DisplayOnly(aPrsObj)
431 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTPLANES." + thePictureExt
432 aPictureName = re.sub("\s+","_", aPictureName)
433 theView.SavePicture(aPictureName)
434 aVISUObjList.append(aPrsObj)
436 if HasValue(thePrsTypeList,VISU.TCUTLINES) :
437 print " Creating CutLinesOnField",
438 aPrsObj = theVisu.CutLinesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
439 if aPrsObj is None : print "Error"
441 aPrsObj.SetOrientationType(VISU.CutPlanes.XY)
442 aPrsObj.SetOrientationType2(VISU.CutPlanes.ZX)
443 theView.DisplayOnly(aPrsObj)
445 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTLINES." + thePictureExt
446 aPictureName = re.sub("\s+","_", aPictureName)
447 theView.SavePicture(aPictureName)
448 aVISUObjList.append(aPrsObj)
450 aNumComponent = atoi(aMap["myNumComponent"])
451 if aNumComponent > 1 :
452 if HasValue(thePrsTypeList,VISU.TDEFORMEDSHAPE) :
453 print " Creating DeformedShapeOnField",
454 aPrsObj = theVisu.DeformedShapeOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
455 if aPrsObj is None : print "Error"
457 theView.DisplayOnly(aPrsObj)
459 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TDEFORMEDSHAPE." + thePictureExt
460 aPictureName = re.sub("\s+","_", aPictureName)
461 theView.SavePicture(aPictureName)
462 aVISUObjList.append(aPrsObj)
464 if HasValue(thePrsTypeList,VISU.TVECTORS) :
465 print " Creating VectorsOnField",
466 aPrsObj = theVisu.VectorsOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
467 if aPrsObj is None : print "Error"
469 theView.DisplayOnly(aPrsObj)
471 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TVECTORS." + thePictureExt
472 aPictureName = re.sub("\s+","_", aPictureName)
473 theView.SavePicture(aPictureName)
474 aVISUObjList.append(aPrsObj)
476 if HasValue(thePrsTypeList,VISU.TSTREAMLINES) :
477 print " Creating StreamLinesOnField",
478 aPrsObj = theVisu.StreamLinesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
479 if aPrsObj is None : print "Error"
481 theView.DisplayOnly(aPrsObj)
483 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSTREAMLINES." + thePictureExt
484 aPictureName = re.sub("\s+","_", aPictureName)
485 theView.SavePicture(aPictureName)
486 aVISUObjList.append(aPrsObj)
491 def generateName(prefix = None):
493 int = whrandom.randint(1,1000);
495 return "Study" + str(int)
497 return prefix + str(int)