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 // File : SALOMEDS_StudyBuilder.cxx
24 // Author : Sergey RUIN
27 #include "utilities.h"
29 #include "SALOMEDS_StudyBuilder.hxx"
31 #include "SALOMEDS.hxx"
32 #include "SALOMEDS_SObject.hxx"
33 #include "SALOMEDS_SComponent.hxx"
34 #include "SALOMEDS_GenericAttribute.hxx"
35 #include "SALOMEDS_StudyBuilder_i.hxx"
37 #include "SALOMEDS_Driver_i.hxx"
39 #include "SALOMEDSImpl_SObject.hxx"
40 #include "SALOMEDSImpl_SComponent.hxx"
41 #include "SALOMEDSImpl_GenericAttribute.hxx"
46 #include "DF_Attribute.hxx"
48 #include "Utils_CorbaException.hxx"
49 #include "Utils_ORB_INIT.hxx"
50 #include "Utils_SINGLETON.hxx"
52 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDSImpl_StudyBuilder* theBuilder)
55 _local_impl = theBuilder;
56 _corba_impl = SALOMEDS::StudyBuilder::_nil();
61 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDS::StudyBuilder_ptr theBuilder)
65 _corba_impl = SALOMEDS::StudyBuilder::_duplicate(theBuilder);
70 SALOMEDS_StudyBuilder::~SALOMEDS_StudyBuilder()
74 _PTR(SComponent) SALOMEDS_StudyBuilder::NewComponent(const std::string& ComponentDataType)
76 SALOMEDSClient_SComponent* aSCO = NULL;
80 SALOMEDS::Locker lock;
82 SALOMEDSImpl_SComponent aSCO_impl =_local_impl->NewComponent(ComponentDataType);
83 if(!aSCO_impl) return _PTR(SComponent)(aSCO);
84 aSCO = new SALOMEDS_SComponent(aSCO_impl);
87 SALOMEDS::SComponent_var aSCO_impl = _corba_impl->NewComponent((char*)ComponentDataType.c_str());
88 if(CORBA::is_nil(aSCO_impl)) return _PTR(SComponent)(aSCO);
89 aSCO = new SALOMEDS_SComponent(aSCO_impl);
92 return _PTR(SComponent)(aSCO);
95 void SALOMEDS_StudyBuilder::DefineComponentInstance (const _PTR(SComponent)& theSCO,
96 const std::string& ComponentIOR)
100 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
103 SALOMEDS::Locker lock;
105 _local_impl->DefineComponentInstance(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())),
109 CORBA::Object_var obj = _orb->string_to_object(ComponentIOR.c_str());
110 _corba_impl->DefineComponentInstance(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), obj);
114 void SALOMEDS_StudyBuilder::RemoveComponent(const _PTR(SComponent)& theSCO)
117 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
120 SALOMEDS::Locker lock;
122 _local_impl->RemoveComponent(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
124 else _corba_impl->RemoveComponent(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
127 _PTR(SObject) SALOMEDS_StudyBuilder::NewObject(const _PTR(SObject)& theFatherObject)
131 SALOMEDSClient_SObject* aSO = NULL;
132 SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
133 if (father == NULL) return _PTR(SObject)(aSO);
135 SALOMEDS::Locker lock;
137 SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObject(*(father->GetLocalImpl()));
138 if(!aSO_impl) return _PTR(SObject)(aSO);
139 aSO = new SALOMEDS_SObject(aSO_impl);
142 SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObject(father->GetCORBAImpl());
143 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
144 aSO = new SALOMEDS_SObject(aSO_impl);
147 return _PTR(SObject)(aSO);
150 _PTR(SObject) SALOMEDS_StudyBuilder::NewObjectToTag(const _PTR(SObject)& theFatherObject, int theTag)
154 SALOMEDSClient_SObject* aSO = NULL;
155 SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
156 if (father == NULL) return _PTR(SObject)(aSO);
158 SALOMEDS::Locker lock;
160 SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObjectToTag(*(father->GetLocalImpl()), theTag);
161 if(aSO_impl.IsNull()) return _PTR(SObject)(aSO);
162 aSO = new SALOMEDS_SObject(aSO_impl);
165 SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObjectToTag(father->GetCORBAImpl(), theTag);
166 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
167 aSO = new SALOMEDS_SObject(aSO_impl);
170 return _PTR(SObject)(aSO);
173 void SALOMEDS_StudyBuilder::LoadWith(const _PTR(SComponent)& theSCO, const std::string& theIOR)
177 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
178 CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
179 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
180 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
183 SALOMEDS::Locker lock;
185 SALOMEDS_Driver_i* drv = new SALOMEDS_Driver_i(anEngine, _orb);
186 SALOMEDSImpl_SComponent aSCO_impl = *(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl()));
187 bool isDone = _local_impl->LoadWith(aSCO_impl, drv);
189 if(!isDone && _local_impl->IsError())
190 THROW_SALOME_CORBA_EXCEPTION(_local_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
193 _corba_impl->LoadWith(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), aDriver);
197 void SALOMEDS_StudyBuilder::Load(const _PTR(SObject)& theSCO)
199 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
200 if (_isLocal) _local_impl->Load(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
201 else _corba_impl->Load(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
204 void SALOMEDS_StudyBuilder::RemoveObject(const _PTR(SObject)& theSO)
208 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
211 SALOMEDS::Locker lock;
213 _local_impl->RemoveObject(*(aSO->GetLocalImpl()));
215 else _corba_impl->RemoveObject(aSO->GetCORBAImpl());
218 void SALOMEDS_StudyBuilder::RemoveObjectWithChildren(const _PTR(SObject)& theSO)
222 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
225 SALOMEDS::Locker lock;
227 _local_impl->RemoveObjectWithChildren(*(aSO->GetLocalImpl()));
229 else _corba_impl->RemoveObjectWithChildren(aSO->GetCORBAImpl());
232 _PTR(GenericAttribute) SALOMEDS_StudyBuilder::FindOrCreateAttribute(const _PTR(SObject)& theSO,
233 const std::string& aTypeOfAttribute)
235 SALOMEDSClient_GenericAttribute* anAttr = NULL;
236 if(!theSO) return _PTR(GenericAttribute)(anAttr);
237 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
239 SALOMEDS::Locker lock;
241 SALOMEDSImpl_GenericAttribute* aGA;
243 aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>
244 (_local_impl->FindOrCreateAttribute(*(aSO->GetLocalImpl()), aTypeOfAttribute));
247 throw SALOMEDS::StudyBuilder::LockProtection();
249 anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
252 SALOMEDS::GenericAttribute_var aGA =
253 _corba_impl->FindOrCreateAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
254 anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
257 return _PTR(GenericAttribute)(anAttr);
260 bool SALOMEDS_StudyBuilder::FindAttribute(const _PTR(SObject)& theSO,
261 _PTR(GenericAttribute)& anAttribute,
262 const std::string& aTypeOfAttribute)
266 if(!theSO) return false;
268 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
270 SALOMEDS::Locker lock;
272 DF_Attribute* anAttr = NULL;
273 ret = _local_impl->FindAttribute(*(aSO->GetLocalImpl()), anAttr, aTypeOfAttribute);
275 SALOMEDSImpl_GenericAttribute* aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
276 anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
280 SALOMEDS::GenericAttribute_var aGA;
281 ret = _corba_impl->FindAttribute(aSO->GetCORBAImpl(), aGA.out(), (char*)aTypeOfAttribute.c_str());
282 if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
288 void SALOMEDS_StudyBuilder::RemoveAttribute(const _PTR(SObject)& theSO, const std::string& aTypeOfAttribute)
292 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
295 SALOMEDS::Locker lock;
297 _local_impl->RemoveAttribute(*(aSO->GetLocalImpl()), (char*)aTypeOfAttribute.c_str());
299 else _corba_impl->RemoveAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
302 void SALOMEDS_StudyBuilder::Addreference(const _PTR(SObject)& me, const _PTR(SObject)& thereferencedObject)
304 if(!me || !thereferencedObject) {
305 throw DFexception("Invalid arguments");
308 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
309 SALOMEDS_SObject* aRefSO = dynamic_cast<SALOMEDS_SObject*>(thereferencedObject.get());
312 SALOMEDS::Locker lock;
314 _local_impl->Addreference(*(aSO->GetLocalImpl()), *(aRefSO->GetLocalImpl()));
316 else _corba_impl->Addreference(aSO->GetCORBAImpl(), aRefSO->GetCORBAImpl());
319 void SALOMEDS_StudyBuilder::RemoveReference(const _PTR(SObject)& me)
322 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
325 SALOMEDS::Locker lock;
327 _local_impl->RemoveReference(*(aSO->GetLocalImpl()));
329 else _corba_impl->RemoveReference(aSO->GetCORBAImpl());
332 void SALOMEDS_StudyBuilder::SetGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
336 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
339 SALOMEDS::Locker lock;
341 _local_impl->SetGUID(*(aSO->GetLocalImpl()), theGUID);
343 else _corba_impl->SetGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
346 bool SALOMEDS_StudyBuilder::IsGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
348 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
351 SALOMEDS::Locker lock;
353 ret = _local_impl->IsGUID(*(aSO->GetLocalImpl()), (char*)theGUID.c_str());
355 else ret = _corba_impl->IsGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
360 void SALOMEDS_StudyBuilder::NewCommand()
363 SALOMEDS::Locker lock;
364 _local_impl->NewCommand();
366 else _corba_impl->NewCommand();
369 void SALOMEDS_StudyBuilder::CommitCommand()
372 SALOMEDS::Locker lock;
374 _local_impl->CommitCommand();
377 throw SALOMEDS::StudyBuilder::LockProtection();
380 else _corba_impl->CommitCommand();
383 bool SALOMEDS_StudyBuilder::HasOpenCommand()
387 SALOMEDS::Locker lock;
388 ret = _local_impl->HasOpenCommand();
390 else ret = _corba_impl->HasOpenCommand();
394 void SALOMEDS_StudyBuilder::AbortCommand()
397 SALOMEDS::Locker lock;
398 _local_impl->AbortCommand();
400 else _corba_impl->AbortCommand();
403 void SALOMEDS_StudyBuilder::Undo()
406 SALOMEDS::Locker lock;
411 throw SALOMEDS::StudyBuilder::LockProtection();
414 else _corba_impl->Undo();
417 void SALOMEDS_StudyBuilder::Redo()
420 SALOMEDS::Locker lock;
425 throw SALOMEDS::StudyBuilder::LockProtection();
428 else _corba_impl->Redo();
431 bool SALOMEDS_StudyBuilder::GetAvailableUndos()
435 SALOMEDS::Locker lock;
436 ret = _local_impl->GetAvailableUndos();
438 else ret = _corba_impl->GetAvailableUndos();
442 bool SALOMEDS_StudyBuilder::GetAvailableRedos()
446 SALOMEDS::Locker lock;
447 ret = _local_impl->GetAvailableRedos();
449 else ret = _corba_impl->GetAvailableRedos();
453 int SALOMEDS_StudyBuilder::UndoLimit()
457 SALOMEDS::Locker lock;
458 aLimit = _local_impl->UndoLimit();
460 else aLimit = _corba_impl->UndoLimit();
464 void SALOMEDS_StudyBuilder::UndoLimit(int theLimit)
468 SALOMEDS::Locker lock;
470 _local_impl->UndoLimit(theLimit);
472 else _corba_impl->UndoLimit(theLimit);
475 void SALOMEDS_StudyBuilder::CheckLocked()
477 //There is only local part as CORBA part throws the correct exeception
479 SALOMEDS::Locker lock;
481 _local_impl->CheckLocked();
484 throw SALOMEDS::StudyBuilder::LockProtection();
489 void SALOMEDS_StudyBuilder::SetName(const _PTR(SObject)& theSO, const std::string& theValue)
493 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
496 SALOMEDS::Locker lock;
498 _local_impl->SetName(*(aSO->GetLocalImpl()), theValue);
500 else _corba_impl->SetName(aSO->GetCORBAImpl(), (char*)theValue.c_str());
503 void SALOMEDS_StudyBuilder::SetComment(const _PTR(SObject)& theSO, const std::string& theValue)
507 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
510 SALOMEDS::Locker lock;
512 _local_impl->SetComment(*(aSO->GetLocalImpl()), theValue);
514 else _corba_impl->SetComment(aSO->GetCORBAImpl(), (char*)theValue.c_str());
517 void SALOMEDS_StudyBuilder::SetIOR(const _PTR(SObject)& theSO, const std::string& theValue)
521 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
524 SALOMEDS::Locker lock;
526 _local_impl->SetIOR(*(aSO->GetLocalImpl()), theValue);
528 else _corba_impl->SetIOR(aSO->GetCORBAImpl(), (char*)theValue.c_str());
531 SALOMEDS::StudyBuilder_ptr SALOMEDS_StudyBuilder::GetBuilder()
534 if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
535 SALOMEDS_StudyBuilder_i* servant = new SALOMEDS_StudyBuilder_i(_local_impl, _orb);
536 SALOMEDS::StudyBuilder_var aBuilder = servant->StudyBuilder::_this();
537 _corba_impl = SALOMEDS::StudyBuilder::_duplicate(aBuilder);
538 return aBuilder._retn();
541 return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
543 return SALOMEDS::StudyBuilder::_nil();
546 void SALOMEDS_StudyBuilder::init_orb()
548 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
549 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());