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/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSImpl_StudyManager.cxx
21 // Author : Sergey RUIN
24 #include "SALOMEDSImpl_StudyManager.hxx"
26 #include "DF_ChildIterator.hxx"
27 #include "HDFexplorer.hxx"
29 #include "SALOMEDSImpl_Attributes.hxx"
30 #include "SALOMEDSImpl_Tool.hxx"
31 #include "SALOMEDSImpl_SComponent.hxx"
32 #include "SALOMEDSImpl_GenericAttribute.hxx"
41 #define USE_CASE_LABEL_ID "0:2"
43 static void SaveAttributes(const SALOMEDSImpl_SObject& SO, HDFgroup *hdf_group_sobject);
44 static void ReadAttributes(SALOMEDSImpl_Study*, const SALOMEDSImpl_SObject&, HDFdataset* );
45 static void BuildTree (SALOMEDSImpl_Study*, HDFgroup*);
46 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject&,
47 SALOMEDSImpl_Driver*, bool isMultiFile, bool isASCII);
49 //============================================================================
50 /*! Function : SALOMEDSImpl_StudyManager
51 * Purpose : SALOMEDSImpl_StudyManager constructor
53 //============================================================================
54 SALOMEDSImpl_StudyManager::SALOMEDSImpl_StudyManager()
57 _appli = new DF_Application();
59 _clipboard = _appli->NewDocument("SALOME_STUDY");
62 //============================================================================
63 /*! Function : ~SALOMEDSImpl_StudyManager
64 * Purpose : SALOMEDSImpl_StudyManager destructor
66 //============================================================================
67 SALOMEDSImpl_StudyManager::~SALOMEDSImpl_StudyManager()
69 // Destroy application
74 //============================================================================
75 /*! Function : NewStudy
76 * Purpose : Create a New Study of name study_name
78 //==================================================T==========================
79 SALOMEDSImpl_Study* SALOMEDSImpl_StudyManager::NewStudy(const string& study_name)
83 DF_Document* Doc = _appli->NewDocument("SALOME_STUDY");
85 SALOMEDSImpl_Study* Study = new SALOMEDSImpl_Study(Doc, study_name);
88 Study->StudyId( _IDcounter );
90 // set Study properties
91 SALOMEDSImpl_AttributeStudyProperties* aProp = Study->GetProperties();
93 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
94 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
95 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
96 mn, hh, day, month, year);
97 aProp->SetCreationMode(1); //"from scratch"
102 //============================================================================
104 * Purpose : Open a Study from it's persistent reference
106 //============================================================================
107 SALOMEDSImpl_Study* SALOMEDSImpl_StudyManager::Open(const string& aUrl)
112 HDFfile *hdf_file =0;
113 HDFgroup *hdf_group_study_structure =0;
117 bool isASCII = false;
118 if (HDFascii::isASCII(aUrl.c_str())) {
120 char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aUrl.c_str());
121 aC_HDFUrl = new char[strlen(aResultPath) + 19];
122 sprintf(aC_HDFUrl, "%shdf_from_ascii.hdf", aResultPath);
131 hdf_file = new HDFfile((char*)aHDFUrl.c_str());
133 hdf_file->OpenOnDisk(HDF_RDONLY);// mpv: was RDWR, but opened file can be write-protected too
138 // char eStr[strlen(aUrl.ToCString())+17];
141 eStr = new char[strlen(aUrl.c_str())+17];
143 sprintf(eStr,"Can't open file %s",aUrl.c_str());
147 _errorCode = string(eStr);
151 // Temporary aStudyUrl in place of study name
152 DF_Document* Doc = _appli->NewDocument("SALOME_STUDY");
154 SALOMEDSImpl_Study* Study = new SALOMEDSImpl_Study(Doc, aUrl);
157 Study->StudyId( _IDcounter );
159 // Assign the value of the URL in the study object
162 SALOMEDSImpl_AttributePersistentRef::Set(Doc->Main(), aUrl);
164 if (!hdf_file->ExistInternalObject("STUDY_STRUCTURE")) {
165 _errorCode = "Study is empty";
169 //Create the Structure of the Document
170 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
173 BuildTree (Study, hdf_group_study_structure);
178 // char eStr[strlen(aUrl.ToCString())+17];
180 char *eStr = new char [strlen(aUrl.c_str())+17];
182 sprintf(eStr,"Can't open file %s", aUrl.c_str());
183 _errorCode = string(eStr);
187 hdf_file->CloseOnDisk();
190 vector<string> aFilesToRemove;
191 aFilesToRemove.push_back("hdf_from_ascii.hdf");
192 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
195 delete hdf_file; // all related hdf objects will be deleted
202 //============================================================================
204 * Purpose : Close a study.
205 * If the study hasn't been saved, ask the user to confirm the
206 * close action without saving
209 //============================================================================
210 void SALOMEDSImpl_StudyManager::Close(SALOMEDSImpl_Study* aStudy)
215 _errorCode = "Study is null";
222 //============================================================================
224 * Purpose : Save a Study to it's persistent reference
226 //============================================================================
227 bool SALOMEDSImpl_StudyManager::Save(SALOMEDSImpl_Study* aStudy,
228 SALOMEDSImpl_DriverFactory* aFactory,
233 string url = aStudy->URL();
235 _errorCode = "No path specified to save the study. Nothing done";
239 return Impl_SaveAs(url,aStudy, aFactory, theMultiFile, false);
245 bool SALOMEDSImpl_StudyManager::SaveASCII(SALOMEDSImpl_Study* aStudy,
246 SALOMEDSImpl_DriverFactory* aFactory,
251 string url = aStudy->URL();
253 _errorCode = "No path specified to save the study. Nothing done";
257 return Impl_SaveAs(url,aStudy, aFactory, theMultiFile, true);
263 //=============================================================================
264 /*! Function : SaveAs
265 * Purpose : Save a study to the persistent reference aUrl
267 //============================================================================
268 bool SALOMEDSImpl_StudyManager::SaveAs(const string& aUrl,
269 SALOMEDSImpl_Study* aStudy,
270 SALOMEDSImpl_DriverFactory* aFactory,
274 return Impl_SaveAs(aUrl,aStudy, aFactory, theMultiFile, false);
277 bool SALOMEDSImpl_StudyManager::SaveAsASCII(const string& aUrl,
278 SALOMEDSImpl_Study* aStudy,
279 SALOMEDSImpl_DriverFactory* aFactory,
283 return Impl_SaveAs(aUrl,aStudy, aFactory, theMultiFile, true);
286 //============================================================================
287 /*! Function : GetOpenStudies
288 * Purpose : Get name list of open studies in the session
290 //============================================================================
291 vector<SALOMEDSImpl_Study*> SALOMEDSImpl_StudyManager::GetOpenStudies()
294 vector<SALOMEDSImpl_Study*> aList;
296 int nbDocs = _appli->NbDocuments();
299 _errorCode = "No active study in this session";
303 SALOMEDSImpl_Study* aStudy;
304 vector<int> ids = _appli->GetDocumentIDs();
305 for (int i = 0, len = ids.size(); i<len; i++) {
306 DF_Document* D = _appli->GetDocument(ids[i]);
307 if(D == _clipboard) continue;
308 aStudy = SALOMEDSImpl_Study::GetStudy(D->Main());
309 if(!aStudy) continue;
310 aList.push_back(aStudy);
317 //============================================================================
318 /*! Function : GetStudyByName
319 * Purpose : Get a study from its name
321 //============================================================================
322 SALOMEDSImpl_Study* SALOMEDSImpl_StudyManager::GetStudyByName
323 (const string& aStudyName)
326 int nbDocs = _appli->NbDocuments();
329 _errorCode = "No active study in this session";
333 vector<SALOMEDSImpl_Study*> studies = GetOpenStudies();
334 for (int i = 0, len = studies.size(); i<len; i++) {
335 if (studies[i]->Name() == aStudyName) return studies[i];
339 _errorCode = string("Found no study with the name ") + aStudyName;
343 //============================================================================
344 /*! Function : GetStudyByID
345 * Purpose : Get a study from its ID
347 //============================================================================
348 SALOMEDSImpl_Study* SALOMEDSImpl_StudyManager::GetStudyByID(int aStudyID)
351 int nbDocs = _appli->NbDocuments();
354 _errorCode = "No active study in this session";
358 vector<SALOMEDSImpl_Study*> studies = GetOpenStudies();
359 for (int i = 0, len = studies.size(); i<len; i++) {
360 if (studies[i]->StudyId() == aStudyID) return studies[i];
364 _errorCode = "Found no study with the given ID";
368 //=============================================================================
369 /*! Function : _SaveProperties
370 * Purpose : save the study properties in HDF file
372 //============================================================================
373 bool SALOMEDSImpl_StudyManager::Impl_SaveProperties(SALOMEDSImpl_Study* aStudy,
378 HDFdataset *hdf_dataset = 0;
382 // add modifications list (user and date of save)
383 SALOMEDSImpl_AttributeStudyProperties* aProp = aStudy->GetProperties();
384 int aLocked = aProp->IsLocked();
385 if (aLocked) aProp->SetLocked(false);
387 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
388 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
389 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
390 mn, hh, day, month, year);
392 if (aLocked) aProp->SetLocked(true);
394 vector<string> aNames;
395 vector<int> aMinutes, aHours, aDays, aMonths, aYears;
397 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
399 int aLength = 0, anIndex, i;
400 for(i=1; i<=aNames.size(); i++)
401 aLength += aNames[i-1].size() + 1;
403 //string length: 1 byte = locked flag, 1 byte = modified flag, (12 + name length + 1) for each name and date, "zero" byte
404 char* aProperty = new char[3 + aLength + 12 * aNames.size()];
407 sprintf(aProperty,"%c%c", (char)aProp->GetCreationMode(), (aProp->IsLocked())?'l':'u');
409 aLength = aNames.size();
411 for(anIndex = 0; anIndex<aLength; anIndex++) {
412 sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
413 (int)(aMinutes[anIndex]),
414 (int)(aHours[anIndex]),
415 (int)(aDays[anIndex]),
416 (int)(aMonths[anIndex]),
417 (int)(aYears[anIndex]),
418 aNames[anIndex].c_str());
419 a = strlen(aProperty);
424 name_len = (hdf_int32) a;
425 size[0] = name_len + 1 ;
426 hdf_dataset = new HDFdataset("AttributeStudyProperties",hdf_group,HDF_STRING,size,1);
427 hdf_dataset->CreateOnDisk();
428 hdf_dataset->WriteOnDisk(aProperty);
429 hdf_dataset->CloseOnDisk();
430 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
433 aProp->SetModified(0);
437 //=============================================================================
438 /*! Function : _SaveAs
439 * Purpose : save the study in HDF file
441 //============================================================================
442 bool SALOMEDSImpl_StudyManager::Impl_SaveAs(const string& aUrl,
443 SALOMEDSImpl_Study* aStudy,
444 SALOMEDSImpl_DriverFactory* aFactory,
448 // HDF File will be composed of differents part :
449 // * For each ComponentDataType, all data created by the component
450 // Informations in data group hdf_group_datacomponent
451 // * Study Structure -> Exactly what is contained in Document
452 // Informations in data group hdf_group_study_structure
457 HDFgroup *hdf_group_study_structure =0;
458 HDFgroup *hdf_sco_group =0;
459 HDFgroup *hdf_sco_group2 =0;
461 HDFgroup *hdf_group_datacomponent =0;
462 HDFdataset *hdf_dataset =0;
464 hdf_int32 name_len = 0;
465 char *component_name = 0;
468 _errorCode = "Study is null";
472 int aLocked = aStudy->GetProperties()->IsLocked();
473 if (aLocked) aStudy->GetProperties()->SetLocked(false);
475 SALOMEDSImpl_StudyBuilder* SB= aStudy->NewBuilder();
476 map<string, SALOMEDSImpl_Driver*> aMapTypeDriver;
480 // mpv 15.12.2003: for saving components we have to load all data from all modules
481 SALOMEDSImpl_SComponentIterator itcomponent1 = aStudy->NewComponentIterator();
482 for (; itcomponent1.More(); itcomponent1.Next())
484 SALOMEDSImpl_SComponent sco = itcomponent1.Value();
485 // if there is an associated Engine call its method for saving
488 if (!sco.ComponentIOR(IOREngine)) {
489 string aCompType = sco.GetComment();
490 if (!aCompType.empty()) {
492 SALOMEDSImpl_Driver* aDriver = aFactory->GetDriverByType(aCompType);
493 aMapTypeDriver[aCompType] = aDriver;
495 if (aDriver != NULL) {
496 if(!SB->LoadWith(sco, aDriver)) {
497 _errorCode = SB->GetErrorCode();
504 _errorCode = "Can not restore information to resave it";
509 string anOldName = aStudy->Name();
512 // To change for Save
513 // Do not have to do a new file but just a Open??? Rewrite all informations after erasing evrything??
514 hdf_file = new HDFfile((char*)aUrl.c_str());
515 hdf_file->CreateOnDisk();
517 //-----------------------------------------------------------------------
518 // 1 - Create a groupe for each SComponent and Update the PersistanceRef
519 //-----------------------------------------------------------------------
520 hdf_group_datacomponent = new HDFgroup("DATACOMPONENT",hdf_file);
521 hdf_group_datacomponent->CreateOnDisk();
523 SALOMEDSImpl_SComponentIterator itcomponent = aStudy->NewComponentIterator();
525 for (; itcomponent.More(); itcomponent.Next())
527 SALOMEDSImpl_SComponent sco = itcomponent.Value();
529 string scoid = sco.GetID();
530 hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group_datacomponent);
531 hdf_sco_group->CreateOnDisk();
533 string componentDataType = sco.ComponentDataType();
535 if (sco.ComponentIOR(IOREngine))
537 SALOMEDSImpl_Driver* Engine = NULL;
538 if(aMapTypeDriver.find(componentDataType) != aMapTypeDriver.end()) {
539 // we have found the associated engine to write the data
540 Engine = aMapTypeDriver[componentDataType];
543 Engine = aFactory->GetDriverByIOR(IOREngine);
548 SALOMEDSImpl_TMPFile* aStream = NULL;
551 if (theASCII) aStream = Engine->SaveASCII(sco,
552 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
555 else aStream = Engine->Save(sco,
556 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
559 HDFdataset *hdf_dataset;
560 hdf_size aHDFSize[1];
561 if(length > 0) { //The component saved some auxiliary files, then put them into HDF file
563 aHDFSize[0] = length;
565 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group, HDF_STRING, aHDFSize, 1);
566 hdf_dataset->CreateOnDisk();
567 hdf_dataset->WriteOnDisk(aStream->Data()); //Save the stream in the HDF file
568 hdf_dataset->CloseOnDisk();
571 if(aStream) delete aStream;
573 // store multifile state
575 hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
576 hdf_dataset->CreateOnDisk();
577 hdf_dataset->WriteOnDisk((void*)(theMultiFile?"M":"S")); // save: multi or single
578 hdf_dataset->CloseOnDisk();
579 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
582 hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
583 hdf_dataset->CreateOnDisk();
584 hdf_dataset->WriteOnDisk((void*)(theASCII?"A":"B")); // save: ASCII or BINARY
585 hdf_dataset->CloseOnDisk();
586 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
587 // Creation of the persistance reference attribute
588 Translate_IOR_to_persistentID (sco, Engine, theMultiFile, theASCII);
591 hdf_sco_group->CloseOnDisk();
592 hdf_sco_group=0; // will be deleted by hdf_group_datacomponent destructor
594 hdf_group_datacomponent->CloseOnDisk();
595 hdf_group_datacomponent =0; // will be deleted by hdf_file destructor
597 //-----------------------------------------------------------------------
598 //3 - Write the Study Structure
599 //-----------------------------------------------------------------------
600 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
601 hdf_group_study_structure->CreateOnDisk();
602 // save component attributes
603 SALOMEDSImpl_SComponentIterator itcomp = aStudy->NewComponentIterator();
604 for (; itcomp.More(); itcomp.Next())
606 SALOMEDSImpl_SComponent SC = itcomp.Value();
607 string scid = SC.GetID();
608 hdf_sco_group2 = new HDFgroup((char*)scid.c_str(), hdf_group_study_structure);
609 hdf_sco_group2->CreateOnDisk();
610 SaveAttributes(SC, hdf_sco_group2);
611 // ComponentDataType treatment
612 component_name = (char*)SC.ComponentDataType().c_str();
613 name_len = (hdf_int32)strlen(component_name);
614 size[0] = name_len +1 ;
615 hdf_dataset = new HDFdataset("COMPONENTDATATYPE",hdf_sco_group2,HDF_STRING,size,1);
616 hdf_dataset->CreateOnDisk();
617 hdf_dataset->WriteOnDisk(component_name);
618 hdf_dataset->CloseOnDisk();
619 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
620 Impl_SaveObject(SC, hdf_sco_group2);
621 hdf_sco_group2->CloseOnDisk();
622 hdf_sco_group2=0; // will be deleted by hdf_group_study_structure destructor
624 //-----------------------------------------------------------------------
625 //4 - Write the Study UseCases Structure
626 //-----------------------------------------------------------------------
627 SALOMEDSImpl_SObject aSO = aStudy->FindObjectID(USE_CASE_LABEL_ID);
629 HDFgroup *hdf_soo_group = new HDFgroup(USE_CASE_LABEL_ID,hdf_group_study_structure);
630 hdf_soo_group->CreateOnDisk();
631 SaveAttributes(aSO, hdf_soo_group);
632 Impl_SaveObject(aSO, hdf_soo_group);
633 hdf_soo_group->CloseOnDisk();
634 hdf_soo_group=0; // will be deleted by hdf_group_study_structure destructor
637 if (aLocked) aStudy->GetProperties()->SetLocked(true);
638 //-----------------------------------------------------------------------
639 //5 - Write the Study Properties
640 //-----------------------------------------------------------------------
641 name_len = (hdf_int32) aStudy->Name().size();
642 size[0] = name_len +1 ;
643 hdf_dataset = new HDFdataset("STUDY_NAME",hdf_group_study_structure,HDF_STRING,size,1);
644 hdf_dataset->CreateOnDisk();
645 char* studid = (char*)aStudy->Name().c_str();
646 hdf_dataset->WriteOnDisk(studid);
647 hdf_dataset->CloseOnDisk();
648 hdf_dataset=0; // will be deleted by hdf_group_study_structure destructor
650 Impl_SaveProperties(aStudy, hdf_group_study_structure);
651 hdf_group_study_structure->CloseOnDisk();
652 hdf_file->CloseOnDisk();
654 aStudy->IsSaved(true);
655 hdf_group_study_structure =0; // will be deleted by hdf_file destructor
656 delete hdf_file; // recursively deletes all hdf objects...
660 _errorCode = "HDFexception ! ";
663 catch(std::exception& exc)
665 _errorCode = const_cast<char*>(exc.what());
670 _errorCode = "Unknown exception ! ";
673 if (theASCII) { // save file in ASCII format
674 HDFascii::ConvertFromHDFToASCII(aUrl.c_str(), true);
680 //============================================================================
681 /*! Function : Impl_SaveObject
684 //============================================================================
685 bool SALOMEDSImpl_StudyManager::Impl_SaveObject(const SALOMEDSImpl_SObject& SC,
686 HDFgroup *hdf_group_datatype)
690 // Write in group hdf_group_datatype all informations of SObject SC
691 // Iterative function to parse all SObjects under a SComponent
693 HDFgroup *hdf_group_sobject = 0;
695 DF_ChildIterator itchild(SC.GetLabel());
696 for (; itchild.More(); itchild.Next())
699 // mpv: don't save empty labels
700 vector<DF_Attribute*> attr = itchild.Value().GetAttributes();
701 if (attr.size() == 0) { //No attributes on the label
702 DF_ChildIterator subchild(itchild.Value());
703 if (!subchild.More()) {
706 subchild.Init(itchild.Value(), true);
708 for (; subchild.More() && anEmpty; subchild.Next()) {
709 vector<DF_Attribute*> attr2 = subchild.Value().GetAttributes();
711 anEmpty = false; //There are attributes on the child label
715 if (anEmpty) continue;
718 SALOMEDSImpl_SObject SO = SALOMEDSImpl_Study::SObject(itchild.Value());
720 char* scoid = (char*) SO.GetID().c_str();
721 hdf_group_sobject = new HDFgroup(scoid, hdf_group_datatype);
722 hdf_group_sobject->CreateOnDisk();
723 SaveAttributes(SO, hdf_group_sobject);
724 Impl_SaveObject(SO, hdf_group_sobject);
725 hdf_group_sobject->CloseOnDisk();
726 hdf_group_sobject =0; // will be deleted by father hdf object destructor
732 //============================================================================
733 /*! Function : Impl_SubstituteSlash
736 //============================================================================
737 string SALOMEDSImpl_StudyManager::Impl_SubstituteSlash(const string& aUrl)
741 std::string theUrl(aUrl);
742 for(int i = 0; i<theUrl.size(); i++)
743 if(theUrl[i] == '/') theUrl[i] = ':';
747 //============================================================================
748 /*! Function : GetDocumentOfStudy
751 //============================================================================
752 DF_Document* SALOMEDSImpl_StudyManager::GetDocumentOfStudy(SALOMEDSImpl_Study* theStudy)
755 return theStudy->_doc;
758 //============================================================================
759 /*! Function : CanCopy
762 //============================================================================
763 bool SALOMEDSImpl_StudyManager::CanCopy(const SALOMEDSImpl_SObject& theObject,
764 SALOMEDSImpl_Driver* theEngine)
767 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
768 if (!aComponent) return false;
769 if (aComponent.GetLabel() == theObject.GetLabel()) return false;
771 if (!aComponent.ComponentIOR(IOREngine)) return false;
772 if (theEngine == NULL) return false;
773 return theEngine->CanCopy(theObject);
776 //============================================================================
777 /*! Function : CopyLabel
780 //============================================================================
781 bool SALOMEDSImpl_StudyManager::CopyLabel(SALOMEDSImpl_Study* theSourceStudy,
782 SALOMEDSImpl_Driver* theEngine,
783 const int theSourceStartDepth,
784 const DF_Label& theSource,
785 const DF_Label& theDestinationMain)
790 DF_Label aTargetLabel = theDestinationMain;
791 DF_Label aAuxTargetLabel = theDestinationMain.Father().FindChild(2);
792 for(a = theSource.Depth() - theSourceStartDepth; a > 0 ; a--) {
793 DF_Label aSourceLabel = theSource;
794 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
795 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
796 aAuxTargetLabel = aAuxTargetLabel.FindChild(aSourceLabel.Tag());
798 // iterate attributes
799 vector<DF_Attribute*> attrList = theSource.GetAttributes();
800 for(int i = 0, len = attrList.size(); i<len; i++) {
801 DF_Attribute* anAttr = attrList[i];
802 string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
803 if (type.substr(0, 17) == string("AttributeTreeNode")) continue; // never copy tree node attribute
804 if (type == string("AttributeTarget")) continue; // and target attribute
806 if (type == string("AttributeReference")) { // reference copied as Comment in aux tree
807 DF_Label aReferenced = dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Get();
808 string anEntry = aReferenced.Entry();
809 // store the value of name attribute of referenced label
810 SALOMEDSImpl_AttributeName* aNameAttribute;
811 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aReferenced.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
813 anEntry += aNameAttribute->Value();
815 SALOMEDSImpl_AttributeComment::Set(aAuxTargetLabel, anEntry);
819 if (type == string("AttributeIOR")) { // IOR => ID and TMPFile of Engine
820 string anEntry = theSource.Entry();
821 SALOMEDSImpl_SObject aSO = theSourceStudy->FindObjectID(anEntry);
824 SALOMEDSImpl_TMPFile* aStream = theEngine->CopyFrom(aSO, anObjID, aLen);
826 for(a = 0; a < aLen; a++) {
827 aResStr += (char)(aStream->Get(a));
830 if(aStream) delete aStream;
832 SALOMEDSImpl_AttributeInteger::Set(aAuxTargetLabel, anObjID);
833 SALOMEDSImpl_AttributeName::Set(aAuxTargetLabel, aResStr);
836 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
837 aTargetLabel.AddAttribute(aNewAttribute);
838 anAttr->Paste(aNewAttribute);
844 //============================================================================
848 //============================================================================
849 bool SALOMEDSImpl_StudyManager::Copy(const SALOMEDSImpl_SObject& theObject,
850 SALOMEDSImpl_Driver* theEngine)
854 // adoptation for alliances datamodel copy: without IOR attributes !!!
855 bool aStructureOnly; // copy only SObjects and attributes without component help
856 aStructureOnly = !theObject.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID());
858 // get component-engine
859 SALOMEDSImpl_Study* aStudy = theObject.GetStudy();
861 // CAF document of current study usage
862 DF_Document* aDocument = GetDocumentOfStudy(aStudy);
864 _errorCode = "Document is null";
868 //Clear the clipboard
869 _clipboard->Main().Root().ForgetAllAttributes(true);
870 _appli->Close(_clipboard);
871 _clipboard = _appli->NewDocument("SALOME_STUDY");
873 // set component data type to the name attribute of root label
874 if (!aStructureOnly) {
875 SALOMEDSImpl_AttributeComment::Set(_clipboard->Main().Root(),
876 theEngine->ComponentDataType());
878 // set to the Root label integer attribute: study id
879 SALOMEDSImpl_AttributeInteger::Set(_clipboard->Main().Root(), aStudy->StudyId());
880 // iterate all theObject's label children
881 DF_Label aStartLabel = theObject.GetLabel();
882 int aSourceStartDepth = aStartLabel.Depth();
884 // copy main source label
885 CopyLabel(aStudy, theEngine, aSourceStartDepth, aStartLabel, _clipboard->Main());
887 // copy all subchildren of the main source label (all levels)
888 DF_ChildIterator anIterator(aStartLabel, true);
889 for(; anIterator.More(); anIterator.Next()) {
890 CopyLabel(aStudy, theEngine, aSourceStartDepth, anIterator.Value(), _clipboard->Main());
895 //============================================================================
896 /*! Function : CanPaste
899 //============================================================================
900 bool SALOMEDSImpl_StudyManager::CanPaste(const SALOMEDSImpl_SObject& theObject,
901 SALOMEDSImpl_Driver* theEngine)
906 _errorCode = "Clipboard is null";
910 SALOMEDSImpl_AttributeComment* aCompName = NULL;
911 if (!(aCompName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
912 _errorCode = "Clipboard has no component type";
915 SALOMEDSImpl_AttributeInteger* anObjID;
916 if (!(anObjID=(SALOMEDSImpl_AttributeInteger*)_clipboard->Main().Father().FindChild(2).FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
917 _errorCode = "Clipboard has no object id";
920 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
922 _errorCode = "Object doesn't belong to component";
927 if (!aComponent.ComponentIOR(IOREngine)) {
928 _errorCode = "component has no IOR";
931 return theEngine->CanPaste(aCompName->Value(), anObjID->Value());
934 //============================================================================
935 /*! Function : PasteLabel
938 //============================================================================
939 DF_Label SALOMEDSImpl_StudyManager::PasteLabel(SALOMEDSImpl_Study* theDestinationStudy,
940 SALOMEDSImpl_Driver* theEngine,
941 const DF_Label& theSource,
942 const DF_Label& theDestinationStart,
943 const int theCopiedStudyID,
944 const bool isFirstElement)
948 // get corresponding source, target and auxiliary labels
949 DF_Label aTargetLabel = theDestinationStart;
951 DF_Label aAuxSourceLabel = theSource.Root().FindChild(2);
953 if (!isFirstElement) {
954 for(a = theSource.Depth() - 1; a > 0 ; a--) {
955 DF_Label aSourceLabel = theSource;
956 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
957 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
958 aAuxSourceLabel = aAuxSourceLabel.FindChild(aSourceLabel.Tag());
962 // check auxiliary label for TMPFile => IOR
963 SALOMEDSImpl_AttributeName* aNameAttribute = NULL;
964 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
965 SALOMEDSImpl_AttributeInteger* anObjID = (SALOMEDSImpl_AttributeInteger*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeInteger::GetID());
966 SALOMEDSImpl_AttributeComment* aComponentName = (SALOMEDSImpl_AttributeComment*)theSource.Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID());
967 string aCompName = aComponentName->Value();
969 if (theEngine->CanPaste(aCompName, anObjID->Value())) {
970 std::string aTMPStr = aNameAttribute->Value();
971 int aLen = aTMPStr.size();
972 unsigned char* aStream = NULL;
974 aStream = new unsigned char[aLen+10];
975 for(a = 0; a < aLen; a++) {
976 aStream[a] = aTMPStr[a];
980 string anEntry = aTargetLabel.Entry();
981 SALOMEDSImpl_SObject aPastedSO = theDestinationStudy->FindObjectID(anEntry);
983 if (isFirstElement) {
984 string aDestEntry = theEngine->PasteInto(aStream,
987 aPastedSO.GetFatherComponent());
988 aTargetLabel = DF_Label::Label(theDestinationStart, aDestEntry);
990 theEngine->PasteInto(aStream, aLen, anObjID->Value(), aPastedSO);
992 if(aStream != NULL) delete []aStream;
996 // iterate attributes
997 vector<DF_Attribute*> attrList = theSource.GetAttributes();
998 for(int i = 0, len = attrList.size(); i<len; i++) {
999 DF_Attribute* anAttr = attrList[i];
1000 if (aTargetLabel.FindAttribute(anAttr->ID())) {
1001 aTargetLabel.ForgetAttribute(anAttr->ID());
1003 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
1004 aTargetLabel.AddAttribute(aNewAttribute);
1005 anAttr->Paste(aNewAttribute);
1008 // check auxiliary label for Comment => reference or name attribute of the referenced object
1009 SALOMEDSImpl_AttributeComment* aCommentAttribute = NULL;
1010 if ((aCommentAttribute=(SALOMEDSImpl_AttributeComment*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1011 char * anEntry = new char[aCommentAttribute->Value().size() + 1];
1012 strcpy(anEntry, string(aCommentAttribute->Value()).c_str());
1013 char* aNameStart = strchr(anEntry, ' ');
1018 if (theCopiedStudyID == theDestinationStudy->StudyId()) { // if copy to the same study, reanimate reference
1019 DF_Label aRefLabel = DF_Label::Label(aTargetLabel, anEntry);
1020 SALOMEDSImpl_AttributeReference::Set(aTargetLabel, aRefLabel);
1021 // target attributes structure support
1022 SALOMEDSImpl_AttributeTarget::Set(aRefLabel)->Add(SALOMEDSImpl_Study::SObject(aTargetLabel));
1024 if (aNameStart) SALOMEDSImpl_AttributeName::Set(aTargetLabel, aNameStart);
1025 else SALOMEDSImpl_AttributeName::Set(aTargetLabel, std::string("Reference to:")+anEntry);
1030 return aTargetLabel;
1033 //============================================================================
1034 /*! Function : Paste
1037 //============================================================================
1038 SALOMEDSImpl_SObject SALOMEDSImpl_StudyManager::Paste(const SALOMEDSImpl_SObject& theObject,
1039 SALOMEDSImpl_Driver* theEngine)
1043 SALOMEDSImpl_SObject so;
1044 SALOMEDSImpl_Study* aStudy = theObject.GetStudy();
1046 // if study is locked, then paste can't be done
1047 if (aStudy->GetProperties()->IsLocked()) {
1048 _errorCode = "LockProtection";
1049 throw LockProtection("LockProtection");
1052 // if there is no component name, then paste only SObjects and attributes: without component help
1053 SALOMEDSImpl_AttributeComment* aComponentName = NULL;
1054 bool aStructureOnly = !(aComponentName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()));
1056 // get copied study ID
1057 SALOMEDSImpl_AttributeInteger* aStudyIDAttribute = NULL;
1058 if (!(aStudyIDAttribute=(SALOMEDSImpl_AttributeInteger*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
1059 _errorCode = "No study ID was found";
1062 int aCStudyID = aStudyIDAttribute->Value();
1064 // CAF document of current study usage
1065 DF_Document* aDocument = GetDocumentOfStudy(aStudy);
1067 _errorCode = "Document is null";
1071 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1073 // fill root inserted SObject
1074 DF_Label aStartLabel;
1075 if (aStructureOnly) {
1076 DF_Label anObjectLabel = DF_Label::Label(aDocument->Main(), theObject.GetID());
1077 aStartLabel = PasteLabel(aStudy, theEngine, _clipboard->Main(), anObjectLabel, aCStudyID, false);
1079 DF_Label aComponentLabel = DF_Label::Label(aDocument->Main(), aComponent.GetID());
1080 aStartLabel = PasteLabel(aStudy, theEngine, _clipboard->Main(), aComponentLabel, aCStudyID, true);
1083 // paste all sublebels
1084 DF_ChildIterator anIterator(_clipboard->Main(), true);
1085 for(; anIterator.More(); anIterator.Next()) {
1086 PasteLabel(aStudy, theEngine, anIterator.Value(), aStartLabel, aCStudyID, false);
1089 return SALOMEDSImpl_Study::SObject(aStartLabel);
1092 //#######################################################################################################
1093 //# STATIC PRIVATE FUNCTIONS
1094 //#######################################################################################################
1096 //============================================================================
1097 /*! Function : SaveAttributes
1098 * Purpose : Save attributes for object
1100 //============================================================================
1101 static void SaveAttributes(const SALOMEDSImpl_SObject& aSO, HDFgroup *hdf_group_sobject)
1104 vector<DF_Attribute*> attrList = aSO.GetLabel().GetAttributes();
1105 DF_Attribute* anAttr = NULL;
1106 for(int i = 0, len = attrList.size(); i<len; i++) {
1107 anAttr = attrList[i];
1108 //The following attributes are not supposed to be written to the file
1109 string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
1110 if(type == string("AttributeIOR")) continue; //IOR attribute is not saved
1111 string aSaveStr =anAttr->Save();
1112 //cout << "Saving: " << aSO.GetID() << " type: "<< type<<"|" << endl;
1113 size[0] = (hdf_int32) strlen(aSaveStr.c_str()) + 1;
1114 HDFdataset *hdf_dataset = new HDFdataset((char*)type.c_str(), hdf_group_sobject, HDF_STRING,size, 1);
1115 hdf_dataset->CreateOnDisk();
1116 hdf_dataset->WriteOnDisk((char*)aSaveStr.c_str());
1117 hdf_dataset->CloseOnDisk();
1118 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
1122 //===========================================================================
1123 //Function : ReadAttributes
1124 //===========================================================================
1125 static void ReadAttributes(SALOMEDSImpl_Study* theStudy,
1126 const SALOMEDSImpl_SObject& aSO,
1127 HDFdataset* hdf_dataset)
1129 hdf_dataset->OpenOnDisk();
1131 DF_Attribute* anAttr = NULL;
1132 char* current_string = new char[hdf_dataset->GetSize()+1];
1133 hdf_dataset->ReadFromDisk(current_string);
1134 //cout << "Reading attr type = " << hdf_dataset->GetName() << " SO = " << aSO.GetID() << endl;
1135 if (!strcmp(hdf_dataset->GetName(),"COMPONENTDATATYPE")) {
1136 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, "AttributeComment");
1137 } else if (!strcmp(hdf_dataset->GetName(),"AttributeReference") ||
1138 !strcmp(hdf_dataset->GetName(),"Reference")) { // Old format maintainance
1139 theStudy->NewBuilder()->Addreference(aSO, theStudy->CreateObjectID(current_string));
1140 delete(current_string);
1141 hdf_dataset->CloseOnDisk();
1144 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, hdf_dataset->GetName());
1148 anAttr->Load(current_string);
1151 delete(current_string);
1152 hdf_dataset->CloseOnDisk();
1155 //============================================================================
1156 //Function : BuildlTree
1157 //============================================================================
1158 static void BuildTree (SALOMEDSImpl_Study* theStudy, HDFgroup* hdf_current_group)
1160 hdf_current_group->OpenOnDisk();
1161 SALOMEDSImpl_SObject aSO;
1162 char* Entry = hdf_current_group->GetName();
1163 if (strcmp(Entry,"STUDY_STRUCTURE") == 0) {
1164 aSO = theStudy->CreateObjectID("0:1");
1167 aSO = theStudy->CreateObjectID(Entry);
1170 char name[HDF_NAME_MAX_LEN+1];
1171 int nbsons = hdf_current_group->nInternalObjects();
1172 for (int i=0; i<nbsons; i++) {
1173 hdf_current_group->InternalObjectIndentify(i,name);
1174 if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
1175 hdf_object_type type = hdf_current_group->InternalObjectType(name);
1177 if (type == HDF_DATASET) {
1178 HDFdataset* new_dataset = new HDFdataset(name,hdf_current_group);
1179 ReadAttributes(theStudy,aSO,new_dataset);
1180 new_dataset = 0; // will be deleted by father destructor
1183 else if (type == HDF_GROUP) {
1184 HDFgroup* new_group = new HDFgroup(name,hdf_current_group);
1185 BuildTree (theStudy, new_group);
1186 new_group = 0; // will be deleted by father destructor
1189 hdf_current_group->CloseOnDisk();
1193 //============================================================================
1194 //Function : Translate_IOR_to_persistentID
1195 //============================================================================
1196 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject& so,
1197 SALOMEDSImpl_Driver* engine,
1201 DF_ChildIterator itchild(so.GetLabel());
1202 string ior_string, persistent_string, curid;
1204 for (; itchild.More(); itchild.Next()) {
1205 SALOMEDSImpl_SObject current = SALOMEDSImpl_Study::SObject(itchild.Value());
1206 SALOMEDSImpl_AttributeIOR* IOR = NULL;
1207 if ((IOR=(SALOMEDSImpl_AttributeIOR*)current.GetLabel().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
1208 ior_string = IOR->Value();
1210 persistent_string = engine->IORToLocalPersistentID (current, ior_string, isMultiFile, isASCII);
1211 SALOMEDSImpl_AttributePersistentRef::Set(current.GetLabel(), persistent_string);
1213 Translate_IOR_to_persistentID (current, engine, isMultiFile, isASCII);