Salome HOME
[MEDCalc] Simplifying IDL enum names.
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationSlices.cxx
1 // Copyright (C) 2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "MEDPresentationSlices.hxx"
21
22 #include <SALOME_KernelServices.hxx>
23 #undef LOG  // should be fixed in KERNEL - double definition
24 #include <Basics_Utils.hxx>
25
26 #include <sstream>
27
28 const std::string MEDPresentationSlices::TYPE_NAME = "MEDPresentationSlices";
29 const std::string MEDPresentationSlices::PROP_NB_SLICES = "nbSlices";
30 const std::string MEDPresentationSlices::PROP_SLICE_ORIENTATION = "slicesOrientation";
31
32 MEDPresentationSlices::MEDPresentationSlices(const MEDCALC::SlicesParameters& params,
33                                                const MEDCALC::ViewModeType viewMode) :
34         MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange),
35         _params(params)
36 {
37   setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, params.nbSlices);
38   setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, params.orientation);
39 }
40
41 std::string
42 MEDPresentationSlices::getNbSlicesCommand() const
43 {
44   std::ostringstream oss1;
45   // TODO
46 //  oss1 << _objVar << ".SliceType.Normal = " << norm << ";";
47   return oss1.str();
48 }
49
50 std::string
51 MEDPresentationSlices::getOrientationCommand() const
52 {
53   std::ostringstream oss1;
54   std::string norm;
55
56   switch(_params.orientation)
57   {
58     case MEDCALC::SLICE_NORMAL_TO_X:
59       norm = "[1.0, 0.0, 0.0]";
60       break;
61     case MEDCALC::SLICE_NORMAL_TO_Y:
62       norm = "[0.0, 1.0, 0.0]";
63       break;
64     case MEDCALC::SLICE_NORMAL_TO_Z:
65       norm = "[0.0, 0.0, 1.0]";
66       break;
67     case MEDCALC::SLICE_NORMAL_TO_XY:
68       norm = "[1.0, 1.0, 0.0]";
69       break;
70     case MEDCALC::SLICE_NORMAL_TO_XZ:
71       norm = "[1.0, 0.0, 1.0]";
72       break;
73     case MEDCALC::SLICE_NORMAL_TO_YZ:
74       norm = "[0.0, 1.0, 1.0]";
75       break;
76     case MEDCALC::SLICE_NORMAL_TO_XYZ:
77       norm = "[1.0, 1.0, 1.0]";
78       break;
79     default:
80       const char * mes = "Unexpected getSliceOrientationCommand() error!";
81       STDLOG(mes);
82       throw KERNEL::createSalomeException(mes);
83   }
84
85   oss1 << _objVar << ".SliceType.Normal = " << norm << ";";
86   return oss1.str();
87 }
88
89
90 void
91 MEDPresentationSlices::internalGeneratePipeline()
92 {
93   MEDPresentation::internalGeneratePipeline();
94
95   MEDPyLockWrapper lock;
96
97   std::ostringstream oss_o, oss;
98   std::string view(getRenderViewVar());
99
100   oss << _srcObjVar << " = pvs.MEDReader(FileName='" << _fileName << "');";
101   pushAndExecPyLine(oss.str()); oss.str("");
102
103   // Populate internal array of available components:
104   fillAvailableFieldComponents();
105   pushAndExecPyLine( getRenderViewCommand() ); // instanciate __viewXXX
106   oss << _objVar << " = pvs.Slice(Input=" << _srcObjVar << ");";
107   pushAndExecPyLine(oss.str()); oss.str("");
108   oss << _objVar << ".SliceType = 'Plane';";
109   pushAndExecPyLine(oss.str()); oss.str("");
110
111   // Set slice orientation
112   pushAndExecPyLine(getOrientationCommand());
113
114   oss << _dispVar << " = pvs.Show(" << _objVar << ", " << view << ");";
115   pushAndExecPyLine(oss.str()); oss.str("");
116   oss << "pvs.ColorBy(" << _dispVar << ", ('" << _fieldType << "', '" << _fieldName << "'));";
117   pushAndExecPyLine(oss.str()); oss.str("");
118   oss << _dispVar <<  ".SetScalarBarVisibility(" << view << ", True);";
119   pushAndExecPyLine(oss.str()); oss.str("");
120   oss << getRescaleCommand();
121   pushAndExecPyLine(oss.str()); oss.str("");
122   oss << _lutVar << " = pvs.GetColorTransferFunction('" << _fieldName << "');";
123   pushAndExecPyLine(oss.str()); oss.str("");
124   pushAndExecPyLine(getColorMapCommand()); oss.str("");
125   pushAndExecPyLine(getResetCameraCommand());
126   pushAndExecPyLine("pvs.Render();");
127 }
128
129 void
130 MEDPresentationSlices::updatePipeline(const MEDCALC::SlicesParameters& params)
131 {
132   if (params.fieldHandlerId != _params.fieldHandlerId)
133     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
134
135   if (std::string(params.displayedComponent) != std::string(_params.displayedComponent))
136     updateComponent<MEDPresentationSlices, MEDCALC::SlicesParameters>(std::string(params.displayedComponent));
137   if (params.scalarBarRange != _params.scalarBarRange)
138     updateScalarBarRange<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarRange);
139   if (params.colorMap != _params.colorMap)
140     updateColorMap<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.colorMap);
141
142   if (params.orientation != _params.orientation)
143     updateOrientation(params.orientation);
144   if (params.nbSlices != _params.nbSlices)
145     updateNbSlices(params.nbSlices);
146 }
147
148 void
149 MEDPresentationSlices::updateNbSlices(const int nbSlices)
150 {
151   _params.nbSlices = nbSlices;
152
153   // GUI helper:
154   setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, nbSlices);
155
156   // Update the pipeline:
157   {
158     MEDPyLockWrapper lock;
159     std::string cmd = getNbSlicesCommand();
160     pushAndExecPyLine(cmd);
161     pushAndExecPyLine("pvs.Render();");
162   }
163 }
164
165 void
166 MEDPresentationSlices::updateOrientation(const MEDCALC::SliceOrientationType orientation)
167 {
168   _params.orientation = orientation;
169
170   // GUI helper:
171   setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, orientation);
172
173   // Update the pipeline:
174   {
175     MEDPyLockWrapper lock;
176     std::string cmd = getOrientationCommand();
177     pushAndExecPyLine(cmd);
178     pushAndExecPyLine("pvs.Render();");
179   }
180 }
181