Salome HOME
[MEDCalc] Fully functional scalar map (other pres deactivated)
[modules/med.git] / src / MEDCalc / cmp / MED.cxx
1 // Copyright (C) 2015-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 "MED.hxx"
21 #include "MEDFactoryClient.hxx"
22 #include <MEDCalcConstants.hxx>
23
24 #include <SALOMEconfig.h>
25 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
26
27 #include <SALOMEDS_SObject.hxx>
28 #include <Utils_ExceptHandlers.hxx>
29 #include <SALOME_LifeCycleCORBA.hxx>
30 #include <SALOME_NamingService.hxx>
31 #include <SALOME_KernelServices.hxx>
32
33 #include <string>
34 #include <sstream>
35
36 /*!
37   \brief Constructor
38
39   Creates an instance of the MED component engine
40
41   \param orb reference to the ORB
42   \param poa reference to the POA
43   \param contId CORBA object ID, pointing to the owner SALOME container
44   \param instanceName SALOME component instance name
45   \param interfaceName SALOME component interface name
46 */
47 MED::MED(CORBA::ORB_ptr orb,
48          PortableServer::POA_ptr poa,
49          PortableServer::ObjectId* contId,
50          const char* instanceName,
51          const char* interfaceName)
52   : Engines_Component_i(orb, poa, contId, instanceName, interfaceName),
53     _fieldSeriesEntries()
54 {
55   _thisObj = this;
56   _id = _poa->activate_object(_thisObj); // register and activate this servant object
57 }
58
59 MED::~MED()
60 {
61   // nothing to do
62 }
63
64 MED_ORB::status
65 MED::addDatasourceToStudy(SALOMEDS::Study_ptr study,
66                           const MEDCALC::DatasourceHandler& datasourceHandler)
67 {
68   // set exception handler to catch unexpected CORBA exceptions
69   Unexpect aCatch(SALOME_SalomeException);
70
71   // check if reference to study is valid
72   if (!CORBA::is_nil(study)) {
73     // get full object path
74     std::string fullName = CORBA::string_dup(datasourceHandler.name);
75     // check if the object with the same name is already registered in the study
76     SALOMEDS::SObject_var sobj = study->FindObjectByPath(fullName.c_str());
77     if (CORBA::is_nil(sobj)) {
78       // object is not registered yet -> register
79       SALOMEDS::GenericAttribute_var anAttr;
80       SALOMEDS::AttributeParameter_var aParam;
81       SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
82       SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
83
84       // find MED component; create it if not found
85       SALOMEDS::SComponent_var father = study->FindComponent("MED");
86       if (CORBA::is_nil(father)) {
87         // create component
88         father = studyBuilder->NewComponent("MED");
89         // set name attribute
90         father->SetAttrString("AttributeName", "MEDCalc");
91         // set icon attribute
92         father->SetAttrString("AttributePixMap", "ICO_MED");
93         // register component in the study
94         studyBuilder->DefineComponentInstance(father, MED_Gen::_this());
95         // add component to the use case tree
96         // (to support tree representation customization and drag-n-drop)
97         useCaseBuilder->SetRootCurrent();
98         useCaseBuilder->Append(father); // component object is added as the top level item
99       }
100
101       // create new sub-object, as a child of the component object
102       SALOMEDS::SObject_var soDatasource = studyBuilder->NewObject(father);
103       soDatasource->SetAttrString("AttributeName", fullName.c_str());
104       soDatasource->SetAttrString("AttributePixMap", "ICO_DATASOURCE");
105       anAttr = studyBuilder->FindOrCreateAttribute(soDatasource, "AttributeParameter");
106       aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
107       aParam->SetInt(SOURCE_ID, datasourceHandler.id);
108       useCaseBuilder->AppendTo(soDatasource->GetFather(), soDatasource);
109
110       // We can add the meshes as children of the datasource
111       MEDCALC::MeshHandlerList* meshHandlerList =
112         MEDFactoryClient::getDataManager()->getMeshList(datasourceHandler.id);
113
114       for(CORBA::ULong iMesh=0; iMesh<meshHandlerList->length(); iMesh++) {
115         MEDCALC::MeshHandler meshHandler = (*meshHandlerList)[iMesh];
116         SALOMEDS::SObject_var soMesh = studyBuilder->NewObject(soDatasource);
117         soMesh->SetAttrString("AttributeName", meshHandler.name);
118         soMesh->SetAttrString("AttributePixMap", "ICO_DATASOURCE_MESH");
119         anAttr = studyBuilder->FindOrCreateAttribute(soMesh, "AttributeParameter");
120         aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
121         aParam->SetInt(MESH_ID, meshHandler.id);
122         aParam->SetBool(IS_IN_WORKSPACE, false);
123         useCaseBuilder->AppendTo(soMesh->GetFather(), soMesh);
124
125         // We add the field timeseries defined on this mesh, as children of the mesh SObject
126         MEDCALC::FieldseriesHandlerList * fieldseriesHandlerList =
127           MEDFactoryClient::getDataManager()->getFieldseriesListOnMesh(meshHandler.id);
128
129         for(CORBA::ULong iFieldseries=0; iFieldseries<fieldseriesHandlerList->length(); iFieldseries++) {
130           MEDCALC::FieldseriesHandler fieldseriesHandler = (*fieldseriesHandlerList)[iFieldseries];
131           SALOMEDS::SObject_var soFieldseries = studyBuilder->NewObject(soMesh);
132           _fieldSeriesEntries[fieldseriesHandler.id] = soFieldseries->GetID();
133
134           std::string label(fieldseriesHandler.name);
135           label +=" ("+std::string(mapTypeOfFieldLabel[fieldseriesHandler.type])+")";
136           soFieldseries->SetAttrString("AttributeName", label.c_str());
137           soFieldseries->SetAttrString("AttributePixMap", "ICO_DATASOURCE_FIELD");
138           anAttr = studyBuilder->FindOrCreateAttribute(soFieldseries, "AttributeParameter");
139           aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
140           aParam->SetInt(FIELD_SERIES_ID, fieldseriesHandler.id);
141           //aParam->SetInt(FIELD_ID, fieldseriesHandler.id);
142           aParam->SetBool(IS_IN_WORKSPACE, false);
143
144           useCaseBuilder->AppendTo(soFieldseries->GetFather(), soFieldseries);
145           soFieldseries->UnRegister();
146         }
147         soMesh->UnRegister();
148       }
149
150       // cleanup
151       father->UnRegister();
152       soDatasource->UnRegister();
153     }
154   }
155
156   return MED_ORB::OP_OK;
157 }
158
159 MED_ORB::status
160 MED::registerPresentation(SALOMEDS::Study_ptr study,
161                           CORBA::Long fieldId,
162                           const char* name,
163                           const char* type,
164                           const char* ico,
165                           CORBA::Long presentationId)
166 {
167   // set exception handler to catch unexpected CORBA exceptions
168   Unexpect aCatch(SALOME_SalomeException);
169
170   MEDCALC::FieldHandler_var fldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
171   int fieldSeriesId = fldHandler->fieldseriesId;
172   if (fieldSeriesId < 0){
173       std::cerr << "MED::registerPresentation(): Error getting field handler\n";
174       return MED_ORB::OP_ERROR ;
175     }
176
177   if (_fieldSeriesEntries.find(fieldSeriesId) == _fieldSeriesEntries.end()) {
178     std::cerr << "MED::registerPresentation(): Field series not found\n";
179     return MED_ORB::OP_ERROR ;
180   }
181   std::string entry = _fieldSeriesEntries[fieldSeriesId];
182   SALOMEDS::SObject_var sobject = study->FindObjectID(entry.c_str());
183   SALOMEDS::SObject_ptr soFieldseries = sobject._retn();
184
185   if (soFieldseries->IsNull()) {
186     std::cerr << "MED::registerPresentation(): Entry not found\n";
187     return MED_ORB::OP_ERROR;
188   }
189
190   SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
191   SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
192   SALOMEDS::SObject_var soPresentation = studyBuilder->NewObject(soFieldseries);
193   useCaseBuilder->AppendTo(soPresentation->GetFather(), soPresentation);
194
195   soPresentation->SetAttrString("AttributeName", name);
196   soPresentation->SetAttrString("AttributePixMap", ico);
197
198   SALOMEDS::GenericAttribute_var anAttr;
199   SALOMEDS::AttributeParameter_var aParam;
200   anAttr = studyBuilder->FindOrCreateAttribute(soPresentation, "AttributeParameter");
201   aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
202   aParam->SetInt(FIELD_ID, fieldId);
203   aParam->SetBool(IS_PRESENTATION, true);
204   aParam->SetInt(PRESENTATION_ID, presentationId);
205   aParam->SetString(PRESENTATION_TYPE, type);
206
207   return MED_ORB::OP_OK;
208 }
209
210 MED_ORB::status
211 MED::unregisterPresentation(SALOMEDS::Study_ptr study,
212                             CORBA::Long presentationId)
213 {
214   // set exception handler to catch unexpected CORBA exceptions
215   Unexpect aCatch(SALOME_SalomeException);
216
217   SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
218   SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
219
220   SALOMEDS::GenericAttribute_var anAttribute;
221   SALOMEDS::SComponent_var father = study->FindComponent("MED");
222   SALOMEDS::ChildIterator_var it = study->NewChildIterator(father);
223   for (it->InitEx(true); it->More(); it->Next()) {
224     SALOMEDS::SObject_var child(it->Value());
225
226     if (child->FindAttribute(anAttribute, "AttributeParameter")) {
227       SALOMEDS::AttributeParameter_var attrParam = SALOMEDS::AttributeParameter::_narrow(anAttribute);
228       if (!attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN) || !attrParam->GetBool(IS_PRESENTATION) || !attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
229         continue;
230
231       if (presentationId == attrParam->GetInt(PRESENTATION_ID)) {
232         // remove object from study
233         studyBuilder->RemoveObjectWithChildren(child);
234         // remove object from use case tree
235         useCaseBuilder->Remove(child);
236       }
237     }
238   }
239
240   return MED_ORB::OP_OK;
241 }
242
243 MED_ORB::PresentationsList*
244 MED::getSiblingPresentations(SALOMEDS::Study_ptr study, CORBA::Long presentationId)
245 {
246   // set exception handler to catch unexpected CORBA exceptions
247   Unexpect aCatch(SALOME_SalomeException);
248
249   MED_ORB::PresentationsList* presList = new MED_ORB::PresentationsList;
250
251   SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
252   SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
253
254   SALOMEDS::GenericAttribute_var anAttribute;
255   SALOMEDS::SComponent_var father = study->FindComponent("MED");
256   SALOMEDS::ChildIterator_var it = study->NewChildIterator(father);
257   for (it->InitEx(true); it->More(); it->Next()) {
258     SALOMEDS::SObject_var child(it->Value());
259
260     if (child->FindAttribute(anAttribute, "AttributeParameter")) {
261       SALOMEDS::AttributeParameter_var attrParam = SALOMEDS::AttributeParameter::_narrow(anAttribute);
262       if (!attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN) || !attrParam->GetBool(IS_PRESENTATION) || !attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
263         continue;
264
265       if (presentationId == attrParam->GetInt(PRESENTATION_ID)) {
266         // get siblings
267         SALOMEDS::ChildIterator_var siblItr = study->NewChildIterator(child->GetFather());
268         for (siblItr->InitEx(true); siblItr->More(); siblItr->Next()) {
269           SALOMEDS::SObject_var sibl(siblItr->Value());
270
271           if (sibl->FindAttribute(anAttribute, "AttributeParameter")) {
272             SALOMEDS::AttributeParameter_var attrParam = SALOMEDS::AttributeParameter::_narrow(anAttribute);
273             if (!attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN) || !attrParam->GetBool(IS_PRESENTATION) || !attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
274               continue;
275
276             if (attrParam->GetInt(PRESENTATION_ID) != presentationId) {
277               CORBA::ULong size = presList->length();
278               presList->length(size+1);
279               (*presList)[size] = attrParam->GetInt(PRESENTATION_ID);
280             }
281           }
282         }
283         return presList;
284       }
285     }
286   }
287
288   return presList;
289 }
290
291 Engines::TMPFile*
292 MED::DumpPython(CORBA::Object_ptr theStudy,
293                 CORBA::Boolean isPublished,
294                 CORBA::Boolean isMultiFile,
295                 CORBA::Boolean& isValidScript)
296 {
297   std::cout << "In MED::DumpPython\n";
298
299   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
300   if(CORBA::is_nil(aStudy)) {
301     std::cerr << "Error: Cannot find the study\n";
302     return new Engines::TMPFile(0);
303   }
304
305   SALOMEDS::SObject_var aSO = aStudy->FindComponent("MED");
306   if(CORBA::is_nil(aSO)) {
307     std::cerr << "Error: Cannot find component MED\n";
308     return new Engines::TMPFile(0);
309   }
310
311   std::string aScript;
312
313   MEDCALC::CommandsList* history = MEDFactoryClient::getCommandsHistoryManager()->getCommandsHistory();
314   for (CORBA::ULong i = 0; i < history->length(); ++i) {
315     aScript += (*history)[i];
316     aScript += "\n";
317   }
318
319   int aLen = aScript.size();
320   unsigned char* aBuffer = new unsigned char[aLen+1];
321   strcpy((char*)aBuffer, aScript.c_str());
322
323   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
324   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
325
326   isValidScript = true;
327   return aStreamFile._retn();
328 }
329
330 CORBA::Boolean
331 MED::hasObjectInfo()
332 {
333   return true;
334 }
335
336 char*
337 MED::getObjectInfo(CORBA::Long studyId, const char* entry)
338 {
339   SALOME_LifeCycleCORBA lcc;
340   CORBA::Object_var aSMObject = lcc.namingService()->Resolve( "/myStudyManager" );
341   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
342   SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
343   SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( entry );
344   SALOMEDS::SObject_var aResultSObj;
345   if (aSObj->ReferencedObject(aResultSObj))
346     aSObj = aResultSObj;
347
348   if (aSObj->_is_nil())
349     return CORBA::string_dup("unknown");
350
351   SALOMEDS::GenericAttribute_var anAttribute;
352
353   std::string name("unknown");
354   if (aSObj->FindAttribute(anAttribute, "AttributeName")) {
355     SALOMEDS::AttributeName_var attrName = SALOMEDS::AttributeName::_narrow(anAttribute);
356     name = std::string(attrName->Value());
357   }
358
359   bool isInWorkspace = false;
360   //bool isPresentation = false;
361   int sourceId = -1;
362   int meshId = -1;
363   int fieldSeriesId = -1;
364   int fieldId = -1;
365   int presentationId = -1;
366   if (aSObj->FindAttribute(anAttribute, "AttributeParameter")) {
367     SALOMEDS::AttributeParameter_var attrParam = SALOMEDS::AttributeParameter::_narrow(anAttribute);
368     if (attrParam->IsSet(IS_IN_WORKSPACE, PT_BOOLEAN))
369       isInWorkspace = attrParam->GetBool(IS_IN_WORKSPACE);
370     //if (attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN))
371     //  isPresentation = attrParam->GetBool(IS_PRESENTATION);
372     if (attrParam->IsSet(SOURCE_ID, PT_INTEGER))
373       sourceId = attrParam->GetInt(SOURCE_ID);
374     if (attrParam->IsSet(MESH_ID, PT_INTEGER))
375       meshId = attrParam->GetInt(MESH_ID);
376     if (attrParam->IsSet(FIELD_SERIES_ID, PT_INTEGER))
377       fieldSeriesId = attrParam->GetInt(FIELD_SERIES_ID);
378     if (attrParam->IsSet(FIELD_ID, PT_INTEGER))
379       fieldId = attrParam->GetInt(FIELD_ID);
380     if (attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
381       presentationId = attrParam->GetInt(PRESENTATION_ID);
382   }
383
384   if (!aSObj->_is_nil() )
385     aSObj->UnRegister();
386
387   std::ostringstream oss;
388   if (sourceId > -1)
389     oss << "Source id: " << sourceId << std::endl;
390   if (meshId > -1)
391     oss << "Mesh id: " << meshId << std::endl;
392   if (fieldSeriesId > -1)
393     oss << "Field series id: " << fieldSeriesId << std::endl;
394   if (fieldId > -1)
395     oss << "Field id: " << fieldId << std::endl;
396   //oss << "Is presentation: " << isPresentation << std::endl;
397   if (presentationId > -1)
398     oss << "Presentation id: " << presentationId << std::endl;
399   oss << "Is in workspace: " << isInWorkspace << std::endl;
400
401   return CORBA::string_dup(oss.str().c_str());
402 }
403
404 extern "C"
405 {
406   /*!
407     \brief Exportable factory function: create an instance of the MED component engine
408     \param orb reference to the ORB
409     \param poa reference to the POA
410     \param contId CORBA object ID, pointing to the owner SALOME container
411     \param instanceName SALOME component instance name
412     \param interfaceName SALOME component interface name
413     \return CORBA object identifier of the registered servant
414   */
415   PortableServer::ObjectId* MEDEngine_factory(CORBA::ORB_ptr orb,
416                                               PortableServer::POA_ptr poa,
417                                               PortableServer::ObjectId* contId,
418                                               const char* instanceName,
419                                               const char* interfaceName)
420   {
421     MED* component = new MED(orb, poa, contId, instanceName, interfaceName);
422     return component->getId();
423   }
424 }