Salome HOME
Update copyrights 2014.
[modules/med.git] / src / MEDOP / tui / xmedpy / driver_visu.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2014  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Guillaume Boulant (EDF) 
21
22 import VISU
23 import MEDCoupling
24 __mapMedType2VisuType={
25     MEDCoupling.ON_CELLS:VISU.CELL,
26     MEDCoupling.ON_NODES:VISU.NODE,
27     MEDCoupling.ON_GAUSS_PT:VISU.TGAUSSPOINTS
28     }
29
30 def visu_typeOfField(medTypeOfField):
31     """
32     This function gives the visu type corresponding to the specified
33     med type.
34     """
35     try:
36         return __mapMedType2VisuType[medTypeOfField]
37     except IndexError, e:
38         return "UNCKNOWN"
39
40
41 import salome
42 from libSALOME_Swig import SALOMEGUI_Swig
43
44 def visu_scalarmap(filename,meshname,fieldname,typeOfField,iteration=-1):
45     """
46     This is the minimalist function to render a scalar map on a field
47     load from a med file using the VISU module.
48     """
49
50     # We first have to prepare a pointer to the VISU component engine.
51     visuComp = salome.lcc.FindOrLoadComponent("FactoryServer", "VISU")
52     visuComp.SetCurrentStudy(salome.myStudy)
53
54     visumed = visuComp.CreateResult(filename)
55     visumed.SetBuildGroups(True)
56     visumed.SetBuildFields(True, True)
57     visumed.Build(False, True)
58     if not visumed.IsDone() :
59         print "ERR: can't create a representation of med data"
60         return False
61     
62     visuComp.RenameEntityInStudy(visumed, meshname, VISU.NODE, 'onNodes')
63     visuComp.RenameEntityInStudy(visumed, meshname, VISU.CELL, 'onCells')
64
65     visuType = visu_typeOfField(typeOfField)
66     scalarmap = visuComp.ScalarMapOnField(visumed,
67                                           meshname,
68                                           visuType,
69                                           fieldname,
70                                           iteration)
71     if scalarmap is None:
72         print "ERR: can't create a scalar map"
73         return False
74     
75     # __GBO__ maybe it could be appreciated to select the component to
76     # display. In this interface, the modulus of the field is
77     # considered.
78     component  = 1
79     scalarmap.SetScalarMode(component)
80     scalarmap.SetSourceRange()
81     scalarmap.SetScaling(VISU.LINEAR)
82     scalarmap.SetTitle(fieldname)
83     
84     # This final part is to automatically display the scalar map in a
85     # VISU viewer.
86     sg = SALOMEGUI_Swig()
87     sg.updateObjBrowser(1)
88     
89     # Display the scalar map in the viewer
90     myViewManager = visuComp.GetViewManager()
91     myView = myViewManager.Create3DView()
92     myView.Maximize()
93     myView.Display(scalarmap);
94     myView.SetFocalPoint([0,0,0]);
95     myView.FitAll();
96
97     return True
98
99
100 def TEST_scalarmap():
101     import os
102     from xmed import properties
103     #properties.setup(properties.testdata_02) # test with nodes
104     properties.setup(properties.testdata_03) # test with cells
105
106     # __GBO__: WARN due to a specific feature of VISU, when only one
107     # field timestamps exists in the med file, we have to specify an
108     # iteration number of 1, whatever the iteration value is in the
109     # med file.
110     #iteration = properties.testFieldIt
111     iteration = 1
112     
113     visu_scalarmap(properties.testFilePath,
114                    properties.testMeshName,
115                    properties.testFieldName,
116                    properties.testTypeOfField,
117                    iteration)
118     
119 if __name__ == "__main__":
120     TEST_scalarmap()