Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyBuilder.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.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "utilities.h"
28
29 #include "SALOMEDS_StudyBuilder.hxx"
30
31 #include "SALOMEDS.hxx"
32 #include "SALOMEDS_SObject.hxx"
33 #include "SALOMEDS_SComponent.hxx"
34 #include "SALOMEDS_GenericAttribute.hxx"
35 #include "SALOMEDS_StudyBuilder_i.hxx"
36
37 #include "SALOMEDS_Driver_i.hxx"
38
39 #include "SALOMEDSImpl_SObject.hxx"
40 #include "SALOMEDSImpl_SComponent.hxx"
41 #include "SALOMEDSImpl_GenericAttribute.hxx"
42
43 #include <string>
44 #include <stdexcept>
45
46 #include "DF_Attribute.hxx"
47
48 #include "Utils_CorbaException.hxx"
49 #include "OpUtil.hxx"
50
51 pthread_mutex_t SALOMEDS_StudyBuilder::_remoteBuilderMutex;
52
53 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDSImpl_StudyBuilder* theBuilder)
54 {
55   _isLocal = true;
56   _local_impl = theBuilder;
57   _corba_impl = SALOMEDS::StudyBuilder::_nil();
58
59   init_orb();
60 }
61
62 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDS::StudyBuilder_ptr theBuilder)
63 {
64   pthread_mutex_lock( &_remoteBuilderMutex );
65   _isLocal = false;
66   _local_impl = NULL;
67   _corba_impl = SALOMEDS::StudyBuilder::_duplicate(theBuilder);
68
69   init_orb();
70 }
71
72 SALOMEDS_StudyBuilder::~SALOMEDS_StudyBuilder() 
73 {
74   if (!_isLocal) pthread_mutex_unlock( &_remoteBuilderMutex );
75 }
76
77 _PTR(SComponent) SALOMEDS_StudyBuilder::NewComponent(const std::string& ComponentDataType)
78 {
79   SALOMEDSClient_SComponent* aSCO = NULL;
80
81   if (_isLocal) {
82     CheckLocked();
83     SALOMEDS::Locker lock;
84
85     SALOMEDSImpl_SComponent aSCO_impl =_local_impl->NewComponent(ComponentDataType);
86     if(!aSCO_impl) return _PTR(SComponent)(aSCO);
87     aSCO = new SALOMEDS_SComponent(aSCO_impl);
88   }
89   else {
90     SALOMEDS::SComponent_var aSCO_impl = _corba_impl->NewComponent((char*)ComponentDataType.c_str());
91     if(CORBA::is_nil(aSCO_impl)) return _PTR(SComponent)(aSCO);
92     aSCO = new SALOMEDS_SComponent(aSCO_impl);
93   }
94
95   return _PTR(SComponent)(aSCO);
96 }
97
98 void SALOMEDS_StudyBuilder::DefineComponentInstance (const _PTR(SComponent)& theSCO, 
99                                                      const std::string& ComponentIOR)
100 {
101   if(!theSCO) return;
102
103   SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
104   if (_isLocal) {
105     CheckLocked();
106     SALOMEDS::Locker lock;
107
108     _local_impl->DefineComponentInstance(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())),
109                                          ComponentIOR);
110   }
111   else {
112     CORBA::Object_var obj = _orb->string_to_object(ComponentIOR.c_str());
113     _corba_impl->DefineComponentInstance(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), obj);
114   }
115 }
116
117 void SALOMEDS_StudyBuilder::RemoveComponent(const _PTR(SComponent)& theSCO)
118 {
119   if(!theSCO) return;
120   SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
121   if (_isLocal) {
122     CheckLocked();
123     SALOMEDS::Locker lock;
124
125     _local_impl->RemoveComponent(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
126   }
127   else _corba_impl->RemoveComponent(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
128 }
129
130 _PTR(SObject) SALOMEDS_StudyBuilder::NewObject(const _PTR(SObject)& theFatherObject)
131 {
132   CheckLocked();
133
134   SALOMEDSClient_SObject* aSO = NULL;
135   SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
136   if (father == NULL) return _PTR(SObject)(aSO);
137   if (_isLocal) {
138     SALOMEDS::Locker lock;
139
140     SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObject(*(father->GetLocalImpl()));
141     if(!aSO_impl) return _PTR(SObject)(aSO);
142     aSO = new SALOMEDS_SObject(aSO_impl);
143   }
144   else {
145     SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObject(father->GetCORBAImpl());
146     if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
147     aSO = new SALOMEDS_SObject(aSO_impl);
148   }
149
150   return _PTR(SObject)(aSO);
151 }
152
153 _PTR(SObject) SALOMEDS_StudyBuilder::NewObjectToTag(const _PTR(SObject)& theFatherObject, int theTag)
154 {  
155   CheckLocked();
156
157   SALOMEDSClient_SObject* aSO = NULL;
158   SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
159   if (father == NULL) return _PTR(SObject)(aSO);
160   if (_isLocal) {
161     SALOMEDS::Locker lock;
162
163     SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObjectToTag(*(father->GetLocalImpl()), theTag);
164     if(aSO_impl.IsNull()) return _PTR(SObject)(aSO);
165     aSO = new SALOMEDS_SObject(aSO_impl);
166   }
167   else {
168     SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObjectToTag(father->GetCORBAImpl(), theTag);
169     if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
170     aSO = new SALOMEDS_SObject(aSO_impl);
171   }
172
173   return _PTR(SObject)(aSO);
174 }
175
176 void SALOMEDS_StudyBuilder::LoadWith(const _PTR(SComponent)& theSCO, const std::string& theIOR)
177 {
178   if(!theSCO) return;
179
180   SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
181   CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
182   Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
183   SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
184   
185   if (_isLocal) {
186     SALOMEDS::Locker lock;
187
188     SALOMEDS_Driver_i* drv = new SALOMEDS_Driver_i(anEngine, _orb);    
189     SALOMEDSImpl_SComponent aSCO_impl = *(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl()));
190     bool isDone = _local_impl->LoadWith(aSCO_impl, drv);
191     delete drv;
192     if(!isDone && _local_impl->IsError()) 
193       THROW_SALOME_CORBA_EXCEPTION(_local_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
194   }
195   else {
196     _corba_impl->LoadWith(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), aDriver);
197   }
198 }
199
200 void SALOMEDS_StudyBuilder::Load(const _PTR(SObject)& theSCO)
201 {
202   SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
203   if (_isLocal) _local_impl->Load(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
204   else _corba_impl->Load(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
205 }
206
207 void SALOMEDS_StudyBuilder::RemoveObject(const _PTR(SObject)& theSO)
208 {
209   if(!theSO) return;
210
211   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
212   if (_isLocal) {
213     CheckLocked();
214     SALOMEDS::Locker lock;
215
216     _local_impl->RemoveObject(*(aSO->GetLocalImpl()));
217   }
218   else _corba_impl->RemoveObject(aSO->GetCORBAImpl());
219 }
220
221 void SALOMEDS_StudyBuilder::RemoveObjectWithChildren(const _PTR(SObject)& theSO)
222 {
223   if(!theSO) return;
224
225   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
226   if (_isLocal) {
227     CheckLocked();
228     SALOMEDS::Locker lock;
229
230     _local_impl->RemoveObjectWithChildren(*(aSO->GetLocalImpl()));
231   }
232   else _corba_impl->RemoveObjectWithChildren(aSO->GetCORBAImpl());
233 }
234
235 _PTR(GenericAttribute) SALOMEDS_StudyBuilder::FindOrCreateAttribute(const _PTR(SObject)& theSO, 
236                                                                     const std::string& aTypeOfAttribute)
237 {  
238   SALOMEDSClient_GenericAttribute* anAttr = NULL;
239   if(!theSO) return _PTR(GenericAttribute)(anAttr);
240   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
241   if (_isLocal) {
242     SALOMEDS::Locker lock;
243
244     SALOMEDSImpl_GenericAttribute* aGA;
245     try {
246       aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>
247         (_local_impl->FindOrCreateAttribute(*(aSO->GetLocalImpl()), aTypeOfAttribute));
248     }
249     catch (...) {
250       throw SALOMEDS::StudyBuilder::LockProtection();
251     }
252     anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
253   }
254   else {
255     SALOMEDS::GenericAttribute_var aGA =
256       _corba_impl->FindOrCreateAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
257     anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
258   }
259
260   return _PTR(GenericAttribute)(anAttr);
261 }
262
263 bool SALOMEDS_StudyBuilder::FindAttribute(const _PTR(SObject)& theSO, 
264                                           _PTR(GenericAttribute)& anAttribute, 
265                                           const std::string& aTypeOfAttribute)
266 {
267   bool ret;
268
269   if(!theSO) return false;
270
271   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
272   if (_isLocal) {
273     SALOMEDS::Locker lock;
274
275     DF_Attribute* anAttr = NULL;
276     ret = _local_impl->FindAttribute(*(aSO->GetLocalImpl()), anAttr, aTypeOfAttribute);
277     if(ret) {
278       SALOMEDSImpl_GenericAttribute* aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
279       anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
280     }
281   }
282   else {
283     SALOMEDS::GenericAttribute_var aGA;
284     ret = _corba_impl->FindAttribute(aSO->GetCORBAImpl(), aGA.out(), (char*)aTypeOfAttribute.c_str()); 
285     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
286   }
287
288   return ret;
289 }
290
291 void SALOMEDS_StudyBuilder::RemoveAttribute(const _PTR(SObject)& theSO, const std::string& aTypeOfAttribute)
292 {
293   if(!theSO) return;
294
295   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
296   if (_isLocal) {
297     CheckLocked();
298     SALOMEDS::Locker lock;
299
300     _local_impl->RemoveAttribute(*(aSO->GetLocalImpl()), (char*)aTypeOfAttribute.c_str());
301   }
302   else _corba_impl->RemoveAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
303 }
304
305 void SALOMEDS_StudyBuilder::Addreference(const _PTR(SObject)& me, const _PTR(SObject)& thereferencedObject)
306 {
307   if(!me || !thereferencedObject) {
308     throw DFexception("Invalid arguments");
309   }
310
311   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
312   SALOMEDS_SObject* aRefSO = dynamic_cast<SALOMEDS_SObject*>(thereferencedObject.get());
313   if (_isLocal) {
314     CheckLocked();
315     SALOMEDS::Locker lock;
316
317     _local_impl->Addreference(*(aSO->GetLocalImpl()), *(aRefSO->GetLocalImpl()));
318   }
319   else _corba_impl->Addreference(aSO->GetCORBAImpl(), aRefSO->GetCORBAImpl());
320 }
321
322 void SALOMEDS_StudyBuilder::RemoveReference(const _PTR(SObject)& me)
323 {
324   if(!me) return;
325   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
326   if (_isLocal) {
327     CheckLocked();
328     SALOMEDS::Locker lock;
329
330     _local_impl->RemoveReference(*(aSO->GetLocalImpl()));
331   }
332   else _corba_impl->RemoveReference(aSO->GetCORBAImpl());
333 }
334
335 void SALOMEDS_StudyBuilder::SetGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
336 {
337   if(!theSO) return;
338
339   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
340   if (_isLocal) {
341     CheckLocked();
342     SALOMEDS::Locker lock;
343
344     _local_impl->SetGUID(*(aSO->GetLocalImpl()), theGUID);
345   }
346   else _corba_impl->SetGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
347 }
348  
349 bool SALOMEDS_StudyBuilder::IsGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
350 {
351   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
352   bool ret;
353   if (_isLocal) {
354     SALOMEDS::Locker lock;
355
356     ret = _local_impl->IsGUID(*(aSO->GetLocalImpl()), (char*)theGUID.c_str());
357   }
358   else ret = _corba_impl->IsGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
359
360   return ret;
361 }
362
363 void SALOMEDS_StudyBuilder::NewCommand()
364 {
365   if (_isLocal) {
366     SALOMEDS::Locker lock;
367     _local_impl->NewCommand();
368   }
369   else _corba_impl->NewCommand();
370 }
371  
372 void SALOMEDS_StudyBuilder::CommitCommand()
373 {
374   if (_isLocal) {
375     SALOMEDS::Locker lock;
376     try {
377       _local_impl->CommitCommand();
378     }
379     catch(...) {
380       throw SALOMEDS::StudyBuilder::LockProtection();
381     }
382   }
383   else _corba_impl->CommitCommand();
384 }
385
386 bool SALOMEDS_StudyBuilder::HasOpenCommand()
387 {
388   bool ret;
389   if (_isLocal) {
390     SALOMEDS::Locker lock;
391     ret = _local_impl->HasOpenCommand();
392   }
393   else ret = _corba_impl->HasOpenCommand();
394   return ret;
395 }
396
397 void SALOMEDS_StudyBuilder::AbortCommand()
398 {
399   if (_isLocal) {
400     SALOMEDS::Locker lock;
401     _local_impl->AbortCommand();
402   }
403   else _corba_impl->AbortCommand();
404 }
405
406 void SALOMEDS_StudyBuilder::Undo()
407 {
408   if (_isLocal) {
409     SALOMEDS::Locker lock;
410     try {
411       _local_impl->Undo();
412     }
413     catch(...) {
414       throw SALOMEDS::StudyBuilder::LockProtection();
415     }
416   }
417   else _corba_impl->Undo();
418 }
419
420 void SALOMEDS_StudyBuilder::Redo()
421 {
422   if (_isLocal) {
423     SALOMEDS::Locker lock;
424     try {
425       _local_impl->Redo();
426     }
427     catch(...) {
428       throw SALOMEDS::StudyBuilder::LockProtection();
429     }
430   }
431   else _corba_impl->Redo(); 
432 }
433
434 bool SALOMEDS_StudyBuilder::GetAvailableUndos()
435 {
436   bool ret;
437   if (_isLocal) {
438     SALOMEDS::Locker lock;
439     ret = _local_impl->GetAvailableUndos();
440   }
441   else ret = _corba_impl->GetAvailableUndos();
442   return ret;
443 }
444
445 bool SALOMEDS_StudyBuilder::GetAvailableRedos()
446 {
447   bool ret;
448   if (_isLocal) {
449     SALOMEDS::Locker lock;
450     ret = _local_impl->GetAvailableRedos();
451   }
452   else ret = _corba_impl->GetAvailableRedos();
453   return ret; 
454 }
455
456 int SALOMEDS_StudyBuilder::UndoLimit()
457 {
458   int aLimit;
459   if (_isLocal) {
460     SALOMEDS::Locker lock;
461     aLimit = _local_impl->UndoLimit();
462   }
463   else aLimit = _corba_impl->UndoLimit();
464   return aLimit;
465 }
466  
467 void SALOMEDS_StudyBuilder::UndoLimit(int theLimit)
468 {
469   if (_isLocal) {
470     CheckLocked();
471     SALOMEDS::Locker lock;
472
473     _local_impl->UndoLimit(theLimit);
474   }
475   else _corba_impl->UndoLimit(theLimit);
476 }
477  
478 void SALOMEDS_StudyBuilder::CheckLocked()
479 {
480   //There is only local part as CORBA part throws the correct exception
481   if (_isLocal) {
482     SALOMEDS::Locker lock;
483     try {
484       _local_impl->CheckLocked();
485     }
486     catch(...) {
487       throw SALOMEDS::StudyBuilder::LockProtection();
488     }
489   }
490 }
491
492 void SALOMEDS_StudyBuilder::SetName(const _PTR(SObject)& theSO, const std::string& theValue)
493 {
494   if(!theSO) return;
495
496   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
497   if (_isLocal) {
498     CheckLocked();
499     SALOMEDS::Locker lock;
500
501     _local_impl->SetName(*(aSO->GetLocalImpl()), theValue);
502   }
503   else _corba_impl->SetName(aSO->GetCORBAImpl(), (char*)theValue.c_str());
504 }
505
506 void SALOMEDS_StudyBuilder::SetComment(const _PTR(SObject)& theSO, const std::string& theValue)
507 {
508   if(!theSO) return;
509
510   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
511   if (_isLocal) {
512     CheckLocked();
513     SALOMEDS::Locker lock;
514
515     _local_impl->SetComment(*(aSO->GetLocalImpl()), theValue);
516   }
517   else _corba_impl->SetComment(aSO->GetCORBAImpl(), (char*)theValue.c_str());
518 }
519
520 void SALOMEDS_StudyBuilder::SetIOR(const _PTR(SObject)& theSO, const std::string& theValue)
521 {
522   if(!theSO) return;
523
524   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
525   if (_isLocal) {
526     CheckLocked();
527     SALOMEDS::Locker lock;
528
529     _local_impl->SetIOR(*(aSO->GetLocalImpl()), theValue);
530   }
531   else _corba_impl->SetIOR(aSO->GetCORBAImpl(), (char*)theValue.c_str());
532 }
533
534 SALOMEDS::StudyBuilder_ptr SALOMEDS_StudyBuilder::GetBuilder()
535 {
536   if(_isLocal) {
537     if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
538     SALOMEDS_StudyBuilder_i* servant = new SALOMEDS_StudyBuilder_i(_local_impl, _orb);
539     SALOMEDS::StudyBuilder_var aBuilder = servant->StudyBuilder::_this();
540     _corba_impl = SALOMEDS::StudyBuilder::_duplicate(aBuilder);
541     return aBuilder._retn();
542   }
543   else {
544     return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
545   }
546   return SALOMEDS::StudyBuilder::_nil();
547 }
548
549 void SALOMEDS_StudyBuilder::init_orb()
550 {
551   _orb = KERNEL::GetRefToORB();  
552 }