Salome HOME
Merge branch 'master' into V9_dev
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyBuilder_i.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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
23 //  File   : SALOMEDS_StudyBuilder_i.cxx
24 //  Author : Seregy RUIN
25 //  Module : SALOME
26 //
27 #include "utilities.h"
28 #include "SALOMEDS_StudyBuilder_i.hxx"
29 #include "SALOMEDS_Study_i.hxx"
30 #include "SALOMEDS_SObject_i.hxx"
31 #include "SALOMEDS_SComponent_i.hxx"
32 #include "SALOMEDS_GenericAttribute_i.hxx"
33 #include "SALOMEDS_Driver_i.hxx"
34 #include "SALOMEDS.hxx"
35
36 #include "SALOMEDSImpl_Study.hxx"
37 #include "SALOMEDSImpl_SObject.hxx"
38 #include "SALOMEDSImpl_SComponent.hxx"
39
40 #include "Utils_CorbaException.hxx"
41 #include "Utils_ExceptHandlers.hxx"
42
43 #include <DF_Attribute.hxx>
44 #include <DF_Document.hxx>
45 #include <stdlib.h> 
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(std::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   CORBA::String_var anID=aComponent->GetID();
99   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(anID.in());
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   CORBA::String_var cid=aComponent->GetID();
116   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(cid.in());
117   _impl->RemoveComponent(aSCO);
118 }
119
120 //============================================================================
121 /*! Function : NewObject
122  *  Purpose  : Create a new SObject
123  */
124 //============================================================================
125 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObject(SALOMEDS::SObject_ptr theFatherObject)
126 {
127   SALOMEDS::Locker lock;
128   CheckLocked();
129   
130   SALOMEDSImpl_SObject aFO, aSO;
131   CORBA::String_var anID=theFatherObject->GetID();
132   aFO = _impl->GetOwner()->GetSObject(anID.in());
133   aSO = _impl->NewObject(aFO);
134   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
135   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO,_orb);
136
137   return so._retn();
138 }
139
140 //============================================================================
141 /*! Function : NewObjectToTag
142  *  Purpose  :
143  */
144 //============================================================================
145 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObjectToTag(SALOMEDS::SObject_ptr theFatherObject,
146                                                               CORBA::Long atag)
147 {
148   SALOMEDS::Locker lock;
149   CheckLocked();
150   SALOMEDSImpl_SObject aFO, aSO;
151   CORBA::String_var fatherEntry = theFatherObject->GetID();
152   aFO = _impl->GetOwner()->GetSObject( fatherEntry.in() );
153   aSO = _impl->NewObjectToTag(aFO, atag);
154   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
155   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
156   return so._retn();
157 }
158
159 //============================================================================
160 /*! Function : RemoveObject
161  *  Purpose  :
162  */
163 //============================================================================
164 void SALOMEDS_StudyBuilder_i::RemoveObject(SALOMEDS::SObject_ptr anObject)
165 {
166   SALOMEDS::Locker lock;
167   CheckLocked();
168   CORBA::String_var anID=anObject->GetID();
169   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());
170   _impl->RemoveObject(aSO);
171 }
172
173 //============================================================================
174 /*! Function : RemoveObjectWithChildren
175  *  Purpose  :
176  */
177 //============================================================================
178 void SALOMEDS_StudyBuilder_i::RemoveObjectWithChildren(SALOMEDS::SObject_ptr anObject)
179 {
180   SALOMEDS::Locker lock;
181   CheckLocked();
182   CORBA::String_var anID=anObject->GetID();
183   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());
184   _impl->RemoveObjectWithChildren(aSO);
185 }
186
187 //============================================================================
188 /*! Function : LoadWith
189  *  Purpose  : 
190  */
191 //============================================================================
192 void SALOMEDS_StudyBuilder_i::LoadWith(SALOMEDS::SComponent_ptr anSCO, 
193                                        SALOMEDS::Driver_ptr aDriver) throw(SALOME::SALOME_Exception)
194 {
195   SALOMEDS::Locker lock;
196   Unexpect aCatch(SBSalomeException);
197
198   CORBA::String_var anID=anSCO->GetID();
199   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(anID.in());
200   SALOMEDS_Driver_i* driver = new SALOMEDS_Driver_i(aDriver, _orb);
201   bool isDone = _impl->LoadWith(aSCO, driver); 
202   delete driver;
203
204   if(!isDone && _impl->IsError()) {
205     THROW_SALOME_CORBA_EXCEPTION(_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
206   }
207 }
208
209
210 //============================================================================
211 /*! Function : Load
212  *  Purpose  : 
213  */
214 //============================================================================
215 void SALOMEDS_StudyBuilder_i::Load(SALOMEDS::SObject_ptr sco)
216 {
217   MESSAGE ( "This function is not yet implemented");
218 }
219
220 //============================================================================
221 /*! Function : FindOrCreateAttribute
222  *  Purpose  : Add attribute of given type to SObject, if there is attribute of such type, returns
223  *  existing one
224  */
225 //============================================================================
226 SALOMEDS::GenericAttribute_ptr SALOMEDS_StudyBuilder_i::FindOrCreateAttribute(SALOMEDS::SObject_ptr anObject, 
227                                                                               const char* aTypeOfAttribute)
228 {
229   SALOMEDS::Locker lock;
230   CORBA::String_var anID = anObject->GetID();
231   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.inout());
232   DF_Attribute* anAttr;
233   try {
234      anAttr = _impl->FindOrCreateAttribute(aSO, std::string(aTypeOfAttribute));
235   }
236   catch (...) {
237     throw SALOMEDS::StudyBuilder::LockProtection();
238   }
239
240   SALOMEDS::GenericAttribute_var anAttribute;
241   
242   if(anAttr)     
243      anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb);
244
245   return anAttribute._retn();
246 }
247
248 //============================================================================
249 /*! Function : FindAttribute
250  *  Purpose  : Find attribute of given type assigned SObject, returns true if it is found
251  */
252 //============================================================================
253
254 CORBA::Boolean SALOMEDS_StudyBuilder_i::FindAttribute(SALOMEDS::SObject_ptr anObject, 
255                                                       SALOMEDS::GenericAttribute_out anAttribute, 
256                                                       const char* aTypeOfAttribute)
257 {
258   SALOMEDS::Locker lock;
259   ASSERT(!CORBA::is_nil(anObject));
260   CORBA::String_var anID = anObject->GetID();
261   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());
262   DF_Attribute* anAttr;
263
264   if(!_impl->FindAttribute(aSO, anAttr, std::string(aTypeOfAttribute))) return false;
265     
266   anAttribute = SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb);
267   return true;  
268 }
269
270 //============================================================================
271 /*! Function : RemoveAttribute
272  *  Purpose  : Remove attribute of given type assigned SObject
273  */
274 //============================================================================
275
276 void SALOMEDS_StudyBuilder_i::RemoveAttribute(SALOMEDS::SObject_ptr anObject, 
277                                               const char* aTypeOfAttribute)
278 {
279   SALOMEDS::Locker lock;
280   CheckLocked();
281   ASSERT(!CORBA::is_nil(anObject));
282   CORBA::String_var anID = anObject->GetID();
283   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());
284   _impl->RemoveAttribute(aSO, std::string(aTypeOfAttribute));
285 }
286
287 //============================================================================
288 /*! Function : Addreference
289  *  Purpose  : 
290  */
291 //============================================================================
292 void SALOMEDS_StudyBuilder_i::Addreference(SALOMEDS::SObject_ptr me, 
293                                            SALOMEDS::SObject_ptr theReferencedObject)
294 {
295   SALOMEDS::Locker lock;
296   CheckLocked();
297   ASSERT(!CORBA::is_nil(me));
298   ASSERT(!CORBA::is_nil(theReferencedObject));
299  
300   SALOMEDSImpl_SObject aSO, aRefSO;
301   CORBA::String_var anID = me->GetID();
302   aSO = _impl->GetOwner()->GetSObject(anID.in());
303   anID=theReferencedObject->GetID();
304   aRefSO = _impl->GetOwner()->GetSObject(anID.in());
305   _impl->Addreference(aSO, aRefSO);
306 }
307
308 //============================================================================
309 /*! Function : RemoveReference
310  *  Purpose  : 
311  */
312 //============================================================================
313 void SALOMEDS_StudyBuilder_i::RemoveReference(SALOMEDS::SObject_ptr me)
314 {
315   SALOMEDS::Locker lock;
316   CheckLocked();
317   ASSERT(!CORBA::is_nil(me));
318   CORBA::String_var anID = me->GetID();
319   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());
320   _impl->RemoveReference(aSO);
321 }
322
323 //============================================================================
324 /*! Function : SetGUID
325  *  Purpose  : 
326  */
327 //============================================================================
328 void SALOMEDS_StudyBuilder_i::SetGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
329 {
330   SALOMEDS::Locker lock;
331   CheckLocked();
332   ASSERT(!CORBA::is_nil(anObject));
333   CORBA::String_var anID=anObject->GetID();
334   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());
335   _impl->SetGUID(aSO, std::string(theGUID));
336 }
337
338 //============================================================================
339 /*! Function : IsGUID
340  *  Purpose  : 
341  */
342 //============================================================================
343 bool SALOMEDS_StudyBuilder_i::IsGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
344 {
345   SALOMEDS::Locker lock;
346   ASSERT(!CORBA::is_nil(anObject));
347   CORBA::String_var anID=anObject->GetID();
348   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());
349   return _impl->IsGUID(aSO, std::string(theGUID));
350 }
351
352
353 //============================================================================
354 /*! Function : NewCommand
355  *  Purpose  : 
356  */
357 //============================================================================
358 void SALOMEDS_StudyBuilder_i::NewCommand()
359 {
360   SALOMEDS::Locker lock;
361   _impl->NewCommand();
362 }
363
364 //============================================================================
365 /*! Function : CommitCommand
366  *  Purpose  : 
367  */
368 //============================================================================
369 void SALOMEDS_StudyBuilder_i::CommitCommand() throw (SALOMEDS::StudyBuilder::LockProtection)
370 {
371   SALOMEDS::Locker lock;
372   Unexpect aCatch(SBLockProtection);
373   try {
374     _impl->CommitCommand();
375   }
376   catch(...) {
377     MESSAGE("Locked document modification !!!");
378     throw SALOMEDS::StudyBuilder::LockProtection();
379   }
380 }
381
382 //============================================================================
383 /*! Function : HasOpenCommand
384  *  Purpose  : 
385  */
386 //============================================================================
387 CORBA::Boolean SALOMEDS_StudyBuilder_i::HasOpenCommand()
388 {
389   SALOMEDS::Locker lock;
390   return _impl->HasOpenCommand();
391 }
392
393 //============================================================================
394 /*! Function : AbortCommand
395  *  Purpose  : 
396  */
397 //============================================================================
398 void SALOMEDS_StudyBuilder_i::AbortCommand()
399 {
400   SALOMEDS::Locker lock;
401   _impl->AbortCommand();
402 }
403
404 //============================================================================
405 /*! Function : Undo
406  *  Purpose  : 
407  */
408 //============================================================================
409 void SALOMEDS_StudyBuilder_i::Undo() throw (SALOMEDS::StudyBuilder::LockProtection)
410 {
411   SALOMEDS::Locker lock;
412   Unexpect aCatch(SBLockProtection);
413   try {
414     _impl->Undo();
415   }
416   catch(...) {
417     MESSAGE("Locked document modification !!!");
418     throw SALOMEDS::StudyBuilder::LockProtection();
419   }
420 }
421
422 //============================================================================
423 /*! Function : Redo
424  *  Purpose  : 
425  */
426 //============================================================================
427 void SALOMEDS_StudyBuilder_i::Redo() throw (SALOMEDS::StudyBuilder::LockProtection)
428 {
429   SALOMEDS::Locker lock;
430   Unexpect aCatch(SBLockProtection);
431   try {
432     _impl->Redo();
433   }
434   catch(...) {
435     MESSAGE("Locked document modification !!!");
436     throw SALOMEDS::StudyBuilder::LockProtection();
437   }
438 }
439
440 //============================================================================
441 /*! Function : GetAvailableUndos
442  *  Purpose  : 
443  */
444 //============================================================================
445 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableUndos()
446 {
447   SALOMEDS::Locker lock;
448   return _impl->GetAvailableUndos();
449 }
450
451 //============================================================================
452 /*! Function : GetAvailableRedos
453  *  Purpose  : 
454  */
455 //============================================================================
456 CORBA::Boolean  SALOMEDS_StudyBuilder_i::GetAvailableRedos()
457 {
458   SALOMEDS::Locker lock;
459   return _impl->GetAvailableRedos();
460 }
461
462 //============================================================================
463 /*! Function : UndoLimit
464  *  Purpose  : 
465  */
466 //============================================================================
467 CORBA::Long  SALOMEDS_StudyBuilder_i::UndoLimit()
468 {
469   SALOMEDS::Locker lock;
470   return _impl->UndoLimit();
471 }
472
473 //============================================================================
474 /*! Function : UndoLimit
475  *  Purpose  : 
476  */
477 //============================================================================
478 void  SALOMEDS_StudyBuilder_i::UndoLimit(CORBA::Long n)
479 {
480   SALOMEDS::Locker lock;
481   CheckLocked();
482   _impl->UndoLimit(n);
483 }
484
485 //============================================================================
486 /*! Function : CheckLocked
487  *  Purpose  : 
488  */
489 //============================================================================
490 void SALOMEDS_StudyBuilder_i::CheckLocked() throw (SALOMEDS::StudyBuilder::LockProtection) 
491 {
492   SALOMEDS::Locker lock;
493   Unexpect aCatch(SBLockProtection);
494   try {
495     _impl->CheckLocked();
496   }
497   catch(...) {
498     throw SALOMEDS::StudyBuilder::LockProtection();
499   }
500 }
501
502 //============================================================================
503 /*! Function : SetName
504  *  Purpose  : 
505  */
506 //============================================================================
507 void SALOMEDS_StudyBuilder_i::SetName(SALOMEDS::SObject_ptr theSO, const char* theValue)
508      throw(SALOMEDS::StudyBuilder::LockProtection)
509 {
510   SALOMEDS::Locker lock;
511   Unexpect aCatch(SBLockProtection);
512   CheckLocked();
513  
514   CORBA::String_var anID=theSO->GetID();
515   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());  
516   _impl->SetName(aSO, std::string(theValue));
517 }
518
519 //============================================================================
520 /*! Function : SetComment
521  *  Purpose  : 
522  */
523 //============================================================================
524 void SALOMEDS_StudyBuilder_i::SetComment(SALOMEDS::SObject_ptr theSO, const char* theValue)
525      throw(SALOMEDS::StudyBuilder::LockProtection)
526 {
527   SALOMEDS::Locker lock;
528   Unexpect aCatch(SBLockProtection);
529   CheckLocked();
530
531   CORBA::String_var anID=theSO->GetID();
532   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());  
533   _impl->SetComment(aSO, std::string(theValue));
534 }
535
536 //============================================================================
537 /*! Function : SetIOR
538  *  Purpose  : 
539  */
540 //============================================================================
541 void SALOMEDS_StudyBuilder_i::SetIOR(SALOMEDS::SObject_ptr theSO, const char* theValue)
542  throw(SALOMEDS::StudyBuilder::LockProtection)
543 {
544   SALOMEDS::Locker lock;
545   Unexpect aCatch(SBLockProtection);
546   CheckLocked();
547
548   CORBA::String_var anID=theSO->GetID();
549   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.in());  
550   _impl->SetIOR(aSO, std::string(theValue));
551 }