1 // Copyright (C) 2007-2016 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>
31 #include <SALOME_KernelServices.hxx>
34 #include CORBA_CLIENT_HEADER(SALOME_Session)
36 SALOMEDS_Driver_i::SALOMEDS_Driver_i(Engines::EngineComponent_ptr theEngine, CORBA::ORB_ptr theORB)
38 // engine should not be null - component is supposed to be inherited from Engines::EngineComponent
39 _engine = Engines::EngineComponent::_duplicate(theEngine);
40 // driver can be null - if component interface does not inherit SALOMEDS::Driver
41 _driver = SALOMEDS::Driver::_narrow(theEngine);
42 _orb = CORBA::ORB::_duplicate(theORB);
45 SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB)
47 // driver can be null - if component interface does not inherit SALOMEDS::Driver
48 _driver = SALOMEDS::Driver::_duplicate(theDriver);
49 // engine can be null - since it is narrowed from SALOMEDS::Driver ptr which can be null
50 _engine = Engines::EngineComponent::_narrow(theDriver);
51 _orb = CORBA::ORB::_duplicate(theORB);
54 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
58 std::string SALOMEDS_Driver_i::GetIOR()
61 if ( !CORBA::is_nil(_engine) ) {
62 CORBA::String_var cior = _orb->object_to_string(_engine);
68 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
69 const std::string& theURL,
70 long& theStreamLength,
73 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
74 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
77 SALOMEDS::TMPFile_var aStream;
79 if ( !CORBA::is_nil(_driver) )
80 aStream = _driver->Save(sco.in(), url, isMultiFile);
82 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
83 theStreamLength = aTMPFile->Size();
91 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
92 const std::string& theURL,
93 long& theStreamLength,
96 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
97 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
100 SALOMEDS::TMPFile_var aStream;
102 if ( !CORBA::is_nil(_driver) )
103 aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
105 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
106 theStreamLength = aTMPFile->Size();
114 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
115 const unsigned char* theStream,
116 const long theStreamLength,
117 const std::string& theURL,
120 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
121 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
122 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
124 SALOMEDS::TMPFile_var aStream;
125 if (theStreamLength > 0)
126 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
128 aStream = new SALOMEDS::TMPFile(0);
133 if ( !CORBA::is_nil(_driver) )
134 isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
141 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
142 const unsigned char* theStream,
143 const long theStreamLength,
144 const std::string& theURL,
147 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
148 CORBA::String_var url = CORBA::string_dup(theURL.c_str());
149 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
151 SALOMEDS::TMPFile_var aStream;
152 if(theStreamLength > 0)
153 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
155 aStream = new SALOMEDS::TMPFile(0);
160 if ( !CORBA::is_nil(_driver) )
161 isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
168 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
170 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
173 if ( !CORBA::is_nil(_driver) )
174 _driver->Close(sco.in());
179 std::string SALOMEDS_Driver_i::ComponentDataType()
181 std::string dtype = "";
182 if ( !CORBA::is_nil(_driver) ) {
183 CORBA::String_var type = _driver->ComponentDataType();
189 std::string SALOMEDS_Driver_i::Version()
191 return !CORBA::is_nil( _engine ) ? _engine->getVersion() : std::string("");
194 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
195 const std::string& IORString,
199 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
202 std::string pstr = "";
204 if ( !CORBA::is_nil(_driver) ) {
205 CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), IORString.c_str(),
206 isMultiFile, isASCII);
207 if ( pers_string.in() )
218 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
219 const std::string& aLocalPersistentID,
223 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
224 CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
227 std::string ior = "";
229 if ( !CORBA::is_nil(_driver) ) {
230 CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
239 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
241 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
246 if ( !CORBA::is_nil(_driver) )
247 isOk = _driver->CanCopy(so.in());
256 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
258 long& theStreamLength)
260 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
263 CORBA::Long anObjectID;
264 SALOMEDS::TMPFile_var aStream;
266 if ( !CORBA::is_nil(_driver) )
267 aStream = _driver->CopyFrom(so.in(), anObjectID);
269 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
270 theStreamLength = aTMPFile->Size();
271 theObjectID = anObjectID;
279 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
282 bool canPaste = false;
284 if ( !CORBA::is_nil(_driver) )
285 canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
291 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
292 const long theStreamLength,
294 const SALOMEDSImpl_SObject& theObject)
296 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
297 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
299 SALOMEDS::TMPFile_var aStream;
300 if(theStreamLength > 0)
301 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
303 aStream = new SALOMEDS::TMPFile(0);
307 std::string entry = "";
309 if ( !CORBA::is_nil(_driver) ) {
310 SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
311 entry = ret_so->GetID();
320 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(bool isPublished,
323 long& theStreamLength)
325 SALOME_NamingService* namingService = KERNEL::getNamingService();
326 CORBA::Object_var obj = namingService->Resolve("/Study");
327 SALOMEDS::Study_var st = SALOMEDS::Study::_narrow(obj);
331 Engines::TMPFile_var aStream;
332 CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
334 if ( !CORBA::is_nil( _engine ) )
335 aStream = _engine->DumpPython(st.in(), isPublished, isMultiFile, aValidScript);
337 SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
338 theStreamLength = aTMPFile->Size();
339 isValidScript = aValidScript;
346 //###############################################################################################################
347 // SALOMEDS_DriverFactory
348 //###############################################################################################################
350 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB)
352 _orb = CORBA::ORB::_duplicate(theORB);
353 _name_service = new SALOME_NamingService(_orb);
357 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
359 delete _name_service;
362 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
365 if ( theComponentType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() )
366 return NULL; // skip the "Interface Applicative" component
368 SALOMEDSImpl_Driver* driver = 0;
370 CORBA::Object_var obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
372 if (!CORBA::is_nil(obj)) {
373 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
374 driver = new SALOMEDS_Driver_i(anEngine, _orb);
377 // It can be "light" module
378 obj = _name_service->Resolve("/Kernel/Session");
379 if (!CORBA::is_nil(obj)) {
380 SALOME::Session_var session = SALOME::Session::_narrow(obj);
381 if (!CORBA::is_nil(session)) {
382 Engines::EngineComponent_var anEngine = session->GetComponent(theComponentType.c_str());
383 if (!CORBA::is_nil(anEngine))
384 driver = new SALOMEDS_Driver_i(anEngine, _orb);
392 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
394 CORBA::Object_var obj;
395 obj = _orb->string_to_object(theIOR.c_str());
397 if (!CORBA::is_nil(obj)) {
398 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
399 return new SALOMEDS_Driver_i(anEngine, _orb);