Salome HOME
a05075ed14bc6b6d9b85b7930ba2c91fef5d6860
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationVectorField.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 "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::autoScale()
41 {
42   std::ostringstream oss;
43 //  oss << "import medcalc;";
44 //  pushAndExecPyLine(oss.str()); oss.str("");
45   oss << _objVar << ".ScaleFactor = 2.0*medcalc.ComputeCellAverageSize(__srcObj0)/(" << _rangeVar
46       << "[1]-" << _rangeVar << "[0]);";
47   pushAndExecPyLine(oss.str()); oss.str("");
48 }
49
50 void
51 MEDPresentationVectorField::internalGeneratePipeline()
52 {
53   MEDPresentation::internalGeneratePipeline();
54
55   MEDPyLockWrapper lock;
56
57   setOrCreateRenderView();
58   createSource();
59
60   // Populate internal array of available components:
61   fillAvailableFieldComponents();
62   if (getIntProperty(MEDPresentation::PROP_NB_COMPONENTS) <= 1)
63     {
64       const char * msg = "Vector field presentation does not work for scalar field!"; // this message will appear in GUI too
65       STDLOG(msg);
66       throw KERNEL::createSalomeException(msg);
67     }
68
69   std::ostringstream oss;
70   oss << _objVar << " = pvs.Glyph(Input=" << _srcObjVar << ", GlyphType='Arrow');";
71   pushAndExecPyLine(oss.str()); oss.str("");
72
73   showObject(); // to be done first so that the scale factor computation properly works
74
75   oss << _objVar << ".Scalars = ['"<< _pvFieldType << "', 'None'];";
76   pushAndExecPyLine(oss.str()); oss.str("");
77   oss << _objVar << ".Vectors = ['"<< _pvFieldType << "', '" << _fieldName << "'];";
78   pushAndExecPyLine(oss.str()); oss.str("");
79   oss << _objVar << ".ScaleMode = 'vector';";
80   pushAndExecPyLine(oss.str()); oss.str("");
81   oss << _objVar << ".GlyphMode = 'All Points';";
82   pushAndExecPyLine(oss.str()); oss.str("");
83   oss << _objVar << "GlyphTransform = 'Transform2';";  // not sure this is really needed?
84   pushAndExecPyLine(oss.str()); oss.str("");
85
86   colorBy("POINTS");
87   showScalarBar();
88   selectColorMap();
89   rescaleTransferFunction();
90
91   autoScale();   // to be called after transfer function so we have the range
92
93   resetCameraAndRender();
94 }
95
96 void
97 MEDPresentationVectorField::updatePipeline(const MEDCALC::VectorFieldParameters& params)
98 {
99   if (params.fieldHandlerId != _params.fieldHandlerId)
100     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
101
102   if (params.scalarBarRange != _params.scalarBarRange)
103     {
104       updateScalarBarRange<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.scalarBarRange);
105       autoScale();
106       pushAndExecPyLine("pvs.Render();");
107     }
108   if (params.colorMap != _params.colorMap)
109     updateColorMap<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.colorMap);
110 }