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/XmedDataModel
63 static const int NB_TYPE_OF_FIELDS = 4;
64 static const char* mapTypeOfFieldLabel[NB_TYPE_OF_FIELDS] =
65 {"ON_CELLS", "ON_NODES", "ON_GAUSS_PT", "ON_GAUSS_NE" };
68 MED::addDatasourceToStudy(SALOMEDS::Study_ptr study,
69 const MEDCALC::DatasourceHandler& datasourceHandler)
71 // set exception handler to catch unexpected CORBA exceptions
72 Unexpect aCatch(SALOME_SalomeException);
74 // set result status to error initially
75 MED_ORB::status result = MED_ORB::OP_ERROR;
77 // check if reference to study is valid
78 if (!CORBA::is_nil(study)) {
79 // get full object path
80 std::string fullName = CORBA::string_dup(datasourceHandler.name);
81 // check if the object with the same name is already registered in the study
82 SALOMEDS::SObject_var sobj = study->FindObjectByPath(fullName.c_str());
83 if (CORBA::is_nil(sobj)) {
84 // object is not registered yet -> register
85 SALOMEDS::GenericAttribute_var anAttr;
86 SALOMEDS::AttributeParameter_var aParam;
87 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
88 SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();
90 // find MED component; create it if not found
91 SALOMEDS::SComponent_var father = study->FindComponent("MED");
92 if (CORBA::is_nil(father)) {
94 father = studyBuilder->NewComponent("MED");
96 father->SetAttrString("AttributeName", "MEDCalc");
98 father->SetAttrString("AttributePixMap", "ICO_MED");
99 // register component in the study
100 studyBuilder->DefineComponentInstance(father, MED_Gen::_this());
101 // add component to the use case tree
102 // (to support tree representation customization and drag-n-drop)
103 useCaseBuilder->SetRootCurrent();
104 useCaseBuilder->Append(father); // component object is added as the top level item
107 // create new sub-object, as a child of the component object
108 SALOMEDS::SObject_var soDatasource = studyBuilder->NewObject(father);
109 soDatasource->SetAttrString("AttributeName", fullName.c_str());
110 soDatasource->SetAttrString("AttributePixMap", "ICO_DATASOURCE");
111 anAttr = studyBuilder->FindOrCreateAttribute(soDatasource, "AttributeParameter");
112 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
113 aParam->SetInt("objectid", datasourceHandler.id);
114 useCaseBuilder->AppendTo(soDatasource->GetFather(), soDatasource);
116 // We can add the meshes as children of the datasource
117 MEDCALC::MeshHandlerList* meshHandlerList =
118 MEDFactoryClient::getDataManager()->getMeshList(datasourceHandler.id);
120 for(CORBA::ULong iMesh=0; iMesh<meshHandlerList->length(); iMesh++) {
121 MEDCALC::MeshHandler meshHandler = (*meshHandlerList)[iMesh];
122 SALOMEDS::SObject_var soMesh = studyBuilder->NewObject(soDatasource);
123 soMesh->SetAttrString("AttributeName", meshHandler.name);
124 soMesh->SetAttrString("AttributePixMap", "ICO_DATASOURCE_MESH");
125 anAttr = studyBuilder->FindOrCreateAttribute(soMesh, "AttributeParameter");
126 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
127 aParam->SetInt("objectid", meshHandler.id);
128 anAttr = studyBuilder->FindOrCreateAttribute(soMesh, "AttributeParameter");
129 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
130 aParam->SetBool("isInWorkspace", false);
131 useCaseBuilder->AppendTo(soMesh->GetFather(), soMesh);
133 // We add the field timeseries defined on this mesh, as children of the mesh SObject
134 MEDCALC::FieldseriesHandlerList * fieldseriesHandlerList =
135 MEDFactoryClient::getDataManager()->getFieldseriesListOnMesh(meshHandler.id);
137 for(CORBA::ULong iFieldseries=0; iFieldseries<fieldseriesHandlerList->length(); iFieldseries++) {
138 MEDCALC::FieldseriesHandler fieldseriesHandler = (*fieldseriesHandlerList)[iFieldseries];
139 SALOMEDS::SObject_var soFieldseries = studyBuilder->NewObject(soMesh);
140 _fieldSeriesEntries[fieldseriesHandler.id] = soFieldseries->GetID();
142 std::string label(fieldseriesHandler.name);
143 label +=" ("+std::string(mapTypeOfFieldLabel[fieldseriesHandler.type])+")";
144 soFieldseries->SetAttrString("AttributeName", label.c_str());
145 soFieldseries->SetAttrString("AttributePixMap", "ICO_DATASOURCE_FIELD");
146 anAttr = studyBuilder->FindOrCreateAttribute(soFieldseries, "AttributeParameter");
147 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
148 aParam->SetInt("objectid", fieldseriesHandler.id);
149 anAttr = studyBuilder->FindOrCreateAttribute(soFieldseries, "AttributeParameter");
150 aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
151 aParam->SetBool("isInWorkspace", false);
153 useCaseBuilder->AppendTo(soFieldseries->GetFather(), soFieldseries);
154 soFieldseries->UnRegister();
156 soMesh->UnRegister();
160 father->UnRegister();
161 soDatasource->UnRegister();
165 result = MED_ORB::OP_OK;
170 MED::registerPresentation(SALOMEDS::Study_ptr study,
175 // set exception handler to catch unexpected CORBA exceptions
176 Unexpect aCatch(SALOME_SalomeException);
178 // set result status to error initially
179 MED_ORB::status result = MED_ORB::OP_ERROR;
181 if (_fieldSeriesEntries.find(fieldId) == _fieldSeriesEntries.end()) {
182 std::cerr << "Field not found\n";
183 return MED_ORB::OP_ERROR ;
185 std::string entry = _fieldSeriesEntries[fieldId];
186 //SALOMEDS::SObject_ptr soFieldseries = _studyEditor->findObject(entry.c_str());
187 SALOMEDS::SObject_var sobject = study->FindObjectID(entry.c_str());
188 SALOMEDS::SObject_ptr soFieldseries = sobject._retn();
190 if (soFieldseries->IsNull()) {
191 std::cerr << "Entry not found\n";
192 return MED_ORB::OP_ERROR;
195 //SALOMEDS::SObject_var soPresentation = _studyEditor->newObject(soFieldseries);
196 SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();
197 SALOMEDS::SObject_var soPresentation = studyBuilder->NewObject(soFieldseries);
199 //_studyEditor->setName(soPresentation, tr(name.c_str()).toStdString().c_str());
200 //_studyEditor->setIcon(soPresentation, tr("ICO_MED_PRESENTATION").toStdString().c_str());
201 soPresentation->SetAttrString("AttributeName", name);
202 soPresentation->SetAttrString("AttributePixMap", label);
205 result = MED_ORB::OP_OK;
210 MED::DumpPython(CORBA::Object_ptr theStudy,
211 CORBA::Boolean isPublished,
212 CORBA::Boolean isMultiFile,
213 CORBA::Boolean& isValidScript)
215 std::cout << "In MED::DumpPython\n";
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);
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);
229 TCollection_AsciiString aScript;
231 aScript += "dumping MED";
233 int aLen = aScript.Length();
234 unsigned char* aBuffer = new unsigned char[aLen+1];
235 strcpy((char*)aBuffer, aScript.ToCString());
237 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
238 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
240 isValidScript = true;
241 return aStreamFile._retn();
247 \brief Exportable factory function: create an instance of the MED component engine
248 \param orb reference to the ORB
249 \param poa reference to the POA
250 \param contId CORBA object ID, pointing to the owner SALOME container
251 \param instanceName SALOME component instance name
252 \param interfaceName SALOME component interface name
253 \return CORBA object identifier of the registered servant
255 //PortableServer::ObjectId* MEDEngine_factory( CORBA::ORB_ptr orb,
256 PortableServer::ObjectId* MEDEngine_factory(CORBA::ORB_ptr orb,
257 PortableServer::POA_ptr poa,
258 PortableServer::ObjectId* contId,
259 const char* instanceName,
260 const char* interfaceName)
262 MED* component = new MED(orb, poa, contId, instanceName, interfaceName);
263 return component->getId();