Salome HOME
[MEDCalc] Reset camera
[modules/med.git] / src / MEDCalc / cmp / MEDPresentation.cxx
1 // Copyright (C) 2011-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 // Authors: A Bruneton (CEA), C Aguerre (EdF)
20
21 #include "MEDFactoryClient.hxx"
22 #include "MEDPresentation.hxx"
23 #include "MEDPresentationException.hxx"
24 #include "MEDCouplingRefCountObject.hxx"
25 #include <iostream>
26
27 MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, std::string name)
28     : _fieldHandlerId(fieldHandlerId), _pipeline(0), _display(0), _properties()
29 {
30   MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager());
31   MEDCALC::FieldHandler* fieldHandler = dataManager->getFieldHandler(fieldHandlerId);
32   MEDCALC::MeshHandler* meshHandler = dataManager->getMesh(fieldHandler->meshid);
33   MEDCALC::DatasourceHandler* dataSHandler = dataManager->getDatasourceHandlerFromID(meshHandler->sourceid);
34
35   _fileName = dataSHandler->uri;
36   _fieldName = fieldHandler->fieldname;
37   _fieldType = getFieldTypeString((MEDCoupling::TypeOfField) fieldHandler->type);
38
39   if (_fileName.substr(0, 7) != std::string("file://")) {
40     const char* msg = "Data source is not a file! Can not proceed.";
41     throw MEDPresentationException(msg);
42   }
43   _fileName = _fileName.substr(7, _fileName.size());
44
45   setProperty("name", name);
46 }
47
48 void MEDPresentation::generatePipeline()
49 {
50   // Might be more complicated in the future:
51
52   this->internalGeneratePipeline();
53 }
54
55 void MEDPresentation::pushInternal(PyObject * obj, PyObject * disp)
56 {
57   _pipeline.push_back(obj);
58   _display.push_back(disp);
59 }
60
61 void MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
62 {
63   // LIMITED!!! For now switch the first display element to Wireframe
64   /*
65   PyLockWrapper lock;
66   PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
67   */
68
69   _properties[propName] = propValue;
70 }
71
72 const std::string
73 MEDPresentation::getProperty(const std::string& propName) const
74 {
75   std::map<std::string, std::string>::const_iterator it = _properties.find(propName);
76   if (it != _properties.end()) {
77     return (*it).second;
78   }
79   else {
80     std::cerr << "getProperty(): no property named " << propName << std::endl;
81     return std::string();
82   }
83 }
84
85 PyObject * MEDPresentation::getPythonObjectFromMain(const char * python_var) const
86 {
87   // TODO: improve to avoid getting dict at each call
88
89   // All the calls below returns *borrowed* references
90   PyObject* main_module = PyImport_AddModule((char*)"__main__");
91   PyObject* global_dict = PyModule_GetDict(main_module);
92   return PyDict_GetItemString(global_dict, python_var);
93 }
94
95 std::string MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType) const
96 {
97   switch(fieldType)
98   {
99     case MEDCoupling::ON_CELLS:
100       return "CELLS";
101     case MEDCoupling::ON_NODES:
102       return "POINTS";
103     default:
104       std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
105       return "";
106   }
107 }
108
109 std::string
110 MEDPresentation::getRenderViewCommand(MEDCALC::MEDPresentationViewMode viewMode) const
111 {
112   std::string cmd = std::string("pvs._DisableFirstRenderCameraReset();");
113   if (viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
114     cmd += std::string("__view1 = pvs.GetActiveViewOrCreate('RenderView');");
115   } else if (viewMode == MEDCALC::VIEW_MODE_REPLACE) {
116     cmd += std::string("__view1 = pvs.GetActiveViewOrCreate('RenderView');");
117     cmd += std::string("pvs.active_objects.source and pvs.Hide(view=__view1);");
118     cmd += std::string("pvs.Render();");
119   } else if (viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
120     cmd += std::string("__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');");
121     cmd += std::string("__view1 = pvs.CreateView('RenderView');");
122   } else if (viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
123     cmd += std::string("__view1 = pvs.CreateView('RenderView');");
124   }
125   return cmd;
126 }
127
128 std::string
129 MEDPresentation::getResetCameraCommand() const
130 {
131   return std::string("__view1.ResetCamera();");
132 }
133
134
135 std::string
136 MEDPresentation::getColorMapCommand(MEDCALC::MEDPresentationColorMap colorMap) const
137 {
138   switch(colorMap) {
139   case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "Blue to Red Rainbow";
140   case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "Cool to Warm";
141   }
142 }