1 // Copyright (C) 2007-2021 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 pthread_mutex_t SALOMEDS_StudyBuilder::_remoteBuilderMutex;
54 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDSImpl_StudyBuilder* theBuilder)
57 _local_impl = theBuilder;
58 _corba_impl = SALOMEDS::StudyBuilder::_nil();
63 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDS::StudyBuilder_ptr theBuilder)
65 pthread_mutex_lock( &_remoteBuilderMutex );
68 _corba_impl = SALOMEDS::StudyBuilder::_duplicate(theBuilder);
73 SALOMEDS_StudyBuilder::~SALOMEDS_StudyBuilder()
75 if (!_isLocal) pthread_mutex_unlock( &_remoteBuilderMutex );
78 _PTR(SComponent) SALOMEDS_StudyBuilder::NewComponent(const std::string& ComponentDataType)
80 SALOMEDSClient_SComponent* aSCO = NULL;
84 SALOMEDS::Locker lock;
86 SALOMEDSImpl_SComponent aSCO_impl =_local_impl->NewComponent(ComponentDataType);
87 if(!aSCO_impl) return _PTR(SComponent)(aSCO);
88 aSCO = new SALOMEDS_SComponent(aSCO_impl);
91 SALOMEDS::SComponent_var aSCO_impl = _corba_impl->NewComponent((char*)ComponentDataType.c_str());
92 if(CORBA::is_nil(aSCO_impl)) return _PTR(SComponent)(aSCO);
93 aSCO = new SALOMEDS_SComponent(aSCO_impl);
96 return _PTR(SComponent)(aSCO);
99 void SALOMEDS_StudyBuilder::DefineComponentInstance (const _PTR(SComponent)& theSCO,
100 const std::string& ComponentIOR)
104 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
107 SALOMEDS::Locker lock;
109 _local_impl->DefineComponentInstance(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())),
113 CORBA::Object_var obj = _orb->string_to_object(ComponentIOR.c_str());
114 _corba_impl->DefineComponentInstance(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), obj);
118 void SALOMEDS_StudyBuilder::RemoveComponent(const _PTR(SComponent)& theSCO)
121 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
124 SALOMEDS::Locker lock;
126 _local_impl->RemoveComponent(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
128 else _corba_impl->RemoveComponent(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
131 _PTR(SObject) SALOMEDS_StudyBuilder::NewObject(const _PTR(SObject)& theFatherObject)
135 SALOMEDSClient_SObject* aSO = NULL;
136 SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
137 if (father == NULL) return _PTR(SObject)(aSO);
139 SALOMEDS::Locker lock;
141 SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObject(*(father->GetLocalImpl()));
142 if(!aSO_impl) return _PTR(SObject)(aSO);
143 aSO = new SALOMEDS_SObject(aSO_impl);
146 SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObject(father->GetCORBAImpl());
147 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
148 aSO = new SALOMEDS_SObject(aSO_impl);
151 return _PTR(SObject)(aSO);
154 _PTR(SObject) SALOMEDS_StudyBuilder::NewObjectToTag(const _PTR(SObject)& theFatherObject, int theTag)
158 SALOMEDSClient_SObject* aSO = NULL;
159 SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
160 if (father == NULL) return _PTR(SObject)(aSO);
162 SALOMEDS::Locker lock;
164 SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObjectToTag(*(father->GetLocalImpl()), theTag);
165 if(aSO_impl.IsNull()) return _PTR(SObject)(aSO);
166 aSO = new SALOMEDS_SObject(aSO_impl);
169 SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObjectToTag(father->GetCORBAImpl(), theTag);
170 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
171 aSO = new SALOMEDS_SObject(aSO_impl);
174 return _PTR(SObject)(aSO);
177 void SALOMEDS_StudyBuilder::LoadWith(const _PTR(SComponent)& theSCO, const std::string& theIOR)
181 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
182 CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
183 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
184 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
187 SALOMEDS::Locker lock;
189 SALOMEDS_Driver_i* drv = new SALOMEDS_Driver_i(anEngine, _orb);
190 SALOMEDSImpl_SComponent aSCO_impl = *(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl()));
191 bool isDone = _local_impl->LoadWith(aSCO_impl, drv);
193 if(!isDone && _local_impl->IsError())
194 THROW_SALOME_CORBA_EXCEPTION(_local_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
197 _corba_impl->LoadWith(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), aDriver);
201 void SALOMEDS_StudyBuilder::Load(const _PTR(SObject)& theSCO)
203 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
204 if (_isLocal) _local_impl->Load(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
205 else _corba_impl->Load(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
208 void SALOMEDS_StudyBuilder::RemoveObject(const _PTR(SObject)& theSO)
212 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
215 SALOMEDS::Locker lock;
217 _local_impl->RemoveObject(*(aSO->GetLocalImpl()));
219 else _corba_impl->RemoveObject(aSO->GetCORBAImpl());
222 void SALOMEDS_StudyBuilder::RemoveObjectWithChildren(const _PTR(SObject)& theSO)
226 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
229 SALOMEDS::Locker lock;
231 _local_impl->RemoveObjectWithChildren(*(aSO->GetLocalImpl()));
233 else _corba_impl->RemoveObjectWithChildren(aSO->GetCORBAImpl());
236 _PTR(GenericAttribute) SALOMEDS_StudyBuilder::FindOrCreateAttribute(const _PTR(SObject)& theSO,
237 const std::string& aTypeOfAttribute)
239 SALOMEDSClient_GenericAttribute* anAttr = NULL;
240 if(!theSO) return _PTR(GenericAttribute)(anAttr);
241 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
243 SALOMEDS::Locker lock;
245 SALOMEDSImpl_GenericAttribute* aGA;
247 aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>
248 (_local_impl->FindOrCreateAttribute(*(aSO->GetLocalImpl()), aTypeOfAttribute));
251 throw SALOMEDS::StudyBuilder::LockProtection();
253 anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
256 SALOMEDS::GenericAttribute_var aGA =
257 _corba_impl->FindOrCreateAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
258 anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
261 return _PTR(GenericAttribute)(anAttr);
264 bool SALOMEDS_StudyBuilder::FindAttribute(const _PTR(SObject)& theSO,
265 _PTR(GenericAttribute)& anAttribute,
266 const std::string& aTypeOfAttribute)
270 if(!theSO) return false;
272 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
274 SALOMEDS::Locker lock;
276 DF_Attribute* anAttr = NULL;
277 ret = _local_impl->FindAttribute(*(aSO->GetLocalImpl()), anAttr, aTypeOfAttribute);
279 SALOMEDSImpl_GenericAttribute* aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
280 anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
284 SALOMEDS::GenericAttribute_var aGA;
285 ret = _corba_impl->FindAttribute(aSO->GetCORBAImpl(), aGA.out(), (char*)aTypeOfAttribute.c_str());
286 if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
292 void SALOMEDS_StudyBuilder::RemoveAttribute(const _PTR(SObject)& theSO, const std::string& aTypeOfAttribute)
296 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
299 SALOMEDS::Locker lock;
301 _local_impl->RemoveAttribute(*(aSO->GetLocalImpl()), (char*)aTypeOfAttribute.c_str());
303 else _corba_impl->RemoveAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
306 void SALOMEDS_StudyBuilder::Addreference(const _PTR(SObject)& me, const _PTR(SObject)& thereferencedObject)
308 if(!me || !thereferencedObject) {
309 throw DFexception("Invalid arguments");
312 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
313 SALOMEDS_SObject* aRefSO = dynamic_cast<SALOMEDS_SObject*>(thereferencedObject.get());
316 SALOMEDS::Locker lock;
318 _local_impl->Addreference(*(aSO->GetLocalImpl()), *(aRefSO->GetLocalImpl()));
320 else _corba_impl->Addreference(aSO->GetCORBAImpl(), aRefSO->GetCORBAImpl());
323 void SALOMEDS_StudyBuilder::RemoveReference(const _PTR(SObject)& me)
326 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
329 SALOMEDS::Locker lock;
331 _local_impl->RemoveReference(*(aSO->GetLocalImpl()));
333 else _corba_impl->RemoveReference(aSO->GetCORBAImpl());
336 void SALOMEDS_StudyBuilder::SetGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
340 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
343 SALOMEDS::Locker lock;
345 _local_impl->SetGUID(*(aSO->GetLocalImpl()), theGUID);
347 else _corba_impl->SetGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
350 bool SALOMEDS_StudyBuilder::IsGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
352 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
355 SALOMEDS::Locker lock;
357 ret = _local_impl->IsGUID(*(aSO->GetLocalImpl()), (char*)theGUID.c_str());
359 else ret = _corba_impl->IsGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
364 void SALOMEDS_StudyBuilder::NewCommand()
367 SALOMEDS::Locker lock;
368 _local_impl->NewCommand();
370 else _corba_impl->NewCommand();
373 void SALOMEDS_StudyBuilder::CommitCommand()
376 SALOMEDS::Locker lock;
378 _local_impl->CommitCommand();
381 throw SALOMEDS::StudyBuilder::LockProtection();
384 else _corba_impl->CommitCommand();
387 bool SALOMEDS_StudyBuilder::HasOpenCommand()
391 SALOMEDS::Locker lock;
392 ret = _local_impl->HasOpenCommand();
394 else ret = _corba_impl->HasOpenCommand();
398 void SALOMEDS_StudyBuilder::AbortCommand()
401 SALOMEDS::Locker lock;
402 _local_impl->AbortCommand();
404 else _corba_impl->AbortCommand();
407 void SALOMEDS_StudyBuilder::Undo()
410 SALOMEDS::Locker lock;
415 throw SALOMEDS::StudyBuilder::LockProtection();
418 else _corba_impl->Undo();
421 void SALOMEDS_StudyBuilder::Redo()
424 SALOMEDS::Locker lock;
429 throw SALOMEDS::StudyBuilder::LockProtection();
432 else _corba_impl->Redo();
435 bool SALOMEDS_StudyBuilder::GetAvailableUndos()
439 SALOMEDS::Locker lock;
440 ret = _local_impl->GetAvailableUndos();
442 else ret = _corba_impl->GetAvailableUndos();
446 bool SALOMEDS_StudyBuilder::GetAvailableRedos()
450 SALOMEDS::Locker lock;
451 ret = _local_impl->GetAvailableRedos();
453 else ret = _corba_impl->GetAvailableRedos();
457 int SALOMEDS_StudyBuilder::UndoLimit()
461 SALOMEDS::Locker lock;
462 aLimit = _local_impl->UndoLimit();
464 else aLimit = _corba_impl->UndoLimit();
468 void SALOMEDS_StudyBuilder::UndoLimit(int theLimit)
472 SALOMEDS::Locker lock;
474 _local_impl->UndoLimit(theLimit);
476 else _corba_impl->UndoLimit(theLimit);
479 void SALOMEDS_StudyBuilder::CheckLocked()
481 //There is only local part as CORBA part throws the correct exception
483 SALOMEDS::Locker lock;
485 _local_impl->CheckLocked();
488 throw SALOMEDS::StudyBuilder::LockProtection();
493 void SALOMEDS_StudyBuilder::SetName(const _PTR(SObject)& theSO, const std::string& theValue)
497 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
500 SALOMEDS::Locker lock;
502 _local_impl->SetName(*(aSO->GetLocalImpl()), theValue);
504 else _corba_impl->SetName(aSO->GetCORBAImpl(), (char*)theValue.c_str());
507 void SALOMEDS_StudyBuilder::SetComment(const _PTR(SObject)& theSO, const std::string& theValue)
511 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
514 SALOMEDS::Locker lock;
516 _local_impl->SetComment(*(aSO->GetLocalImpl()), theValue);
518 else _corba_impl->SetComment(aSO->GetCORBAImpl(), (char*)theValue.c_str());
521 void SALOMEDS_StudyBuilder::SetIOR(const _PTR(SObject)& theSO, const std::string& theValue)
525 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
528 SALOMEDS::Locker lock;
530 _local_impl->SetIOR(*(aSO->GetLocalImpl()), theValue);
532 else _corba_impl->SetIOR(aSO->GetCORBAImpl(), (char*)theValue.c_str());
535 SALOMEDS::StudyBuilder_ptr SALOMEDS_StudyBuilder::GetBuilder()
538 if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
539 SALOMEDS_StudyBuilder_i* servant = new SALOMEDS_StudyBuilder_i(_local_impl, _orb);
540 SALOMEDS::StudyBuilder_var aBuilder = servant->StudyBuilder::_this();
541 _corba_impl = SALOMEDS::StudyBuilder::_duplicate(aBuilder);
542 return aBuilder._retn();
545 return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
547 return SALOMEDS::StudyBuilder::_nil();
550 void SALOMEDS_StudyBuilder::init_orb()
552 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
553 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());