Salome HOME
9beb6774bd6306e1ec1295eb2a562018584f4039
[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/DatasourceConstants
63 #define OBJECT_ID              "objectid"
64 #define OBJECT_IS_IN_WORKSPACE "isInWorkspace"
65
66 // Duplicate gui/XmedDataModel
67 static const int NB_TYPE_OF_FIELDS = 4;
68 static const char* mapTypeOfFieldLabel[NB_TYPE_OF_FIELDS] =
69   {"ON_CELLS", "ON_NODES", "ON_GAUSS_PT", "ON_GAUSS_NE" };
70
71 MED_ORB::status
72 MED::addDatasourceToStudy(SALOMEDS::Study_ptr study,
73                           const MEDCALC::DatasourceHandler& datasourceHandler)
74 {
75   // set exception handler to catch unexpected CORBA exceptions
76   Unexpect aCatch(SALOME_SalomeException);
77
78   // set result status to error initially
79   MED_ORB::status result = MED_ORB::OP_ERROR;
80
81   // check if reference to study is valid
82   if (!CORBA::is_nil(study)) {
83     // get full object path
84     std::string fullName = CORBA::string_dup(datasourceHandler.name);
85     // check if the object with the same name is already registered in the study
86     SALOMEDS::SObject_var sobj = study->FindObjectByPath(fullName.c_str());
87     if (CORBA::is_nil(sobj)) {
88       // object is not registered yet -> register
89       SALOMEDS::GenericAttribute_var anAttr;
90       SALOMEDS::AttributeParameter_var aParam;
91       SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
92       SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
93
94       // find MED component; create it if not found
95       SALOMEDS::SComponent_var father = study->FindComponent("MED");
96       if (CORBA::is_nil(father)) {
97         // create component
98         father = studyBuilder->NewComponent("MED");
99         // set name attribute
100         father->SetAttrString("AttributeName", "MEDCalc");
101         // set icon attribute
102         father->SetAttrString("AttributePixMap", "ICO_MED");
103         // register component in the study
104         studyBuilder->DefineComponentInstance(father, MED_Gen::_this());
105         // add component to the use case tree
106         // (to support tree representation customization and drag-n-drop)
107         useCaseBuilder->SetRootCurrent();
108         useCaseBuilder->Append(father); // component object is added as the top level item
109       }
110
111       // create new sub-object, as a child of the component object
112       SALOMEDS::SObject_var soDatasource = studyBuilder->NewObject(father);
113       soDatasource->SetAttrString("AttributeName", fullName.c_str());
114       soDatasource->SetAttrString("AttributePixMap", "ICO_DATASOURCE");
115       anAttr = studyBuilder->FindOrCreateAttribute(soDatasource, "AttributeParameter");
116       aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
117       aParam->SetInt(OBJECT_ID, datasourceHandler.id);
118       useCaseBuilder->AppendTo(soDatasource->GetFather(), soDatasource);
119
120       // We can add the meshes as children of the datasource
121       MEDCALC::MeshHandlerList* meshHandlerList =
122         MEDFactoryClient::getDataManager()->getMeshList(datasourceHandler.id);
123
124       for(CORBA::ULong iMesh=0; iMesh<meshHandlerList->length(); iMesh++) {
125         MEDCALC::MeshHandler meshHandler = (*meshHandlerList)[iMesh];
126         SALOMEDS::SObject_var soMesh = studyBuilder->NewObject(soDatasource);
127         soMesh->SetAttrString("AttributeName", meshHandler.name);
128         soMesh->SetAttrString("AttributePixMap", "ICO_DATASOURCE_MESH");
129         anAttr = studyBuilder->FindOrCreateAttribute(soMesh, "AttributeParameter");
130         aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
131         aParam->SetInt(OBJECT_ID, meshHandler.id);
132         anAttr = studyBuilder->FindOrCreateAttribute(soMesh, "AttributeParameter");
133         aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
134         aParam->SetBool(OBJECT_IS_IN_WORKSPACE, false);
135         useCaseBuilder->AppendTo(soMesh->GetFather(), soMesh);
136
137         // We add the field timeseries defined on this mesh, as children of the mesh SObject
138         MEDCALC::FieldseriesHandlerList * fieldseriesHandlerList =
139           MEDFactoryClient::getDataManager()->getFieldseriesListOnMesh(meshHandler.id);
140
141         for(CORBA::ULong iFieldseries=0; iFieldseries<fieldseriesHandlerList->length(); iFieldseries++) {
142           MEDCALC::FieldseriesHandler fieldseriesHandler = (*fieldseriesHandlerList)[iFieldseries];
143           SALOMEDS::SObject_var soFieldseries = studyBuilder->NewObject(soMesh);
144           _fieldSeriesEntries[fieldseriesHandler.id] = soFieldseries->GetID();
145
146           std::string label(fieldseriesHandler.name);
147           label +=" ("+std::string(mapTypeOfFieldLabel[fieldseriesHandler.type])+")";
148           soFieldseries->SetAttrString("AttributeName", label.c_str());
149           soFieldseries->SetAttrString("AttributePixMap", "ICO_DATASOURCE_FIELD");
150           anAttr = studyBuilder->FindOrCreateAttribute(soFieldseries, "AttributeParameter");
151           aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
152           aParam->SetInt(OBJECT_ID, fieldseriesHandler.id);
153           anAttr = studyBuilder->FindOrCreateAttribute(soFieldseries, "AttributeParameter");
154           aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
155           aParam->SetBool(OBJECT_IS_IN_WORKSPACE, false);
156
157           useCaseBuilder->AppendTo(soFieldseries->GetFather(), soFieldseries);
158           soFieldseries->UnRegister();
159         }
160         soMesh->UnRegister();
161       }
162
163       // cleanup
164       father->UnRegister();
165       soDatasource->UnRegister();
166     }
167   }
168
169   result = MED_ORB::OP_OK;
170   return result;
171 }
172
173 MED_ORB::status
174 MED::registerPresentation(SALOMEDS::Study_ptr study,
175                           CORBA::Long fieldId,
176                           const char* name,
177                           const char* label)
178 {
179   // set exception handler to catch unexpected CORBA exceptions
180   Unexpect aCatch(SALOME_SalomeException);
181
182   // set result status to error initially
183   MED_ORB::status result = MED_ORB::OP_ERROR;
184
185   if (_fieldSeriesEntries.find(fieldId) == _fieldSeriesEntries.end()) {
186     std::cerr << "Field not found\n";
187     return MED_ORB::OP_ERROR ;
188   }
189   std::string entry = _fieldSeriesEntries[fieldId];
190   SALOMEDS::SObject_var sobject = study->FindObjectID(entry.c_str());
191   SALOMEDS::SObject_ptr soFieldseries = sobject._retn();
192
193   if (soFieldseries->IsNull()) {
194     std::cerr << "Entry not found\n";
195     return  MED_ORB::OP_ERROR;
196   }
197
198   SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
199   SALOMEDS::SObject_var soPresentation = studyBuilder->NewObject(soFieldseries);
200
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   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.Length();
238   unsigned char* aBuffer = new unsigned char[aLen+1];
239   strcpy((char*)aBuffer, aScript.ToCString());
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 }