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