]> SALOME platform Git repositories - modules/med.git/blob - src/MEDCalc/cmp/MED.cxx
Salome HOME
fix medcoupling api changes
[modules/med.git] / src / MEDCalc / cmp / MED.cxx
1 // Copyright (C) 2015  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 "MED.hxx"
21
22 #include "MEDFactoryClient.hxx"
23
24 #include <SALOMEconfig.h>
25 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
26
27 #include <SALOMEDS_SObject.hxx>
28
29 #include <Utils_ExceptHandlers.hxx>
30
31 #include <string>
32
33 /*!
34   \brief Constructor
35
36   Creates an instance of the MED component engine
37
38   \param orb reference to the ORB
39   \param poa reference to the POA
40   \param contId CORBA object ID, pointing to the owner SALOME container
41   \param instanceName SALOME component instance name
42   \param interfaceName SALOME component interface name
43 */
44 MED::MED(CORBA::ORB_ptr orb,
45          PortableServer::POA_ptr poa,
46          PortableServer::ObjectId* contId,
47          const char* instanceName,
48          const char* interfaceName)
49   : Engines_Component_i(orb, poa, contId, instanceName, interfaceName),
50     _fieldSeriesEntries()
51 {
52   _thisObj = this;
53   _id = _poa->activate_object(_thisObj); // register and activate this servant object
54 }
55
56 MED::~MED()
57 {
58   // nothing to do
59 }
60
61 // Duplicate gui/DatasourceConstants
62 #define OBJECT_ID              "objectid"
63 #define OBJECT_IS_IN_WORKSPACE "isInWorkspace"
64
65 // Duplicate gui/XmedDataModel
66 static const int NB_TYPE_OF_FIELDS = 4;
67 static const char* mapTypeOfFieldLabel[NB_TYPE_OF_FIELDS] =
68   {"ON_CELLS", "ON_NODES", "ON_GAUSS_PT", "ON_GAUSS_NE" };
69
70 MED_ORB::status
71 MED::addDatasourceToStudy(SALOMEDS::Study_ptr study,
72                           const MEDCALC::DatasourceHandler& datasourceHandler)
73 {
74   // set exception handler to catch unexpected CORBA exceptions
75   Unexpect aCatch(SALOME_SalomeException);
76
77   // set result status to error initially
78   MED_ORB::status result = MED_ORB::OP_ERROR;
79
80   // check if reference to study is valid
81   if (!CORBA::is_nil(study)) {
82     // get full object path
83     std::string fullName = CORBA::string_dup(datasourceHandler.name);
84     // check if the object with the same name is already registered in the study
85     SALOMEDS::SObject_var sobj = study->FindObjectByPath(fullName.c_str());
86     if (CORBA::is_nil(sobj)) {
87       // object is not registered yet -> register
88       SALOMEDS::GenericAttribute_var anAttr;
89       SALOMEDS::AttributeParameter_var aParam;
90       SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
91       SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
92
93       // find MED component; create it if not found
94       SALOMEDS::SComponent_var father = study->FindComponent("MED");
95       if (CORBA::is_nil(father)) {
96         // create component
97         father = studyBuilder->NewComponent("MED");
98         // set name attribute
99         father->SetAttrString("AttributeName", "MEDCalc");
100         // set icon attribute
101         father->SetAttrString("AttributePixMap", "ICO_MED");
102         // register component in the study
103         studyBuilder->DefineComponentInstance(father, MED_Gen::_this());
104         // add component to the use case tree
105         // (to support tree representation customization and drag-n-drop)
106         useCaseBuilder->SetRootCurrent();
107         useCaseBuilder->Append(father); // component object is added as the top level item
108       }
109
110       // create new sub-object, as a child of the component object
111       SALOMEDS::SObject_var soDatasource = studyBuilder->NewObject(father);
112       soDatasource->SetAttrString("AttributeName", fullName.c_str());
113       soDatasource->SetAttrString("AttributePixMap", "ICO_DATASOURCE");
114       anAttr = studyBuilder->FindOrCreateAttribute(soDatasource, "AttributeParameter");
115       aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
116       aParam->SetInt(OBJECT_ID, datasourceHandler.id);
117       useCaseBuilder->AppendTo(soDatasource->GetFather(), soDatasource);
118
119       // We can add the meshes as children of the datasource
120       MEDCALC::MeshHandlerList* meshHandlerList =
121         MEDFactoryClient::getDataManager()->getMeshList(datasourceHandler.id);
122
123       for(CORBA::ULong iMesh=0; iMesh<meshHandlerList->length(); iMesh++) {
124         MEDCALC::MeshHandler meshHandler = (*meshHandlerList)[iMesh];
125         SALOMEDS::SObject_var soMesh = studyBuilder->NewObject(soDatasource);
126         soMesh->SetAttrString("AttributeName", meshHandler.name);
127         soMesh->SetAttrString("AttributePixMap", "ICO_DATASOURCE_MESH");
128         anAttr = studyBuilder->FindOrCreateAttribute(soMesh, "AttributeParameter");
129         aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
130         aParam->SetInt(OBJECT_ID, meshHandler.id);
131         anAttr = studyBuilder->FindOrCreateAttribute(soMesh, "AttributeParameter");
132         aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
133         aParam->SetBool(OBJECT_IS_IN_WORKSPACE, false);
134         useCaseBuilder->AppendTo(soMesh->GetFather(), soMesh);
135
136         // We add the field timeseries defined on this mesh, as children of the mesh SObject
137         MEDCALC::FieldseriesHandlerList * fieldseriesHandlerList =
138           MEDFactoryClient::getDataManager()->getFieldseriesListOnMesh(meshHandler.id);
139
140         for(CORBA::ULong iFieldseries=0; iFieldseries<fieldseriesHandlerList->length(); iFieldseries++) {
141           MEDCALC::FieldseriesHandler fieldseriesHandler = (*fieldseriesHandlerList)[iFieldseries];
142           SALOMEDS::SObject_var soFieldseries = studyBuilder->NewObject(soMesh);
143           _fieldSeriesEntries[fieldseriesHandler.id] = soFieldseries->GetID();
144
145           std::string label(fieldseriesHandler.name);
146           label +=" ("+std::string(mapTypeOfFieldLabel[fieldseriesHandler.type])+")";
147           soFieldseries->SetAttrString("AttributeName", label.c_str());
148           soFieldseries->SetAttrString("AttributePixMap", "ICO_DATASOURCE_FIELD");
149           anAttr = studyBuilder->FindOrCreateAttribute(soFieldseries, "AttributeParameter");
150           aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
151           aParam->SetInt(OBJECT_ID, fieldseriesHandler.id);
152           anAttr = studyBuilder->FindOrCreateAttribute(soFieldseries, "AttributeParameter");
153           aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
154           aParam->SetBool(OBJECT_IS_IN_WORKSPACE, false);
155
156           useCaseBuilder->AppendTo(soFieldseries->GetFather(), soFieldseries);
157           soFieldseries->UnRegister();
158         }
159         soMesh->UnRegister();
160       }
161
162       // cleanup
163       father->UnRegister();
164       soDatasource->UnRegister();
165     }
166   }
167
168   result = MED_ORB::OP_OK;
169   return result;
170 }
171
172 MED_ORB::status
173 MED::registerPresentation(SALOMEDS::Study_ptr study,
174                           CORBA::Long fieldId,
175                           const char* name,
176                           const char* label)
177 {
178   // set exception handler to catch unexpected CORBA exceptions
179   Unexpect aCatch(SALOME_SalomeException);
180
181   // set result status to error initially
182   MED_ORB::status result = MED_ORB::OP_ERROR;
183
184   if (_fieldSeriesEntries.find(fieldId) == _fieldSeriesEntries.end()) {
185     std::cerr << "Field not found\n";
186     return MED_ORB::OP_ERROR ;
187   }
188   std::string entry = _fieldSeriesEntries[fieldId];
189   SALOMEDS::SObject_var sobject = study->FindObjectID(entry.c_str());
190   SALOMEDS::SObject_ptr soFieldseries = sobject._retn();
191
192   if (soFieldseries->IsNull()) {
193     std::cerr << "Entry not found\n";
194     return  MED_ORB::OP_ERROR;
195   }
196
197   SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
198   SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
199   SALOMEDS::SObject_var soPresentation = studyBuilder->NewObject(soFieldseries);
200   useCaseBuilder->AppendTo(soPresentation->GetFather(), soPresentation);
201
202   soPresentation->SetAttrString("AttributeName", name);
203   soPresentation->SetAttrString("AttributePixMap", label);
204
205   result = MED_ORB::OP_OK;
206   return result;
207 }
208
209 Engines::TMPFile*
210 MED::DumpPython(CORBA::Object_ptr theStudy,
211                 CORBA::Boolean isPublished,
212                 CORBA::Boolean isMultiFile,
213                 CORBA::Boolean& isValidScript)
214 {
215   std::cout << "In MED::DumpPython\n";
216
217   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
218   if(CORBA::is_nil(aStudy)) {
219     std::cerr << "Error: Cannot find the study\n";
220     return new Engines::TMPFile(0);
221   }
222
223   SALOMEDS::SObject_var aSO = aStudy->FindComponent("MED");
224   if(CORBA::is_nil(aSO)) {
225     std::cerr << "Error: Cannot find component MED\n";
226     return new Engines::TMPFile(0);
227   }
228
229   std::string aScript;
230
231   MEDCALC::CommandsList* history = MEDFactoryClient::getCommandsHistoryManager()->getCommandsHistory();
232   for (CORBA::ULong i = 0; i < history->length(); ++i) {
233     aScript += (*history)[i];
234     aScript += "\n";
235   }
236
237   int aLen = aScript.size();
238   unsigned char* aBuffer = new unsigned char[aLen+1];
239   strcpy((char*)aBuffer, aScript.c_str());
240
241   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
242   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
243
244   isValidScript = true;
245   return aStreamFile._retn();
246 }
247
248 extern "C"
249 {
250   /*!
251     \brief Exportable factory function: create an instance of the MED component engine
252     \param orb reference to the ORB
253     \param poa reference to the POA
254     \param contId CORBA object ID, pointing to the owner SALOME container
255     \param instanceName SALOME component instance name
256     \param interfaceName SALOME component interface name
257     \return CORBA object identifier of the registered servant
258   */
259   PortableServer::ObjectId* MEDEngine_factory(CORBA::ORB_ptr orb,
260                                               PortableServer::POA_ptr poa,
261                                               PortableServer::ObjectId* contId,
262                                               const char* instanceName,
263                                               const char* interfaceName)
264   {
265     MED* component = new MED(orb, poa, contId, instanceName, interfaceName);
266     return component->getId();
267   }
268 }