1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/
20 // File : SALOMEDS_Study.cxx
21 // Author : Sergey RUIN
26 #include "utilities.h"
28 #include "SALOMEDS_Study.hxx"
30 #include "SALOMEDS.hxx"
31 #include "SALOMEDS_SComponent.hxx"
32 #include "SALOMEDS_SObject.hxx"
33 #include "SALOMEDS_StudyBuilder.hxx"
34 #include "SALOMEDS_ChildIterator.hxx"
35 #include "SALOMEDS_SComponentIterator.hxx"
36 #include "SALOMEDS_AttributeStudyProperties.hxx"
37 #include "SALOMEDS_UseCaseBuilder.hxx"
39 #include "SALOMEDSImpl_SComponent.hxx"
40 #include "SALOMEDSImpl_SObject.hxx"
41 #include "SALOMEDSImpl_StudyBuilder.hxx"
42 #include "SALOMEDSImpl_ChildIterator.hxx"
43 #include "SALOMEDSImpl_SComponentIterator.hxx"
44 #include "SALOMEDSImpl_AttributeStudyProperties.hxx"
45 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
47 #include "SALOMEDS_Driver_i.hxx"
48 #include "SALOMEDS_Study_i.hxx"
50 #include <TCollection_AsciiString.hxx>
51 #include <TColStd_HSequenceOfAsciiString.hxx>
52 #include <TColStd_HSequenceOfTransient.hxx>
54 #include "Utils_ORB_INIT.hxx"
55 #include "Utils_SINGLETON.hxx"
60 #include <sys/types.h>
68 SALOMEDS_Study::SALOMEDS_Study(const Handle(SALOMEDSImpl_Study)& theStudy)
71 _local_impl = theStudy;
72 _corba_impl = SALOMEDS::Study::_nil();
76 SALOMEDS_Study::SALOMEDS_Study(SALOMEDS::Study_ptr theStudy)
79 long pid = (long)_getpid();
81 long pid = (long)getpid();
84 long addr = theStudy->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
86 _local_impl = ((SALOMEDSImpl_Study*)(addr));
87 _corba_impl = SALOMEDS::Study::_duplicate(theStudy);
91 _corba_impl = SALOMEDS::Study::_duplicate(theStudy);
97 SALOMEDS_Study::~SALOMEDS_Study()
101 std::string SALOMEDS_Study::GetPersistentReference()
105 SALOMEDS::Locker lock;
106 aRef = _local_impl->GetPersistentReference().ToCString();
108 else aRef = _corba_impl->GetPersistentReference();
112 std::string SALOMEDS_Study::GetTransientReference()
116 SALOMEDS::Locker lock;
117 aRef = _local_impl->GetTransientReference().ToCString();
119 else aRef = _corba_impl->GetTransientReference();
123 bool SALOMEDS_Study::IsEmpty()
127 SALOMEDS::Locker lock;
128 ret = _local_impl->IsEmpty();
130 else ret = _corba_impl->IsEmpty();
134 _PTR(SComponent) SALOMEDS_Study::FindComponent (const std::string& aComponentName)
136 SALOMEDSClient_SComponent* aSCO = NULL;
138 SALOMEDS::Locker lock;
140 Handle(SALOMEDSImpl_SComponent) aSCO_impl =
141 _local_impl->FindComponent((char*)aComponentName.c_str());
142 if (aSCO_impl.IsNull()) return _PTR(SComponent)(aSCO);
143 aSCO = new SALOMEDS_SComponent(aSCO_impl);
146 SALOMEDS::SComponent_var aSCO_impl = _corba_impl->FindComponent((char*)aComponentName.c_str());
147 if (CORBA::is_nil(aSCO_impl)) return _PTR(SComponent)(aSCO);
148 aSCO = new SALOMEDS_SComponent(aSCO_impl);
150 return _PTR(SComponent)(aSCO);
153 _PTR(SComponent) SALOMEDS_Study::FindComponentID(const std::string& aComponentID)
155 SALOMEDSClient_SComponent* aSCO = NULL;
157 SALOMEDS::Locker lock;
159 Handle(SALOMEDSImpl_SComponent) aSCO_impl =
160 _local_impl->FindComponentID((char*)aComponentID.c_str());
161 if (aSCO_impl.IsNull()) return _PTR(SComponent)(aSCO);
162 aSCO = new SALOMEDS_SComponent(aSCO_impl);
165 SALOMEDS::SComponent_var aSCO_impl = _corba_impl->FindComponentID((char*)aComponentID.c_str());
166 if(CORBA::is_nil(aSCO_impl)) return _PTR(SComponent)(aSCO);
167 aSCO = new SALOMEDS_SComponent(aSCO_impl);
169 return _PTR(SComponent)(aSCO);
172 _PTR(SObject) SALOMEDS_Study::FindObject(const std::string& anObjectName)
174 SALOMEDSClient_SObject* aSO = NULL;
177 SALOMEDS::Locker lock;
179 Handle(SALOMEDSImpl_SObject) aSO_impl = _local_impl->FindObject((char*)anObjectName.c_str());
180 if (aSO_impl.IsNull()) return _PTR(SObject)(aSO);
181 Handle(SALOMEDSImpl_SComponent) aSCO_impl = Handle(SALOMEDSImpl_SComponent)::DownCast(aSO_impl);
182 if (!aSCO_impl.IsNull()) return _PTR(SObject)(new SALOMEDS_SComponent(aSCO_impl));
183 aSO = new SALOMEDS_SObject(aSO_impl);
186 SALOMEDS::SObject_var aSO_impl = _corba_impl->FindObject((char*)anObjectName.c_str());
187 if (CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
188 SALOMEDS::SComponent_var aSCO_impl = SALOMEDS::SComponent::_narrow(aSO_impl);
189 if (!CORBA::is_nil(aSCO_impl)) return _PTR(SObject)(new SALOMEDS_SComponent(aSCO_impl));
190 aSO = new SALOMEDS_SObject(aSO_impl);
193 return _PTR(SObject)(aSO);
196 std::vector<_PTR(SObject)> SALOMEDS_Study::FindObjectByName(const std::string& anObjectName,
197 const std::string& aComponentName)
199 std::vector<_PTR(SObject)> aVector;
203 SALOMEDS::Locker lock;
205 Handle(TColStd_HSequenceOfTransient) aSeq =
206 _local_impl->FindObjectByName((char*)anObjectName.c_str(), (char*)aComponentName.c_str());
207 aLength = aSeq->Length();
208 for (i = 1; i<= aLength; i++)
209 aVector.push_back(_PTR(SObject)(new SALOMEDS_SObject
210 (Handle(SALOMEDSImpl_SObject)::DownCast(aSeq->Value(i)))));
213 SALOMEDS::Study::ListOfSObject_var aSeq = _corba_impl->FindObjectByName((char*)anObjectName.c_str(),
214 (char*)aComponentName.c_str());
215 aLength = aSeq->length();
216 for (i = 0; i< aLength; i++) aVector.push_back(_PTR(SObject)(new SALOMEDS_SObject(aSeq[i])));
222 _PTR(SObject) SALOMEDS_Study::FindObjectID(const std::string& anObjectID)
224 SALOMEDSClient_SObject* aSO = NULL;
226 SALOMEDS::Locker lock;
228 Handle(SALOMEDSImpl_SObject) aSO_impl = _local_impl->FindObjectID((char*)anObjectID.c_str());
229 if(aSO_impl.IsNull()) return _PTR(SObject)(aSO);
230 return _PTR(SObject)(new SALOMEDS_SObject(aSO_impl));
233 SALOMEDS::SObject_var aSO_impl = _corba_impl->FindObjectID((char*)anObjectID.c_str());
234 if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
235 return _PTR(SObject)(new SALOMEDS_SObject(aSO_impl));
237 return _PTR(SObject)(aSO);
240 _PTR(SObject) SALOMEDS_Study::CreateObjectID(const std::string& anObjectID)
242 SALOMEDSClient_SObject* aSO = NULL;
244 SALOMEDS::Locker lock;
245 aSO = new SALOMEDS_SObject(_local_impl->CreateObjectID((char*)anObjectID.c_str()));
247 else aSO = new SALOMEDS_SObject(_corba_impl->CreateObjectID((char*)anObjectID.c_str()));
248 return _PTR(SObject)(aSO);
251 _PTR(SObject) SALOMEDS_Study::FindObjectIOR(const std::string& anObjectIOR)
253 SALOMEDSClient_SObject* aSO = NULL;
255 SALOMEDS::Locker lock;
257 Handle(SALOMEDSImpl_SObject) aSO_impl = _local_impl->FindObjectIOR((char*)anObjectIOR.c_str());
258 if (aSO_impl.IsNull()) return _PTR(SObject)(aSO);
259 aSO = new SALOMEDS_SObject(aSO_impl);
262 SALOMEDS::SObject_var aSO_impl = _corba_impl->FindObjectIOR((char*)anObjectIOR.c_str());
263 if (CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
264 aSO = new SALOMEDS_SObject(aSO_impl);
266 return _PTR(SObject)(aSO);
269 _PTR(SObject) SALOMEDS_Study::FindObjectByPath(const std::string& thePath)
271 SALOMEDSClient_SObject* aSO = NULL;
273 SALOMEDS::Locker lock;
275 Handle(SALOMEDSImpl_SObject) aSO_impl = _local_impl->FindObjectByPath((char*)thePath.c_str());
276 if (aSO_impl.IsNull()) return _PTR(SObject)(aSO);
277 aSO = new SALOMEDS_SObject(aSO_impl);
280 SALOMEDS::SObject_var aSO_impl = _corba_impl->FindObjectByPath((char*)thePath.c_str());
281 if (CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
282 aSO = new SALOMEDS_SObject(aSO_impl);
284 return _PTR(SObject)(aSO);
287 std::string SALOMEDS_Study::GetObjectPath(const _PTR(SObject)& theSO)
289 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
292 SALOMEDS::Locker lock;
293 aPath = _local_impl->GetObjectPath(aSO->GetLocalImpl()).ToCString();
295 else aPath = _corba_impl->GetObjectPath(aSO->GetCORBAImpl());
299 void SALOMEDS_Study::SetContext(const std::string& thePath)
302 SALOMEDS::Locker lock;
303 _local_impl->SetContext((char*)thePath.c_str());
305 else _corba_impl->SetContext((char*)thePath.c_str());
308 std::string SALOMEDS_Study::GetContext()
312 SALOMEDS::Locker lock;
313 aPath = _local_impl->GetContext().ToCString();
315 else aPath = _corba_impl->GetContext();
319 std::vector<std::string> SALOMEDS_Study::GetObjectNames(const std::string& theContext)
321 std::vector<std::string> aVector;
324 SALOMEDS::Locker lock;
326 Handle(TColStd_HSequenceOfAsciiString) aSeq = _local_impl->GetObjectNames((char*)theContext.c_str());
327 aLength = aSeq->Length();
328 for (i = 1; i <= aLength; i++) aVector.push_back(aSeq->Value(i).ToCString());
331 SALOMEDS::ListOfStrings_var aSeq = _corba_impl->GetObjectNames((char*)theContext.c_str());
332 aLength = aSeq->length();
333 for (i = 0; i < aLength; i++) aVector.push_back(std::string((std::string)aSeq[i].in()));
338 std::vector<std::string> SALOMEDS_Study::GetDirectoryNames(const std::string& theContext)
340 std::vector<std::string> aVector;
343 SALOMEDS::Locker lock;
345 Handle(TColStd_HSequenceOfAsciiString) aSeq =
346 _local_impl->GetDirectoryNames((char*)theContext.c_str());
347 aLength = aSeq->Length();
348 for (i = 1; i <= aLength; i++) aVector.push_back(aSeq->Value(i).ToCString());
351 SALOMEDS::ListOfStrings_var aSeq = _corba_impl->GetDirectoryNames((char*)theContext.c_str());
352 aLength = aSeq->length();
353 for (i = 0; i < aLength; i++) aVector.push_back((char*)aSeq[i].in());
358 std::vector<std::string> SALOMEDS_Study::GetFileNames(const std::string& theContext)
360 std::vector<std::string> aVector;
363 SALOMEDS::Locker lock;
365 Handle(TColStd_HSequenceOfAsciiString) aSeq = _local_impl->GetFileNames((char*)theContext.c_str());
366 aLength = aSeq->Length();
367 for (i = 1; i <= aLength; i++) aVector.push_back(aSeq->Value(i).ToCString());
370 SALOMEDS::ListOfStrings_var aSeq = _corba_impl->GetFileNames((char*)theContext.c_str());
371 aLength = aSeq->length();
373 for (i = 0; i < aLength; i++) aVector.push_back((char*)aSeq[i].in());
378 std::vector<std::string> SALOMEDS_Study::GetComponentNames(const std::string& theContext)
380 std::vector<std::string> aVector;
383 SALOMEDS::Locker lock;
385 Handle(TColStd_HSequenceOfAsciiString) aSeq = _local_impl->GetComponentNames((char*)theContext.c_str());
386 aLength = aSeq->Length();
387 for (i = 1; i <= aLength; i++) aVector.push_back(aSeq->Value(i).ToCString());
390 SALOMEDS::ListOfStrings_var aSeq = _corba_impl->GetComponentNames((char*)theContext.c_str());
391 aLength = aSeq->length();
392 for (i = 0; i < aLength; i++) aVector.push_back((char*)aSeq[i].in());
397 _PTR(ChildIterator) SALOMEDS_Study::NewChildIterator(const _PTR(SObject)& theSO)
399 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
400 SALOMEDSClient_ChildIterator* aCI = NULL;
402 SALOMEDS::Locker lock;
404 Handle(SALOMEDSImpl_ChildIterator) aCIimpl = _local_impl->NewChildIterator(aSO->GetLocalImpl());
405 aCI = new SALOMEDS_ChildIterator(aCIimpl);
408 SALOMEDS::ChildIterator_var aCIimpl = _corba_impl->NewChildIterator(aSO->GetCORBAImpl());
409 aCI = new SALOMEDS_ChildIterator(aCIimpl);
412 return _PTR(ChildIterator)(aCI);
415 _PTR(SComponentIterator) SALOMEDS_Study::NewComponentIterator()
417 SALOMEDSClient_SComponentIterator* aCI = NULL;
419 SALOMEDS::Locker lock;
421 SALOMEDSImpl_SComponentIterator aCIimpl = _local_impl->NewComponentIterator();
422 aCI = new SALOMEDS_SComponentIterator(aCIimpl);
425 SALOMEDS::SComponentIterator_var aCIimpl = _corba_impl->NewComponentIterator();
426 aCI = new SALOMEDS_SComponentIterator(aCIimpl);
429 return _PTR(SComponentIterator)(aCI);
432 _PTR(StudyBuilder) SALOMEDS_Study::NewBuilder()
434 SALOMEDSClient_StudyBuilder* aSB = NULL;
436 SALOMEDS::Locker lock;
438 Handle(SALOMEDSImpl_StudyBuilder) aSBimpl = _local_impl->NewBuilder();
439 aSB = new SALOMEDS_StudyBuilder(aSBimpl);
442 SALOMEDS::StudyBuilder_var aSBimpl = _corba_impl->NewBuilder();
443 aSB = new SALOMEDS_StudyBuilder(aSBimpl);
446 return _PTR(StudyBuilder)(aSB);
449 std::string SALOMEDS_Study::Name()
453 SALOMEDS::Locker lock;
454 aName = _local_impl->Name().ToCString();
456 else aName = _corba_impl->Name();
460 void SALOMEDS_Study::Name(const std::string& theName)
463 SALOMEDS::Locker lock;
464 _local_impl->Name((char*)theName.c_str());
466 else _corba_impl->Name((char*)theName.c_str());
469 bool SALOMEDS_Study::IsSaved()
473 SALOMEDS::Locker lock;
474 isSaved = _local_impl->IsSaved();
476 else isSaved = _corba_impl->IsSaved();
480 void SALOMEDS_Study::IsSaved(bool save)
483 SALOMEDS::Locker lock;
484 _local_impl->IsSaved(save);
486 else _corba_impl->IsSaved(save);
489 bool SALOMEDS_Study::IsModified()
493 SALOMEDS::Locker lock;
494 isModified = _local_impl->IsModified();
496 else isModified = _corba_impl->IsModified();
500 std::string SALOMEDS_Study::URL()
504 SALOMEDS::Locker lock;
505 aURL = _local_impl->URL().ToCString();
507 else aURL = _corba_impl->URL();
511 void SALOMEDS_Study::URL(const std::string& url)
514 SALOMEDS::Locker lock;
515 _local_impl->URL((char*)url.c_str());
517 else _corba_impl->URL((char*)url.c_str());
520 int SALOMEDS_Study::StudyId()
524 SALOMEDS::Locker lock;
525 anID = _local_impl->StudyId();
527 else anID = _corba_impl->StudyId();
531 void SALOMEDS_Study::StudyId(int id)
534 SALOMEDS::Locker lock;
535 _local_impl->StudyId(id);
537 else _corba_impl->StudyId(id);
540 std::vector<_PTR(SObject)> SALOMEDS_Study::FindDependances(const _PTR(SObject)& theSO)
542 std::vector<_PTR(SObject)> aVector;
543 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
546 SALOMEDS::Locker lock;
548 Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->FindDependances(aSO->GetLocalImpl());
549 if (!aSeq.IsNull()) {
550 aLength = aSeq->Length();
551 for (i = 1; i <= aLength; i++)
552 aVector.push_back(_PTR(SObject)(
553 new SALOMEDS_SObject(Handle(SALOMEDSImpl_SObject)::DownCast(aSeq->Value(i)))));
557 SALOMEDS::Study::ListOfSObject_var aSeq = _corba_impl->FindDependances(aSO->GetCORBAImpl());
558 aLength = aSeq->length();
559 for (i = 0; i < aLength; i++) aVector.push_back(_PTR(SObject)(new SALOMEDS_SObject(aSeq[i])));
564 _PTR(AttributeStudyProperties) SALOMEDS_Study::GetProperties()
566 SALOMEDSClient_AttributeStudyProperties* aProp;
568 SALOMEDS::Locker lock;
569 aProp = new SALOMEDS_AttributeStudyProperties(_local_impl->GetProperties());
571 else aProp = new SALOMEDS_AttributeStudyProperties(_corba_impl->GetProperties());
572 return _PTR(AttributeStudyProperties)(aProp);
575 std::string SALOMEDS_Study::GetLastModificationDate()
579 SALOMEDS::Locker lock;
580 aDate = _local_impl->GetLastModificationDate().ToCString();
582 else aDate = _corba_impl->GetLastModificationDate();
586 std::vector<std::string> SALOMEDS_Study::GetModificationsDate()
588 std::vector<std::string> aVector;
591 SALOMEDS::Locker lock;
593 Handle(TColStd_HSequenceOfAsciiString) aSeq = _local_impl->GetModificationsDate();
594 aLength = aSeq->Length();
595 for (i = 1; i <= aLength; i++) aVector.push_back(aSeq->Value(i).ToCString());
598 SALOMEDS::ListOfDates_var aSeq = _corba_impl->GetModificationsDate();
599 aLength = aSeq->length();
600 for (i = 0; i < aLength; i++) aVector.push_back((char*)aSeq[i].in());
605 _PTR(UseCaseBuilder) SALOMEDS_Study::GetUseCaseBuilder()
607 SALOMEDSClient_UseCaseBuilder* aUB = NULL;
609 SALOMEDS::Locker lock;
611 Handle(SALOMEDSImpl_UseCaseBuilder) aUBimpl = _local_impl->GetUseCaseBuilder();
612 aUB = new SALOMEDS_UseCaseBuilder(aUBimpl);
615 SALOMEDS::UseCaseBuilder_var aUBimpl = _corba_impl->GetUseCaseBuilder();
616 aUB = new SALOMEDS_UseCaseBuilder(aUBimpl);
619 return _PTR(UseCaseBuilder)(aUB);
622 void SALOMEDS_Study::Close()
625 SALOMEDS::Locker lock;
626 _local_impl->Close();
628 else _corba_impl->Close();
631 void SALOMEDS_Study::EnableUseCaseAutoFilling(bool isEnabled)
633 if(_isLocal) _local_impl->EnableUseCaseAutoFilling(isEnabled);
634 else _corba_impl->EnableUseCaseAutoFilling(isEnabled);
637 bool SALOMEDS_Study::DumpStudy(const std::string& thePath, const std::string& theBaseName, bool isPublished)
639 //SRN: Pure CORBA DumpStudy as it does more cleaning than the local one
640 bool ret = _corba_impl->DumpStudy((char*)thePath.c_str(), (char*)theBaseName.c_str(), isPublished);
644 std::string SALOMEDS_Study::ConvertObjectToIOR(CORBA::Object_ptr theObject)
646 return _orb->object_to_string(theObject);
649 CORBA::Object_ptr SALOMEDS_Study::ConvertIORToObject(const std::string& theIOR)
651 return _orb->string_to_object(theIOR.c_str());
654 void SALOMEDS_Study::init_orb()
656 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
657 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
658 _orb = init(0 , 0 ) ;
661 SALOMEDS::Study_ptr SALOMEDS_Study::GetStudy()
664 SALOMEDS::Locker lock;
666 if (!CORBA::is_nil(_corba_impl)) return _corba_impl;
667 std::string anIOR = _local_impl->GetTransientReference().ToCString();
668 SALOMEDS::Study_var aStudy;
669 if (!_local_impl->IsError() && anIOR != "") {
670 aStudy = SALOMEDS::Study::_narrow(_orb->string_to_object(anIOR.c_str()));
673 SALOMEDS_Study_i *aStudy_servant = new SALOMEDS_Study_i(_local_impl, _orb);
674 aStudy = aStudy_servant->_this();
675 _local_impl->SetTransientReference(_orb->object_to_string(aStudy));
677 return aStudy._retn();
683 return SALOMEDS::Study::_nil();