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