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