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