Salome HOME
fix of typos by kunda http://www.salome-platform.org/forum/forum_10/789486812
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationContour.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 "MEDPresentationContour.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 MEDPresentationContour::TYPE_NAME = "MEDPresentationContour";
29 const std::string MEDPresentationContour::PROP_NB_CONTOUR = "nbContour";
30
31 MEDPresentationContour::MEDPresentationContour(const MEDCALC::ContourParameters& params,
32                                                const MEDCALC::ViewModeType viewMode) :
33         MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange),
34         _params(params)
35 {
36   setIntProperty(MEDPresentationContour::PROP_NB_CONTOUR, params.nbContours);
37 }
38
39 void
40 MEDPresentationContour::initFieldMeshInfos()
41 {
42   MEDPresentation::initFieldMeshInfos();
43   _colorByType = "POINTS";
44 }
45
46 void
47 MEDPresentationContour::setNumberContours()
48 {
49   std::ostringstream oss;
50
51   oss << "min_max = " << _srcObjVar << ".PointData.GetArray('" << _fieldName << "').GetRange();";
52   pushAndExecPyLine(oss.str()); oss.str("");
53   oss << "delta = (min_max[1]-min_max[0])/float(" << _params.nbContours << ");";
54   pushAndExecPyLine(oss.str()); oss.str("");
55   oss << _objVar << ".Isosurfaces = [min_max[0]+0.5*delta+i*delta for i in range(" << _params.nbContours << ")];";
56   pushAndExecPyLine(oss.str()); oss.str("");
57 }
58
59 void
60 MEDPresentationContour::internalGeneratePipeline()
61 {
62   MEDPresentation::internalGeneratePipeline();
63
64   MEDPyLockWrapper lock;
65
66   createSource();
67
68   // Populate internal array of available components:
69   fillAvailableFieldComponents();
70   if (getIntProperty(MEDPresentation::PROP_NB_COMPONENTS) > 1)
71     {
72       const char * msg = "Contour presentation only works for scalar field!"; // this message will appear in GUI too
73       STDLOG(msg);
74       throw KERNEL::createSalomeException(msg);
75     }
76   if (_params.nbContours < 1)
77     {
78       const char * mes = "Invalid number of contours!";
79       STDLOG(mes);
80       throw KERNEL::createSalomeException(mes);
81     }
82
83   setOrCreateRenderView(); // instantiate __viewXXX, needs to be after the exception above otherwise previous elements in the view will be hidden.
84
85   // Contour needs point data:
86   applyCellToPointIfNeeded();
87
88   std::ostringstream oss;
89   oss << _objVar << " = pvs.Contour(Input=" << _srcObjVar << ");";
90   pushAndExecPyLine(oss.str()); oss.str("");
91
92   showObject();
93
94   oss << _objVar << ".ContourBy = ['POINTS', '" << _fieldName << "'];";
95   pushAndExecPyLine(oss.str()); oss.str("");
96
97   // Colorize contour
98   oss << _objVar << ".ComputeScalars = 1;";
99   pushAndExecPyLine(oss.str()); oss.str("");
100
101   // Set number of contours
102   setNumberContours();
103
104   colorBy();    // see initFieldInfo() - necessarily POINTS because of the conversion above
105   showScalarBar();
106   selectColorMap();
107   rescaleTransferFunction();
108   resetCameraAndRender();
109 }
110
111 void
112 MEDPresentationContour::updatePipeline(const MEDCALC::ContourParameters& params)
113 {
114   if (params.fieldHandlerId != _params.fieldHandlerId)
115     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
116
117   if (params.scalarBarRange != _params.scalarBarRange)
118     updateScalarBarRange<MEDPresentationContour, MEDCALC::ContourParameters>(params.scalarBarRange);
119   if (params.colorMap != _params.colorMap)
120     updateColorMap<MEDPresentationContour, MEDCALC::ContourParameters>(params.colorMap);
121
122   if (params.nbContours != _params.nbContours)
123     {
124       if (params.nbContours < 1)
125         {
126           const char * mes = "Invalid number of contours!";
127           STDLOG(mes);
128           throw KERNEL::createSalomeException(mes);
129         }
130       updateNbContours(params.nbContours);
131     }
132 }
133
134 void
135 MEDPresentationContour::updateNbContours(const int nbContours)
136 {
137   _params.nbContours = nbContours;
138
139   // GUI helper:
140   setIntProperty(MEDPresentationContour::PROP_NB_CONTOUR, nbContours);
141
142   // Update the pipeline:
143   {
144     MEDPyLockWrapper lock;
145     setNumberContours();
146     pushAndExecPyLine("pvs.Render();");
147   }
148 }