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