Salome HOME
NRI : Correction.
[modules/visu.git] / src / VISU_SWIG / visu.py
1 #  VISU VISU_SWIG : binding of C++ implementation and Python
2 #
3 #  Copyright (C) 2003  CEA/DEN, EDF R&D
4 #
5 #
6 #
7 #  File   : visu.py
8 #  Module : VISU
9
10 import os
11 import re
12 from time import sleep
13
14 import VISU
15 import SALOME
16 import SALOMEDS
17 import SALOME_MED
18 from omniORB import CORBA
19 from string import *
20 from time import sleep
21
22 #NRI : interface MED dans le Module Engines !!!!
23 import MED_idl
24 import Med_Gen_idl
25
26 myORB = None
27 myNamingService = None
28 myLifeCycleCORBA = None
29 myNamingService = None
30 myLocalStudyManager = None
31 myLocalStudy = None
32 myLocalVisu = None
33 myDelay = None
34 mySession = None
35  
36 def Initialize(theORB, theNamingService, theLifeCycleCORBA, theStudyManager, theStudy, theDelay) :
37     global myORB, myNamingService, myLifeCycleCORBA, myLocalStudyManager, myLocalStudy
38     global mySession, myLocalVisu, myDelay
39     myDelay = theDelay
40     myORB = theORB
41     myNamingService = theNamingService
42     myLifeCycleCORBA = theLifeCycleCORBA
43     myLocalStudyManager = theStudyManager
44     mySession = myNamingService.Resolve("/Kernel/Session")
45     mySession.GetInterface()
46     myDelay = theDelay
47     sleep(myDelay)
48     myLocalVisu = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "VISU")
49     myLocalStudy = theStudy
50     myLocalVisu.SetCurrentStudy(myLocalStudy)
51     return myLocalVisu
52     
53 def StrToMap(theString) :
54     aPairList = split(theString,";")
55     aMap = {}
56     iEnd = len(aPairList)
57     for i in range(iEnd) :
58         aPair = split(aPairList[i],"=")
59         aMap[aPair[0]] = aPair[1]
60     return aMap
61
62 def IntToEntity(theInt) :
63     theInt = atoi(theInt)
64     if theInt == 0 :
65         return VISU.NODE;
66     elif theInt == 1 :
67         return VISU.EDGE;
68     elif theInt == 2 :
69         return VISU.FACE;
70     else :
71         return VISU.CELL;
72         
73 def HasValue(theList, theValue) :
74     try :
75         return theList.index(theValue) + 1
76     except StandardError, e :
77         return 0
78
79 def CreateStudyForMed(theVisu, theFileName) :
80     print "CreateStudyForMed ", theFileName,
81     if os.path.isfile(theFileName) :
82         if os.path.splitext(theFileName)[1] == '.med' :
83             print "OK"
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"
90             else : print "OK"
91             
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"
98             myView.Maximize()
99             print "OK"
100
101             return [aStudy, myViewManager, myView]
102     raise RuntimeError, "Error: There is no shuch file."
103
104 def getMedObjectFromStudy(theStudy = myLocalStudy):
105     if theStudy is None:
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)
111     return myObj
112
113 def getMedSObject(theStudy = myLocalStudy ):
114     return theStudy.FindObject("Objet MED")
115
116 def getFieldObjectFromStudy(number, subnumber, theStudy = myLocalStudy):
117     if theStudy is None:
118         theStudy = myLocalStudy
119     mySO = theStudy.FindObject("MEDFIELD")
120     if mySO is None:
121         raise Runtime, "getFieldObjectFromStudy mySO is None"
122     mysub = mySO.FindSubObject(number)[1]
123     if mysub:
124         mysubsub = mysub.FindSubObject(subnumber)[1]
125         if mysubsub:
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)
130             if (myObj == None):
131                 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
132             return myObj
133     else:
134         print "ERROR: No Field Object stored in this Study"
135         return None
136
137 def SObjectToObject(theSObject) :
138      #    global myORB
139     if theSObject is None :
140         print "SObjectToObject : argument is None"
141     anObj = 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);
148       else:
149         print "SObjectToObject - IOR = ''"
150     return anObj;
151  
152 def CreatePrsForMed(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt) :
153 #    try:
154         global myLifeCycleCORBA
155         print "lcc.FindOrLoadComponent...",
156         aMedComp = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "MED")
157         if aMedComp is None : print "Error"
158         else : print "OK"
159         
160         aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
161
162         print "aMedComp.readStructFileWithFieldType...",
163         aMedComp.readStructFileWithFieldType(theFileName,myLocalStudy._get_Name())
164         aMedSObj = getMedSObject(myLocalStudy)
165         if aMedSObj is None : raise RuntimeError, "Error"
166         else : print "OK"
167         
168         print "theVisu.ImportMed...",
169         aResult = theVisu.ImportMed(aMedSObj)
170         if aResult is None : raise RuntimeError, "Error"
171         else : print "OK"
172         aVISUObjList.append(aResult)
173         aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
174         
175         aFolderIter = myLocalStudy.NewChildIterator(aMedSObj);
176         while aFolderIter.More() :
177             aFolderSObj = aFolderIter.Value()
178             aFolderIter.Next()
179             anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
180             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
181             aFolderName = anAttr.Value()
182             print "  ", aFolderName
183             
184             if aFolderName == 'MEDMESH' :
185                 aMeshIter = myLocalStudy.NewChildIterator(aFolderSObj);
186                 while aMeshIter.More() :
187                     aMeshSObj = aMeshIter.Value()
188                     aMeshIter.Next()
189                     anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
190                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
191                     aMeshName = anAttr.Value()
192                     print "    ", aMeshName
193                 
194                     aSupportIter = myLocalStudy.NewChildIterator(aMeshSObj);
195                     while aSupportIter.More() :
196                         aSupportSObj = aSupportIter.Value()
197                         aSupportIter.Next()
198                         anAttr = aSupportSObj.FindAttribute("AttributeName")[1]
199                         anAttr = anAttr._narrow(SALOMEDS.AttributeName);
200                         aSupportName = anAttr.Value()
201                         print "      ", aSupportName
202
203             if aFolderName == 'MEDFIELD' :
204                 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
205                 while aFieldIter.More() :
206                     aFieldSObj = aFieldIter.Value()
207                     aFieldIter.Next()
208                     anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
209                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
210                     aFieldName = anAttr.Value()
211                     print "    ", aFieldName
212                 
213                     print "theVisu.ImportMed...",
214                     aResult = theVisu.ImportMed(aFieldSObj)
215                     if aResult is None : raise RuntimeError, "Error"
216                     else : print "OK"
217                     aVISUObjList.append(aResult)
218                     aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
219
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
230
231                             print "theVisu.ImportMed...",
232                             aMedField = SObjectToObject(aTimeStampSObj)
233                             aResult = theVisu.ImportMedFiled(aMedField)
234                             if aResult is None : raise RuntimeError, "Error"
235                             else : print "OK"
236                             aVISUObjList.append(aResult)
237                             aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
238                     
239         return aVISUObjList
240     
241 #    except Exception, e:
242 #        print str(e)
243         
244 def CreatePrsForFile(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt) :
245  #   try:
246         aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
247
248         print "theVisu.ImportMed..."
249         aResult = theVisu.ImportFile(theFileName)
250         if aResult is None : raise RuntimeError, "Error"
251         else : print "OK"
252         
253         aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
254         
255         return aVISUObjList
256     
257 #    except Exception, e:
258 #        print str(e)
259                 
260 def CreatePrsForResult(theVisu, theResult, theView, thePrsTypeList, thePictureDir, thePictureExt) :
261     aVISUObjList = []
262     print "CreatePrsForResult - myLocalStudy.FindObjectIOR...",
263     myLocalStudy = theVisu.GetCurrentStudy()
264     aSObj = myLocalStudy.FindObjectIOR(theResult.GetID())
265     if aSObj is None : raise RuntimeError, "Error"
266     else : print "OK"
267     aMeshIter = myLocalStudy.NewChildIterator(aSObj);
268     while aMeshIter.More() :
269         aMeshSObj = aMeshIter.Value()
270         aMeshIter.Next()
271         anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
272         if anAttr is None :
273             aMeshSObj = aMeshIter.Value()
274             aMeshIter.Next()
275             anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
276         anAttr = anAttr._narrow(SALOMEDS.AttributeName);
277         aMeshName = anAttr.Value()
278         print "  ", aMeshName
279         
280         aFolderIter = myLocalStudy.NewChildIterator(aMeshSObj);
281         while aFolderIter.More() :
282             aFolderSObj = aFolderIter.Value()
283             aFolderIter.Next()
284             anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
285             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
286             aFolderName = anAttr.Value()
287             print "    ", aFolderName
288             
289             if aFolderName == 'Families' :
290                 anEntityIter = myLocalStudy.NewChildIterator(aFolderSObj);
291                 while anEntityIter.More() :
292                     anEntitySObj = anEntityIter.Value()
293                     anEntityIter.Next()
294                     anAttr = anEntitySObj.FindAttribute("AttributeName")[1]
295                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
296                     anEntityName = anAttr.Value()
297                     
298                     anEntityId = "3"
299                     if anEntityName == 'onNodes' :
300                         anEntity = VISU.NODE
301                         anEntityId = "0"
302                     if anEntityName == 'onEdges' :
303                         anEntity = VISU.EDGE
304                         anEntityId = "1"
305                     if anEntityName == 'onFaces' :
306                         anEntity = VISU.FACE
307                         anEntityId = "2"
308                     if anEntityName == 'onCells' :
309                         anEntity = VISU.CELL
310                         anEntityId = "3"
311
312                     if HasValue(thePrsTypeList,VISU.TMESH) :
313                         print "      ", anEntityName,
314                         aMesh = theVisu.MeshOnEntity(theResult,aMeshName,anEntity)
315                         if aMesh is None : print "Error"
316                         else : print ",OK"
317                         theView.DisplayOnly(aMesh)
318                         theView.FitAll()
319                         aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "." + thePictureExt
320                         aPictureName = re.sub("\s+","_", aPictureName);
321                         theView.SavePicture(aPictureName)
322                         aVISUObjList.append(aMesh)
323                         
324                         anFamilyIter = myLocalStudy.NewChildIterator(anEntitySObj);
325                         while anFamilyIter.More() :
326                             aFamilySObj = anFamilyIter.Value()
327                             anFamilyIter.Next()
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"
335                                 else : print ",OK"
336                                 theView.DisplayOnly(aMesh)
337                                 theView.FitAll()
338                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + anFamilyName + "." + thePictureExt
339                                 aPictureName = re.sub("\s+","_", aPictureName);
340                                 theView.SavePicture(aPictureName)
341                                 aVISUObjList.append(aMesh)
342                                 
343             if aFolderName == 'Groups' :
344                 aGroupIter = myLocalStudy.NewChildIterator(aFolderSObj);
345                 while aGroupIter.More() :
346                     aGroupSObj = aGroupIter.Value()
347                     aGroupIter.Next()
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"
355                         else : print ",OK"
356                         theView.DisplayOnly(aMesh)
357                         theView.FitAll()
358                         aPictureName = thePictureDir + aMeshName + "_" + aGroupName + "." + thePictureExt
359                         aPictureName = re.sub("\s+","_", aPictureName);
360                         theView.SavePicture(aPictureName)
361                         aVISUObjList.append(aMesh)
362                         
363             if aFolderName == 'Fields' :
364                 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
365                 while aFieldIter.More() :
366                     aFieldSObj = aFieldIter.Value()
367                     aFieldIter.Next()
368                     anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
369                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
370                     aFieldName = anAttr.Value()
371                     print "      ", aFieldName
372                     
373                     anAttr = aFieldSObj.FindAttribute("AttributeComment")[1]
374                     anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
375                     aFieldComment = anAttr.Value()
376                     aMap = StrToMap(aFieldComment)
377                     
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
388                             
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"
403                                 else : print ",OK"
404                                 theView.DisplayOnly(aPrsObj)
405                                 theView.FitAll()
406                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSCALARMAP." + thePictureExt
407                                 aPictureName = re.sub("\s+","_", aPictureName);
408                                 theView.SavePicture(aPictureName)
409                                 aVISUObjList.append(aPrsObj)
410                                 
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"
415                                 else : print ",OK"
416                                 theView.DisplayOnly(aPrsObj)
417                                 theView.FitAll()
418                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TISOSURFACE." + thePictureExt
419                                 aPictureName = re.sub("\s+","_", aPictureName);
420                                 theView.SavePicture(aPictureName)
421                                 aVISUObjList.append(aPrsObj)
422                                 
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"
427                                 else : print ",OK"
428                                 aPrsObj.SetOrientationType(VISU.CutPlanes.ZX)
429                                 theView.DisplayOnly(aPrsObj)
430                                 theView.FitAll()
431                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTPLANES." + thePictureExt
432                                 aPictureName = re.sub("\s+","_", aPictureName)
433                                 theView.SavePicture(aPictureName)
434                                 aVISUObjList.append(aPrsObj)
435                                 
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"
440                                 else : print ",OK"
441                                 aPrsObj.SetOrientationType(VISU.CutPlanes.XY)
442                                 aPrsObj.SetOrientationType2(VISU.CutPlanes.ZX)
443                                 theView.DisplayOnly(aPrsObj)
444                                 theView.FitAll()
445                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTLINES." + thePictureExt
446                                 aPictureName = re.sub("\s+","_", aPictureName)
447                                 theView.SavePicture(aPictureName)
448                                 aVISUObjList.append(aPrsObj)
449                                 
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"
456                                     else : print ",OK"
457                                     theView.DisplayOnly(aPrsObj)
458                                     theView.FitAll()
459                                     aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TDEFORMEDSHAPE." + thePictureExt
460                                     aPictureName = re.sub("\s+","_", aPictureName)
461                                     theView.SavePicture(aPictureName)
462                                     aVISUObjList.append(aPrsObj)
463
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"
468                                     else : print ",OK"
469                                     theView.DisplayOnly(aPrsObj)
470                                     theView.FitAll()
471                                     aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TVECTORS." + thePictureExt
472                                     aPictureName = re.sub("\s+","_", aPictureName)
473                                     theView.SavePicture(aPictureName)
474                                     aVISUObjList.append(aPrsObj)
475                                     
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"
480                                     else : print ",OK"
481                                     theView.DisplayOnly(aPrsObj)
482                                     theView.FitAll()
483                                     aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSTREAMLINES." + thePictureExt
484                                     aPictureName = re.sub("\s+","_", aPictureName)
485                                     theView.SavePicture(aPictureName)
486                                     aVISUObjList.append(aPrsObj)
487                     
488     return aVISUObjList
489
490
491 def generateName(prefix = None):
492     import whrandom;
493     int = whrandom.randint(1,1000);
494     if prefix is None:
495         return "Study" + str(int)
496     else :
497         return prefix + str(int)