1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDS_StudyBuilder_i.cxx
21 // Author : Seregy RUIN
25 #include "utilities.h"
26 #include "SALOMEDS_StudyBuilder_i.hxx"
27 #include "SALOMEDS_Study_i.hxx"
28 #include "SALOMEDS_SObject_i.hxx"
29 #include "SALOMEDS_SComponent_i.hxx"
30 #include "SALOMEDS_GenericAttribute_i.hxx"
31 #include "SALOMEDS_Driver_i.hxx"
32 #include "SALOMEDS.hxx"
34 #include "SALOMEDSImpl_Study.hxx"
35 #include "SALOMEDSImpl_SObject.hxx"
36 #include "SALOMEDSImpl_SComponent.hxx"
38 #include "Utils_CorbaException.hxx"
39 #include "Utils_ExceptHandlers.hxx"
41 #include <TDF_Attribute.hxx>
46 UNEXPECT_CATCH(SBSalomeException, SALOME::SALOME_Exception);
47 UNEXPECT_CATCH(SBLockProtection, SALOMEDS::StudyBuilder::LockProtection);
49 //============================================================================
50 /*! Function : constructor
53 //============================================================================
54 SALOMEDS_StudyBuilder_i::SALOMEDS_StudyBuilder_i(const Handle(SALOMEDSImpl_StudyBuilder) theImpl,
57 _orb = CORBA::ORB::_duplicate(orb);
61 //============================================================================
62 /*! Function : destructor
65 //============================================================================
66 SALOMEDS_StudyBuilder_i::~SALOMEDS_StudyBuilder_i()
69 //============================================================================
70 /*! Function : NewComponent
71 * Purpose : Create a new component (Scomponent)
73 //============================================================================
74 SALOMEDS::SComponent_ptr SALOMEDS_StudyBuilder_i::NewComponent(const char* DataType)
76 SALOMEDS::Locker lock;
78 //char* aDataType = CORBA::string_dup(DataType);
79 Handle(SALOMEDSImpl_SComponent) aSCO = _impl->NewComponent(TCollection_AsciiString((char*)DataType));
80 //CORBA::free_string(aDataType);
81 if(aSCO.IsNull()) return SALOMEDS::SComponent::_nil();
83 SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aSCO,_orb);
87 //============================================================================
88 /*! Function : DefineComponentInstance
89 * Purpose : Add IOR attribute of a Scomponent
91 //============================================================================
92 void SALOMEDS_StudyBuilder_i::DefineComponentInstance(SALOMEDS::SComponent_ptr aComponent,
93 CORBA::Object_ptr IOR)
95 SALOMEDS::Locker lock;
97 Handle(SALOMEDSImpl_SComponent) aSCO;
98 aSCO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSComponent((char*)aComponent->GetID());
100 CORBA::String_var iorstr = _orb->object_to_string(IOR);
101 _impl->DefineComponentInstance(aSCO, (char*)iorstr);
104 //============================================================================
105 /*! Function : RemoveComponent
106 * Purpose : Delete a Scomponent
108 //============================================================================
109 void SALOMEDS_StudyBuilder_i::RemoveComponent(SALOMEDS::SComponent_ptr aComponent)
111 SALOMEDS::Locker lock;
113 ASSERT(!CORBA::is_nil(aComponent));
114 Handle(SALOMEDSImpl_SComponent) aSCO;
115 aSCO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSComponent((char*)aComponent->GetID());
116 _impl->RemoveComponent(aSCO);
119 //============================================================================
120 /*! Function : NewObject
121 * Purpose : Create a new SObject
123 //============================================================================
124 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObject(SALOMEDS::SObject_ptr theFatherObject)
126 SALOMEDS::Locker lock;
129 Handle(SALOMEDSImpl_SObject) aFO, aSO;
130 aFO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)theFatherObject->GetID());
131 aSO = _impl->NewObject(aFO);
132 if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
133 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO,_orb);
138 //============================================================================
139 /*! Function : NewObjectToTag
142 //============================================================================
143 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObjectToTag(SALOMEDS::SObject_ptr theFatherObject,
146 SALOMEDS::Locker lock;
148 Handle(SALOMEDSImpl_SObject) aFO, aSO;
149 aFO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)theFatherObject->GetID());
150 aSO = _impl->NewObjectToTag(aFO, atag);
151 if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
152 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
156 //============================================================================
157 /*! Function : RemoveObject
160 //============================================================================
161 void SALOMEDS_StudyBuilder_i::RemoveObject(SALOMEDS::SObject_ptr anObject)
163 SALOMEDS::Locker lock;
165 Handle(SALOMEDSImpl_SObject) aSO;
166 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)anObject->GetID());
167 _impl->RemoveObject(aSO);
170 //============================================================================
171 /*! Function : RemoveObjectWithChildren
174 //============================================================================
175 void SALOMEDS_StudyBuilder_i::RemoveObjectWithChildren(SALOMEDS::SObject_ptr anObject)
177 SALOMEDS::Locker lock;
179 Handle(SALOMEDSImpl_SObject) aSO;
180 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)anObject->GetID());
181 _impl->RemoveObjectWithChildren(aSO);
184 //============================================================================
185 /*! Function : LoadWith
188 //============================================================================
189 void SALOMEDS_StudyBuilder_i::LoadWith(SALOMEDS::SComponent_ptr anSCO,
190 SALOMEDS::Driver_ptr aDriver) throw(SALOME::SALOME_Exception)
192 SALOMEDS::Locker lock;
193 Unexpect aCatch(SBSalomeException);
195 Handle(SALOMEDSImpl_SComponent) aSCO;
196 aSCO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSComponent((char*)anSCO->GetID());
197 SALOMEDS_Driver_i* driver = new SALOMEDS_Driver_i(aDriver, _orb);
198 bool isDone = _impl->LoadWith(aSCO, driver);
201 if(!isDone && _impl->IsError()) {
202 THROW_SALOME_CORBA_EXCEPTION(_impl->GetErrorCode().ToCString(),SALOME::BAD_PARAM);
207 //============================================================================
211 //============================================================================
212 void SALOMEDS_StudyBuilder_i::Load(SALOMEDS::SObject_ptr sco)
214 MESSAGE ( "This function is not yet implemented");
217 //============================================================================
218 /*! Function : FindOrCreateAttribute
219 * Purpose : Add attribute of given type to SObject, if there is attribute of such type, returns
222 //============================================================================
223 SALOMEDS::GenericAttribute_ptr SALOMEDS_StudyBuilder_i::FindOrCreateAttribute(SALOMEDS::SObject_ptr anObject,
224 const char* aTypeOfAttribute)
226 SALOMEDS::Locker lock;
227 Handle(SALOMEDSImpl_SObject) aSO;
228 CORBA::String_var anID = anObject->GetID();
229 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject(anID.inout());
230 Handle(TDF_Attribute) anAttr;
232 anAttr = _impl->FindOrCreateAttribute(aSO, TCollection_AsciiString((char*)aTypeOfAttribute));
235 throw SALOMEDS::StudyBuilder::LockProtection();
238 SALOMEDS::GenericAttribute_var anAttribute;
239 anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
241 return anAttribute._retn();
244 //============================================================================
245 /*! Function : FindAttribute
246 * Purpose : Find attribute of given type assigned SObject, returns Standard_True if it is found
248 //============================================================================
250 CORBA::Boolean SALOMEDS_StudyBuilder_i::FindAttribute(SALOMEDS::SObject_ptr anObject,
251 SALOMEDS::GenericAttribute_out anAttribute,
252 const char* aTypeOfAttribute)
254 SALOMEDS::Locker lock;
255 ASSERT(!CORBA::is_nil(anObject));
256 Handle(SALOMEDSImpl_SObject) aSO;
257 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)anObject->GetID());
258 Handle(TDF_Attribute) anAttr;
260 if(!_impl->FindAttribute(aSO, anAttr, TCollection_AsciiString((char*)aTypeOfAttribute))) return false;
262 anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
266 //============================================================================
267 /*! Function : RemoveAttribute
268 * Purpose : Remove attribute of given type assigned SObject
270 //============================================================================
272 void SALOMEDS_StudyBuilder_i::RemoveAttribute(SALOMEDS::SObject_ptr anObject,
273 const char* aTypeOfAttribute)
275 SALOMEDS::Locker lock;
277 ASSERT(!CORBA::is_nil(anObject));
278 Handle(SALOMEDSImpl_SObject) aSO;
279 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)anObject->GetID());
280 _impl->RemoveAttribute(aSO, TCollection_AsciiString((char*)aTypeOfAttribute));
283 //============================================================================
284 /*! Function : Addreference
287 //============================================================================
288 void SALOMEDS_StudyBuilder_i::Addreference(SALOMEDS::SObject_ptr me,
289 SALOMEDS::SObject_ptr theReferencedObject)
291 SALOMEDS::Locker lock;
293 ASSERT(!CORBA::is_nil(me));
294 ASSERT(!CORBA::is_nil(theReferencedObject));
296 Handle(SALOMEDSImpl_SObject) aSO, aRefSO;
297 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)me->GetID());
298 aRefSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)theReferencedObject->GetID());
299 _impl->Addreference(aSO, aRefSO);
302 //============================================================================
303 /*! Function : RemoveReference
306 //============================================================================
307 void SALOMEDS_StudyBuilder_i::RemoveReference(SALOMEDS::SObject_ptr me)
309 SALOMEDS::Locker lock;
311 ASSERT(!CORBA::is_nil(me));
312 Handle(SALOMEDSImpl_SObject) aSO;
313 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)me->GetID());
314 _impl->RemoveReference(aSO);
318 //============================================================================
319 /*! Function : AddDirectory
320 * Purpose : adds a new directory with a path = thePath
322 //============================================================================
323 void SALOMEDS_StudyBuilder_i::AddDirectory(const char* thePath)
325 SALOMEDS::Locker lock;
327 if(thePath == NULL || strlen(thePath) == 0) throw SALOMEDS::Study::StudyInvalidDirectory();
328 if(!_impl->AddDirectory(TCollection_AsciiString((char*)thePath))) {
329 TCollection_AsciiString anErrorCode = _impl->GetErrorCode();
330 if(anErrorCode == "StudyNameAlreadyUsed") throw SALOMEDS::Study::StudyNameAlreadyUsed();
331 if(anErrorCode == "StudyInvalidDirectory") throw SALOMEDS::Study::StudyInvalidDirectory();
332 if(anErrorCode == "StudyInvalidComponent") throw SALOMEDS::Study::StudyInvalidComponent();
337 //============================================================================
338 /*! Function : SetGUID
341 //============================================================================
342 void SALOMEDS_StudyBuilder_i::SetGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
344 SALOMEDS::Locker lock;
346 ASSERT(!CORBA::is_nil(anObject));
347 Handle(SALOMEDSImpl_SObject) aSO;
348 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)anObject->GetID());
349 _impl->SetGUID(aSO, TCollection_AsciiString((char*)theGUID));
352 //============================================================================
353 /*! Function : IsGUID
356 //============================================================================
357 bool SALOMEDS_StudyBuilder_i::IsGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
359 SALOMEDS::Locker lock;
360 ASSERT(!CORBA::is_nil(anObject));
361 Handle(SALOMEDSImpl_SObject) aSO;
362 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)anObject->GetID());
363 return _impl->IsGUID(aSO, TCollection_AsciiString((char*)theGUID));
367 //============================================================================
368 /*! Function : NewCommand
371 //============================================================================
372 void SALOMEDS_StudyBuilder_i::NewCommand()
374 SALOMEDS::Locker lock;
378 //============================================================================
379 /*! Function : CommitCommand
382 //============================================================================
383 void SALOMEDS_StudyBuilder_i::CommitCommand() throw (SALOMEDS::StudyBuilder::LockProtection)
385 SALOMEDS::Locker lock;
386 Unexpect aCatch(SBLockProtection);
388 _impl->CommitCommand();
391 MESSAGE("Locked document modification !!!");
392 throw SALOMEDS::StudyBuilder::LockProtection();
396 //============================================================================
397 /*! Function : HasOpenCommand
400 //============================================================================
401 CORBA::Boolean SALOMEDS_StudyBuilder_i::HasOpenCommand()
403 SALOMEDS::Locker lock;
404 return _impl->HasOpenCommand();
407 //============================================================================
408 /*! Function : AbortCommand
411 //============================================================================
412 void SALOMEDS_StudyBuilder_i::AbortCommand()
414 SALOMEDS::Locker lock;
415 _impl->AbortCommand();
418 //============================================================================
422 //============================================================================
423 void SALOMEDS_StudyBuilder_i::Undo() throw (SALOMEDS::StudyBuilder::LockProtection)
425 SALOMEDS::Locker lock;
426 Unexpect aCatch(SBLockProtection);
431 MESSAGE("Locked document modification !!!");
432 throw SALOMEDS::StudyBuilder::LockProtection();
436 //============================================================================
440 //============================================================================
441 void SALOMEDS_StudyBuilder_i::Redo() throw (SALOMEDS::StudyBuilder::LockProtection)
443 SALOMEDS::Locker lock;
444 Unexpect aCatch(SBLockProtection);
449 MESSAGE("Locked document modification !!!");
450 throw SALOMEDS::StudyBuilder::LockProtection();
454 //============================================================================
455 /*! Function : GetAvailableUndos
458 //============================================================================
459 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableUndos()
461 SALOMEDS::Locker lock;
462 return _impl->GetAvailableUndos();
465 //============================================================================
466 /*! Function : GetAvailableRedos
469 //============================================================================
470 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableRedos()
472 SALOMEDS::Locker lock;
473 return _impl->GetAvailableRedos();
476 //============================================================================
477 /*! Function : UndoLimit
480 //============================================================================
481 CORBA::Long SALOMEDS_StudyBuilder_i::UndoLimit()
483 SALOMEDS::Locker lock;
484 return _impl->UndoLimit();
487 //============================================================================
488 /*! Function : UndoLimit
491 //============================================================================
492 void SALOMEDS_StudyBuilder_i::UndoLimit(CORBA::Long n)
494 SALOMEDS::Locker lock;
499 //============================================================================
500 /*! Function : CheckLocked
503 //============================================================================
504 void SALOMEDS_StudyBuilder_i::CheckLocked() throw (SALOMEDS::StudyBuilder::LockProtection)
506 SALOMEDS::Locker lock;
507 Unexpect aCatch(SBLockProtection);
509 _impl->CheckLocked();
512 throw SALOMEDS::StudyBuilder::LockProtection();
516 //============================================================================
517 /*! Function : SetName
520 //============================================================================
521 void SALOMEDS_StudyBuilder_i::SetName(SALOMEDS::SObject_ptr theSO, const char* theValue)
522 throw(SALOMEDS::StudyBuilder::LockProtection)
524 SALOMEDS::Locker lock;
525 Unexpect aCatch(SBLockProtection);
528 Handle(SALOMEDSImpl_SObject) aSO;
529 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)theSO->GetID());
530 _impl->SetName(aSO, TCollection_AsciiString((char*)theValue));
533 //============================================================================
534 /*! Function : SetComment
537 //============================================================================
538 void SALOMEDS_StudyBuilder_i::SetComment(SALOMEDS::SObject_ptr theSO, const char* theValue)
539 throw(SALOMEDS::StudyBuilder::LockProtection)
541 SALOMEDS::Locker lock;
542 Unexpect aCatch(SBLockProtection);
545 Handle(SALOMEDSImpl_SObject) aSO;
546 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)theSO->GetID());
547 _impl->SetComment(aSO, TCollection_AsciiString((char*)theValue));
550 //============================================================================
551 /*! Function : SetIOR
554 //============================================================================
555 void SALOMEDS_StudyBuilder_i::SetIOR(SALOMEDS::SObject_ptr theSO, const char* theValue)
556 throw(SALOMEDS::StudyBuilder::LockProtection)
558 SALOMEDS::Locker lock;
559 Unexpect aCatch(SBLockProtection);
562 Handle(SALOMEDSImpl_SObject) aSO;
563 aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)theSO->GetID());
564 _impl->SetIOR(aSO, TCollection_AsciiString((char*)theValue));