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>
38 Creates an instance of the MED component engine
40 \param orb reference to the ORB
41 \param poa reference to the POA
42 \param contId CORBA object ID, pointing to the owner SALOME container
43 \param instanceName SALOME component instance name
44 \param interfaceName SALOME component interface name
46 MED::MED(CORBA::ORB_ptr orb,
47 PortableServer::POA_ptr poa,
48 PortableServer::ObjectId* contId,
49 const char* instanceName,
50 const char* interfaceName)
51 : Engines_Component_i(orb, poa, contId, instanceName, interfaceName),
55 _id = _poa->activate_object(_thisObj); // register and activate this servant object
64 MED::addDatasourceToStudy(SALOMEDS::Study_ptr study,
65 const MEDCALC::DatasourceHandler& datasourceHandler)
67 // set exception handler to catch unexpected CORBA exceptions
68 Unexpect aCatch(SALOME_SalomeException);
70 // check if reference to study is valid
71 if (!CORBA::is_nil(study)) {
72 // get full object path
73 std::string fullName = CORBA::string_dup(datasourceHandler.name);
74 // check if the object with the same name is already registered in the study
75 SALOMEDS::SObject_var sobj = study->FindObjectByPath(fullName.c_str());
76 if (CORBA::is_nil(sobj)) {
77 // object is not registered yet -> register
78 SALOMEDS::GenericAttribute_var anAttr;
79 SALOMEDS::AttributeParameter_var aParam;
80 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
81 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
83 // find MED component; create it if not found
84 SALOMEDS::SComponent_var father = study->FindComponent("MED");
85 if (CORBA::is_nil(father)) {
87 father = studyBuilder->NewComponent("MED");
89 father->SetAttrString("AttributeName", "MEDCalc");
91 father->SetAttrString("AttributePixMap", "ICO_MED");
92 // register component in the study
93 studyBuilder->DefineComponentInstance(father, MED_Gen::_this());
94 // add component to the use case tree
95 // (to support tree representation customization and drag-n-drop)
96 useCaseBuilder->SetRootCurrent();
97 useCaseBuilder->Append(father); // component object is added as the top level item
100 // create new sub-object, as a child of the component object
101 SALOMEDS::SObject_var soDatasource = studyBuilder->NewObject(father);
102 soDatasource->SetAttrString("AttributeName", fullName.c_str());
103 soDatasource->SetAttrString("AttributePixMap", "ICO_DATASOURCE");
104 anAttr = studyBuilder->FindOrCreateAttribute(soDatasource, "AttributeParameter");
105 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
106 aParam->SetInt(SOURCE_ID, datasourceHandler.id);
107 useCaseBuilder->AppendTo(soDatasource->GetFather(), soDatasource);
109 // We can add the meshes as children of the datasource
110 MEDCALC::MeshHandlerList* meshHandlerList =
111 MEDFactoryClient::getDataManager()->getMeshList(datasourceHandler.id);
113 for(CORBA::ULong iMesh=0; iMesh<meshHandlerList->length(); iMesh++) {
114 MEDCALC::MeshHandler meshHandler = (*meshHandlerList)[iMesh];
115 SALOMEDS::SObject_var soMesh = studyBuilder->NewObject(soDatasource);
116 soMesh->SetAttrString("AttributeName", meshHandler.name);
117 soMesh->SetAttrString("AttributePixMap", "ICO_DATASOURCE_MESH");
118 anAttr = studyBuilder->FindOrCreateAttribute(soMesh, "AttributeParameter");
119 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
120 aParam->SetInt(MESH_ID, meshHandler.id);
121 aParam->SetBool(IS_IN_WORKSPACE, false);
122 useCaseBuilder->AppendTo(soMesh->GetFather(), soMesh);
124 // We add the field timeseries defined on this mesh, as children of the mesh SObject
125 MEDCALC::FieldseriesHandlerList * fieldseriesHandlerList =
126 MEDFactoryClient::getDataManager()->getFieldseriesListOnMesh(meshHandler.id);
128 for(CORBA::ULong iFieldseries=0; iFieldseries<fieldseriesHandlerList->length(); iFieldseries++) {
129 MEDCALC::FieldseriesHandler fieldseriesHandler = (*fieldseriesHandlerList)[iFieldseries];
130 SALOMEDS::SObject_var soFieldseries = studyBuilder->NewObject(soMesh);
131 _fieldSeriesEntries[fieldseriesHandler.id] = soFieldseries->GetID();
133 std::string label(fieldseriesHandler.name);
134 label +=" ("+std::string(mapTypeOfFieldLabel[fieldseriesHandler.type])+")";
135 soFieldseries->SetAttrString("AttributeName", label.c_str());
136 soFieldseries->SetAttrString("AttributePixMap", "ICO_DATASOURCE_FIELD");
137 anAttr = studyBuilder->FindOrCreateAttribute(soFieldseries, "AttributeParameter");
138 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
139 aParam->SetInt(FIELD_SERIES_ID, fieldseriesHandler.id);
140 //aParam->SetInt(FIELD_ID, fieldseriesHandler.id);
141 aParam->SetBool(IS_IN_WORKSPACE, false);
143 useCaseBuilder->AppendTo(soFieldseries->GetFather(), soFieldseries);
144 soFieldseries->UnRegister();
146 soMesh->UnRegister();
150 father->UnRegister();
151 soDatasource->UnRegister();
155 return MED_ORB::OP_OK;
159 MED::registerPresentation(SALOMEDS::Study_ptr study,
163 CORBA::Long presentationId)
165 // set exception handler to catch unexpected CORBA exceptions
166 Unexpect aCatch(SALOME_SalomeException);
168 if (_fieldSeriesEntries.find(fieldId) == _fieldSeriesEntries.end()) {
169 std::cerr << "Field not found\n";
170 return MED_ORB::OP_ERROR ;
172 std::string entry = _fieldSeriesEntries[fieldId];
173 SALOMEDS::SObject_var sobject = study->FindObjectID(entry.c_str());
174 SALOMEDS::SObject_ptr soFieldseries = sobject._retn();
176 if (soFieldseries->IsNull()) {
177 std::cerr << "Entry not found\n";
178 return MED_ORB::OP_ERROR;
181 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
182 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
183 SALOMEDS::SObject_var soPresentation = studyBuilder->NewObject(soFieldseries);
184 useCaseBuilder->AppendTo(soPresentation->GetFather(), soPresentation);
186 soPresentation->SetAttrString("AttributeName", name);
187 soPresentation->SetAttrString("AttributePixMap", label);
189 SALOMEDS::GenericAttribute_var anAttr;
190 SALOMEDS::AttributeParameter_var aParam;
191 anAttr = studyBuilder->FindOrCreateAttribute(soPresentation, "AttributeParameter");
192 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
193 aParam->SetInt(FIELD_ID, fieldId);
194 aParam->SetBool(IS_PRESENTATION, true);
195 aParam->SetInt(PRESENTATION_ID, presentationId);
197 return MED_ORB::OP_OK;
201 MED::unregisterPresentation(SALOMEDS::Study_ptr study,
202 CORBA::Long presentationId)
204 // set exception handler to catch unexpected CORBA exceptions
205 Unexpect aCatch(SALOME_SalomeException);
207 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
208 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
210 SALOMEDS::GenericAttribute_var anAttribute;
211 SALOMEDS::SComponent_var father = study->FindComponent("MED");
212 SALOMEDS::ChildIterator_var it = study->NewChildIterator(father);
213 for (it->InitEx(true); it->More(); it->Next()) {
214 SALOMEDS::SObject_var child(it->Value());
216 if (child->FindAttribute(anAttribute, "AttributeParameter")) {
217 SALOMEDS::AttributeParameter_var attrParam = SALOMEDS::AttributeParameter::_narrow(anAttribute);
218 if (!attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN) || !attrParam->GetBool(IS_PRESENTATION) || !attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
221 if (presentationId == attrParam->GetInt(PRESENTATION_ID)) {
222 // remove object from study
223 studyBuilder->RemoveObjectWithChildren(child);
224 // remove object from use case tree
225 useCaseBuilder->Remove(child);
230 return MED_ORB::OP_OK;
233 MED_ORB::PresentationsList*
234 MED::getSiblingPresentations(SALOMEDS::Study_ptr study, CORBA::Long presentationId)
236 // set exception handler to catch unexpected CORBA exceptions
237 Unexpect aCatch(SALOME_SalomeException);
239 MED_ORB::PresentationsList* presList = new MED_ORB::PresentationsList;
241 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
242 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
244 SALOMEDS::GenericAttribute_var anAttribute;
245 SALOMEDS::SComponent_var father = study->FindComponent("MED");
246 SALOMEDS::ChildIterator_var it = study->NewChildIterator(father);
247 for (it->InitEx(true); it->More(); it->Next()) {
248 SALOMEDS::SObject_var child(it->Value());
250 if (child->FindAttribute(anAttribute, "AttributeParameter")) {
251 SALOMEDS::AttributeParameter_var attrParam = SALOMEDS::AttributeParameter::_narrow(anAttribute);
252 if (!attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN) || !attrParam->GetBool(IS_PRESENTATION) || !attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
255 if (presentationId == attrParam->GetInt(PRESENTATION_ID)) {
257 SALOMEDS::ChildIterator_var siblItr = study->NewChildIterator(child->GetFather());
258 for (siblItr->InitEx(true); siblItr->More(); siblItr->Next()) {
259 SALOMEDS::SObject_var sibl(siblItr->Value());
261 if (sibl->FindAttribute(anAttribute, "AttributeParameter")) {
262 SALOMEDS::AttributeParameter_var attrParam = SALOMEDS::AttributeParameter::_narrow(anAttribute);
263 if (!attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN) || !attrParam->GetBool(IS_PRESENTATION) || !attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
266 if (attrParam->GetInt(PRESENTATION_ID) != presentationId) {
267 CORBA::ULong size = presList->length();
268 presList->length(size+1);
269 (*presList)[size] = attrParam->GetInt(PRESENTATION_ID);
282 MED::DumpPython(CORBA::Object_ptr theStudy,
283 CORBA::Boolean isPublished,
284 CORBA::Boolean isMultiFile,
285 CORBA::Boolean& isValidScript)
287 std::cout << "In MED::DumpPython\n";
289 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
290 if(CORBA::is_nil(aStudy)) {
291 std::cerr << "Error: Cannot find the study\n";
292 return new Engines::TMPFile(0);
295 SALOMEDS::SObject_var aSO = aStudy->FindComponent("MED");
296 if(CORBA::is_nil(aSO)) {
297 std::cerr << "Error: Cannot find component MED\n";
298 return new Engines::TMPFile(0);
303 MEDCALC::CommandsList* history = MEDFactoryClient::getCommandsHistoryManager()->getCommandsHistory();
304 for (CORBA::ULong i = 0; i < history->length(); ++i) {
305 aScript += (*history)[i];
309 int aLen = aScript.size();
310 unsigned char* aBuffer = new unsigned char[aLen+1];
311 strcpy((char*)aBuffer, aScript.c_str());
313 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
314 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
316 isValidScript = true;
317 return aStreamFile._retn();
327 MED::getObjectInfo(CORBA::Long studyId, const char* entry)
329 SALOME_LifeCycleCORBA lcc;
330 CORBA::Object_var aSMObject = lcc.namingService()->Resolve( "/myStudyManager" );
331 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
332 SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
333 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( entry );
334 SALOMEDS::SObject_var aResultSObj;
335 if (aSObj->ReferencedObject(aResultSObj))
338 if (aSObj->_is_nil())
339 return CORBA::string_dup("unknown");
341 SALOMEDS::GenericAttribute_var anAttribute;
343 std::string name("unknown");
344 if (aSObj->FindAttribute(anAttribute, "AttributeName")) {
345 SALOMEDS::AttributeName_var attrName = SALOMEDS::AttributeName::_narrow(anAttribute);
346 name = std::string(attrName->Value());
349 bool isInWorkspace = false;
350 //bool isPresentation = false;
353 int fieldSeriesId = -1;
355 int presentationId = -1;
356 if (aSObj->FindAttribute(anAttribute, "AttributeParameter")) {
357 SALOMEDS::AttributeParameter_var attrParam = SALOMEDS::AttributeParameter::_narrow(anAttribute);
358 if (attrParam->IsSet(IS_IN_WORKSPACE, PT_BOOLEAN))
359 isInWorkspace = attrParam->GetBool(IS_IN_WORKSPACE);
360 //if (attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN))
361 // isPresentation = attrParam->GetBool(IS_PRESENTATION);
362 if (attrParam->IsSet(SOURCE_ID, PT_INTEGER))
363 sourceId = attrParam->GetInt(SOURCE_ID);
364 if (attrParam->IsSet(MESH_ID, PT_INTEGER))
365 meshId = attrParam->GetInt(MESH_ID);
366 if (attrParam->IsSet(FIELD_SERIES_ID, PT_INTEGER))
367 fieldSeriesId = attrParam->GetInt(FIELD_SERIES_ID);
368 if (attrParam->IsSet(FIELD_ID, PT_INTEGER))
369 fieldId = attrParam->GetInt(FIELD_ID);
370 if (attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
371 presentationId = attrParam->GetInt(PRESENTATION_ID);
374 if (!aSObj->_is_nil() )
377 std::ostringstream oss;
379 oss << "Source id: " << sourceId << std::endl;
381 oss << "Mesh id: " << meshId << std::endl;
382 if (fieldSeriesId > -1)
383 oss << "Field series id: " << fieldSeriesId << std::endl;
385 oss << "Field id: " << fieldId << std::endl;
386 //oss << "Is presentation: " << isPresentation << std::endl;
387 if (presentationId > -1)
388 oss << "Presentation id: " << presentationId << std::endl;
389 oss << "Is in workspace: " << isInWorkspace << std::endl;
391 return CORBA::string_dup(oss.str().c_str());
397 \brief Exportable factory function: create an instance of the MED component engine
398 \param orb reference to the ORB
399 \param poa reference to the POA
400 \param contId CORBA object ID, pointing to the owner SALOME container
401 \param instanceName SALOME component instance name
402 \param interfaceName SALOME component interface name
403 \return CORBA object identifier of the registered servant
405 PortableServer::ObjectId* MEDEngine_factory(CORBA::ORB_ptr orb,
406 PortableServer::POA_ptr poa,
407 PortableServer::ObjectId* contId,
408 const char* instanceName,
409 const char* interfaceName)
411 MED* component = new MED(orb, poa, contId, instanceName, interfaceName);
412 return component->getId();