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