Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyBuilder_i.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SALOMEDS_StudyBuilder_i.cxx
21 //  Author : Seregy RUIN
22 //  Module : SALOME
23
24
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"
33
34 #include "SALOMEDSImpl_Study.hxx"
35 #include "SALOMEDSImpl_SObject.hxx"
36 #include "SALOMEDSImpl_SComponent.hxx"
37
38 #include "Utils_CorbaException.hxx"
39 #include "Utils_ExceptHandlers.hxx"
40
41 #include <DF_Attribute.hxx>
42 #include <DF_Document.hxx>
43 #include <stdlib.h> 
44
45 using namespace std;
46
47 UNEXPECT_CATCH(SBSalomeException, SALOME::SALOME_Exception);
48 UNEXPECT_CATCH(SBLockProtection, SALOMEDS::StudyBuilder::LockProtection);
49
50 //============================================================================
51 /*! Function : constructor
52  *  Purpose  :
53  */
54 //============================================================================
55 SALOMEDS_StudyBuilder_i::SALOMEDS_StudyBuilder_i(SALOMEDSImpl_StudyBuilder* theImpl, 
56                                                  CORBA::ORB_ptr orb) 
57 {
58   _orb = CORBA::ORB::_duplicate(orb);
59   _impl = theImpl;
60 }
61
62 //============================================================================
63 /*! Function : destructor
64  *  Purpose  :
65  */
66 //============================================================================
67 SALOMEDS_StudyBuilder_i::~SALOMEDS_StudyBuilder_i()
68 {}
69
70 //============================================================================
71 /*! Function : NewComponent
72  *  Purpose  : Create a new component (Scomponent)
73  */
74 //============================================================================
75 SALOMEDS::SComponent_ptr SALOMEDS_StudyBuilder_i::NewComponent(const char* DataType)
76 {
77   SALOMEDS::Locker lock;
78   CheckLocked();
79   //char* aDataType = CORBA::string_dup(DataType);
80   SALOMEDSImpl_SComponent aSCO = _impl->NewComponent(string(DataType));
81   //CORBA::free_string(aDataType);
82   if(aSCO.IsNull()) return SALOMEDS::SComponent::_nil();
83
84   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aSCO,_orb);
85   return sco._retn();
86 }
87
88 //============================================================================
89 /*! Function : DefineComponentInstance
90  *  Purpose  : Add IOR attribute of a Scomponent
91  */
92 //============================================================================
93 void SALOMEDS_StudyBuilder_i::DefineComponentInstance(SALOMEDS::SComponent_ptr aComponent,
94                                                       CORBA::Object_ptr IOR)
95 {
96   SALOMEDS::Locker lock;
97   CheckLocked();
98   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(aComponent->GetID());
99
100   CORBA::String_var iorstr = _orb->object_to_string(IOR);
101   _impl->DefineComponentInstance(aSCO, (char*)iorstr.in());
102 }
103
104 //============================================================================
105 /*! Function : RemoveComponent
106  *  Purpose  : Delete a Scomponent
107  */
108 //============================================================================
109 void SALOMEDS_StudyBuilder_i::RemoveComponent(SALOMEDS::SComponent_ptr aComponent)
110 {
111   SALOMEDS::Locker lock;
112   CheckLocked();
113   ASSERT(!CORBA::is_nil(aComponent));
114   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(aComponent->GetID());
115   _impl->RemoveComponent(aSCO);
116 }
117
118 //============================================================================
119 /*! Function : NewObject
120  *  Purpose  : Create a new SObject
121  */
122 //============================================================================
123 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObject(SALOMEDS::SObject_ptr theFatherObject)
124 {
125   SALOMEDS::Locker lock;
126   CheckLocked();
127   
128   SALOMEDSImpl_SObject aFO, aSO;
129   aFO = _impl->GetOwner()->GetSObject(theFatherObject->GetID());
130   aSO = _impl->NewObject(aFO);
131   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
132   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO,_orb);
133
134   return so._retn();
135 }
136
137 //============================================================================
138 /*! Function : NewObjectToTag
139  *  Purpose  :
140  */
141 //============================================================================
142 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObjectToTag(SALOMEDS::SObject_ptr theFatherObject,
143                                                               CORBA::Long atag)
144 {
145   SALOMEDS::Locker lock;
146   CheckLocked();
147   SALOMEDSImpl_SObject aFO, aSO;
148   aFO = _impl->GetOwner()->GetSObject(theFatherObject->GetID());
149   aSO = _impl->NewObjectToTag(aFO, atag);
150   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
151   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
152   return so._retn();
153 }
154
155 //============================================================================
156 /*! Function : RemoveObject
157  *  Purpose  :
158  */
159 //============================================================================
160 void SALOMEDS_StudyBuilder_i::RemoveObject(SALOMEDS::SObject_ptr anObject)
161 {
162   SALOMEDS::Locker lock;
163   CheckLocked();
164   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
165   _impl->RemoveObject(aSO);
166 }
167
168 //============================================================================
169 /*! Function : RemoveObjectWithChildren
170  *  Purpose  :
171  */
172 //============================================================================
173 void SALOMEDS_StudyBuilder_i::RemoveObjectWithChildren(SALOMEDS::SObject_ptr anObject)
174 {
175   SALOMEDS::Locker lock;
176   CheckLocked();
177   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
178   _impl->RemoveObjectWithChildren(aSO);
179 }
180
181 //============================================================================
182 /*! Function : LoadWith
183  *  Purpose  : 
184  */
185 //============================================================================
186 void SALOMEDS_StudyBuilder_i::LoadWith(SALOMEDS::SComponent_ptr anSCO, 
187                                        SALOMEDS::Driver_ptr aDriver) throw(SALOME::SALOME_Exception)
188 {
189   SALOMEDS::Locker lock;
190   Unexpect aCatch(SBSalomeException);
191
192   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(anSCO->GetID());
193   SALOMEDS_Driver_i* driver = new SALOMEDS_Driver_i(aDriver, _orb);
194   bool isDone = _impl->LoadWith(aSCO, driver); 
195   delete driver;
196
197   if(!isDone && _impl->IsError()) {
198     THROW_SALOME_CORBA_EXCEPTION(_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
199   }
200 }
201
202
203 //============================================================================
204 /*! Function : Load
205  *  Purpose  : 
206  */
207 //============================================================================
208 void SALOMEDS_StudyBuilder_i::Load(SALOMEDS::SObject_ptr sco)
209 {
210   MESSAGE ( "This function is not yet implemented");
211 }
212
213 //============================================================================
214 /*! Function : FindOrCreateAttribute
215  *  Purpose  : Add attribute of given type to SObject, if there is attribute of such type, returns
216  *  existing one
217  */
218 //============================================================================
219 SALOMEDS::GenericAttribute_ptr SALOMEDS_StudyBuilder_i::FindOrCreateAttribute(SALOMEDS::SObject_ptr anObject, 
220                                                                               const char* aTypeOfAttribute)
221 {
222   SALOMEDS::Locker lock;
223   CORBA::String_var anID = anObject->GetID();
224   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.inout());
225   DF_Attribute* anAttr;
226   try {
227      anAttr = _impl->FindOrCreateAttribute(aSO, string(aTypeOfAttribute));
228   }
229   catch (...) {
230     throw SALOMEDS::StudyBuilder::LockProtection();
231   }
232
233   SALOMEDS::GenericAttribute_var anAttribute;
234   
235   if(anAttr)     
236      anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
237
238   return anAttribute._retn();
239 }
240
241 //============================================================================
242 /*! Function : FindAttribute
243  *  Purpose  : Find attribute of given type assigned SObject, returns true if it is found
244  */
245 //============================================================================
246
247 CORBA::Boolean SALOMEDS_StudyBuilder_i::FindAttribute(SALOMEDS::SObject_ptr anObject, 
248                                                       SALOMEDS::GenericAttribute_out anAttribute, 
249                                                       const char* aTypeOfAttribute)
250 {
251   SALOMEDS::Locker lock;
252   ASSERT(!CORBA::is_nil(anObject));
253   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
254   DF_Attribute* anAttr;
255
256   if(!_impl->FindAttribute(aSO, anAttr, string(aTypeOfAttribute))) return false;
257     
258   anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
259   return true;  
260 }
261
262 //============================================================================
263 /*! Function : RemoveAttribute
264  *  Purpose  : Remove attribute of given type assigned SObject
265  */
266 //============================================================================
267
268 void SALOMEDS_StudyBuilder_i::RemoveAttribute(SALOMEDS::SObject_ptr anObject, 
269                                               const char* aTypeOfAttribute)
270 {
271   SALOMEDS::Locker lock;
272   CheckLocked();
273   ASSERT(!CORBA::is_nil(anObject));
274   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
275   _impl->RemoveAttribute(aSO, string(aTypeOfAttribute));
276 }
277
278 //============================================================================
279 /*! Function : Addreference
280  *  Purpose  : 
281  */
282 //============================================================================
283 void SALOMEDS_StudyBuilder_i::Addreference(SALOMEDS::SObject_ptr me, 
284                                            SALOMEDS::SObject_ptr theReferencedObject)
285 {
286   SALOMEDS::Locker lock;
287   CheckLocked();
288   ASSERT(!CORBA::is_nil(me));
289   ASSERT(!CORBA::is_nil(theReferencedObject));
290  
291   SALOMEDSImpl_SObject aSO, aRefSO;
292   aSO = _impl->GetOwner()->GetSObject(me->GetID());
293   aRefSO = _impl->GetOwner()->GetSObject(theReferencedObject->GetID());
294   _impl->Addreference(aSO, aRefSO);
295 }
296
297 //============================================================================
298 /*! Function : RemoveReference
299  *  Purpose  : 
300  */
301 //============================================================================
302 void SALOMEDS_StudyBuilder_i::RemoveReference(SALOMEDS::SObject_ptr me)
303 {
304   SALOMEDS::Locker lock;
305   CheckLocked();
306   ASSERT(!CORBA::is_nil(me));
307   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(me->GetID());
308   _impl->RemoveReference(aSO);
309 }
310
311
312 //============================================================================
313 /*! Function : AddDirectory
314  *  Purpose  : adds a new directory with a path = thePath
315  */
316 //============================================================================
317 void SALOMEDS_StudyBuilder_i::AddDirectory(const char* thePath) 
318 {
319   SALOMEDS::Locker lock;
320   CheckLocked();
321   if(thePath == NULL || strlen(thePath) == 0) throw SALOMEDS::Study::StudyInvalidDirectory();
322   if(!_impl->AddDirectory(string(thePath))) {
323     string anErrorCode = _impl->GetErrorCode();
324     if(anErrorCode == "StudyNameAlreadyUsed") throw SALOMEDS::Study::StudyNameAlreadyUsed(); 
325     if(anErrorCode == "StudyInvalidDirectory") throw SALOMEDS::Study::StudyInvalidDirectory(); 
326     if(anErrorCode == "StudyInvalidComponent") throw SALOMEDS::Study::StudyInvalidComponent();  
327   }
328 }
329
330
331 //============================================================================
332 /*! Function : SetGUID
333  *  Purpose  : 
334  */
335 //============================================================================
336 void SALOMEDS_StudyBuilder_i::SetGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
337 {
338   SALOMEDS::Locker lock;
339   CheckLocked();
340   ASSERT(!CORBA::is_nil(anObject));
341   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
342   _impl->SetGUID(aSO, string(theGUID));
343 }
344
345 //============================================================================
346 /*! Function : IsGUID
347  *  Purpose  : 
348  */
349 //============================================================================
350 bool SALOMEDS_StudyBuilder_i::IsGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
351 {
352   SALOMEDS::Locker lock;
353   ASSERT(!CORBA::is_nil(anObject));
354   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
355   return _impl->IsGUID(aSO, string(theGUID));
356 }
357
358
359 //============================================================================
360 /*! Function : NewCommand
361  *  Purpose  : 
362  */
363 //============================================================================
364 void SALOMEDS_StudyBuilder_i::NewCommand()
365 {
366   SALOMEDS::Locker lock;
367   _impl->NewCommand();
368 }
369
370 //============================================================================
371 /*! Function : CommitCommand
372  *  Purpose  : 
373  */
374 //============================================================================
375 void SALOMEDS_StudyBuilder_i::CommitCommand() throw (SALOMEDS::StudyBuilder::LockProtection)
376 {
377   SALOMEDS::Locker lock;
378   Unexpect aCatch(SBLockProtection);
379   try {
380     _impl->CommitCommand();
381   }
382   catch(...) {
383     MESSAGE("Locked document modification !!!");
384     throw SALOMEDS::StudyBuilder::LockProtection();
385   }
386 }
387
388 //============================================================================
389 /*! Function : HasOpenCommand
390  *  Purpose  : 
391  */
392 //============================================================================
393 CORBA::Boolean SALOMEDS_StudyBuilder_i::HasOpenCommand()
394 {
395   SALOMEDS::Locker lock;
396   return _impl->HasOpenCommand();
397 }
398
399 //============================================================================
400 /*! Function : AbortCommand
401  *  Purpose  : 
402  */
403 //============================================================================
404 void SALOMEDS_StudyBuilder_i::AbortCommand()
405 {
406   SALOMEDS::Locker lock;
407   _impl->AbortCommand();
408 }
409
410 //============================================================================
411 /*! Function : Undo
412  *  Purpose  : 
413  */
414 //============================================================================
415 void SALOMEDS_StudyBuilder_i::Undo() throw (SALOMEDS::StudyBuilder::LockProtection)
416 {
417   SALOMEDS::Locker lock;
418   Unexpect aCatch(SBLockProtection);
419   try {
420     _impl->Undo();
421   }
422   catch(...) {
423     MESSAGE("Locked document modification !!!");
424     throw SALOMEDS::StudyBuilder::LockProtection();
425   }
426 }
427
428 //============================================================================
429 /*! Function : Redo
430  *  Purpose  : 
431  */
432 //============================================================================
433 void SALOMEDS_StudyBuilder_i::Redo() throw (SALOMEDS::StudyBuilder::LockProtection)
434 {
435   SALOMEDS::Locker lock;
436   Unexpect aCatch(SBLockProtection);
437   try {
438     _impl->Redo();
439   }
440   catch(...) {
441     MESSAGE("Locked document modification !!!");
442     throw SALOMEDS::StudyBuilder::LockProtection();
443   }
444 }
445
446 //============================================================================
447 /*! Function : GetAvailableUndos
448  *  Purpose  : 
449  */
450 //============================================================================
451 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableUndos()
452 {
453   SALOMEDS::Locker lock;
454   return _impl->GetAvailableUndos();
455 }
456
457 //============================================================================
458 /*! Function : GetAvailableRedos
459  *  Purpose  : 
460  */
461 //============================================================================
462 CORBA::Boolean  SALOMEDS_StudyBuilder_i::GetAvailableRedos()
463 {
464   SALOMEDS::Locker lock;
465   return _impl->GetAvailableRedos();
466 }
467
468 //============================================================================
469 /*! Function : UndoLimit
470  *  Purpose  : 
471  */
472 //============================================================================
473 CORBA::Long  SALOMEDS_StudyBuilder_i::UndoLimit()
474 {
475   SALOMEDS::Locker lock;
476   return _impl->UndoLimit();
477 }
478
479 //============================================================================
480 /*! Function : UndoLimit
481  *  Purpose  : 
482  */
483 //============================================================================
484 void  SALOMEDS_StudyBuilder_i::UndoLimit(CORBA::Long n)
485 {
486   SALOMEDS::Locker lock;
487   CheckLocked();
488   _impl->UndoLimit(n);
489 }
490
491 //============================================================================
492 /*! Function : CheckLocked
493  *  Purpose  : 
494  */
495 //============================================================================
496 void SALOMEDS_StudyBuilder_i::CheckLocked() throw (SALOMEDS::StudyBuilder::LockProtection) 
497 {
498   SALOMEDS::Locker lock;
499   Unexpect aCatch(SBLockProtection);
500   try {
501     _impl->CheckLocked();
502   }
503   catch(...) {
504     throw SALOMEDS::StudyBuilder::LockProtection();
505   }
506 }
507
508 //============================================================================
509 /*! Function : SetName
510  *  Purpose  : 
511  */
512 //============================================================================
513 void SALOMEDS_StudyBuilder_i::SetName(SALOMEDS::SObject_ptr theSO, const char* theValue)
514      throw(SALOMEDS::StudyBuilder::LockProtection)
515 {
516   SALOMEDS::Locker lock;
517   Unexpect aCatch(SBLockProtection);
518   CheckLocked();
519  
520   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(theSO->GetID());  
521   _impl->SetName(aSO, string(theValue));
522 }
523
524 //============================================================================
525 /*! Function : SetComment
526  *  Purpose  : 
527  */
528 //============================================================================
529 void SALOMEDS_StudyBuilder_i::SetComment(SALOMEDS::SObject_ptr theSO, const char* theValue)
530      throw(SALOMEDS::StudyBuilder::LockProtection)
531 {
532   SALOMEDS::Locker lock;
533   Unexpect aCatch(SBLockProtection);
534   CheckLocked();
535
536   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(theSO->GetID());  
537   _impl->SetComment(aSO, string(theValue));
538 }
539
540 //============================================================================
541 /*! Function : SetIOR
542  *  Purpose  : 
543  */
544 //============================================================================
545 void SALOMEDS_StudyBuilder_i::SetIOR(SALOMEDS::SObject_ptr theSO, const char* theValue)
546  throw(SALOMEDS::StudyBuilder::LockProtection)
547 {
548   SALOMEDS::Locker lock;
549   Unexpect aCatch(SBLockProtection);
550   CheckLocked();
551
552   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(theSO->GetID());  
553   _impl->SetIOR(aSO, string(theValue));
554 }