1 // Copyright (C) 2016-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MEDPresentationSlices.hxx"
22 #include <SALOME_KernelServices.hxx>
23 #undef LOG // should be fixed in KERNEL - double definition
24 #include <Basics_Utils.hxx>
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";
32 MEDPresentationSlices::MEDPresentationSlices(const MEDCALC::SlicesParameters& params,
33 const MEDCALC::ViewModeType viewMode) :
34 MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange),
37 setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, params.nbSlices);
38 setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, params.orientation);
40 int id = GeneratePythonId();
41 std::ostringstream oss;
42 oss << "__objLst" << id;
43 _sliceListVar = oss.str();
47 MEDPresentationSlices::setSliceParametersAndGroup()
49 std::ostringstream oss;
50 int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
51 std::string normal = getNormalVector();
53 oss << "__origins = medcalc.GetSliceOrigins(" << _srcObjVar << ", " << nbSlices << ", " << normal << ");";
54 pushAndExecPyLine(oss.str()); oss.str("");
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("");
61 oss << _objVar << " = pvs.GroupDatasets(Input=" << _sliceListVar << ");";
62 pushAndExecPyLine(oss.str()); oss.str("");
66 MEDPresentationSlices::deleteGroup()
69 std::ostringstream oss;
70 oss << "pvs.Hide(" << _objVar << ");";
71 oss << "pvs.Delete(" << _objVar << ");del " <<_objVar<<";";
72 pushAndExecPyLine(oss.str()); oss.str("");
76 MEDPresentationSlices::adaptNumberOfSlices()
78 std::ostringstream oss;
79 int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
81 oss << "for _ in range(int(max(len(" << _sliceListVar << ")-" << nbSlices << ", 0))):\n";
82 oss << " pvs.Delete(" << _sliceListVar << ".pop());\n";
83 pushAndExecPyLine(oss.str()); oss.str("");
85 oss << "for _ in range(" << nbSlices << "-int(max(len(" << _sliceListVar << "), 0))):\n";
86 oss << " obj = pvs.Slice(Input=" << _srcObjVar << ");\n";
87 oss << " obj.SliceType = 'Plane';\n";
88 oss << " " << _sliceListVar << ".append(obj);\n";
89 pushAndExecPyLine(oss.str()); oss.str("");
93 MEDPresentationSlices::generateAndDisplay()
95 adaptNumberOfSlices();
96 setSliceParametersAndGroup();
101 MEDPresentationSlices::getNormalVector() const
103 switch(_params.orientation)
105 case MEDCALC::SLICE_NORMAL_TO_X:
106 return "[1.0, 0.0, 0.0]";
107 case MEDCALC::SLICE_NORMAL_TO_Y:
108 return "[0.0, 1.0, 0.0]";
109 case MEDCALC::SLICE_NORMAL_TO_Z:
110 return "[0.0, 0.0, 1.0]";
111 case MEDCALC::SLICE_NORMAL_TO_XY:
112 return "[1.0, 1.0, 0.0]";
113 case MEDCALC::SLICE_NORMAL_TO_XZ:
114 return "[1.0, 0.0, 1.0]";
115 case MEDCALC::SLICE_NORMAL_TO_YZ:
116 return "[0.0, 1.0, 1.0]";
117 case MEDCALC::SLICE_NORMAL_TO_XYZ:
118 return "[1.0, 1.0, 1.0]";
120 const char * mes = "Unexpected getSliceOrientationCommand() error!";
122 throw KERNEL::createSalomeException(mes);
124 return ""; // never happens
128 MEDPresentationSlices::selectSliceOrientation(const std::string & obj)
130 std::ostringstream oss1;
131 oss1 << obj << ".SliceType.Normal = " << getNormalVector() << ";";
132 pushAndExecPyLine(oss1.str()); oss1.str("");
136 MEDPresentationSlices::internalGeneratePipeline()
138 MEDPresentation::internalGeneratePipeline();
140 MEDPyLockWrapper lock;
142 std::ostringstream oss;
147 // Populate internal array of available components:
148 fillAvailableFieldComponents();
149 if (_params.nbSlices < 1)
151 const char * mes = "Invalid number of slices!";
153 throw KERNEL::createSalomeException(mes);
156 setOrCreateRenderView(); // instantiate __viewXXX
158 // Now create the initial slices list
159 oss << _sliceListVar << " = [];";
160 pushAndExecPyLine(oss.str()); oss.str("");
162 generateAndDisplay();
166 MEDPresentationSlices::updatePipeline(const MEDCALC::SlicesParameters& params)
168 if (params.fieldHandlerId != _params.fieldHandlerId)
169 throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
171 if (std::string(params.displayedComponent) != std::string(_params.displayedComponent))
172 updateComponent<MEDPresentationSlices, MEDCALC::SlicesParameters>(std::string(params.displayedComponent));
173 if (params.scalarBarRange != _params.scalarBarRange ||
174 params.hideDataOutsideCustomRange != _params.hideDataOutsideCustomRange ||
175 params.scalarBarRangeArray[0] != _params.scalarBarRangeArray[0] ||
176 params.scalarBarRangeArray[1] != _params.scalarBarRangeArray[1])
177 updateScalarBarRange<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarRange,
178 params.hideDataOutsideCustomRange,
179 params.scalarBarRangeArray[0],
180 params.scalarBarRangeArray[1]);
181 if (params.colorMap != _params.colorMap)
182 updateColorMap<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.colorMap);
184 if (params.orientation != _params.orientation)
185 updateOrientation(params.orientation);
186 if (params.nbSlices != _params.nbSlices)
188 if (params.nbSlices < 1)
190 const char * mes = "Invalid number of slices!";
192 throw KERNEL::createSalomeException(mes);
194 updateNbSlices(params.nbSlices);
196 if (params.visibility != _params.visibility)
197 updateVisibility<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.visibility);
199 if (params.scalarBarVisibility != _params.scalarBarVisibility)
200 updateScalarBarVisibility<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarVisibility);
204 MEDPresentationSlices::updateNbSlices(const int nbSlices)
206 _params.nbSlices = nbSlices;
209 setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, nbSlices);
211 // Update the pipeline:
213 MEDPyLockWrapper lock;
215 generateAndDisplay();
220 MEDPresentationSlices::updateOrientation(const MEDCALC::SliceOrientationType orientation)
222 _params.orientation = orientation;
225 setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, static_cast<int>(orientation));
227 // Update the pipeline:
229 MEDPyLockWrapper lock;
232 generateAndDisplay();