1 // Copyright (C) 2007-2020 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()
192 if ( !CORBA::is_nil( _engine )) {
193 CORBA::String_var v = _engine->getVersion();
199 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
200 const std::string& IORString,
204 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
207 std::string pstr = "";
209 if ( !CORBA::is_nil(_driver) ) {
210 CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), IORString.c_str(),
211 isMultiFile, isASCII);
212 if ( pers_string.in() )
223 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
224 const std::string& aLocalPersistentID,
228 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
229 CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
232 std::string ior = "";
234 if ( !CORBA::is_nil(_driver) ) {
235 CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
244 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
246 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
251 if ( !CORBA::is_nil(_driver) )
252 isOk = _driver->CanCopy(so.in());
261 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
263 long& theStreamLength)
265 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
268 CORBA::Long anObjectID;
269 SALOMEDS::TMPFile_var aStream;
271 if ( !CORBA::is_nil(_driver) )
272 aStream = _driver->CopyFrom(so.in(), anObjectID);
274 SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
275 theStreamLength = aTMPFile->Size();
276 theObjectID = anObjectID;
284 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
287 bool canPaste = false;
289 if ( !CORBA::is_nil(_driver) )
290 canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
296 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
297 const long theStreamLength,
299 const SALOMEDSImpl_SObject& theObject)
301 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
302 CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
304 SALOMEDS::TMPFile_var aStream;
305 if(theStreamLength > 0)
306 aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
308 aStream = new SALOMEDS::TMPFile(0);
312 std::string entry = "";
314 if ( !CORBA::is_nil(_driver) ) {
315 SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
316 entry = ret_so->GetID();
325 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(bool isPublished,
328 long& theStreamLength)
332 Engines::TMPFile_var aStream;
333 CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
335 if ( !CORBA::is_nil( _engine ) )
336 aStream = _engine->DumpPython(isPublished, isMultiFile, aValidScript);
338 SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
339 theStreamLength = aTMPFile->Size();
340 isValidScript = aValidScript;
347 //###############################################################################################################
348 // SALOMEDS_DriverFactory
349 //###############################################################################################################
351 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB)
353 _orb = CORBA::ORB::_duplicate(theORB);
354 _name_service = new SALOME_NamingService(_orb);
358 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
360 delete _name_service;
363 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
366 if ( theComponentType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() )
367 return NULL; // skip the "Interface Applicative" component
369 SALOMEDSImpl_Driver* driver = 0;
371 CORBA::Object_var obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
373 if (!CORBA::is_nil(obj)) {
374 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
375 driver = new SALOMEDS_Driver_i(anEngine, _orb);
378 // It can be "light" module
379 obj = _name_service->Resolve("/Kernel/Session");
380 if (!CORBA::is_nil(obj)) {
381 SALOME::Session_var session = SALOME::Session::_narrow(obj);
382 if (!CORBA::is_nil(session)) {
383 Engines::EngineComponent_var anEngine = session->GetComponent(theComponentType.c_str());
384 if (!CORBA::is_nil(anEngine))
385 driver = new SALOMEDS_Driver_i(anEngine, _orb);
393 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
395 CORBA::Object_var obj;
396 obj = _orb->string_to_object(theIOR.c_str());
398 if (!CORBA::is_nil(obj)) {
399 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
400 return new SALOMEDS_Driver_i(anEngine, _orb);