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