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