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"
32 #include CORBA_CLIENT_HEADER(SALOME_Session)
34 SALOMEDS_Driver_i::SALOMEDS_Driver_i(Engines::EngineComponent_ptr theEngine, CORBA::ORB_ptr theORB)
36 // engine should not be null - component is supposed to be inherited from Engines::EngineComponent
37 _engine = Engines::EngineComponent::_duplicate(theEngine);
38 // driver can be null - if component interface does not inherit SALOMEDS::Driver
39 _driver = SALOMEDS::Driver::_narrow(theEngine);
40 _orb = CORBA::ORB::_duplicate(theORB);
43 SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB)
45 // driver can be null - if component interface does not inherit SALOMEDS::Driver
46 _driver = SALOMEDS::Driver::_duplicate(theDriver);
47 // engine can be null - since it is narrowed from SALOMEDS::Driver ptr which can be null
48 _engine = Engines::EngineComponent::_narrow(theDriver);
49 _orb = CORBA::ORB::_duplicate(theORB);
52 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
56 std::string SALOMEDS_Driver_i::GetIOR()
59 if ( !CORBA::is_nil(_engine) ) {
60 CORBA::String_var cior = _orb->object_to_string(_engine);
66 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
67 const std::string& theURL,
68 long& theStreamLength,
71 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
72 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
75 SALOMEDS::TMPFile_var aStream;
77 if ( !CORBA::is_nil(_driver) )
78 aStream = _driver->Save(sco.in(), url, isMultiFile);
80 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
81 theStreamLength = aTMPFile->Size();
89 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
90 const std::string& theURL,
91 long& theStreamLength,
94 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
95 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
98 SALOMEDS::TMPFile_var aStream;
100 if ( !CORBA::is_nil(_driver) )
101 aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
103 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
104 theStreamLength = aTMPFile->Size();
112 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
113 const unsigned char* theStream,
114 const long theStreamLength,
115 const std::string& theURL,
118 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
119 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
120 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
122 SALOMEDS::TMPFile_var aStream;
123 if (theStreamLength > 0)
124 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
126 aStream = new SALOMEDS::TMPFile(0);
131 if ( !CORBA::is_nil(_driver) )
132 isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
139 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
140 const unsigned char* theStream,
141 const long theStreamLength,
142 const std::string& theURL,
145 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
146 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
147 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
149 SALOMEDS::TMPFile_var aStream;
150 if(theStreamLength > 0)
151 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
153 aStream = new SALOMEDS::TMPFile(0);
158 if ( !CORBA::is_nil(_driver) )
159 isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
166 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
168 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
171 if ( !CORBA::is_nil(_driver) )
172 _driver->Close(sco.in());
177 std::string SALOMEDS_Driver_i::ComponentDataType()
179 std::string dtype = "";
180 if ( !CORBA::is_nil(_driver) ) {
181 CORBA::String_var type = _driver->ComponentDataType();
187 std::string SALOMEDS_Driver_i::Version()
189 return !CORBA::is_nil( _engine ) ? _engine->getVersion() : std::string("");
192 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
193 const std::string& IORString,
197 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
200 std::string pstr = "";
202 if ( !CORBA::is_nil(_driver) ) {
203 CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), IORString.c_str(),
204 isMultiFile, isASCII);
215 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
216 const std::string& aLocalPersistentID,
220 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
221 CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
224 std::string ior = "";
226 if ( !CORBA::is_nil(_driver) ) {
227 CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
235 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
237 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
242 if ( !CORBA::is_nil(_driver) )
243 isOk = _driver->CanCopy(so.in());
252 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
254 long& theStreamLength)
256 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
259 CORBA::Long anObjectID;
260 SALOMEDS::TMPFile_var aStream;
262 if ( !CORBA::is_nil(_driver) )
263 aStream = _driver->CopyFrom(so.in(), anObjectID);
265 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
266 theStreamLength = aTMPFile->Size();
267 theObjectID = anObjectID;
275 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
278 bool canPaste = false;
280 if ( !CORBA::is_nil(_driver) )
281 canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
287 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
288 const long theStreamLength,
290 const SALOMEDSImpl_SObject& theObject)
292 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
293 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
295 SALOMEDS::TMPFile_var aStream;
296 if(theStreamLength > 0)
297 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
299 aStream = new SALOMEDS::TMPFile(0);
303 std::string entry = "";
305 if ( !CORBA::is_nil(_driver) ) {
306 SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
307 entry = ret_so->GetID();
316 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy,
320 long& theStreamLength)
322 SALOMEDS_Study_i * st_servant = SALOMEDS_Study_i::GetStudyServant(theStudy, _orb);//new SALOMEDS_Study_i (theStudy, _orb);
323 SALOMEDS::Study_var st = SALOMEDS::Study::_narrow(st_servant->_this());
327 Engines::TMPFile_var aStream;
328 CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
330 if ( !CORBA::is_nil( _engine ) )
331 aStream = _engine->DumpPython(st.in(), isPublished, isMultiFile, aValidScript);
333 SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
334 theStreamLength = aTMPFile->Size();
335 isValidScript = aValidScript;
342 //###############################################################################################################
343 // SALOMEDS_DriverFactory
344 //###############################################################################################################
346 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB)
348 _orb = CORBA::ORB::_duplicate(theORB);
349 _name_service = new SALOME_NamingService(_orb);
353 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
355 delete _name_service;
358 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
360 SALOMEDSImpl_Driver* driver = 0;
362 CORBA::Object_var obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
364 if (!CORBA::is_nil(obj)) {
365 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
366 driver = new SALOMEDS_Driver_i(anEngine, _orb);
369 // It can be "light" module
370 obj = _name_service->Resolve("/Kernel/Session");
371 if (!CORBA::is_nil(obj)) {
372 SALOME::Session_var session = SALOME::Session::_narrow(obj);
373 if (!CORBA::is_nil(session)) {
374 Engines::EngineComponent_var anEngine = session->GetComponent(theComponentType.c_str());
375 if (!CORBA::is_nil(anEngine))
376 driver = new SALOMEDS_Driver_i(anEngine, _orb);
384 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
386 CORBA::Object_var obj;
387 obj = _orb->string_to_object(theIOR.c_str());
389 if (!CORBA::is_nil(obj)) {
390 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
391 return new SALOMEDS_Driver_i(anEngine, _orb);