1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SALOMEDS_Driver_i.hxx"
24 #include "SALOMEDS_TMPFile_i.hxx"
25 #include "utilities.h"
26 #include "SALOMEDS_SObject_i.hxx"
27 #include "SALOMEDS_SComponent_i.hxx"
28 #include "SALOMEDS_Study_i.hxx"
29 #include "SALOMEDS.hxx"
30 #include <SALOMEDSImpl_IParameters.hxx>
33 #include CORBA_CLIENT_HEADER(SALOME_Session)
35 SALOMEDS_Driver_i::SALOMEDS_Driver_i(Engines::EngineComponent_ptr theEngine, CORBA::ORB_ptr theORB)
37 // engine should not be null - component is supposed to be inherited from Engines::EngineComponent
38 _engine = Engines::EngineComponent::_duplicate(theEngine);
39 // driver can be null - if component interface does not inherit SALOMEDS::Driver
40 _driver = SALOMEDS::Driver::_narrow(theEngine);
41 _orb = CORBA::ORB::_duplicate(theORB);
44 SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB)
46 // driver can be null - if component interface does not inherit SALOMEDS::Driver
47 _driver = SALOMEDS::Driver::_duplicate(theDriver);
48 // engine can be null - since it is narrowed from SALOMEDS::Driver ptr which can be null
49 _engine = Engines::EngineComponent::_narrow(theDriver);
50 _orb = CORBA::ORB::_duplicate(theORB);
53 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
57 std::string SALOMEDS_Driver_i::GetIOR()
60 if ( !CORBA::is_nil(_engine) ) {
61 CORBA::String_var cior = _orb->object_to_string(_engine);
67 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
68 const std::string& theURL,
69 long& theStreamLength,
72 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
73 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
76 SALOMEDS::TMPFile_var aStream;
78 if ( !CORBA::is_nil(_driver) )
79 aStream = _driver->Save(sco.in(), url, isMultiFile);
81 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
82 theStreamLength = aTMPFile->Size();
90 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
91 const std::string& theURL,
92 long& theStreamLength,
95 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
96 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
99 SALOMEDS::TMPFile_var aStream;
101 if ( !CORBA::is_nil(_driver) )
102 aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
104 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
105 theStreamLength = aTMPFile->Size();
113 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
114 const unsigned char* theStream,
115 const long theStreamLength,
116 const std::string& theURL,
119 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
120 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
121 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
123 SALOMEDS::TMPFile_var aStream;
124 if (theStreamLength > 0)
125 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
127 aStream = new SALOMEDS::TMPFile(0);
132 if ( !CORBA::is_nil(_driver) )
133 isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
140 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
141 const unsigned char* theStream,
142 const long theStreamLength,
143 const std::string& theURL,
146 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
147 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
148 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
150 SALOMEDS::TMPFile_var aStream;
151 if(theStreamLength > 0)
152 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
154 aStream = new SALOMEDS::TMPFile(0);
159 if ( !CORBA::is_nil(_driver) )
160 isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
167 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
169 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
172 if ( !CORBA::is_nil(_driver) )
173 _driver->Close(sco.in());
178 std::string SALOMEDS_Driver_i::ComponentDataType()
180 std::string dtype = "";
181 if ( !CORBA::is_nil(_driver) ) {
182 CORBA::String_var type = _driver->ComponentDataType();
188 std::string SALOMEDS_Driver_i::Version()
190 return !CORBA::is_nil( _engine ) ? _engine->getVersion() : std::string("");
193 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
194 const std::string& IORString,
198 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
201 std::string pstr = "";
203 if ( !CORBA::is_nil(_driver) ) {
204 CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), IORString.c_str(),
205 isMultiFile, isASCII);
206 if ( pers_string.in() )
217 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
218 const std::string& aLocalPersistentID,
222 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
223 CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
226 std::string ior = "";
228 if ( !CORBA::is_nil(_driver) ) {
229 CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
237 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
239 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
244 if ( !CORBA::is_nil(_driver) )
245 isOk = _driver->CanCopy(so.in());
254 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
256 long& theStreamLength)
258 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
261 CORBA::Long anObjectID;
262 SALOMEDS::TMPFile_var aStream;
264 if ( !CORBA::is_nil(_driver) )
265 aStream = _driver->CopyFrom(so.in(), anObjectID);
267 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
268 theStreamLength = aTMPFile->Size();
269 theObjectID = anObjectID;
277 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
280 bool canPaste = false;
282 if ( !CORBA::is_nil(_driver) )
283 canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
289 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
290 const long theStreamLength,
292 const SALOMEDSImpl_SObject& theObject)
294 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
295 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
297 SALOMEDS::TMPFile_var aStream;
298 if(theStreamLength > 0)
299 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
301 aStream = new SALOMEDS::TMPFile(0);
305 std::string entry = "";
307 if ( !CORBA::is_nil(_driver) ) {
308 SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
309 entry = ret_so->GetID();
318 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy,
322 long& theStreamLength)
324 SALOMEDS_Study_i * st_servant = SALOMEDS_Study_i::GetStudyServant(theStudy, _orb);//new SALOMEDS_Study_i (theStudy, _orb);
325 SALOMEDS::Study_var st = SALOMEDS::Study::_narrow(st_servant->_this());
329 Engines::TMPFile_var aStream;
330 CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
332 if ( !CORBA::is_nil( _engine ) )
333 aStream = _engine->DumpPython(st.in(), isPublished, isMultiFile, aValidScript);
335 SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
336 theStreamLength = aTMPFile->Size();
337 isValidScript = aValidScript;
344 //###############################################################################################################
345 // SALOMEDS_DriverFactory
346 //###############################################################################################################
348 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB)
350 _orb = CORBA::ORB::_duplicate(theORB);
351 _name_service = new SALOME_NamingService(_orb);
355 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
357 delete _name_service;
360 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
363 if ( theComponentType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() )
364 return NULL; // skip the "Interface Applicative" component
366 SALOMEDSImpl_Driver* driver = 0;
368 CORBA::Object_var obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
370 if (!CORBA::is_nil(obj)) {
371 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
372 driver = new SALOMEDS_Driver_i(anEngine, _orb);
375 // It can be "light" module
376 obj = _name_service->Resolve("/Kernel/Session");
377 if (!CORBA::is_nil(obj)) {
378 SALOME::Session_var session = SALOME::Session::_narrow(obj);
379 if (!CORBA::is_nil(session)) {
380 Engines::EngineComponent_var anEngine = session->GetComponent(theComponentType.c_str());
381 if (!CORBA::is_nil(anEngine))
382 driver = new SALOMEDS_Driver_i(anEngine, _orb);
390 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
392 CORBA::Object_var obj;
393 obj = _orb->string_to_object(theIOR.c_str());
395 if (!CORBA::is_nil(obj)) {
396 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
397 return new SALOMEDS_Driver_i(anEngine, _orb);