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_StudyManager.hxx"
36 #include "SALOMEDS_StudyBuilder_i.hxx"
38 #include "SALOMEDS_Driver_i.hxx"
40 #include "SALOMEDSImpl_SObject.hxx"
41 #include "SALOMEDSImpl_SComponent.hxx"
42 #include "SALOMEDSImpl_GenericAttribute.hxx"
47 #include "DF_Attribute.hxx"
49 #include "Utils_CorbaException.hxx"
50 #include "Utils_ORB_INIT.hxx"
51 #include "Utils_SINGLETON.hxx"
53 pthread_mutex_t SALOMEDS_StudyBuilder::_remoteBuilderMutex;
55 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDSImpl_StudyBuilder* theBuilder)
58 _local_impl = theBuilder;
59 _corba_impl = SALOMEDS::StudyBuilder::_nil();
64 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDS::StudyBuilder_ptr theBuilder)
66 pthread_mutex_lock( &_remoteBuilderMutex );
69 _corba_impl = SALOMEDS::StudyBuilder::_duplicate(theBuilder);
74 SALOMEDS_StudyBuilder::~SALOMEDS_StudyBuilder()
76 if (!_isLocal) pthread_mutex_unlock( &_remoteBuilderMutex );
79 _PTR(SComponent) SALOMEDS_StudyBuilder::NewComponent(const std::string& ComponentDataType)
81 SALOMEDSClient_SComponent* aSCO = NULL;
85 SALOMEDS::Locker lock;
87 SALOMEDSImpl_SComponent aSCO_impl =_local_impl->NewComponent(ComponentDataType);
88 if(!aSCO_impl) return _PTR(SComponent)(aSCO);
89 aSCO = new SALOMEDS_SComponent(aSCO_impl);
92 SALOMEDS::SComponent_var aSCO_impl = _corba_impl->NewComponent((char*)ComponentDataType.c_str());
93 if(CORBA::is_nil(aSCO_impl)) return _PTR(SComponent)(aSCO);
94 aSCO = new SALOMEDS_SComponent(aSCO_impl);
97 return _PTR(SComponent)(aSCO);
100 void SALOMEDS_StudyBuilder::DefineComponentInstance (const _PTR(SComponent)& theSCO,
101 const std::string& ComponentIOR)
105 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
108 SALOMEDS::Locker lock;
110 _local_impl->DefineComponentInstance(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())),
114 CORBA::Object_var obj = _orb->string_to_object(ComponentIOR.c_str());
115 _corba_impl->DefineComponentInstance(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), obj);
119 void SALOMEDS_StudyBuilder::RemoveComponent(const _PTR(SComponent)& theSCO)
122 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
125 SALOMEDS::Locker lock;
127 _local_impl->RemoveComponent(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
129 else _corba_impl->RemoveComponent(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
132 _PTR(SObject) SALOMEDS_StudyBuilder::NewObject(const _PTR(SObject)& theFatherObject)
136 SALOMEDSClient_SObject* aSO = NULL;
137 SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
138 if (father == NULL) return _PTR(SObject)(aSO);
140 SALOMEDS::Locker lock;
142 SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObject(*(father->GetLocalImpl()));
143 if(!aSO_impl) return _PTR(SObject)(aSO);
144 aSO = new SALOMEDS_SObject(aSO_impl);
147 SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObject(father->GetCORBAImpl());
148 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
149 aSO = new SALOMEDS_SObject(aSO_impl);
152 return _PTR(SObject)(aSO);
155 _PTR(SObject) SALOMEDS_StudyBuilder::NewObjectToTag(const _PTR(SObject)& theFatherObject, int theTag)
159 SALOMEDSClient_SObject* aSO = NULL;
160 SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
161 if (father == NULL) return _PTR(SObject)(aSO);
163 SALOMEDS::Locker lock;
165 SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObjectToTag(*(father->GetLocalImpl()), theTag);
166 if(aSO_impl.IsNull()) return _PTR(SObject)(aSO);
167 aSO = new SALOMEDS_SObject(aSO_impl);
170 SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObjectToTag(father->GetCORBAImpl(), theTag);
171 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
172 aSO = new SALOMEDS_SObject(aSO_impl);
175 return _PTR(SObject)(aSO);
178 void SALOMEDS_StudyBuilder::AddDirectory(const std::string& thePath)
182 SALOMEDS::Locker lock;
184 _local_impl->AddDirectory((char*)thePath.c_str());
185 if (_local_impl->IsError()) {
186 std::string anErrorCode = _local_impl->GetErrorCode();
187 if (anErrorCode == "StudyNameAlreadyUsed") throw SALOMEDS::Study::StudyNameAlreadyUsed();
188 if (anErrorCode == "StudyInvalidDirectory") throw SALOMEDS::Study::StudyInvalidDirectory();
189 if (anErrorCode == "StudyInvalidComponent") throw SALOMEDS::Study::StudyInvalidComponent();
192 else _corba_impl->AddDirectory((char*)thePath.c_str());
195 void SALOMEDS_StudyBuilder::LoadWith(const _PTR(SComponent)& theSCO, const std::string& theIOR)
199 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
200 CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
201 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
202 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
205 SALOMEDS::Locker lock;
207 SALOMEDS_Driver_i* drv = new SALOMEDS_Driver_i(anEngine, _orb);
208 SALOMEDSImpl_SComponent aSCO_impl = *(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl()));
209 bool isDone = _local_impl->LoadWith(aSCO_impl, drv);
211 if(!isDone && _local_impl->IsError())
212 THROW_SALOME_CORBA_EXCEPTION(_local_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
215 _corba_impl->LoadWith(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), aDriver);
219 void SALOMEDS_StudyBuilder::Load(const _PTR(SObject)& theSCO)
221 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
222 if (_isLocal) _local_impl->Load(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
223 else _corba_impl->Load(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
226 void SALOMEDS_StudyBuilder::RemoveObject(const _PTR(SObject)& theSO)
230 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
233 SALOMEDS::Locker lock;
235 _local_impl->RemoveObject(*(aSO->GetLocalImpl()));
237 else _corba_impl->RemoveObject(aSO->GetCORBAImpl());
240 void SALOMEDS_StudyBuilder::RemoveObjectWithChildren(const _PTR(SObject)& theSO)
244 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
247 SALOMEDS::Locker lock;
249 _local_impl->RemoveObjectWithChildren(*(aSO->GetLocalImpl()));
251 else _corba_impl->RemoveObjectWithChildren(aSO->GetCORBAImpl());
254 _PTR(GenericAttribute) SALOMEDS_StudyBuilder::FindOrCreateAttribute(const _PTR(SObject)& theSO,
255 const std::string& aTypeOfAttribute)
257 SALOMEDSClient_GenericAttribute* anAttr = NULL;
258 if(!theSO) return _PTR(GenericAttribute)(anAttr);
259 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
261 SALOMEDS::Locker lock;
263 SALOMEDSImpl_GenericAttribute* aGA;
265 aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>
266 (_local_impl->FindOrCreateAttribute(*(aSO->GetLocalImpl()), aTypeOfAttribute));
269 throw SALOMEDS::StudyBuilder::LockProtection();
271 anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
274 SALOMEDS::GenericAttribute_var aGA =
275 _corba_impl->FindOrCreateAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
276 anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
279 return _PTR(GenericAttribute)(anAttr);
282 bool SALOMEDS_StudyBuilder::FindAttribute(const _PTR(SObject)& theSO,
283 _PTR(GenericAttribute)& anAttribute,
284 const std::string& aTypeOfAttribute)
288 if(!theSO) return false;
290 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
292 SALOMEDS::Locker lock;
294 DF_Attribute* anAttr = NULL;
295 ret = _local_impl->FindAttribute(*(aSO->GetLocalImpl()), anAttr, aTypeOfAttribute);
297 SALOMEDSImpl_GenericAttribute* aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
298 anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
302 SALOMEDS::GenericAttribute_var aGA;
303 ret = _corba_impl->FindAttribute(aSO->GetCORBAImpl(), aGA.out(), (char*)aTypeOfAttribute.c_str());
304 if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
310 void SALOMEDS_StudyBuilder::RemoveAttribute(const _PTR(SObject)& theSO, const std::string& aTypeOfAttribute)
314 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
317 SALOMEDS::Locker lock;
319 _local_impl->RemoveAttribute(*(aSO->GetLocalImpl()), (char*)aTypeOfAttribute.c_str());
321 else _corba_impl->RemoveAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
324 void SALOMEDS_StudyBuilder::Addreference(const _PTR(SObject)& me, const _PTR(SObject)& thereferencedObject)
326 if(!me || !thereferencedObject) {
327 throw DFexception("Invalid arguments");
330 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
331 SALOMEDS_SObject* aRefSO = dynamic_cast<SALOMEDS_SObject*>(thereferencedObject.get());
334 SALOMEDS::Locker lock;
336 _local_impl->Addreference(*(aSO->GetLocalImpl()), *(aRefSO->GetLocalImpl()));
338 else _corba_impl->Addreference(aSO->GetCORBAImpl(), aRefSO->GetCORBAImpl());
341 void SALOMEDS_StudyBuilder::RemoveReference(const _PTR(SObject)& me)
344 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
347 SALOMEDS::Locker lock;
349 _local_impl->RemoveReference(*(aSO->GetLocalImpl()));
351 else _corba_impl->RemoveReference(aSO->GetCORBAImpl());
354 void SALOMEDS_StudyBuilder::SetGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
358 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
361 SALOMEDS::Locker lock;
363 _local_impl->SetGUID(*(aSO->GetLocalImpl()), theGUID);
365 else _corba_impl->SetGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
368 bool SALOMEDS_StudyBuilder::IsGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
370 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
373 SALOMEDS::Locker lock;
375 ret = _local_impl->IsGUID(*(aSO->GetLocalImpl()), (char*)theGUID.c_str());
377 else ret = _corba_impl->IsGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
382 void SALOMEDS_StudyBuilder::NewCommand()
385 SALOMEDS::Locker lock;
386 _local_impl->NewCommand();
388 else _corba_impl->NewCommand();
391 void SALOMEDS_StudyBuilder::CommitCommand()
394 SALOMEDS::Locker lock;
396 _local_impl->CommitCommand();
399 throw SALOMEDS::StudyBuilder::LockProtection();
402 else _corba_impl->CommitCommand();
405 bool SALOMEDS_StudyBuilder::HasOpenCommand()
409 SALOMEDS::Locker lock;
410 ret = _local_impl->HasOpenCommand();
412 else ret = _corba_impl->HasOpenCommand();
416 void SALOMEDS_StudyBuilder::AbortCommand()
419 SALOMEDS::Locker lock;
420 _local_impl->AbortCommand();
422 else _corba_impl->AbortCommand();
425 void SALOMEDS_StudyBuilder::Undo()
428 SALOMEDS::Locker lock;
433 throw SALOMEDS::StudyBuilder::LockProtection();
436 else _corba_impl->Undo();
439 void SALOMEDS_StudyBuilder::Redo()
442 SALOMEDS::Locker lock;
447 throw SALOMEDS::StudyBuilder::LockProtection();
450 else _corba_impl->Redo();
453 bool SALOMEDS_StudyBuilder::GetAvailableUndos()
457 SALOMEDS::Locker lock;
458 ret = _local_impl->GetAvailableUndos();
460 else ret = _corba_impl->GetAvailableUndos();
464 bool SALOMEDS_StudyBuilder::GetAvailableRedos()
468 SALOMEDS::Locker lock;
469 ret = _local_impl->GetAvailableRedos();
471 else ret = _corba_impl->GetAvailableRedos();
475 int SALOMEDS_StudyBuilder::UndoLimit()
479 SALOMEDS::Locker lock;
480 aLimit = _local_impl->UndoLimit();
482 else aLimit = _corba_impl->UndoLimit();
486 void SALOMEDS_StudyBuilder::UndoLimit(int theLimit)
490 SALOMEDS::Locker lock;
492 _local_impl->UndoLimit(theLimit);
494 else _corba_impl->UndoLimit(theLimit);
497 void SALOMEDS_StudyBuilder::CheckLocked()
499 //There is only local part as CORBA part throws the correct exception
501 SALOMEDS::Locker lock;
503 _local_impl->CheckLocked();
506 throw SALOMEDS::StudyBuilder::LockProtection();
511 void SALOMEDS_StudyBuilder::SetName(const _PTR(SObject)& theSO, const std::string& theValue)
515 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
518 SALOMEDS::Locker lock;
520 _local_impl->SetName(*(aSO->GetLocalImpl()), theValue);
522 else _corba_impl->SetName(aSO->GetCORBAImpl(), (char*)theValue.c_str());
525 void SALOMEDS_StudyBuilder::SetComment(const _PTR(SObject)& theSO, const std::string& theValue)
529 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
532 SALOMEDS::Locker lock;
534 _local_impl->SetComment(*(aSO->GetLocalImpl()), theValue);
536 else _corba_impl->SetComment(aSO->GetCORBAImpl(), (char*)theValue.c_str());
539 void SALOMEDS_StudyBuilder::SetIOR(const _PTR(SObject)& theSO, const std::string& theValue)
543 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
546 SALOMEDS::Locker lock;
548 _local_impl->SetIOR(*(aSO->GetLocalImpl()), theValue);
550 else _corba_impl->SetIOR(aSO->GetCORBAImpl(), (char*)theValue.c_str());
553 SALOMEDS::StudyBuilder_ptr SALOMEDS_StudyBuilder::GetBuilder()
556 if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
557 SALOMEDS_StudyBuilder_i* servant = new SALOMEDS_StudyBuilder_i(_local_impl, _orb);
558 SALOMEDS::StudyBuilder_var aBuilder = servant->StudyBuilder::_this();
559 _corba_impl = SALOMEDS::StudyBuilder::_duplicate(aBuilder);
560 return aBuilder._retn();
563 return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
565 return SALOMEDS::StudyBuilder::_nil();
568 void SALOMEDS_StudyBuilder::init_orb()
570 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
571 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());