Salome HOME
Update version to 3.2.0a1
[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 SALOME_Session_idl
17 import SALOMEDS
18 import SALOME_MED
19 from omniORB import CORBA
20 from string import *
21 from time import sleep
22
23 myORB = None
24 myNamingService = None
25 myLifeCycleCORBA = None
26 myNamingService = None
27 myLocalStudyManager = None
28 myLocalStudy = None
29 myLocalVisu = None
30 myDelay = None
31 mySession = None
32  
33 def Initialize(theORB, theNamingService, theLifeCycleCORBA, theStudyManager, theStudy, theDelay) :
34     global myORB, myNamingService, myLifeCycleCORBA, myLocalStudyManager, myLocalStudy
35     global mySession, myLocalVisu, myDelay
36     myDelay = theDelay
37     myORB = theORB
38     myNamingService = theNamingService
39     myLifeCycleCORBA = theLifeCycleCORBA
40     myLocalStudyManager = theStudyManager
41     while mySession == None:
42         mySession = myNamingService.Resolve("/Kernel/Session")
43     mySession = mySession._narrow(SALOME.Session)
44     mySession.GetInterface()
45     myDelay = theDelay
46     sleep(myDelay)
47     myLocalVisu = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "VISU")
48     myLocalStudy = theStudy
49     myLocalVisu.SetCurrentStudy(myLocalStudy)
50     return myLocalVisu
51
52 def PublishComponent(theStudyDocument):
53     aComponentDataType = myLocalVisu.ComponentDataType()
54     aSComponent = theStudyDocument.FindComponent(aComponentDataType)
55     if aSComponent == None:
56         aStudyBuilder = theStudyDocument.NewBuilder()
57         aStudyBuilder.NewCommand()
58         aLocked = theStudyDocument.GetProperties().IsLocked()
59         if aLocked:
60             theStudyDocument.GetProperties().SetLocked(0)
61             pass
62
63         aSComponent = aStudyBuilder.NewComponent(aComponentDataType);
64         aName = aStudyBuilder.FindOrCreateAttribute(aSComponent,"AttributeName")
65         aModuleCatalog = myNamingService.Resolve("/Kernel/ModulCatalog");
66         aComponent = aModuleCatalog.GetComponent(aComponentDataType);
67         if aComponent != None:
68             aComponentUserName = aComponent._get_componentusername()
69             aName.SetValue(aComponentUserName)
70             pass
71         
72         aPixMap = aStudyBuilder.FindOrCreateAttribute(aSComponent,"AttributePixMap")
73         aPixMap.SetPixMap("ICON_OBJBROWSER_Visu")
74         
75         aStudyBuilder.DefineComponentInstance(aSComponent,myLocalVisu);
76         if aLocked:
77             theStudyDocument.GetProperties().SetLocked(1);
78             pass
79         
80         aStudyBuilder.CommitCommand();
81         pass
82     
83     return aSComponent;
84         
85 def StrToMap(theString) :
86     aPairList = split(theString,";")
87     aMap = {}
88     iEnd = len(aPairList)
89     for i in range(iEnd) :
90         aPair = split(aPairList[i],"=")
91         aMap[aPair[0]] = aPair[1]
92     return aMap
93
94 def IntToEntity(theInt) :
95     theInt = atoi(theInt)
96     if theInt == 0 :
97         return VISU.NODE;
98     elif theInt == 1 :
99         return VISU.EDGE;
100     elif theInt == 2 :
101         return VISU.FACE;
102     else :
103         return VISU.CELL;
104         
105 def HasValue(theList, theValue) :
106     try :
107         return theList.index(theValue) + 1
108     except StandardError, e :
109         return 0
110
111 def CreateStudyForMed(theVisu, theFileName) :
112     print "CreateStudyForMed ", theFileName,
113     if os.path.isfile(theFileName) :
114         if re.search("\.med$", theFileName) is not None :
115             print "OK"
116             aStudyName = os.path.basename(theFileName)
117             aStudyName = re.sub(r'\.med', "", aStudyName)
118             aStudyName = generateName(aStudyName)
119             print "myLocalStudyManager.NewStudy -", aStudyName, "...",
120             aStudy = myLocalStudyManager.NewStudy(aStudyName)
121             if aStudy is None : raise RuntimeError, "Error"
122             else : print "OK"
123             
124             print "CreateStudyForMed - Creating a 3D viewer...",
125             theVisu.SetCurrentStudy(aStudy)
126             myViewManager = theVisu.GetViewManager()
127             if myViewManager is None : raise RuntimeError, "Error - theVisu.GetViewManager"
128             myView = myViewManager.Create3DView()
129             if myView is None : raise RuntimeError, "Error - myViewManager.Create3DView"
130             myView.Maximize()
131             print "OK"
132
133             return [aStudy, myViewManager, myView]
134     raise RuntimeError, "Error: There is no shuch file."
135
136 def getMedSObject(theStudy = myLocalStudy ):
137     if theStudy is None:
138         theStudy = myLocalStudy
139     aSO = theStudy.FindObject("Med")
140     anIsPresetn, aSObject = aSO.FindSubObject(1)
141     return aSObject
142
143 def getMedObjectFromStudy(theStudy = myLocalStudy):
144     aSObject = getMedSObject(theStudy)
145     if aSObject:
146         anAttr = aSObject.FindAttribute("AttributeIOR")[1]
147         anObj = myORB.string_to_object(anAttr.Value())
148         return  anObj._narrow(SALOME_MED.MED)
149     return None
150
151 def getFieldObjectFromStudy(number, subnumber, theStudy = myLocalStudy):
152     if theStudy is None:
153         theStudy = myLocalStudy
154     mySO = theStudy.FindObject("MEDFIELD")
155     if mySO is None:
156         raise Runtime, "getFieldObjectFromStudy mySO is None"
157     mysub = mySO.FindSubObject(number)[1]
158     if mysub:
159         mysubsub = mysub.FindSubObject(subnumber)[1]
160         if mysubsub:
161             Builder = theStudy.NewBuilder()
162             anAttr = Builder.FindOrCreateAttribute(mysubsub, "AttributeIOR")
163             obj = myORB.string_to_object(anAttr.Value())
164             myObj = obj._narrow(SALOME_MED.FIELDINT)
165             if (myObj == None):
166                 myObj = obj._narrow(SALOME_MED.FIELDDOUBLE)
167             return myObj
168     else:
169         print "ERROR: No Field Object stored in this Study"
170         return None
171
172 def SObjectToObject(theSObject) :
173      #    global myORB
174     if theSObject is None :
175         print "SObjectToObject : argument is None"
176     anObj = None                         
177     res,Attr = theSObject.FindAttribute("AttributeIOR")
178     if (res != 0) and (Attr is not None)  : 
179       anIOR  = Attr._narrow(SALOMEDS.AttributeIOR);
180       aValue = anIOR.Value();
181       if(len(aValue) != 0) :
182         anObj = myORB.string_to_object(aValue);
183       else:
184         print "SObjectToObject - IOR = ''"
185     return anObj;
186  
187 def CreatePrsForMed(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt, theIsAutoDelete = 0) :
188 #    try:
189         global myLifeCycleCORBA
190         print "lcc.FindOrLoadComponent...",
191         aMedComp = myLifeCycleCORBA.FindOrLoadComponent("FactoryServer", "MED")
192         if aMedComp is None : print "Error"
193         else : print "OK"
194         
195         aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
196
197         print "aMedComp.readStructFileWithFieldType...",
198         aMedComp.readStructFileWithFieldType(theFileName,myLocalStudy._get_Name())
199         aMedSObj = getMedSObject(myLocalStudy)
200         if aMedSObj is None : raise RuntimeError, "Error"
201         else : print "OK"
202         
203         print "theVisu.ImportMed...",
204         aResult = theVisu.ImportMed(aMedSObj)
205         if aResult is None : raise RuntimeError, "Error"
206         else : print "OK"
207
208         aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt, theIsAutoDelete))
209         if theIsAutoDelete :
210             aResult.RemoveFromStudy()
211         else :
212             aVISUObjList.append(aResult)
213         
214         aFolderIter = myLocalStudy.NewChildIterator(aMedSObj.GetFather());
215         while aFolderIter.More() :
216             aFolderSObj = aFolderIter.Value()
217             aFolderIter.Next()
218             anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
219             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
220             aFolderName = anAttr.Value()
221             print "  ", aFolderName
222             
223             if aFolderName == 'MEDMESH' :
224                 aMeshIter = myLocalStudy.NewChildIterator(aFolderSObj);
225                 while aMeshIter.More() :
226                     aMeshSObj = aMeshIter.Value()
227                     aMeshIter.Next()
228                     anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
229                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
230                     aMeshName = anAttr.Value()
231                     print "    ", aMeshName
232                 
233                     aSupportIter = myLocalStudy.NewChildIterator(aMeshSObj);
234                     while aSupportIter.More() :
235                         aSupportSObj = aSupportIter.Value()
236                         aSupportIter.Next()
237                         anAttr = aSupportSObj.FindAttribute("AttributeName")[1]
238                         anAttr = anAttr._narrow(SALOMEDS.AttributeName);
239                         aSupportName = anAttr.Value()
240                         print "      ", aSupportName
241
242             if aFolderName == 'MEDFIELD' :
243                 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
244                 while aFieldIter.More() :
245                     aFieldSObj = aFieldIter.Value()
246                     aFieldIter.Next()
247                     anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
248                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
249                     aFieldName = anAttr.Value()
250                     print "    ", aFieldName
251                 
252                     print "theVisu.ImportMed...",
253                     aResult = theVisu.ImportMed(aFieldSObj)
254                     if aResult is None : raise RuntimeError, "Error"
255                     else : print "OK"
256
257                     aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt, theIsAutoDelete))
258                     if theIsAutoDelete :
259                         aResult.RemoveFromStudy()
260                     else :
261                         aVISUObjList.append(aResult)
262
263                     aTimeStampIter = myLocalStudy.NewChildIterator(aFieldSObj);
264                     if aTimeStampIter.More() :
265                         aTimeStampIter.Next()
266                         while aTimeStampIter.More() :
267                             aTimeStampSObj = aTimeStampIter.Value()
268                             aTimeStampIter.Next()
269                             anAttr = aTimeStampSObj.FindAttribute("AttributeName")[1]
270                             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
271                             aTimeStampName = anAttr.Value()
272                             print "        ", aTimeStampName
273
274                             print "theVisu.ImportMed...",
275                             aMedField = SObjectToObject(aTimeStampSObj)
276                             aResult = theVisu.ImportMedField(aMedField)
277                             if aResult is None : raise RuntimeError, "Error"
278                             else : print "OK"
279
280                             aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt, theIsAutoDelete))
281                             if theIsAutoDelete :
282                                 aResult.RemoveFromStudy()
283                             else :
284                                 aVISUObjList.append(aResult)
285                     
286         return aVISUObjList
287     
288 #    except Exception, e:
289 #        print str(e)
290         
291 def CreatePrsForFile(theVisu, theFileName, thePrsTypeList, thePictureDir, thePictureExt, theIsAutoDelete = 0) :
292  #   try:
293         aVISUObjList = [myLocalStudy, myViewManager, myView] = CreateStudyForMed(theVisu, theFileName)
294
295         print "theVisu.ImportMed...",
296         aResult = theVisu.ImportFile(theFileName)
297         if aResult is None : raise RuntimeError, "Error"
298         else : print "OK"
299         
300         aVISUObjList.extend(CreatePrsForResult(theVisu, aResult, myView, thePrsTypeList, thePictureDir, thePictureExt, theIsAutoDelete))
301
302         if theIsAutoDelete :
303             aResult.RemoveFromStudy()
304         else :
305             aVISUObjList.append(aResult)
306
307         return aVISUObjList
308     
309 #    except Exception, e:
310 #        print str(e)
311                 
312 def CreatePrsForResult(theVisu, theResult, theView, thePrsTypeList, thePictureDir, thePictureExt, theIsAutoDelete = 0) :
313     aVISUObjList = None
314     if theIsAutoDelete is not None :
315         aVISUObjList = []
316         pass
317         
318     print "CreatePrsForResult - myLocalStudy.FindObjectIOR...",
319     myLocalStudy = theVisu.GetCurrentStudy()
320
321     aSObj = myLocalStudy.FindObjectIOR(theResult.GetID())
322     if aSObj is None : raise RuntimeError, "Error"
323     else : print "OK"
324     aMeshIter = myLocalStudy.NewChildIterator(aSObj);
325     while aMeshIter.More() :
326         aMeshSObj = aMeshIter.Value()
327         aMeshIter.Next()
328         anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
329         if anAttr is None :
330             aMeshSObj = aMeshIter.Value()
331             aMeshIter.Next()
332             anAttr = aMeshSObj.FindAttribute("AttributeName")[1]
333         anAttr = anAttr._narrow(SALOMEDS.AttributeName);
334         aMeshName = anAttr.Value()
335         aMeshNamePic = re.sub(".","_",aMeshName)
336         print "  ", aMeshName
337         
338         aFolderIter = myLocalStudy.NewChildIterator(aMeshSObj);
339         while aFolderIter.More() :
340             aFolderSObj = aFolderIter.Value()
341             aFolderIter.Next()
342             anAttr = aFolderSObj.FindAttribute("AttributeName")[1]
343             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
344             aFolderName = anAttr.Value()
345             print "    ", aFolderName
346             
347             if aFolderName == 'Families' :
348                 anEntityIter = myLocalStudy.NewChildIterator(aFolderSObj);
349                 while anEntityIter.More() :
350                     anEntitySObj = anEntityIter.Value()
351                     anEntityIter.Next()
352                     anAttr = anEntitySObj.FindAttribute("AttributeName")[1]
353                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
354                     anEntityName = anAttr.Value()
355                     
356                     anEntityId = "3"
357                     if anEntityName == 'onNodes' :
358                         anEntity = VISU.NODE
359                         anEntityId = "0"
360                     if anEntityName == 'onEdges' :
361                         anEntity = VISU.EDGE
362                         anEntityId = "1"
363                     if anEntityName == 'onFaces' :
364                         anEntity = VISU.FACE
365                         anEntityId = "2"
366                     if anEntityName == 'onCells' :
367                         anEntity = VISU.CELL
368                         anEntityId = "3"
369
370                     if HasValue(thePrsTypeList,VISU.TMESH) :
371                         print "      ", anEntityName, aMeshName,
372                         aMesh = theVisu.MeshOnEntity(theResult,aMeshName,anEntity)
373                         if aMesh is None : print "Error"
374                         else :
375                             print ",OK"
376                             theView.DisplayOnly(aMesh)
377                             theView.FitAll()
378                             aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "." + thePictureExt
379                             aPictureName = re.sub("\s+","_", aPictureName);
380                             theView.SavePicture(aPictureName)
381                             if theIsAutoDelete :
382                                 aMesh.RemoveFromStudy()
383                             else :
384                                 aVISUObjList.append(aMesh)
385                         
386                         anFamilyIter = myLocalStudy.NewChildIterator(anEntitySObj);
387                         while anFamilyIter.More() :
388                             aFamilySObj = anFamilyIter.Value()
389                             anFamilyIter.Next()
390                             anAttr = aFamilySObj.FindAttribute("AttributeName")[1]
391                             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
392                             anFamilyName = anAttr.Value()
393                             if HasValue(thePrsTypeList,VISU.TMESH) :
394                                 print "        ", anFamilyName,
395                                 aMesh = theVisu.FamilyMeshOnEntity(theResult,aMeshName,anEntity,anFamilyName)
396                                 if aMesh is None : print "Error"
397                                 else :
398                                     print ",OK"
399                                     theView.DisplayOnly(aMesh)
400                                     theView.FitAll()
401                                     aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + anFamilyName + "." + thePictureExt
402                                     aPictureName = re.sub("\s+","_", aPictureName);
403                                     theView.SavePicture(aPictureName)
404                                     if theIsAutoDelete :
405                                         aMesh.RemoveFromStudy()
406                                     else :
407                                         aVISUObjList.append(aMesh)
408                                 
409             if aFolderName == 'Groups' :
410                 aGroupIter = myLocalStudy.NewChildIterator(aFolderSObj);
411                 while aGroupIter.More() :
412                     aGroupSObj = aGroupIter.Value()
413                     aGroupIter.Next()
414                     anAttr = aGroupSObj.FindAttribute("AttributeName")[1]
415                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
416                     aGroupName = anAttr.Value()
417                     if HasValue(thePrsTypeList,VISU.TMESH) :
418                         print "      ", aGroupName,
419                         aMesh = theVisu.GroupMesh(theResult,aMeshName,aGroupName)
420                         if aMesh is None : print "Error"
421                         else :
422                             print ",OK"
423                             theView.DisplayOnly(aMesh)
424                             theView.FitAll()
425                             aPictureName = thePictureDir + aMeshNamePic + "_" + aGroupName + "." + thePictureExt
426                             aPictureName = re.sub("\s+","_", aPictureName);
427                             theView.SavePicture(aPictureName)
428                             if theIsAutoDelete :
429                                 aMesh.RemoveFromStudy()
430                             else :
431                                 aVISUObjList.append(aMesh)
432                         
433             if aFolderName == 'Fields' :
434                 aFieldIter = myLocalStudy.NewChildIterator(aFolderSObj);
435                 while aFieldIter.More() :
436                     aFieldSObj = aFieldIter.Value()
437                     aFieldIter.Next()
438                     anAttr = aFieldSObj.FindAttribute("AttributeName")[1]
439                     anAttr = anAttr._narrow(SALOMEDS.AttributeName);
440                     aFieldName = anAttr.Value()
441                     print "      ", aFieldName
442                     
443                     anAttr = aFieldSObj.FindAttribute("AttributeComment")[1]
444                     anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
445                     aFieldComment = anAttr.Value()
446                     aMap = StrToMap(aFieldComment)
447                     
448                     aTimeStampIter = myLocalStudy.NewChildIterator(aFieldSObj);
449                     if aTimeStampIter.More() :
450                         aTimeStampIter.Next()
451                         while aTimeStampIter.More() :
452                             aTimeStampSObj = aTimeStampIter.Value()
453                             aTimeStampIter.Next()
454                             anAttr = aTimeStampSObj.FindAttribute("AttributeName")[1]
455                             anAttr = anAttr._narrow(SALOMEDS.AttributeName);
456                             aTimeStampName = anAttr.Value()
457                             print "        ", aTimeStampName
458                             
459                             anAttr = aTimeStampSObj.FindAttribute("AttributeComment")[1]
460                             anAttr = anAttr._narrow(SALOMEDS.AttributeComment);
461                             aTimeStampComment = anAttr.Value()
462                             aMap = StrToMap(aTimeStampComment)
463                             aMeshName = aMap["myMeshName"]
464                             aFieldName = aMap["myFieldName"]
465                             aTimeStampId = atoi(aMap["myTimeStampId"])
466                             anEntity = IntToEntity(aMap["myEntityId"])
467                             anEntityId = str(aMap["myEntityId"]);
468                             #print aMeshName, aFieldName, anEntity, aTimeStampId
469                             if HasValue(thePrsTypeList,VISU.TSCALARMAP) :
470                                 print "          Creating ScalarMapOnField",
471                                 aPrsObj = theVisu.ScalarMapOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
472                                 if aPrsObj is None : print "Error"
473                                 else :
474                                     print ",OK"
475                                     theView.DisplayOnly(aPrsObj)
476                                     theView.FitAll()
477                                     aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSCALARMAP." + thePictureExt
478                                     aPictureName = re.sub("\s+","_", aPictureName);
479                                     theView.SavePicture(aPictureName)
480                                     if theIsAutoDelete :
481                                         aPrsObj.RemoveFromStudy()
482                                     else :
483                                         aVISUObjList.append(aPrsObj)
484
485                             if HasValue(thePrsTypeList,VISU.TISOSURFACE) :
486                                 print "          Creating IsoSurfacesOnField",
487                                 aPrsObj = theVisu.IsoSurfacesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
488                                 if aPrsObj is None : print "Error"
489                                 else :
490                                     print ",OK"
491                                     theView.DisplayOnly(aPrsObj)
492                                     theView.FitAll()
493                                     aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TISOSURFACE." + thePictureExt
494                                     aPictureName = re.sub("\s+","_", aPictureName);
495                                     theView.SavePicture(aPictureName)
496                                     if theIsAutoDelete :
497                                         aPrsObj.RemoveFromStudy()
498                                     else :
499                                         aVISUObjList.append(aPrsObj)
500                                 
501                             if HasValue(thePrsTypeList,VISU.TCUTPLANES) :
502                                 print "          Creating CutPlanesOnField",
503                                 aPrsObj = theVisu.CutPlanesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
504                                 if aPrsObj is None : print "Error"
505                                 else :
506                                     print ",OK"
507                                     aPrsObj.SetOrientation(VISU.CutPlanes.ZX,aPrsObj.GetRotateX(),aPrsObj.GetRotateY())
508                                     theView.DisplayOnly(aPrsObj)
509                                     theView.FitAll()
510                                     aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTPLANES." + thePictureExt
511                                     aPictureName = re.sub("\s+","_", aPictureName)
512                                     theView.SavePicture(aPictureName)
513                                     if theIsAutoDelete :
514                                         aPrsObj.RemoveFromStudy()
515                                     else :
516                                         aVISUObjList.append(aPrsObj)
517                                 
518                             if HasValue(thePrsTypeList,VISU.TCUTLINES) :
519                                 print "          Creating CutLinesOnField",
520                                 aPrsObj = theVisu.CutLinesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
521                                 if aPrsObj is None : print "Error"
522                                 else :
523                                     print ",OK"
524                                     aPrsObj.SetOrientation(VISU.CutPlanes.XY,aPrsObj.GetRotateX(),aPrsObj.GetRotateY())
525                                     aPrsObj.SetOrientation2(VISU.CutPlanes.ZX,aPrsObj.GetRotateX2(),aPrsObj.GetRotateY2())
526                                     theView.DisplayOnly(aPrsObj)
527                                     theView.FitAll()
528                                     aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TCUTLINES." + thePictureExt
529                                     aPictureName = re.sub("\s+","_", aPictureName)
530                                     theView.SavePicture(aPictureName)
531                                     if theIsAutoDelete :
532                                         aPrsObj.RemoveFromStudy()
533                                     else :
534                                         aVISUObjList.append(aPrsObj)
535                                 
536                             if HasValue(thePrsTypeList,VISU.TPLOT3D) :
537                                 print "          Creating Plot3DOnField",
538                                 aPrsObj = theVisu.Plot3DOnField(theResult,aMeshName,anEntity,
539                                                                 aFieldName,aTimeStampId)
540                                 if aPrsObj is None : print "Error"
541                                 else :
542                                     print ",OK"
543                                 #aPrsObj.SetOrientation(VISU.CutPlanes.ZX,
544                                 #                       aPrsObj.GetRotateX(),
545                                 #                       aPrsObj.GetRotateY())
546                                     theView.DisplayOnly(aPrsObj)
547                                     theView.FitAll()
548                                     aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TPLOT3D." + thePictureExt
549                                     aPictureName = re.sub("\s+","_", aPictureName)
550                                     theView.SavePicture(aPictureName)
551                                     if theIsAutoDelete :
552                                         aPrsObj.RemoveFromStudy()
553                                     else :
554                                         aVISUObjList.append(aPrsObj)
555                                 
556                             aNumComponent = atoi(aMap["myNumComponent"])
557                             if aNumComponent > 1 :
558                                 if HasValue(thePrsTypeList,VISU.TDEFORMEDSHAPE) :
559                                     print "          Creating DeformedShapeOnField",
560                                     aPrsObj = theVisu.DeformedShapeOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
561                                     if aPrsObj is None : print "Error"
562                                     else :
563                                         print ",OK"
564                                         theView.DisplayOnly(aPrsObj)
565                                         theView.FitAll()
566                                         aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TDEFORMEDSHAPE." + thePictureExt
567                                         aPictureName = re.sub("\s+","_", aPictureName)
568                                         theView.SavePicture(aPictureName)
569                                         if theIsAutoDelete :
570                                             aPrsObj.RemoveFromStudy()
571                                         else :
572                                             aVISUObjList.append(aPrsObj)
573
574                                 if HasValue(thePrsTypeList,VISU.TVECTORS) :
575                                     print "          Creating VectorsOnField",
576                                     aPrsObj = theVisu.VectorsOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
577                                     if aPrsObj is None : print "Error"
578                                     else :
579                                         print ",OK"
580                                         theView.DisplayOnly(aPrsObj)
581                                         theView.FitAll()
582                                         aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TVECTORS." + thePictureExt
583                                         aPictureName = re.sub("\s+","_", aPictureName)
584                                         theView.SavePicture(aPictureName)
585                                         if theIsAutoDelete :
586                                             aPrsObj.RemoveFromStudy()
587                                         else :
588                                             aVISUObjList.append(aPrsObj)
589                                     
590                                 if HasValue(thePrsTypeList,VISU.TSTREAMLINES) :
591                                     print "          Creating StreamLinesOnField",
592                                     aPrsObj = theVisu.StreamLinesOnField(theResult,aMeshName,anEntity,aFieldName,aTimeStampId)
593                                     if aPrsObj is None : print "Error"
594                                     else :
595                                         print ",OK"
596                                         theView.DisplayOnly(aPrsObj)
597                                         theView.FitAll()
598                                         aPictureName = thePictureDir + aMeshNamePic + "_" + anEntityId + "_" + aFieldName + "_" + str(aTimeStampId) + "_TSTREAMLINES." + thePictureExt
599                                         aPictureName = re.sub("\s+","_", aPictureName)
600                                         theView.SavePicture(aPictureName)
601                                         if theIsAutoDelete :
602                                             aPrsObj.RemoveFromStudy()
603                                         else :
604                                             aVISUObjList.append(aPrsObj)
605     return aVISUObjList
606
607
608 def generateName(prefix = None):
609     import whrandom;
610     int = whrandom.randint(1,1000);
611     if prefix is None:
612         return "Study" + str(int)
613     else :
614         return prefix + str(int)
615     
616
617
618 # ----------------------
619 # MESH
620 # ----------------------
621 def try_mesh_parameters(theMeshPattern):
622     aResult = []
623     if theMeshPattern is None : return aResult ;
624     theMeshPattern =  theMeshPattern._narrow(VISU.Mesh)
625     if theMeshPattern is None : return aResult ;
626
627     aTYPES = [VISU.POINT, VISU.WIREFRAME, VISU.SHADED, VISU.INSIDEFRAME, VISU.SHRINK]
628     import copy; import os;
629     for ind in aTYPES:
630         aNewMesh = copy.deepcopy(theMeshPattern);
631         aNewMesh.SetPresentationType(ind)
632         aResult.append(aNewMesh)
633
634     return aResult
635
636
637 # ----------------------------    
638 # SCALAR MAP 
639 # ----------------------------    
640 ind=1  #try safe way
641 def try_scalarmap_parameters(thePattern, dump = 0):
642
643     if thePattern  is None : return None 
644
645     SCALING = [VISU.LINEAR, VISU.LOGARITHMIC]
646     import copy
647     import random
648
649     anObj = thePattern#copy.deepcopy(thePattern);
650     #ind = random.randint(1,2)
651     if ind%2 :
652             #try incorrect value deliberately (but allowed by idl description)
653             #try SetScalarMode(long)
654             mode = random.randint(-100000,100000); #incorrect value deliberately
655     else:
656             #correct value of ScalarMode
657             mode = random.randint(0, 3)
658
659     if dump : print "\tSetScalarMode(" + str(mode) +")"
660     anObj.SetScalarMode(mode)
661
662     # --- SCALING ---
663     scal = random.randint(0,1)
664     if dump : print "\tSetScaling(" + str(SCALING[scal]) +")"
665     anObj.SetScaling(SCALING[scal])
666         
667     # --- BOUNDARIES ---
668     if ind%2 :
669             alfa =  random.random()*random.randint(-100000,100000)
670             betta = random.random()*random.randint(-100000,100000)
671             aMin = alfa; aMax = betta
672     else:
673             #more correct set
674             aPMin = thePattern.GetMin()
675             aPMax = thePattern.GetMax()
676             aLen = aPMax - aPMin
677             alfa =  random.random()%0.5
678             betta = random.random()%0.5
679             aMin = alfa*aLen*random.randint(-1,1) + aPMin
680             aMax = betta*aLen*random.randint(-1,1) + aPMax
681     if dump : print "\tSetRange(" + str(aMin) + ", " + str(aMax) + ")"
682     anObj.SetRange(aMin, aMax)
683
684         # --- POSITION ---
685     if ind%2:
686             X=random.random()*random.randint(-100000,100000)
687             Y=random.random()*random.randint(-100000,100000)
688     else :
689              X=random.random()
690              Y=random.random()
691     if dump : print "SetPosition("+ str(X) + ", " + str(Y) + " )"
692     anObj.SetPosition(X, Y)
693
694         # --- SCALAR BAR SIZE ---
695     if ind%2:
696             aWidth=random.random()*random.randint(-100000,100000)
697             aHeight=random.random()*random.randint(-100000,100000)
698     else :
699              aWidth=random.random()
700              aHeight=random.random()
701     if dump : print " SCALAR BAR Width = " + str(aWidth) + " Height = ", str(aHeight)
702     anObj.SetSize(aWidth, aHeight)
703     
704     return anObj
705
706
707 def dump_scalarmap_parameters(anObj):
708     
709     print "\tGetScalarMode() = " + str(anObj.GetScalarMode()) 
710     print "\tGetScaling() = " + str(anObj.GetScaling()) 
711     print "\tGetMin() = " + str(anObj.GetMin()) + "  GetMax() = " + str (anObj.GetMax())
712     print "\tGetOrientation() = " + str(anObj.GetOrientation())
713     print "\tGetPosX() = ", str(anObj.GetPosX()) + "  GetPosY() = ", str(anObj.GetPosY())
714     print "\tGetWidth() = ", str ( anObj.GetWidth()) + "  GetHeight() = " + str(anObj.GetHeight())
715
716 # ----------------------
717 # DEFORMED SHAPE
718 # ----------------------
719 def try_deformedshape_parameters(thePattern) :
720
721     if thePattern  is None : return None 
722     import copy
723     import random
724
725     anObj = try_scalarmap_parameters(thePattern)
726
727     # --- SCALING ---
728     if ind%2:
729         anObj.SetScale( random.random()*random.randint(-100000, 100000))
730     else :
731         anObj.SetScale( anObj.GetScale()*random.random())
732
733     return anObj
734
735
736 def dump_deformedshape_parameters(theObject):
737     dump_scalarmap_parameters(theObject)
738     print "GetScale() = ", theObject.GetScale()
739     
740 # ----------------------
741 # CUT PLANES
742 # ----------------------
743 def try_cutplanes_parameters(thePattern) :
744
745     if thePattern  is None : return None 
746     import copy
747     import random
748
749     ORIENT = [VISU.CutPlanes.XY, VISU.CutPlanes.YZ, VISU.CutPlanes.ZX]
750
751     ind = random.randint(1,2)
752     anObj = try_scalarmap_parameters(thePattern)
753         
754     if ind%2 :   anObj.SetNbPlanes(random.randint(-40,40))
755     else :       anObj.SetNbPlanes(random.randint(0,10)) #try behaivor if NbPlanes=0
756
757     # --- DISPLACEMENT ---
758     anObj.SetDisplacement(random.randint(-100000,100000))
759
760     # --- PLANE POSITION ---
761     if ind%2:
762         PlaneNb = random.randint(-100000,100000) #incorrect value is possible
763     else    : PlaneNb = random.randint(0, anObj.GetNbPlanes())
764         
765     anObj.SetPlanePosition(PlaneNb, random.random()*random.randint(-100000,100000))
766
767     # --- SET DEFAULT ---
768     anObj.SetDefault(PlaneNb)
769
770     # --- SET X,Y,Z ROTATION ---
771     if ind%2 :
772             angle1 = random.random()*random.randint(-100000,100000)
773             angle2 = random.random()*random.randint(-100000,100000)
774     else :
775             angle1 = random.random()*3.14
776             angle2 = random.random()*3.14
777         
778     # --- ORIENTATION ---
779     anObj.SetOrientation(ORIENT[random.randint(0,2)],angle1,angle2)
780
781     return anObj
782         
783 def dump_cutplanes_parameters(theObject):
784         dump_saclarmap_parameters(theObject)
785
786         print "GetOrientationType = " + str(theObject.GetOrientationType())
787         PlanesNb = theObject.GetNbPlanes()
788         print "GetNbPlanes() = ", str(PlanesNb)
789         for i in range(0,PlanesNb+1):
790             if theObject.IsDefault(i) :
791                 print "Default plane : "+str(i); break
792         print "GetPlanePosition(" + str(i) + ") = ", theObject.GetPlanePosition(i)
793         print "GetDisplacement() = ", str(theObject.GetDisplacement())
794         print "GetRotateX() = ", str(theObject.GetRotateX())
795         print "GetRotateY() = ", str(theObject.GetRotateY())
796         print "GetRotateZ() = ", str(theObject.GetRotateZ())
797
798 # ----------------------
799 # CUT LINES
800 # ----------------------
801 def try_cutlines_parameters(thePattern):
802
803     if thePattern  is None : return None 
804     import copy
805     import random
806
807     ORIENT = [VISU.CutPlanes.XY, VISU.CutPlanes.YZ, VISU.CutPlanes.ZX]
808     ind = random.randint(1,2)
809     anObj = try_scalarmap_parameters(thePattern)
810
811     # --- ORIENTATION ---
812     anObj.SetOrientation(ORIENT[random.randint(0,2)],
813                          random.randint(-100,100)*random.random(),
814                          random.randint(-100,100)*random.random())
815     anObj.SetOrientation2(ORIENT[random.randint(0,2)],
816                           random.randint(-100,100)*random.random(),
817                           random.randint(-100,100)*random.random())
818
819     # --- Base Plane Position ---
820     anObj.SetBasePlanePosition( random.random()*random.randint(-100000,100000))
821
822     # --- NUMBER OF LINES ---
823     if ind%2:
824             anObj.SetNbLines(random.randint(-5, 50))
825
826     return anObj
827
828 def dump_cutlines_parameters(theObject):
829     dump_scalarmap_parameters(theObject)
830
831     print "GetOrientationType() = " + str(theObject.GetOrientationType())
832     print "GetOrientationType2() = " + str(theObject.GetOrientationType2())
833     print "GetBasePlanePosition() = "+ str(theObject.GetBasePlanePosition())
834     print "GetNbLines() = " + str(theObject.GetNbLines())
835     print "GetRotateX() = ", str(theObject.GetRotateX())
836     print "GetRotateX2() = ", str(theObject.GetRotateX2())
837     print "GetRotateY() = ", str(theObject.GetRotateY())
838     print "GetRotateY2() = ", str(theObject.GetRotateY2())
839
840 # ----------------------
841 # STREAM LINES
842 # ----------------------
843 def try_streamlines_parameters(thePattern):
844
845     if thePattern  is None : return None 
846     import copy
847     import random
848
849     DIRECTION = [VISU.StreamLines.FORWARD, VISU.StreamLines.BACKWARD, VISU.StreamLines.BOTH]
850
851     ind = random.randint(1,2)
852     anObj = (try_deformedshape_parameters(thePattern))[0]
853
854     # --- DIREACTION ---
855     anObj.SetDirection(DIRECTION[random.randint(0,2)])
856
857     # --- STEP LENGTH ---
858     if ind%2 : anObj.SetStepLength(random.random()*random.randint(-1000,1000))
859     else :
860             aLen = anObj.GetMax() - anObj.GetMin()
861             anObj.SetStepLength(aLen/random.randint(1,100))
862             
863     # --- PROPAGATION TIME ---
864     anObj.SetPropagationTime(random.random()*random.randint(1,100))
865
866     # --- INTEGRATION STEP ---
867     if ind%2 :
868             anObj.SetIntegrationStep(random.random()*random.randint(-1000,1000))
869     else:
870             anObj.SetIntegrationStep(random.random())
871
872     # --- USED POINT ---
873     anObj.SetUsedPoints(random.random()*random.randint(-10000,10000))
874
875     return anObj
876         
877 def dump_streamlines_parameters(theObject):
878     
879     dump_deformedshape_parameters(theObject)
880     
881     print "GetDirection() = "      + str(theObject.GetDirection())
882     print "GetStepLength() = "     + str(theObject.GetStepLength())
883     print "GetPropagationTime() =" + str(theObject.GetPropagationTime())
884     print "GetIntegrationStep() =" + str(theObject.GetIntegrationStep())
885     print "GetUsedPoints()      =" + str(theObject.GetUsedPoints())
886     
887 # ----------------------
888 # VECTORS     
889 # ----------------------
890 def try_vectors_parameters(thePattern, theNum):
891
892     if thePattern  is None : return None 
893     import copy
894     import random
895     GLIPH_TYPE = [VISU.Vectors.ARROW, VISU.Vectors.CONE2, VISU.Vectors.CONE6, VISU.Vectors.NONE]
896     GLIPH_POS = [VISU.Vectors.CENTER, VISU.Vectors.TAIL, VISU.Vectors.HEAD]
897     ind = random.randint(1,2)
898     anObj = (try_deformedshape_parameters(thePattern))[0]
899
900     # --- LINE WIDTH ---
901     if ind%2 :
902             anObj.SetLineWidth(random.random()*random.randint(-10000,10000))
903     else :
904             anObj.SetLineWidth(random.randint(1, 10))
905
906     # --- GLIPH TYPE ---
907     anObj.SetGlyphType(GLIPH_TYPE[random.randint(0, len(GLIPH_TYPE)-1)])
908         
909     # --- GLIPH POS ---
910     anObj.SetGlyphPos(GLIPH_POS[random.randint(0, len(GLIPH_POS)-1)])
911     
912     return anObj
913
914 def dump_vetctors_parameters(theObject):
915     
916     dump_deformedshape_parameters(theObject)
917     
918     print "GetLineWidth() = " +str(theObject.GetLineWidth())
919     print "GetGlyphType() = " +str(theObject.GetGlyphType())
920     print "GetGlyphPos()  = " +str(theObject.GetGlyphPos())
921
922
923 # ----------------------
924 # ISO SURFACES     
925 # ----------------------
926
927 def try_isosurfaces_parameters(thePattern) :
928     if thePattern  is None : return None 
929     import copy
930     import random
931     
932     anObj = try_scalarmap_parameters(thePattern)
933     ind = random.randint(1,2)   
934     # --- SURFACES NUMBER ---
935     if ind%2 :
936             anObj.SetNbSurfaces(random.randint(-100000,100000))
937     else:
938             anObj.SetNbSurfaces(random.randint(1, 50))
939             
940     return anObj
941
942 def dump_isosurfaces_parameters(theObject):
943     
944     dump_scalarmap_parameters(theObject)
945     print "GetNbSurfaces() = "+ str(theObject.GetNbSurfaces())
946
947
948 # ----------------------
949 # TABLES AND CURVES
950 # ----------------------
951
952 def FillTable( theTable, theValues, theRows, theColumns, theRowTitles, theRowUnits, theColumnTitles ):
953     if theTable is None: return
954     if len(theRows) != len(theColumns): return
955     if len(theRows) != len(theValues): return
956     i = 0
957     for value in theValues:
958         theTable.PutValue( value, theRows[ i ], theColumns[ i ])
959         i = i + 1
960         pass
961     i = 1
962     for title in theRowTitles:
963         theTable.SetRowTitle( i, title )
964         theTable.SetRowUnit( i, theRowUnits[ i - 1 ])
965         i = i + 1
966         pass
967     i = 1
968     for title in theColumnTitles:
969         theTable.SetColumnTitle( i, title )
970         i = i + 1
971         pass
972     pass
973
974 def CreateCurve( theTable, theHRow, theVRow, theTitle, theColor, theMarker, theLineType, theLineWidth ):
975     if theTitle is None: return
976     curve = myLocalVisu.CreateCurve( theTable, theHRow, theVRow );
977     if curve:
978         curve.SetTitle( theTitle )
979         curve.SetColor( theColor )
980         curve.SetMarker( theMarker )
981         curve.SetLine( theLineType, theLineWidth )
982         pass
983     return curve