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