1 // Copyright (C) 2015 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
22 #include "MEDFactoryClient.hxx"
24 #include <SALOMEconfig.h>
25 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
27 #include <SALOMEDS_SObject.hxx>
29 #include <Utils_ExceptHandlers.hxx>
30 #include <TCollection_AsciiString.hxx>
37 Creates an instance of the MED component engine
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
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),
54 _id = _poa->activate_object(_thisObj); // register and activate this servant object
62 // Duplicate gui/DatasourceConstants
63 #define OBJECT_ID "objectid"
64 #define OBJECT_IS_IN_WORKSPACE "isInWorkspace"
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" };
72 MED::addDatasourceToStudy(SALOMEDS::Study_ptr study,
73 const MEDCALC::DatasourceHandler& datasourceHandler)
75 // set exception handler to catch unexpected CORBA exceptions
76 Unexpect aCatch(SALOME_SalomeException);
78 // set result status to error initially
79 MED_ORB::status result = MED_ORB::OP_ERROR;
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();
94 // find MED component; create it if not found
95 SALOMEDS::SComponent_var father = study->FindComponent("MED");
96 if (CORBA::is_nil(father)) {
98 father = studyBuilder->NewComponent("MED");
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
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);
120 // We can add the meshes as children of the datasource
121 MEDCALC::MeshHandlerList* meshHandlerList =
122 MEDFactoryClient::getDataManager()->getMeshList(datasourceHandler.id);
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);
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);
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();
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);
157 useCaseBuilder->AppendTo(soFieldseries->GetFather(), soFieldseries);
158 soFieldseries->UnRegister();
160 soMesh->UnRegister();
164 father->UnRegister();
165 soDatasource->UnRegister();
169 result = MED_ORB::OP_OK;
174 MED::registerPresentation(SALOMEDS::Study_ptr study,
179 // set exception handler to catch unexpected CORBA exceptions
180 Unexpect aCatch(SALOME_SalomeException);
182 // set result status to error initially
183 MED_ORB::status result = MED_ORB::OP_ERROR;
185 if (_fieldSeriesEntries.find(fieldId) == _fieldSeriesEntries.end()) {
186 std::cerr << "Field not found\n";
187 return MED_ORB::OP_ERROR ;
189 std::string entry = _fieldSeriesEntries[fieldId];
190 SALOMEDS::SObject_var sobject = study->FindObjectID(entry.c_str());
191 SALOMEDS::SObject_ptr soFieldseries = sobject._retn();
193 if (soFieldseries->IsNull()) {
194 std::cerr << "Entry not found\n";
195 return MED_ORB::OP_ERROR;
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);
203 soPresentation->SetAttrString("AttributeName", name);
204 soPresentation->SetAttrString("AttributePixMap", label);
206 result = MED_ORB::OP_OK;
211 MED::DumpPython(CORBA::Object_ptr theStudy,
212 CORBA::Boolean isPublished,
213 CORBA::Boolean isMultiFile,
214 CORBA::Boolean& isValidScript)
216 std::cout << "In MED::DumpPython\n";
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);
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);
230 TCollection_AsciiString aScript;
232 MEDCALC::CommandsList* history = MEDFactoryClient::getCommandsHistoryManager()->getCommandsHistory();
233 for (CORBA::ULong i = 0; i < history->length(); ++i) {
234 aScript += (*history)[i];
238 int aLen = aScript.Length();
239 unsigned char* aBuffer = new unsigned char[aLen+1];
240 strcpy((char*)aBuffer, aScript.ToCString());
242 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
243 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
245 isValidScript = true;
246 return aStreamFile._retn();
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
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)
266 MED* component = new MED(orb, poa, contId, instanceName, interfaceName);
267 return component->getId();