Salome HOME
sources v1.2
[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 SALOMEDS
16 import SALOME_MED
17 from omniORB import CORBA
18 from string import *
19 from time import sleep
20
21 myORB = None
22 myNamingService = None
23 myLifeCycleCORBA = None
24 myNamingService = None
25 myLocalStudyManager = None
26 myLocalStudy = None
27 myLocalVisu = None
28 myDelay = None
29 mySession = None
30  
31 def Initialize(theORB, theNamingService, theLifeCycleCORBA, theStudyManager, theStudy, theDelay) :
32     global myORB, myNamingService, myLifeCycleCORBA, myLocalStudyManager, myLocalStudy
33     global mySession, myLocalVisu, myDelay
34     myDelay = theDelay
35     myORB = theORB
36     myNamingService = theNamingService
37     myLifeCycleCORBA = theLifeCycleCORBA
38     myLocalStudyManager = theStudyManager
39     mySession = myNamingService.Resolve("/Kernel/Session")
40     mySession.GetInterface()
41     myDelay = theDelay
42     sleep(myDelay)
43     myLocalVisu = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "Visu")
44     myLocalStudy = theStudy
45     myLocalVisu.SetCurrentStudy(myLocalStudy)
46     return myLocalVisu
47     
48 def StrToMap(theString) :
49     aPairList = split(theString,";")
50     aMap = {}
51     iEnd = len(aPairList)
52     for i in range(iEnd) :
53         aPair = split(aPairList[i],"=")
54         aMap[aPair[0]] = aPair[1]
55     return aMap
56
57 def IntToEntity(theInt) :
58     theInt = atoi(theInt)
59     if theInt == 0 :
60         return VISU.NODE;
61     elif theInt == 1 :
62         return VISU.EDGE;
63     elif theInt == 2 :
64         return VISU.FACE;
65     else :
66         return VISU.CELL;
67         
68 def HasValue(theList, theValue) :
69     try :
70         return theList.index(theValue) + 1
71     except StandardError, e :
72         return 0
73
74 def CreateStudyForMed(theVisu, theFileName) :
75     print "CreateStudyForMed ", theFileName,
76     if os.path.isfile(theFileName) :
77         if os.path.splitext(theFileName)[1] == '.med' :
78             print "OK"
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"
85             else : print "OK"
86             
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"
93             myView.Maximize()
94             print "OK"
95
96             return [aStudy, myViewManager, myView]
97     raise RuntimeError, "Error: There is no shuch file."
98
99 def getMedObjectFromStudy(theStudy = myLocalStudy):
100     if theStudy is None:
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)
106     return myObj
107
108 def getMedSObject(theStudy = myLocalStudy ):
109     return theStudy.FindObject("Objet MED")
110
111 def getFieldObjectFromStudy(number, subnumber, theStudy = myLocalStudy):
112     if theStudy is None:
113         theStudy = myLocalStudy
114     mySO = theStudy.FindObject("MEDFIELD")
115     if mySO is None:
116         raise Runtime, "getFieldObjectFromStudy mySO is None"
117     mysub = mySO.FindSubObject(number)[1]
118     if mysub:
119         mysubsub = mysub.FindSubObject(subnumber)[1]
120         if mysubsub:
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)
125             if (myObj == None):
126                 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
127             return myObj
128     else:
129         print "ERROR: No Field Object stored in this Study"
130         return None
131
132 def SObjectToObject(theSObject) :
133      #    global myORB
134     if theSObject is None :
135         print "SObjectToObject : argument is None"
136     anObj = 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);
143       else:
144         print "SObjectToObject - IOR = ''"
145     return anObj;
146  
147 def CreatePrsForMed(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt) :
148 #    try:
149         global myLifeCycleCORBA
150         print "lcc.FindOrLoadComponent...",
151         aMedComp = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "Med")
152         if aMedComp is None : print "Error"
153         else : print "OK"
154         
155         aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
156
157         print "aMedComp.readStructFileWithFieldType...",
158         aMedComp.readStructFileWithFieldType(theFileName,myLocalStudy._get_Name())
159         aMedSObj = getMedSObject(myLocalStudy)
160         if aMedSObj is None : raise RuntimeError, "Error"
161         else : print "OK"
162         
163         print "theVisu.ImportMed...",
164         aResult = theVisu.ImportMed(aMedSObj)
165         if aResult is None : raise RuntimeError, "Error"
166         else : print "OK"
167         aVISUObjList.append(aResult)
168         aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
169         
170         aFolderIter = myLocalStudy.NewChildIterator(aMedSObj);
171         while aFolderIter.More() :
172             aFolderSObj = aFolderIter.Value()
173             aFolderIter.Next()
174             anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
175             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
176             aFolderName = anAttr.Value()
177             print "  ", aFolderName
178             
179             if aFolderName == 'MEDMESH' :
180                 aMeshIter = myLocalStudy.NewChildIterator(aFolderSObj);
181                 while aMeshIter.More() :
182                     aMeshSObj = aMeshIter.Value()
183                     aMeshIter.Next()
184                     anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
185                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
186                     aMeshName = anAttr.Value()
187                     print "    ", aMeshName
188                 
189                     aSupportIter = myLocalStudy.NewChildIterator(aMeshSObj);
190                     while aSupportIter.More() :
191                         aSupportSObj = aSupportIter.Value()
192                         aSupportIter.Next()
193                         anAttr = aSupportSObj.FindAttribute("AttributeName")[1]
194                         anAttr = anAttr._narrow(SALOMEDS.AttributeName);
195                         aSupportName = anAttr.Value()
196                         print "      ", aSupportName
197
198             if aFolderName == 'MEDFIELD' :
199                 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
200                 while aFieldIter.More() :
201                     aFieldSObj = aFieldIter.Value()
202                     aFieldIter.Next()
203                     anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
204                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
205                     aFieldName = anAttr.Value()
206                     print "    ", aFieldName
207                 
208                     print "theVisu.ImportMed...",
209                     aResult = theVisu.ImportMed(aFieldSObj)
210                     if aResult is None : raise RuntimeError, "Error"
211                     else : print "OK"
212                     aVISUObjList.append(aResult)
213                     aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
214
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
225
226                             print "theVisu.ImportMed...",
227                             aMedField = SObjectToObject(aTimeStampSObj)
228                             aResult = theVisu.ImportMedFiled(aMedField)
229                             if aResult is None : raise RuntimeError, "Error"
230                             else : print "OK"
231                             aVISUObjList.append(aResult)
232                             aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
233                     
234         return aVISUObjList
235     
236 #    except Exception, e:
237 #        print str(e)
238         
239 def CreatePrsForFile(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt) :
240  #   try:
241         aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
242
243         print "theVisu.ImportMed..."
244         aResult = theVisu.ImportFile(theFileName)
245         if aResult is None : raise RuntimeError, "Error"
246         else : print "OK"
247         
248         aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt))
249         
250         return aVISUObjList
251     
252 #    except Exception, e:
253 #        print str(e)
254                 
255 def CreatePrsForResult(theVisu, theResult, theView, thePrsTypeList, thePictureDir, thePictureExt) :
256     aVISUObjList = []
257     print "CreatePrsForResult - myLocalStudy.FindObjectIOR...",
258     myLocalStudy = theVisu.GetCurrentStudy()
259     aSObj = myLocalStudy.FindObjectIOR(theResult.GetID())
260     if aSObj is None : raise RuntimeError, "Error"
261     else : print "OK"
262     aMeshIter = myLocalStudy.NewChildIterator(aSObj);
263     while aMeshIter.More() :
264         aMeshSObj = aMeshIter.Value()
265         aMeshIter.Next()
266         anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
267         if anAttr is None :
268             aMeshSObj = aMeshIter.Value()
269             aMeshIter.Next()
270             anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
271         anAttr = anAttr._narrow(SALOMEDS.AttributeName);
272         aMeshName = anAttr.Value()
273         print "  ", aMeshName
274         
275         aFolderIter = myLocalStudy.NewChildIterator(aMeshSObj);
276         while aFolderIter.More() :
277             aFolderSObj = aFolderIter.Value()
278             aFolderIter.Next()
279             anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
280             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
281             aFolderName = anAttr.Value()
282             print "    ", aFolderName
283             
284             if aFolderName == 'Families' :
285                 anEntityIter = myLocalStudy.NewChildIterator(aFolderSObj);
286                 while anEntityIter.More() :
287                     anEntitySObj = anEntityIter.Value()
288                     anEntityIter.Next()
289                     anAttr = anEntitySObj.FindAttribute("AttributeName")[1]
290                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
291                     anEntityName = anAttr.Value()
292                     
293                     anEntityId = "3"
294                     if anEntityName == 'onNodes' :
295                         anEntity = VISU.NODE
296                         anEntityId = "0"
297                     if anEntityName == 'onEdges' :
298                         anEntity = VISU.EDGE
299                         anEntityId = "1"
300                     if anEntityName == 'onFaces' :
301                         anEntity = VISU.FACE
302                         anEntityId = "2"
303                     if anEntityName == 'onCells' :
304                         anEntity = VISU.CELL
305                         anEntityId = "3"
306
307                     if HasValue(thePrsTypeList,VISU.TMESH) :
308                         print "      ", anEntityName,
309                         aMesh = theVisu.MeshOnEntity(theResult,aMeshName,anEntity)
310                         if aMesh is None : print "Error"
311                         else : print ",OK"
312                         theView.DisplayOnly(aMesh)
313                         theView.FitAll()
314                         aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "." + thePictureExt
315                         aPictureName = re.sub("\s+","_", aPictureName);
316                         theView.SavePicture(aPictureName)
317                         aVISUObjList.append(aMesh)
318                         
319                         anFamilyIter = myLocalStudy.NewChildIterator(anEntitySObj);
320                         while anFamilyIter.More() :
321                             aFamilySObj = anFamilyIter.Value()
322                             anFamilyIter.Next()
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"
330                                 else : print ",OK"
331                                 theView.DisplayOnly(aMesh)
332                                 theView.FitAll()
333                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + anFamilyName + "." + thePictureExt
334                                 aPictureName = re.sub("\s+","_", aPictureName);
335                                 theView.SavePicture(aPictureName)
336                                 aVISUObjList.append(aMesh)
337                                 
338             if aFolderName == 'Groups' :
339                 aGroupIter = myLocalStudy.NewChildIterator(aFolderSObj);
340                 while aGroupIter.More() :
341                     aGroupSObj = aGroupIter.Value()
342                     aGroupIter.Next()
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"
350                         else : print ",OK"
351                         theView.DisplayOnly(aMesh)
352                         theView.FitAll()
353                         aPictureName = thePictureDir + aMeshName + "_" + aGroupName + "." + thePictureExt
354                         aPictureName = re.sub("\s+","_", aPictureName);
355                         theView.SavePicture(aPictureName)
356                         aVISUObjList.append(aMesh)
357                         
358             if aFolderName == 'Fields' :
359                 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
360                 while aFieldIter.More() :
361                     aFieldSObj = aFieldIter.Value()
362                     aFieldIter.Next()
363                     anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
364                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
365                     aFieldName = anAttr.Value()
366                     print "      ", aFieldName
367                     
368                     anAttr = aFieldSObj.FindAttribute("AttributeComment")[1]
369                     anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
370                     aFieldComment = anAttr.Value()
371                     aMap = StrToMap(aFieldComment)
372                     
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
383                             
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"
398                                 else : print ",OK"
399                                 theView.DisplayOnly(aPrsObj)
400                                 theView.FitAll()
401                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSCALARMAP." + thePictureExt
402                                 aPictureName = re.sub("\s+","_", aPictureName);
403                                 theView.SavePicture(aPictureName)
404                                 aVISUObjList.append(aPrsObj)
405                                 
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"
410                                 else : print ",OK"
411                                 theView.DisplayOnly(aPrsObj)
412                                 theView.FitAll()
413                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TISOSURFACE." + thePictureExt
414                                 aPictureName = re.sub("\s+","_", aPictureName);
415                                 theView.SavePicture(aPictureName)
416                                 aVISUObjList.append(aPrsObj)
417                                 
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"
422                                 else : print ",OK"
423                                 aPrsObj.SetOrientationType(VISU.CutPlanes.ZX)
424                                 theView.DisplayOnly(aPrsObj)
425                                 theView.FitAll()
426                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTPLANES." + thePictureExt
427                                 aPictureName = re.sub("\s+","_", aPictureName)
428                                 theView.SavePicture(aPictureName)
429                                 aVISUObjList.append(aPrsObj)
430                                 
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"
435                                 else : print ",OK"
436                                 aPrsObj.SetOrientationType(VISU.CutPlanes.XY)
437                                 aPrsObj.SetOrientationType2(VISU.CutPlanes.ZX)
438                                 theView.DisplayOnly(aPrsObj)
439                                 theView.FitAll()
440                                 aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTLINES." + thePictureExt
441                                 aPictureName = re.sub("\s+","_", aPictureName)
442                                 theView.SavePicture(aPictureName)
443                                 aVISUObjList.append(aPrsObj)
444                                 
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"
451                                     else : print ",OK"
452                                     theView.DisplayOnly(aPrsObj)
453                                     theView.FitAll()
454                                     aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TDEFORMEDSHAPE." + thePictureExt
455                                     aPictureName = re.sub("\s+","_", aPictureName)
456                                     theView.SavePicture(aPictureName)
457                                     aVISUObjList.append(aPrsObj)
458
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"
463                                     else : print ",OK"
464                                     theView.DisplayOnly(aPrsObj)
465                                     theView.FitAll()
466                                     aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TVECTORS." + thePictureExt
467                                     aPictureName = re.sub("\s+","_", aPictureName)
468                                     theView.SavePicture(aPictureName)
469                                     aVISUObjList.append(aPrsObj)
470                                     
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"
475                                     else : print ",OK"
476                                     theView.DisplayOnly(aPrsObj)
477                                     theView.FitAll()
478                                     aPictureName = thePictureDir + aMeshName + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSTREAMLINES." + thePictureExt
479                                     aPictureName = re.sub("\s+","_", aPictureName)
480                                     theView.SavePicture(aPictureName)
481                                     aVISUObjList.append(aPrsObj)
482                     
483     return aVISUObjList
484
485
486 def generateName(prefix = None):
487     import whrandom;
488     int = whrandom.randint(1,1000);
489     if prefix is None:
490         return "Study" + str(int)
491     else :
492         return prefix + str(int)