Salome HOME
[MEDCalc] Prepare to presentation edition
[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
49 MEDPresentation::generatePipeline()
50 {
51   // Might be more complicated in the future:
52
53   this->internalGeneratePipeline();
54 }
55
56 void
57 MEDPresentation::pushInternal(PyObject* obj, PyObject* disp)
58 {
59   _pipeline.push_back(obj);
60   _display.push_back(disp);
61 }
62
63 void
64 MEDPresentation::setProperty(const std::string& propName, const std::string& propValue)
65 {
66   // LIMITED!!! For now switch the first display element to Wireframe
67   /*
68   PyLockWrapper lock;
69   PyObject_CallMethod(_display[0], (char*)"SetRepresentationType", (char*)"(s)", "Wireframe");
70   */
71
72   _properties[propName] = propValue;
73 }
74
75 const std::string
76 MEDPresentation::getProperty(const std::string& propName) const
77 {
78   std::map<std::string, std::string>::const_iterator it = _properties.find(propName);
79   if (it != _properties.end()) {
80     return (*it).second;
81   }
82   else {
83     std::cerr << "getProperty(): no property named " << propName << std::endl;
84     return std::string();
85   }
86 }
87
88 PyObject*
89 MEDPresentation::getPythonObjectFromMain(const char* python_var) const
90 {
91   // TODO: improve to avoid getting dict at each call
92
93   // All the calls below returns *borrowed* references
94   PyObject* main_module = PyImport_AddModule((char*)"__main__");
95   PyObject* global_dict = PyModule_GetDict(main_module);
96   return PyDict_GetItemString(global_dict, python_var);
97 }
98
99 std::string
100 MEDPresentation::getFieldTypeString(MEDCoupling::TypeOfField fieldType) const
101 {
102   switch(fieldType)
103   {
104     case MEDCoupling::ON_CELLS:
105       return "CELLS";
106     case MEDCoupling::ON_NODES:
107       return "POINTS";
108     default:
109       std::cerr << "MEDPresentation::getFieldTypeString() -- Not implemented ! Gauss points?";
110       return "";
111   }
112 }
113
114 std::string
115 MEDPresentation::getRenderViewCommand(MEDCALC::MEDPresentationViewMode viewMode) const
116 {
117   std::string cmd = std::string("pvs._DisableFirstRenderCameraReset();");
118   if (viewMode == MEDCALC::VIEW_MODE_OVERLAP) {
119     cmd += std::string("__view1 = pvs.GetActiveViewOrCreate('RenderView');");
120   } else if (viewMode == MEDCALC::VIEW_MODE_REPLACE) {
121     cmd += std::string("__view1 = pvs.GetActiveViewOrCreate('RenderView');");
122     cmd += std::string("pvs.active_objects.source and pvs.Hide(view=__view1);");
123     cmd += std::string("pvs.Render();");
124   } else if (viewMode == MEDCALC::VIEW_MODE_NEW_LAYOUT) {
125     cmd += std::string("__layout1 = pvs.servermanager.misc.ViewLayout(registrationGroup='layouts');");
126     cmd += std::string("__view1 = pvs.CreateView('RenderView');");
127   } else if (viewMode == MEDCALC::VIEW_MODE_SPLIT_VIEW) {
128     cmd += std::string("__view1 = pvs.CreateView('RenderView');");
129   }
130   return cmd;
131 }
132
133 std::string
134 MEDPresentation::getResetCameraCommand() const
135 {
136   return std::string("__view1.ResetCamera();");
137 }
138
139
140 std::string
141 MEDPresentation::getColorMapCommand(MEDCALC::MEDPresentationColorMap colorMap) const
142 {
143   switch (colorMap) {
144   case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "Blue to Red Rainbow";
145   case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "Cool to Warm";
146   }
147 }