Salome HOME
Merge 'abn/V8_1_fix' branch into V8_1_BR.
[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   int id = GeneratePythonId();
41   std::ostringstream oss;
42   oss << "__objLst" << id;
43   _sliceListVar = oss.str();
44 }
45
46 void
47 MEDPresentationSlices::setSliceParametersAndGroup()
48 {
49   std::ostringstream oss;
50   int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
51   std::string normal = getNormalVector();
52
53   oss << "__origins = medcalc.GetSliceOrigins(" << _srcObjVar << ", " << nbSlices << ", " << normal << ");";
54   pushAndExecPyLine(oss.str()); oss.str("");
55
56   oss << "for sliceNum in range(" << nbSlices << "):\n";
57   oss << "  " << _sliceListVar << "[sliceNum].SliceType.Normal = " << normal << ";\n";
58   oss << "  " << _sliceListVar << "[sliceNum].SliceType.Origin = __origins[sliceNum];\n";
59   pushAndExecPyLine(oss.str()); oss.str("");
60
61   oss << _objVar << " = pvs.GroupDatasets(Input=" << _sliceListVar << ");";
62   pushAndExecPyLine(oss.str()); oss.str("");
63 }
64
65 void
66 MEDPresentationSlices::deleteGroup()
67 {
68   std::ostringstream oss;
69   oss << "pvs.Delete(" << _objVar << ");";
70   pushAndExecPyLine(oss.str()); oss.str("");
71 }
72
73 void
74 MEDPresentationSlices::adaptNumberOfSlices()
75 {
76   std::ostringstream oss;
77   int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
78
79   oss << "for _ in range(max(len(" << _sliceListVar << ")-" << nbSlices << ", 0)):\n";
80   oss << "  pvs.Delete(" << _sliceListVar << ".pop());\n";
81   pushAndExecPyLine(oss.str()); oss.str("");
82
83   oss << "for _ in range(" << nbSlices << "-max(len(" << _sliceListVar << "), 0)):\n";
84   oss << "  obj = pvs.Slice(Input=" << _srcObjVar << ");\n";
85   oss << "  obj.SliceType = 'Plane';\n";
86   oss << "  " << _sliceListVar << ".append(obj);\n";
87   pushAndExecPyLine(oss.str()); oss.str("");
88 }
89
90 void
91 MEDPresentationSlices::generateAndDisplay()
92 {
93   adaptNumberOfSlices();
94   setSliceParametersAndGroup();
95
96   recreateViewSetup();
97 }
98
99 std::string
100 MEDPresentationSlices::getNormalVector() const
101 {
102   switch(_params.orientation)
103   {
104     case MEDCALC::SLICE_NORMAL_TO_X:
105       return "[1.0, 0.0, 0.0]";
106     case MEDCALC::SLICE_NORMAL_TO_Y:
107       return "[0.0, 1.0, 0.0]";
108     case MEDCALC::SLICE_NORMAL_TO_Z:
109       return "[0.0, 0.0, 1.0]";
110     case MEDCALC::SLICE_NORMAL_TO_XY:
111       return "[1.0, 1.0, 0.0]";
112     case MEDCALC::SLICE_NORMAL_TO_XZ:
113       return "[1.0, 0.0, 1.0]";
114     case MEDCALC::SLICE_NORMAL_TO_YZ:
115       return "[0.0, 1.0, 1.0]";
116     case MEDCALC::SLICE_NORMAL_TO_XYZ:
117       return "[1.0, 1.0, 1.0]";
118     default:
119       const char * mes = "Unexpected getSliceOrientationCommand() error!";
120       STDLOG(mes);
121       throw KERNEL::createSalomeException(mes);
122   }
123   return ""; // never happens
124 }
125
126 void
127 MEDPresentationSlices::selectSliceOrientation(const std::string & obj)
128 {
129   std::ostringstream oss1;
130   oss1 << obj << ".SliceType.Normal = " << getNormalVector() << ";";
131   pushAndExecPyLine(oss1.str()); oss1.str("");
132 }
133
134 void
135 MEDPresentationSlices::internalGeneratePipeline()
136 {
137   MEDPresentation::internalGeneratePipeline();
138
139   MEDPyLockWrapper lock;
140
141   std::ostringstream oss;
142
143   createSource();
144
145   // Populate internal array of available components:
146   fillAvailableFieldComponents();
147   if (_params.nbSlices < 1)
148   {
149       const char * mes = "Invalid number of slices!";
150       STDLOG(mes);
151       throw KERNEL::createSalomeException(mes);
152   }
153
154   setOrCreateRenderView(); // instanciate __viewXXX
155
156   // Now create the initial slices list
157   oss << _sliceListVar << " = [];";
158   pushAndExecPyLine(oss.str()); oss.str("");
159
160   generateAndDisplay();
161 }
162
163 void
164 MEDPresentationSlices::updatePipeline(const MEDCALC::SlicesParameters& params)
165 {
166   if (params.fieldHandlerId != _params.fieldHandlerId)
167     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
168
169   if (std::string(params.displayedComponent) != std::string(_params.displayedComponent))
170     updateComponent<MEDPresentationSlices, MEDCALC::SlicesParameters>(std::string(params.displayedComponent));
171   if (params.scalarBarRange != _params.scalarBarRange)
172     updateScalarBarRange<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarRange);
173   if (params.colorMap != _params.colorMap)
174     updateColorMap<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.colorMap);
175
176   if (params.orientation != _params.orientation)
177     updateOrientation(params.orientation);
178   if (params.nbSlices != _params.nbSlices)
179     {
180       if (params.nbSlices < 1)
181       {
182           const char * mes = "Invalid number of slices!";
183           STDLOG(mes);
184           throw KERNEL::createSalomeException(mes);
185       }
186       updateNbSlices(params.nbSlices);
187     }
188 }
189
190 void
191 MEDPresentationSlices::updateNbSlices(const int nbSlices)
192 {
193   _params.nbSlices = nbSlices;
194
195   // GUI helper:
196   setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, nbSlices);
197
198   // Update the pipeline:
199   {
200     MEDPyLockWrapper lock;
201     deleteGroup();
202     generateAndDisplay();
203   }
204 }
205
206 void
207 MEDPresentationSlices::updateOrientation(const MEDCALC::SliceOrientationType orientation)
208 {
209   _params.orientation = orientation;
210
211   // GUI helper:
212   setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, static_cast<int>(orientation));
213
214   // Update the pipeline:
215   {
216     MEDPyLockWrapper lock;
217
218     deleteGroup();
219     generateAndDisplay();
220   }
221 }
222