1 # VISU VISU_SWIG : binding of C++ implementation and Python
3 # Copyright (C) 2003 CEA/DEN, EDF R&D
12 from time import sleep
17 from omniORB import CORBA
19 from time import sleep
22 myNamingService = None
23 myLifeCycleCORBA = None
24 myNamingService = None
25 myLocalStudyManager = None
31 def Initialize(theORB, theNamingService, theLifeCycleCORBA, theStudyManager, theStudy, theDelay) :
32 global myORB, myNamingService, myLifeCycleCORBA, myLocalStudyManager, myLocalStudy
33 global mySession, myLocalVisu, myDelay
36 myNamingService = theNamingService
37 myLifeCycleCORBA = theLifeCycleCORBA
38 myLocalStudyManager = theStudyManager
39 mySession = myNamingService.Resolve("/Kernel/Session")
40 mySession.GetInterface()
43 myLocalVisu = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "Visu")
44 myLocalStudy = theStudy
45 myLocalVisu.SetCurrentStudy(myLocalStudy)
48 def StrToMap(theString) :
49 aPairList = split(theString,";")
52 for i in range(iEnd) :
53 aPair = split(aPairList[i],"=")
54 aMap[aPair[0]] = aPair[1]
57 def IntToEntity(theInt) :
68 def HasValue(theList, theValue) :
70 return theList.index(theValue) + 1
71 except StandardError, e :
74 def CreateStudyForMed(theVisu, theFileName) :
75 print "CreateStudyForMed ", theFileName,
76 if os.path.isfile(theFileName) :
77 if os.path.splitext(theFileName)[1] == '.med' :
79 aStudyName = os.path.basename(theFileName)
80 aStudyName = os.path.splitext(aStudyName)[0]
81 aStudyName = generateName(aStudyName)
82 print "myLocalStudyManager.NewStudy -", aStudyName, "...",
83 aStudy = myLocalStudyManager.NewStudy(aStudyName)
84 if aStudy is None : raise RuntimeError, "Error"
87 print "CreateStudyForMed - Creating a 3D viewer...",
88 theVisu.SetCurrentStudy(aStudy)
89 myViewManager = theVisu.GetViewManager()
90 if myViewManager is None : raise RuntimeError, "Error - theVisu.GetViewManager"
91 myView = myViewManager.Create3DView()
92 if myView is None : raise RuntimeError, "Error - myViewManager.Create3DView"
96 return [aStudy, myViewManager, myView]
97 raise RuntimeError, "Error: There is no shuch file."
99 def getMedObjectFromStudy(theStudy = myLocalStudy):
101 theStudy = myLocalStudy
102 mySO = theStudy.FindObject("Objet MED")
103 anAttr = mySO.FindAttribute("AttributeIOR")[1]
104 obj = myORB.string_to_object(anAttr.Value())
105 myObj = obj._narrow(SALOME_MED.MED)
108 def getMedSObject(theStudy = myLocalStudy ):
109 return theStudy.FindObject("Objet MED")
111 def getFieldObjectFromStudy(number, subnumber, theStudy = myLocalStudy):
113 theStudy = myLocalStudy
114 mySO = theStudy.FindObject("MEDFIELD")
116 raise Runtime, "getFieldObjectFromStudy mySO is None"
117 mysub = mySO.FindSubObject(number)[1]
119 mysubsub = mysub.FindSubObject(subnumber)[1]
121 Builder = theStudy.NewBuilder()
122 anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
123 obj = myORB.string_to_object(anAttr.Value())
124 myObj = obj._narrow(SALOME_MED.FIELDINT)
126 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
129 print "ERROR: No Field Object stored in this Study"
132 def SObjectToObject(theSObject) :
134 if theSObject is None :
135 print "SObjectToObject : argument is None"
137 res,Attr = theSObject.FindAttribute("AttributeIOR")
138 if (res != 0) and (Attr is not None) :
139 anIOR = Attr._narrow(SALOMEDS.AttributeIOR);
140 aValue = anIOR.Value();
141 if(len(aValue) != 0) :
142 anObj = myORB.string_to_object(aValue);
144 print "SObjectToObject - IOR = ''"
147 def CreatePrsForMed(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt) :
149 global myLifeCycleCORBA
150 print "lcc.FindOrLoadComponent...",
151 aMedComp = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "Med")
152 if aMedComp is None : print "Error"
155 aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
157 print "aMedComp.readStructFileWithFieldType...",
158 aMedComp.readStructFileWithFieldType(theFileName,myLocalStudy._get_Name())
159 aMedSObj = getMedSObject(myLocalStudy)
160 if aMedSObj is None : raise RuntimeError, "Error"
163 print "theVisu.ImportMed...",
164 aResult = theVisu.ImportMed(aMedSObj)
165 if aResult is None : raise RuntimeError, "Error"
167 aVISUObjList.append(aResult)
168 aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
170 aFolderIter = myLocalStudy.NewChildIterator(aMedSObj);
171 while aFolderIter.More() :
172 aFolderSObj = aFolderIter.Value()
174 anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
175 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
176 aFolderName = anAttr.Value()
177 print " ", aFolderName
179 if aFolderName == 'MEDMESH' :
180 aMeshIter = myLocalStudy.NewChildIterator(aFolderSObj);
181 while aMeshIter.More() :
182 aMeshSObj = aMeshIter.Value()
184 anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
185 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
186 aMeshName = anAttr.Value()
189 aSupportIter = myLocalStudy.NewChildIterator(aMeshSObj);
190 while aSupportIter.More() :
191 aSupportSObj = aSupportIter.Value()
193 anAttr = aSupportSObj.FindAttribute("AttributeName")[1]
194 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
195 aSupportName = anAttr.Value()
196 print " ", aSupportName
198 if aFolderName == 'MEDFIELD' :
199 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
200 while aFieldIter.More() :
201 aFieldSObj = aFieldIter.Value()
203 anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
204 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
205 aFieldName = anAttr.Value()
206 print " ", aFieldName
208 print "theVisu.ImportMed...",
209 aResult = theVisu.ImportMed(aFieldSObj)
210 if aResult is None : raise RuntimeError, "Error"
212 aVISUObjList.append(aResult)
213 aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
215 aTimeStampIter = myLocalStudy.NewChildIterator(aFieldSObj);
216 if aTimeStampIter.More() :
217 aTimeStampIter.Next()
218 while aTimeStampIter.More() :
219 aTimeStampSObj = aTimeStampIter.Value()
220 aTimeStampIter.Next()
221 anAttr = aTimeStampSObj.FindAttribute("AttributeName")[1]
222 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
223 aTimeStampName = anAttr.Value()
224 print " ", aTimeStampName
226 print "theVisu.ImportMed...",
227 aMedField = SObjectToObject(aTimeStampSObj)
228 aResult = theVisu.ImportMedFiled(aMedField)
229 if aResult is None : raise RuntimeError, "Error"
231 aVISUObjList.append(aResult)
232 aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
236 # except Exception, e:
239 def CreatePrsForFile(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt) :
241 aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
243 print "theVisu.ImportMed..."
244 aResult = theVisu.ImportFile(theFileName)
245 if aResult is None : raise RuntimeError, "Error"
248 aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
252 # except Exception, e:
255 def CreatePrsForResult(theVisu, theResult, theView, thePrsTypeList, thePictureDir, thePictureExt) :
257 print "CreatePrsForResult - myLocalStudy.FindObjectIOR...",
258 myLocalStudy = theVisu.GetCurrentStudy()
259 aSObj = myLocalStudy.FindObjectIOR(theResult.GetID())
260 if aSObj is None : raise RuntimeError, "Error"
262 aMeshIter = myLocalStudy.NewChildIterator(aSObj);
263 while aMeshIter.More() :
264 aMeshSObj = aMeshIter.Value()
266 anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
268 aMeshSObj = aMeshIter.Value()
270 anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
271 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
272 aMeshName = anAttr.Value()
275 aFolderIter = myLocalStudy.NewChildIterator(aMeshSObj);
276 while aFolderIter.More() :
277 aFolderSObj = aFolderIter.Value()
279 anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
280 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
281 aFolderName = anAttr.Value()
282 print " ", aFolderName
284 if aFolderName == 'Families' :
285 anEntityIter = myLocalStudy.NewChildIterator(aFolderSObj);
286 while anEntityIter.More() :
287 anEntitySObj = anEntityIter.Value()
289 anAttr = anEntitySObj.FindAttribute("AttributeName")[1]
290 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
291 anEntityName = anAttr.Value()
294 if anEntityName == 'onNodes' :
297 if anEntityName == 'onEdges' :
300 if anEntityName == 'onFaces' :
303 if anEntityName == 'onCells' :
307 if HasValue(thePrsTypeList,VISU.TMESH) :
308 print " ", anEntityName,
309 aMesh = theVisu.MeshOnEntity(theResult,aMeshName,anEntity)
310 if aMesh is None : print "Error"
312 theView.DisplayOnly(aMesh)
314 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "." + thePictureExt
315 aPictureName = re.sub("\s+","_", aPictureName);
316 theView.SavePicture(aPictureName)
317 aVISUObjList.append(aMesh)
319 anFamilyIter = myLocalStudy.NewChildIterator(anEntitySObj);
320 while anFamilyIter.More() :
321 aFamilySObj = anFamilyIter.Value()
323 anAttr = aFamilySObj.FindAttribute("AttributeName")[1]
324 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
325 anFamilyName = anAttr.Value()
326 if HasValue(thePrsTypeList,VISU.TMESH) :
327 print " ", anFamilyName,
328 aMesh = theVisu.FamilyMeshOnEntity(theResult,aMeshName,anEntity,anFamilyName)
329 if aMesh is None : print "Error"
331 theView.DisplayOnly(aMesh)
333 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + anFamilyName + "." + thePictureExt
334 aPictureName = re.sub("\s+","_", aPictureName);
335 theView.SavePicture(aPictureName)
336 aVISUObjList.append(aMesh)
338 if aFolderName == 'Groups' :
339 aGroupIter = myLocalStudy.NewChildIterator(aFolderSObj);
340 while aGroupIter.More() :
341 aGroupSObj = aGroupIter.Value()
343 anAttr = aGroupSObj.FindAttribute("AttributeName")[1]
344 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
345 aGroupName = anAttr.Value()
346 if HasValue(thePrsTypeList,VISU.TMESH) :
347 print " ", aGroupName,
348 aMesh = theVisu.GroupMesh(theResult,aMeshName,aGroupName)
349 if aMesh is None : print "Error"
351 theView.DisplayOnly(aMesh)
353 aPictureName = thePictureDir + aMeshName + "_" + aGroupName + "." + thePictureExt
354 aPictureName = re.sub("\s+","_", aPictureName);
355 theView.SavePicture(aPictureName)
356 aVISUObjList.append(aMesh)
358 if aFolderName == 'Fields' :
359 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
360 while aFieldIter.More() :
361 aFieldSObj = aFieldIter.Value()
363 anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
364 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
365 aFieldName = anAttr.Value()
366 print " ", aFieldName
368 anAttr = aFieldSObj.FindAttribute("AttributeComment")[1]
369 anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
370 aFieldComment = anAttr.Value()
371 aMap = StrToMap(aFieldComment)
373 aTimeStampIter = myLocalStudy.NewChildIterator(aFieldSObj);
374 if aTimeStampIter.More() :
375 aTimeStampIter.Next()
376 while aTimeStampIter.More() :
377 aTimeStampSObj = aTimeStampIter.Value()
378 aTimeStampIter.Next()
379 anAttr = aTimeStampSObj.FindAttribute("AttributeName")[1]
380 anAttr = anAttr._narrow(SALOMEDS.AttributeName);
381 aTimeStampName = anAttr.Value()
382 print " ", aTimeStampName
384 anAttr = aTimeStampSObj.FindAttribute("AttributeComment")[1]
385 anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
386 aTimeStampComment = anAttr.Value()
387 aMap = StrToMap(aTimeStampComment)
388 aMeshName = aMap["myMeshName"]
389 aFieldName = aMap["myFieldName"]
390 aTimeStampId = atoi(aMap["myTimeStampId"])
391 anEntity = IntToEntity(aMap["myEntityId"])
392 anEntityId = str(aMap["myEntityId"]);
393 #print aMeshName, aFieldName, anEntity, aTimeStampId
394 if HasValue(thePrsTypeList,VISU.TSCALARMAP) :
395 print " Creating ScalarMapOnField",
396 aPrsObj = theVisu.ScalarMapOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
397 if aPrsObj is None : print "Error"
399 theView.DisplayOnly(aPrsObj)
401 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSCALARMAP." + thePictureExt
402 aPictureName = re.sub("\s+","_", aPictureName);
403 theView.SavePicture(aPictureName)
404 aVISUObjList.append(aPrsObj)
406 if HasValue(thePrsTypeList,VISU.TISOSURFACE) :
407 print " Creating IsoSurfacesOnField",
408 aPrsObj = theVisu.IsoSurfacesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
409 if aPrsObj is None : print "Error"
411 theView.DisplayOnly(aPrsObj)
413 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TISOSURFACE." + thePictureExt
414 aPictureName = re.sub("\s+","_", aPictureName);
415 theView.SavePicture(aPictureName)
416 aVISUObjList.append(aPrsObj)
418 if HasValue(thePrsTypeList,VISU.TCUTPLANES) :
419 print " Creating CutPlanesOnField",
420 aPrsObj = theVisu.CutPlanesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
421 if aPrsObj is None : print "Error"
423 aPrsObj.SetOrientationType(VISU.CutPlanes.ZX)
424 theView.DisplayOnly(aPrsObj)
426 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTPLANES." + thePictureExt
427 aPictureName = re.sub("\s+","_", aPictureName)
428 theView.SavePicture(aPictureName)
429 aVISUObjList.append(aPrsObj)
431 if HasValue(thePrsTypeList,VISU.TCUTLINES) :
432 print " Creating CutLinesOnField",
433 aPrsObj = theVisu.CutLinesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
434 if aPrsObj is None : print "Error"
436 aPrsObj.SetOrientationType(VISU.CutPlanes.XY)
437 aPrsObj.SetOrientationType2(VISU.CutPlanes.ZX)
438 theView.DisplayOnly(aPrsObj)
440 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTLINES." + thePictureExt
441 aPictureName = re.sub("\s+","_", aPictureName)
442 theView.SavePicture(aPictureName)
443 aVISUObjList.append(aPrsObj)
445 aNumComponent = atoi(aMap["myNumComponent"])
446 if aNumComponent > 1 :
447 if HasValue(thePrsTypeList,VISU.TDEFORMEDSHAPE) :
448 print " Creating DeformedShapeOnField",
449 aPrsObj = theVisu.DeformedShapeOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
450 if aPrsObj is None : print "Error"
452 theView.DisplayOnly(aPrsObj)
454 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TDEFORMEDSHAPE." + thePictureExt
455 aPictureName = re.sub("\s+","_", aPictureName)
456 theView.SavePicture(aPictureName)
457 aVISUObjList.append(aPrsObj)
459 if HasValue(thePrsTypeList,VISU.TVECTORS) :
460 print " Creating VectorsOnField",
461 aPrsObj = theVisu.VectorsOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
462 if aPrsObj is None : print "Error"
464 theView.DisplayOnly(aPrsObj)
466 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TVECTORS." + thePictureExt
467 aPictureName = re.sub("\s+","_", aPictureName)
468 theView.SavePicture(aPictureName)
469 aVISUObjList.append(aPrsObj)
471 if HasValue(thePrsTypeList,VISU.TSTREAMLINES) :
472 print " Creating StreamLinesOnField",
473 aPrsObj = theVisu.StreamLinesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
474 if aPrsObj is None : print "Error"
476 theView.DisplayOnly(aPrsObj)
478 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSTREAMLINES." + thePictureExt
479 aPictureName = re.sub("\s+","_", aPictureName)
480 theView.SavePicture(aPictureName)
481 aVISUObjList.append(aPrsObj)
486 def generateName(prefix = None):
488 int = whrandom.randint(1,1000);
490 return "Study" + str(int)
492 return prefix + str(int)