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