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