Salome HOME
Better timestamp management to synchronize the timestamp of the view and the field id
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationVectorField.cxx
1 // Copyright (C) 2016-2020  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 "MEDPyLockWrapper.hxx"
21
22 #include "MEDPresentationVectorField.hxx"
23 #include "MEDPresentationException.hxx"
24
25 #include <SALOME_KernelServices.hxx>
26 #undef LOG  // should be fixed in KERNEL - double definition
27 #include <Basics_Utils.hxx>
28
29 #include <sstream>
30
31 const std::string MEDPresentationVectorField::TYPE_NAME = "MEDPresentationVectorField";
32
33 MEDPresentationVectorField::MEDPresentationVectorField(const MEDCALC::VectorFieldParameters& params,
34                                                    const MEDCALC::ViewModeType viewMode) :
35     MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange), _params(params)
36 {
37 }
38
39 void
40 MEDPresentationVectorField::initFieldMeshInfos()
41 {
42   MEDPresentation::initFieldMeshInfos();
43   _colorByType = "POINTS";
44 }
45
46 void
47 MEDPresentationVectorField::autoScale()
48 {
49   std::ostringstream oss;
50 //  oss << "import medcalc;";
51 //  pushAndExecPyLine(oss.str()); oss.str("");
52   oss << _objVar << ".ScaleFactor = 2.0*medcalc.ComputeCellAverageSize(" << _srcObjVar << ")/(" << _rangeVar
53       << "[1]-" << _rangeVar << "[0]);";
54   pushAndExecPyLine(oss.str()); oss.str("");
55 }
56
57 void
58 MEDPresentationVectorField::internalGeneratePipeline()
59 {
60   MEDPresentation::internalGeneratePipeline();
61
62   MEDPyLockWrapper lock;
63
64   createSource();
65   setTimestamp();
66
67   // Populate internal array of available components:
68   fillAvailableFieldComponents();
69   int nbCompo = getIntProperty(MEDPresentation::PROP_NB_COMPONENTS);
70   if (nbCompo <= 1)
71     {
72       const char * msg = "Vector field presentation does not work for scalar field!"; // this message will appear in GUI too
73       STDLOG(msg);
74       throw KERNEL::createSalomeException(msg);
75     }
76
77   setOrCreateRenderView();  // instantiate __viewXXX, needs to be after the exception above otherwise previous elements in the view will be hidden.
78
79   std::ostringstream oss;
80   oss << _objVar << " = pvs.Glyph(Input=" << _srcObjVar << ", GlyphType='Arrow');";
81   pushAndExecPyLine(oss.str()); oss.str("");
82
83   showObject(); // to be done first so that the scale factor computation properly works
84
85   std::string fieldName = nbCompo <= 2 ? _fieldName + "_Vector" : _fieldName;
86
87   oss << _objVar << ".ScaleArray = ['"<< _pvFieldType << "', '" << fieldName << "'];";
88   pushAndExecPyLine(oss.str()); oss.str("");
89
90   oss << _objVar << ".OrientationArray = ['"<< _pvFieldType << "', '" << fieldName << "'];";
91
92   pushAndExecPyLine(oss.str()); oss.str("");
93   oss << _objVar << ".VectorScaleMode = 'Scale by Magnitude';";
94   pushAndExecPyLine(oss.str()); oss.str("");
95   oss << _objVar << ".GlyphMode = 'All Points';";
96   pushAndExecPyLine(oss.str()); oss.str("");
97   oss << _objVar << "GlyphTransform = 'Transform2';";  // not sure this is really needed?
98   pushAndExecPyLine(oss.str()); oss.str("");
99
100   colorBy();    // see initFieldInfo() - necessarily POINTS
101   showScalarBar();
102   selectColorMap();
103   rescaleTransferFunction();
104
105   autoScale();   // to be called after transfer function so we have the range
106
107   resetCameraAndRender();
108 }
109
110 void
111 MEDPresentationVectorField::updatePipeline(const MEDCALC::VectorFieldParameters& params)
112 {
113   if (params.fieldHandlerId != _params.fieldHandlerId)
114     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
115
116   if (params.scalarBarRange != _params.scalarBarRange)
117     {
118       updateScalarBarRange<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.scalarBarRange);
119       autoScale();
120       pushAndExecPyLine("pvs.Render();");
121     }
122   if (params.colorMap != _params.colorMap)
123     updateColorMap<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.colorMap);
124 }