1 // Copyright (C) 2015-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "MEDFactoryClient.hxx"
22 #include <MEDCalcConstants.hxx>
24 #include <SALOMEconfig.h>
25 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
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>
39 Creates an instance of the MED component engine
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
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),
56 _id = _poa->activate_object(_thisObj); // register and activate this servant object
65 MED::addDatasourceToStudy(SALOMEDS::Study_ptr study,
66 const MEDCALC::DatasourceHandler& datasourceHandler)
68 // set exception handler to catch unexpected CORBA exceptions
69 Unexpect aCatch(SALOME_SalomeException);
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();
84 // find MED component; create it if not found
85 SALOMEDS::SComponent_var father = study->FindComponent("MED");
86 if (CORBA::is_nil(father)) {
88 father = studyBuilder->NewComponent("MED");
90 father->SetAttrString("AttributeName", "MEDCalc");
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
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);
110 // We can add the meshes as children of the datasource
111 MEDCALC::MeshHandlerList* meshHandlerList =
112 MEDFactoryClient::getDataManager()->getMeshList(datasourceHandler.id);
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);
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);
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();
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);
144 useCaseBuilder->AppendTo(soFieldseries->GetFather(), soFieldseries);
145 soFieldseries->UnRegister();
147 soMesh->UnRegister();
151 father->UnRegister();
152 soDatasource->UnRegister();
156 return MED_ORB::OP_OK;
160 MED::registerPresentation(SALOMEDS::Study_ptr study,
165 CORBA::Long presentationId)
167 // set exception handler to catch unexpected CORBA exceptions
168 Unexpect aCatch(SALOME_SalomeException);
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 ;
177 if (_fieldSeriesEntries.find(fieldSeriesId) == _fieldSeriesEntries.end()) {
178 std::cerr << "MED::registerPresentation(): Field series not found\n";
179 return MED_ORB::OP_ERROR ;
181 std::string entry = _fieldSeriesEntries[fieldSeriesId];
182 SALOMEDS::SObject_var sobject = study->FindObjectID(entry.c_str());
183 SALOMEDS::SObject_ptr soFieldseries = sobject._retn();
185 if (soFieldseries->IsNull()) {
186 std::cerr << "MED::registerPresentation(): Entry not found\n";
187 return MED_ORB::OP_ERROR;
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);
195 soPresentation->SetAttrString("AttributeName", name);
196 soPresentation->SetAttrString("AttributePixMap", ico);
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);
207 return MED_ORB::OP_OK;
211 MED::unregisterPresentation(SALOMEDS::Study_ptr study,
212 CORBA::Long presentationId)
214 // set exception handler to catch unexpected CORBA exceptions
215 Unexpect aCatch(SALOME_SalomeException);
217 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
218 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
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());
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))
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);
240 return MED_ORB::OP_OK;
243 MED_ORB::PresentationsList*
244 MED::getSiblingPresentations(SALOMEDS::Study_ptr study, CORBA::Long presentationId)
246 // set exception handler to catch unexpected CORBA exceptions
247 Unexpect aCatch(SALOME_SalomeException);
249 MED_ORB::PresentationsList* presList = new MED_ORB::PresentationsList;
251 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
252 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
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());
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))
265 if (presentationId == attrParam->GetInt(PRESENTATION_ID)) {
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());
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))
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);
292 MED::DumpPython(CORBA::Object_ptr theStudy,
293 CORBA::Boolean isPublished,
294 CORBA::Boolean isMultiFile,
295 CORBA::Boolean& isValidScript)
297 std::cout << "In MED::DumpPython\n";
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);
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);
313 MEDCALC::CommandsList* history = MEDFactoryClient::getCommandsHistoryManager()->getCommandsHistory();
314 for (CORBA::ULong i = 0; i < history->length(); ++i) {
315 aScript += (*history)[i];
319 int aLen = aScript.size();
320 unsigned char* aBuffer = new unsigned char[aLen+1];
321 strcpy((char*)aBuffer, aScript.c_str());
323 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
324 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
326 isValidScript = true;
327 return aStreamFile._retn();
337 MED::getObjectInfo(CORBA::Long studyId, const char* entry)
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))
348 if (aSObj->_is_nil())
349 return CORBA::string_dup("unknown");
351 SALOMEDS::GenericAttribute_var anAttribute;
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());
359 bool isInWorkspace = false;
360 //bool isPresentation = false;
363 int fieldSeriesId = -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);
384 if (!aSObj->_is_nil() )
387 std::ostringstream oss;
389 oss << "Source id: " << sourceId << std::endl;
391 oss << "Mesh id: " << meshId << std::endl;
392 if (fieldSeriesId > -1)
393 oss << "Field series id: " << fieldSeriesId << std::endl;
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;
401 return CORBA::string_dup(oss.str().c_str());
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
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)
421 MED* component = new MED(orb, poa, contId, instanceName, interfaceName);
422 return component->getId();