1 // Copyright (C) 2007-2012 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.
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 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDSImpl_StudyBuilder* theBuilder)
56 _local_impl = theBuilder;
57 _corba_impl = SALOMEDS::StudyBuilder::_nil();
62 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDS::StudyBuilder_ptr theBuilder)
66 _corba_impl = SALOMEDS::StudyBuilder::_duplicate(theBuilder);
71 SALOMEDS_StudyBuilder::~SALOMEDS_StudyBuilder()
75 _PTR(SComponent) SALOMEDS_StudyBuilder::NewComponent(const std::string& ComponentDataType)
77 SALOMEDSClient_SComponent* aSCO = NULL;
81 SALOMEDS::Locker lock;
83 SALOMEDSImpl_SComponent aSCO_impl =_local_impl->NewComponent(ComponentDataType);
84 if(!aSCO_impl) return _PTR(SComponent)(aSCO);
85 aSCO = new SALOMEDS_SComponent(aSCO_impl);
88 SALOMEDS::SComponent_var aSCO_impl = _corba_impl->NewComponent((char*)ComponentDataType.c_str());
89 if(CORBA::is_nil(aSCO_impl)) return _PTR(SComponent)(aSCO);
90 aSCO = new SALOMEDS_SComponent(aSCO_impl);
93 return _PTR(SComponent)(aSCO);
96 void SALOMEDS_StudyBuilder::DefineComponentInstance (const _PTR(SComponent)& theSCO,
97 const std::string& ComponentIOR)
101 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
104 SALOMEDS::Locker lock;
106 _local_impl->DefineComponentInstance(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())),
110 CORBA::Object_var obj = _orb->string_to_object(ComponentIOR.c_str());
111 _corba_impl->DefineComponentInstance(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), obj);
115 void SALOMEDS_StudyBuilder::RemoveComponent(const _PTR(SComponent)& theSCO)
118 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
121 SALOMEDS::Locker lock;
123 _local_impl->RemoveComponent(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
125 else _corba_impl->RemoveComponent(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
128 _PTR(SObject) SALOMEDS_StudyBuilder::NewObject(const _PTR(SObject)& theFatherObject)
132 SALOMEDSClient_SObject* aSO = NULL;
133 SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
134 if (father == NULL) return _PTR(SObject)(aSO);
136 SALOMEDS::Locker lock;
138 SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObject(*(father->GetLocalImpl()));
139 if(!aSO_impl) return _PTR(SObject)(aSO);
140 aSO = new SALOMEDS_SObject(aSO_impl);
143 SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObject(father->GetCORBAImpl());
144 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
145 aSO = new SALOMEDS_SObject(aSO_impl);
148 return _PTR(SObject)(aSO);
151 _PTR(SObject) SALOMEDS_StudyBuilder::NewObjectToTag(const _PTR(SObject)& theFatherObject, int theTag)
155 SALOMEDSClient_SObject* aSO = NULL;
156 SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
157 if (father == NULL) return _PTR(SObject)(aSO);
159 SALOMEDS::Locker lock;
161 SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObjectToTag(*(father->GetLocalImpl()), theTag);
162 if(aSO_impl.IsNull()) return _PTR(SObject)(aSO);
163 aSO = new SALOMEDS_SObject(aSO_impl);
166 SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObjectToTag(father->GetCORBAImpl(), theTag);
167 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
168 aSO = new SALOMEDS_SObject(aSO_impl);
171 return _PTR(SObject)(aSO);
174 void SALOMEDS_StudyBuilder::AddDirectory(const std::string& thePath)
178 SALOMEDS::Locker lock;
180 _local_impl->AddDirectory((char*)thePath.c_str());
181 if (_local_impl->IsError()) {
182 std::string anErrorCode = _local_impl->GetErrorCode();
183 if (anErrorCode == "StudyNameAlreadyUsed") throw SALOMEDS::Study::StudyNameAlreadyUsed();
184 if (anErrorCode == "StudyInvalidDirectory") throw SALOMEDS::Study::StudyInvalidDirectory();
185 if (anErrorCode == "StudyInvalidComponent") throw SALOMEDS::Study::StudyInvalidComponent();
188 else _corba_impl->AddDirectory((char*)thePath.c_str());
191 void SALOMEDS_StudyBuilder::LoadWith(const _PTR(SComponent)& theSCO, const std::string& theIOR)
195 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
196 CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
197 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
198 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
201 SALOMEDS::Locker lock;
203 SALOMEDS_Driver_i* drv = new SALOMEDS_Driver_i(anEngine, _orb);
204 SALOMEDSImpl_SComponent aSCO_impl = *(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl()));
205 bool isDone = _local_impl->LoadWith(aSCO_impl, drv);
207 if(!isDone && _local_impl->IsError())
208 THROW_SALOME_CORBA_EXCEPTION(_local_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
211 _corba_impl->LoadWith(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), aDriver);
215 void SALOMEDS_StudyBuilder::Load(const _PTR(SObject)& theSCO)
217 SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
218 if (_isLocal) _local_impl->Load(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
219 else _corba_impl->Load(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
222 void SALOMEDS_StudyBuilder::RemoveObject(const _PTR(SObject)& theSO)
226 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
229 SALOMEDS::Locker lock;
231 _local_impl->RemoveObject(*(aSO->GetLocalImpl()));
233 else _corba_impl->RemoveObject(aSO->GetCORBAImpl());
236 void SALOMEDS_StudyBuilder::RemoveObjectWithChildren(const _PTR(SObject)& theSO)
240 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
243 SALOMEDS::Locker lock;
245 _local_impl->RemoveObjectWithChildren(*(aSO->GetLocalImpl()));
247 else _corba_impl->RemoveObjectWithChildren(aSO->GetCORBAImpl());
250 _PTR(GenericAttribute) SALOMEDS_StudyBuilder::FindOrCreateAttribute(const _PTR(SObject)& theSO,
251 const std::string& aTypeOfAttribute)
253 SALOMEDSClient_GenericAttribute* anAttr = NULL;
254 if(!theSO) return _PTR(GenericAttribute)(anAttr);
255 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
257 SALOMEDS::Locker lock;
259 SALOMEDSImpl_GenericAttribute* aGA;
261 aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>
262 (_local_impl->FindOrCreateAttribute(*(aSO->GetLocalImpl()), aTypeOfAttribute));
265 throw SALOMEDS::StudyBuilder::LockProtection();
267 anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
270 SALOMEDS::GenericAttribute_var aGA =
271 _corba_impl->FindOrCreateAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
272 anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
275 return _PTR(GenericAttribute)(anAttr);
278 bool SALOMEDS_StudyBuilder::FindAttribute(const _PTR(SObject)& theSO,
279 _PTR(GenericAttribute)& anAttribute,
280 const std::string& aTypeOfAttribute)
284 if(!theSO) return false;
286 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
288 SALOMEDS::Locker lock;
290 DF_Attribute* anAttr = NULL;
291 ret = _local_impl->FindAttribute(*(aSO->GetLocalImpl()), anAttr, aTypeOfAttribute);
293 SALOMEDSImpl_GenericAttribute* aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
294 anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
298 SALOMEDS::GenericAttribute_var aGA;
299 ret = _corba_impl->FindAttribute(aSO->GetCORBAImpl(), aGA.out(), (char*)aTypeOfAttribute.c_str());
300 if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
306 void SALOMEDS_StudyBuilder::RemoveAttribute(const _PTR(SObject)& theSO, const std::string& aTypeOfAttribute)
310 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
313 SALOMEDS::Locker lock;
315 _local_impl->RemoveAttribute(*(aSO->GetLocalImpl()), (char*)aTypeOfAttribute.c_str());
317 else _corba_impl->RemoveAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
320 void SALOMEDS_StudyBuilder::Addreference(const _PTR(SObject)& me, const _PTR(SObject)& thereferencedObject)
322 if(!me || !thereferencedObject) {
323 throw DFexception("Invalid arguments");
326 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
327 SALOMEDS_SObject* aRefSO = dynamic_cast<SALOMEDS_SObject*>(thereferencedObject.get());
330 SALOMEDS::Locker lock;
332 _local_impl->Addreference(*(aSO->GetLocalImpl()), *(aRefSO->GetLocalImpl()));
334 else _corba_impl->Addreference(aSO->GetCORBAImpl(), aRefSO->GetCORBAImpl());
337 void SALOMEDS_StudyBuilder::RemoveReference(const _PTR(SObject)& me)
340 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
343 SALOMEDS::Locker lock;
345 _local_impl->RemoveReference(*(aSO->GetLocalImpl()));
347 else _corba_impl->RemoveReference(aSO->GetCORBAImpl());
350 void SALOMEDS_StudyBuilder::SetGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
354 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
357 SALOMEDS::Locker lock;
359 _local_impl->SetGUID(*(aSO->GetLocalImpl()), theGUID);
361 else _corba_impl->SetGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
364 bool SALOMEDS_StudyBuilder::IsGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
366 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
369 SALOMEDS::Locker lock;
371 ret = _local_impl->IsGUID(*(aSO->GetLocalImpl()), (char*)theGUID.c_str());
373 else ret = _corba_impl->IsGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
378 void SALOMEDS_StudyBuilder::NewCommand()
381 SALOMEDS::Locker lock;
382 _local_impl->NewCommand();
384 else _corba_impl->NewCommand();
387 void SALOMEDS_StudyBuilder::CommitCommand()
390 SALOMEDS::Locker lock;
392 _local_impl->CommitCommand();
395 throw SALOMEDS::StudyBuilder::LockProtection();
398 else _corba_impl->CommitCommand();
401 bool SALOMEDS_StudyBuilder::HasOpenCommand()
405 SALOMEDS::Locker lock;
406 ret = _local_impl->HasOpenCommand();
408 else ret = _corba_impl->HasOpenCommand();
412 void SALOMEDS_StudyBuilder::AbortCommand()
415 SALOMEDS::Locker lock;
416 _local_impl->AbortCommand();
418 else _corba_impl->AbortCommand();
421 void SALOMEDS_StudyBuilder::Undo()
424 SALOMEDS::Locker lock;
429 throw SALOMEDS::StudyBuilder::LockProtection();
432 else _corba_impl->Undo();
435 void SALOMEDS_StudyBuilder::Redo()
438 SALOMEDS::Locker lock;
443 throw SALOMEDS::StudyBuilder::LockProtection();
446 else _corba_impl->Redo();
449 bool SALOMEDS_StudyBuilder::GetAvailableUndos()
453 SALOMEDS::Locker lock;
454 ret = _local_impl->GetAvailableUndos();
456 else ret = _corba_impl->GetAvailableUndos();
460 bool SALOMEDS_StudyBuilder::GetAvailableRedos()
464 SALOMEDS::Locker lock;
465 ret = _local_impl->GetAvailableRedos();
467 else ret = _corba_impl->GetAvailableRedos();
471 int SALOMEDS_StudyBuilder::UndoLimit()
475 SALOMEDS::Locker lock;
476 aLimit = _local_impl->UndoLimit();
478 else aLimit = _corba_impl->UndoLimit();
482 void SALOMEDS_StudyBuilder::UndoLimit(int theLimit)
486 SALOMEDS::Locker lock;
488 _local_impl->UndoLimit(theLimit);
490 else _corba_impl->UndoLimit(theLimit);
493 void SALOMEDS_StudyBuilder::CheckLocked()
495 //There is only local part as CORBA part throws the correct exeception
497 SALOMEDS::Locker lock;
499 _local_impl->CheckLocked();
502 throw SALOMEDS::StudyBuilder::LockProtection();
507 void SALOMEDS_StudyBuilder::SetName(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->SetName(*(aSO->GetLocalImpl()), theValue);
518 else _corba_impl->SetName(aSO->GetCORBAImpl(), (char*)theValue.c_str());
521 void SALOMEDS_StudyBuilder::SetComment(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->SetComment(*(aSO->GetLocalImpl()), theValue);
532 else _corba_impl->SetComment(aSO->GetCORBAImpl(), (char*)theValue.c_str());
535 void SALOMEDS_StudyBuilder::SetIOR(const _PTR(SObject)& theSO, const std::string& theValue)
539 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
542 SALOMEDS::Locker lock;
544 _local_impl->SetIOR(*(aSO->GetLocalImpl()), theValue);
546 else _corba_impl->SetIOR(aSO->GetCORBAImpl(), (char*)theValue.c_str());
549 SALOMEDS::StudyBuilder_ptr SALOMEDS_StudyBuilder::GetBuilder()
552 if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
553 SALOMEDS_StudyBuilder_i* servant = new SALOMEDS_StudyBuilder_i(_local_impl, _orb);
554 SALOMEDS::StudyBuilder_var aBuilder = servant->StudyBuilder::_this();
555 _corba_impl = SALOMEDS::StudyBuilder::_duplicate(aBuilder);
556 return aBuilder._retn();
559 return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
561 return SALOMEDS::StudyBuilder::_nil();
564 void SALOMEDS_StudyBuilder::init_orb()
566 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
567 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());