]> SALOME platform Git repositories - modules/med.git/blob - src/MEDCalc/cmp/MED.cxx
Salome HOME
MERGE stage 1: keep doc/dev and src/MEDCalc/doc
[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::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
200   SALOMEDS::SObject_var soPresentation = studyBuilder->NewObject(soFieldseries);
201   useCaseBuilder->AppendTo(soPresentation->GetFather(), soPresentation);
202
203   soPresentation->SetAttrString("AttributeName", name);
204   soPresentation->SetAttrString("AttributePixMap", label);
205
206   result = MED_ORB::OP_OK;
207   return result;
208 }
209
210 Engines::TMPFile*
211 MED::DumpPython(CORBA::Object_ptr theStudy,
212                 CORBA::Boolean isPublished,
213                 CORBA::Boolean isMultiFile,
214                 CORBA::Boolean& isValidScript)
215 {
216   std::cout << "In MED::DumpPython\n";
217
218   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
219   if(CORBA::is_nil(aStudy)) {
220     std::cerr << "Error: Cannot find the study\n";
221     return new Engines::TMPFile(0);
222   }
223
224   SALOMEDS::SObject_var aSO = aStudy->FindComponent("MED");
225   if(CORBA::is_nil(aSO)) {
226     std::cerr << "Error: Cannot find component MED\n";
227     return new Engines::TMPFile(0);
228   }
229
230   TCollection_AsciiString aScript;
231
232   MEDCALC::CommandsList* history = MEDFactoryClient::getCommandsHistoryManager()->getCommandsHistory();
233   for (CORBA::ULong i = 0; i < history->length(); ++i) {
234     aScript += (*history)[i];
235     aScript += "\n";
236   }
237
238   int aLen = aScript.Length();
239   unsigned char* aBuffer = new unsigned char[aLen+1];
240   strcpy((char*)aBuffer, aScript.ToCString());
241
242   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
243   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
244
245   isValidScript = true;
246   return aStreamFile._retn();
247 }
248
249 extern "C"
250 {
251   /*!
252     \brief Exportable factory function: create an instance of the MED component engine
253     \param orb reference to the ORB
254     \param poa reference to the POA
255     \param contId CORBA object ID, pointing to the owner SALOME container
256     \param instanceName SALOME component instance name
257     \param interfaceName SALOME component interface name
258     \return CORBA object identifier of the registered servant
259   */
260   PortableServer::ObjectId* MEDEngine_factory(CORBA::ORB_ptr orb,
261                                               PortableServer::POA_ptr poa,
262                                               PortableServer::ObjectId* contId,
263                                               const char* instanceName,
264                                               const char* interfaceName)
265   {
266     MED* component = new MED(orb, poa, contId, instanceName, interfaceName);
267     return component->getId();
268   }
269 }