Salome HOME
This commit was generated by cvs2git to create branch 'WPdev'.
[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/
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 <TDF_Attribute.hxx>
42 #include <stdlib.h> 
43
44 using namespace std;
45
46 UNEXPECT_CATCH(SBSalomeException, SALOME::SALOME_Exception);
47 UNEXPECT_CATCH(SBLockProtection, SALOMEDS::StudyBuilder::LockProtection);
48
49 //============================================================================
50 /*! Function : constructor
51  *  Purpose  :
52  */
53 //============================================================================
54 SALOMEDS_StudyBuilder_i::SALOMEDS_StudyBuilder_i(const Handle(SALOMEDSImpl_StudyBuilder) theImpl, 
55                                                  CORBA::ORB_ptr orb) 
56 {
57   _orb = CORBA::ORB::_duplicate(orb);
58   _impl = theImpl;
59 }
60
61 //============================================================================
62 /*! Function : destructor
63  *  Purpose  :
64  */
65 //============================================================================
66 SALOMEDS_StudyBuilder_i::~SALOMEDS_StudyBuilder_i()
67 {}
68
69 //============================================================================
70 /*! Function : NewComponent
71  *  Purpose  : Create a new component (Scomponent)
72  */
73 //============================================================================
74 SALOMEDS::SComponent_ptr SALOMEDS_StudyBuilder_i::NewComponent(const char* DataType)
75 {
76   SALOMEDS::Locker lock;
77   CheckLocked();
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();
82
83   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aSCO,_orb);
84   return sco._retn();
85 }
86
87 //============================================================================
88 /*! Function : DefineComponentInstance
89  *  Purpose  : Add IOR attribute of a Scomponent
90  */
91 //============================================================================
92 void SALOMEDS_StudyBuilder_i::DefineComponentInstance(SALOMEDS::SComponent_ptr aComponent,
93                                                       CORBA::Object_ptr IOR)
94 {
95   SALOMEDS::Locker lock;
96   CheckLocked();
97   Handle(SALOMEDSImpl_SComponent) aSCO;
98   aSCO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSComponent((char*)aComponent->GetID());
99
100   CORBA::String_var iorstr = _orb->object_to_string(IOR);
101   _impl->DefineComponentInstance(aSCO, (char*)iorstr);
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   Handle(SALOMEDSImpl_SComponent) aSCO;
115   aSCO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSComponent((char*)aComponent->GetID());
116   _impl->RemoveComponent(aSCO);
117 }
118
119 //============================================================================
120 /*! Function : NewObject
121  *  Purpose  : Create a new SObject
122  */
123 //============================================================================
124 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObject(SALOMEDS::SObject_ptr theFatherObject)
125 {
126   SALOMEDS::Locker lock;
127   CheckLocked();
128   
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);
134
135   return so._retn();
136 }
137
138 //============================================================================
139 /*! Function : NewObjectToTag
140  *  Purpose  :
141  */
142 //============================================================================
143 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObjectToTag(SALOMEDS::SObject_ptr theFatherObject,
144                                                               CORBA::Long atag)
145 {
146   SALOMEDS::Locker lock;
147   CheckLocked();
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);
153   return so._retn();
154 }
155
156 //============================================================================
157 /*! Function : RemoveObject
158  *  Purpose  :
159  */
160 //============================================================================
161 void SALOMEDS_StudyBuilder_i::RemoveObject(SALOMEDS::SObject_ptr anObject)
162 {
163   SALOMEDS::Locker lock;
164   CheckLocked();
165   Handle(SALOMEDSImpl_SObject) aSO;
166   aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)anObject->GetID());
167   _impl->RemoveObject(aSO);
168 }
169
170 //============================================================================
171 /*! Function : RemoveObjectWithChildren
172  *  Purpose  :
173  */
174 //============================================================================
175 void SALOMEDS_StudyBuilder_i::RemoveObjectWithChildren(SALOMEDS::SObject_ptr anObject)
176 {
177   SALOMEDS::Locker lock;
178   CheckLocked();
179   Handle(SALOMEDSImpl_SObject) aSO;
180   aSO = Handle(SALOMEDSImpl_Study)::DownCast(_impl->GetOwner())->GetSObject((char*)anObject->GetID());
181   _impl->RemoveObjectWithChildren(aSO);
182 }
183
184 //============================================================================
185 /*! Function : LoadWith
186  *  Purpose  : 
187  */
188 //============================================================================
189 void SALOMEDS_StudyBuilder_i::LoadWith(SALOMEDS::SComponent_ptr anSCO, 
190                                        SALOMEDS::Driver_ptr aDriver) throw(SALOME::SALOME_Exception)
191 {
192   SALOMEDS::Locker lock;
193   Unexpect aCatch(SBSalomeException);
194
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); 
199   delete driver;
200
201   if(!isDone && _impl->IsError()) {
202     THROW_SALOME_CORBA_EXCEPTION(_impl->GetErrorCode().ToCString(),SALOME::BAD_PARAM);
203   }
204 }
205
206
207 //============================================================================
208 /*! Function : Load
209  *  Purpose  : 
210  */
211 //============================================================================
212 void SALOMEDS_StudyBuilder_i::Load(SALOMEDS::SObject_ptr sco)
213 {
214   MESSAGE ( "This function is not yet implemented");
215 }
216
217 //============================================================================
218 /*! Function : FindOrCreateAttribute
219  *  Purpose  : Add attribute of given type to SObject, if there is attribute of such type, returns
220  *  existing one
221  */
222 //============================================================================
223 SALOMEDS::GenericAttribute_ptr SALOMEDS_StudyBuilder_i::FindOrCreateAttribute(SALOMEDS::SObject_ptr anObject, 
224                                                                               const char* aTypeOfAttribute)
225 {
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;
231   try {
232      anAttr = _impl->FindOrCreateAttribute(aSO, TCollection_AsciiString((char*)aTypeOfAttribute));
233   }
234   catch (...) {
235     throw SALOMEDS::StudyBuilder::LockProtection();
236   }
237
238   SALOMEDS::GenericAttribute_var anAttribute;
239   anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
240
241   return anAttribute._retn();
242 }
243
244 //============================================================================
245 /*! Function : FindAttribute
246  *  Purpose  : Find attribute of given type assigned SObject, returns Standard_True if it is found
247  */
248 //============================================================================
249
250 CORBA::Boolean SALOMEDS_StudyBuilder_i::FindAttribute(SALOMEDS::SObject_ptr anObject, 
251                                                       SALOMEDS::GenericAttribute_out anAttribute, 
252                                                       const char* aTypeOfAttribute)
253 {
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;
259
260   if(!_impl->FindAttribute(aSO, anAttr, TCollection_AsciiString((char*)aTypeOfAttribute))) return false;
261     
262   anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
263   return true;  
264 }
265
266 //============================================================================
267 /*! Function : RemoveAttribute
268  *  Purpose  : Remove attribute of given type assigned SObject
269  */
270 //============================================================================
271
272 void SALOMEDS_StudyBuilder_i::RemoveAttribute(SALOMEDS::SObject_ptr anObject, 
273                                               const char* aTypeOfAttribute)
274 {
275   SALOMEDS::Locker lock;
276   CheckLocked();
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));
281 }
282
283 //============================================================================
284 /*! Function : Addreference
285  *  Purpose  : 
286  */
287 //============================================================================
288 void SALOMEDS_StudyBuilder_i::Addreference(SALOMEDS::SObject_ptr me, 
289                                            SALOMEDS::SObject_ptr theReferencedObject)
290 {
291   SALOMEDS::Locker lock;
292   CheckLocked();
293   ASSERT(!CORBA::is_nil(me));
294   ASSERT(!CORBA::is_nil(theReferencedObject));
295  
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);
300 }
301
302 //============================================================================
303 /*! Function : RemoveReference
304  *  Purpose  : 
305  */
306 //============================================================================
307 void SALOMEDS_StudyBuilder_i::RemoveReference(SALOMEDS::SObject_ptr me)
308 {
309   SALOMEDS::Locker lock;
310   CheckLocked();
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);
315 }
316
317
318 //============================================================================
319 /*! Function : AddDirectory
320  *  Purpose  : adds a new directory with a path = thePath
321  */
322 //============================================================================
323 void SALOMEDS_StudyBuilder_i::AddDirectory(const char* thePath) 
324 {
325   SALOMEDS::Locker lock;
326   CheckLocked();
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();  
333   }
334 }
335
336
337 //============================================================================
338 /*! Function : SetGUID
339  *  Purpose  : 
340  */
341 //============================================================================
342 void SALOMEDS_StudyBuilder_i::SetGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
343 {
344   SALOMEDS::Locker lock;
345   CheckLocked();
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));
350 }
351
352 //============================================================================
353 /*! Function : IsGUID
354  *  Purpose  : 
355  */
356 //============================================================================
357 bool SALOMEDS_StudyBuilder_i::IsGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
358 {
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));
364 }
365
366
367 //============================================================================
368 /*! Function : NewCommand
369  *  Purpose  : 
370  */
371 //============================================================================
372 void SALOMEDS_StudyBuilder_i::NewCommand()
373 {
374   SALOMEDS::Locker lock;
375   _impl->NewCommand();
376 }
377
378 //============================================================================
379 /*! Function : CommitCommand
380  *  Purpose  : 
381  */
382 //============================================================================
383 void SALOMEDS_StudyBuilder_i::CommitCommand() throw (SALOMEDS::StudyBuilder::LockProtection)
384 {
385   SALOMEDS::Locker lock;
386   Unexpect aCatch(SBLockProtection);
387   try {
388     _impl->CommitCommand();
389   }
390   catch(...) {
391     MESSAGE("Locked document modification !!!");
392     throw SALOMEDS::StudyBuilder::LockProtection();
393   }
394 }
395
396 //============================================================================
397 /*! Function : HasOpenCommand
398  *  Purpose  : 
399  */
400 //============================================================================
401 CORBA::Boolean SALOMEDS_StudyBuilder_i::HasOpenCommand()
402 {
403   SALOMEDS::Locker lock;
404   return _impl->HasOpenCommand();
405 }
406
407 //============================================================================
408 /*! Function : AbortCommand
409  *  Purpose  : 
410  */
411 //============================================================================
412 void SALOMEDS_StudyBuilder_i::AbortCommand()
413 {
414   SALOMEDS::Locker lock;
415   _impl->AbortCommand();
416 }
417
418 //============================================================================
419 /*! Function : Undo
420  *  Purpose  : 
421  */
422 //============================================================================
423 void SALOMEDS_StudyBuilder_i::Undo() throw (SALOMEDS::StudyBuilder::LockProtection)
424 {
425   SALOMEDS::Locker lock;
426   Unexpect aCatch(SBLockProtection);
427   try {
428     _impl->Undo();
429   }
430   catch(...) {
431     MESSAGE("Locked document modification !!!");
432     throw SALOMEDS::StudyBuilder::LockProtection();
433   }
434 }
435
436 //============================================================================
437 /*! Function : Redo
438  *  Purpose  : 
439  */
440 //============================================================================
441 void SALOMEDS_StudyBuilder_i::Redo() throw (SALOMEDS::StudyBuilder::LockProtection)
442 {
443   SALOMEDS::Locker lock;
444   Unexpect aCatch(SBLockProtection);
445   try {
446     _impl->Redo();
447   }
448   catch(...) {
449     MESSAGE("Locked document modification !!!");
450     throw SALOMEDS::StudyBuilder::LockProtection();
451   }
452 }
453
454 //============================================================================
455 /*! Function : GetAvailableUndos
456  *  Purpose  : 
457  */
458 //============================================================================
459 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableUndos()
460 {
461   SALOMEDS::Locker lock;
462   return _impl->GetAvailableUndos();
463 }
464
465 //============================================================================
466 /*! Function : GetAvailableRedos
467  *  Purpose  : 
468  */
469 //============================================================================
470 CORBA::Boolean  SALOMEDS_StudyBuilder_i::GetAvailableRedos()
471 {
472   SALOMEDS::Locker lock;
473   return _impl->GetAvailableRedos();
474 }
475
476 //============================================================================
477 /*! Function : UndoLimit
478  *  Purpose  : 
479  */
480 //============================================================================
481 CORBA::Long  SALOMEDS_StudyBuilder_i::UndoLimit()
482 {
483   SALOMEDS::Locker lock;
484   return _impl->UndoLimit();
485 }
486
487 //============================================================================
488 /*! Function : UndoLimit
489  *  Purpose  : 
490  */
491 //============================================================================
492 void  SALOMEDS_StudyBuilder_i::UndoLimit(CORBA::Long n)
493 {
494   SALOMEDS::Locker lock;
495   CheckLocked();
496   _impl->UndoLimit(n);
497 }
498
499 //============================================================================
500 /*! Function : CheckLocked
501  *  Purpose  : 
502  */
503 //============================================================================
504 void SALOMEDS_StudyBuilder_i::CheckLocked() throw (SALOMEDS::StudyBuilder::LockProtection) 
505 {
506   SALOMEDS::Locker lock;
507   Unexpect aCatch(SBLockProtection);
508   try {
509     _impl->CheckLocked();
510   }
511   catch(...) {
512     throw SALOMEDS::StudyBuilder::LockProtection();
513   }
514 }
515
516 //============================================================================
517 /*! Function : SetName
518  *  Purpose  : 
519  */
520 //============================================================================
521 void SALOMEDS_StudyBuilder_i::SetName(SALOMEDS::SObject_ptr theSO, const char* theValue)
522      throw(SALOMEDS::StudyBuilder::LockProtection)
523 {
524   SALOMEDS::Locker lock;
525   Unexpect aCatch(SBLockProtection);
526   CheckLocked();
527  
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));
531 }
532
533 //============================================================================
534 /*! Function : SetComment
535  *  Purpose  : 
536  */
537 //============================================================================
538 void SALOMEDS_StudyBuilder_i::SetComment(SALOMEDS::SObject_ptr theSO, const char* theValue)
539      throw(SALOMEDS::StudyBuilder::LockProtection)
540 {
541   SALOMEDS::Locker lock;
542   Unexpect aCatch(SBLockProtection);
543   CheckLocked();
544
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));
548 }
549
550 //============================================================================
551 /*! Function : SetIOR
552  *  Purpose  : 
553  */
554 //============================================================================
555 void SALOMEDS_StudyBuilder_i::SetIOR(SALOMEDS::SObject_ptr theSO, const char* theValue)
556  throw(SALOMEDS::StudyBuilder::LockProtection)
557 {
558   SALOMEDS::Locker lock;
559   Unexpect aCatch(SBLockProtection);
560   CheckLocked();
561
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));
565 }