Salome HOME
Merge from V5_1_4_BR (5_1_4rc2) 09/06/2010
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_StudyBuilder.cxx
index 801307cecb0060e06522ab1b79d7f1b2b443e323..b42685c3f828f079f66e1526716262db8168f3b6 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -19,6 +19,7 @@
 //
 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File   : SALOMEDSImpl_StudyBuilder.cxx
 //  Author : Sergey RUIN
 //  Module : SALOME
@@ -36,8 +37,7 @@
 
 #include <HDFOI.hxx>
 #include <stdlib.h> 
-
-using namespace std;
+#include <string.h> 
 
 #define USE_CASE_LABEL_TAG            2
 #define DIRECTORYID                   16661
@@ -72,7 +72,7 @@ SALOMEDSImpl_StudyBuilder::~SALOMEDSImpl_StudyBuilder()
  *  Purpose  : Create a new component (Scomponent)
  */
 //============================================================================
-SALOMEDSImpl_SComponent SALOMEDSImpl_StudyBuilder::NewComponent(const string& DataType)
+SALOMEDSImpl_SComponent SALOMEDSImpl_StudyBuilder::NewComponent(const std::string& DataType)
 {
   _errorCode = "";
   CheckLocked();
@@ -103,7 +103,7 @@ SALOMEDSImpl_SComponent SALOMEDSImpl_StudyBuilder::NewComponent(const string& Da
  */
 //============================================================================
 bool SALOMEDSImpl_StudyBuilder::DefineComponentInstance(const SALOMEDSImpl_SComponent& aComponent,
-                                                       const string& IOR)
+                                                        const std::string& IOR)
 {
    _errorCode = "";
 
@@ -159,7 +159,7 @@ SALOMEDSImpl_SObject SALOMEDSImpl_StudyBuilder::NewObject(const SALOMEDSImpl_SOb
  */
 //============================================================================
 SALOMEDSImpl_SObject SALOMEDSImpl_StudyBuilder::NewObjectToTag(const SALOMEDSImpl_SObject& theFatherObject,
-                                                       const int theTag)
+                                                        const int theTag)
 {
   _errorCode = "";
   CheckLocked();
@@ -204,6 +204,7 @@ bool SALOMEDSImpl_StudyBuilder::RemoveObject(const SALOMEDSImpl_SObject& anObjec
 
   SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
   if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
+    _study->DeleteIORLabelMapItem(anAttr->Value());
   }
 
   Lab.ForgetAllAttributes();
@@ -239,6 +240,7 @@ bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const SALOMEDSImpl_SObj
   }
   SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
   if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
+    _study->DeleteIORLabelMapItem(anAttr->Value());
   }
 
   DF_ChildIterator it(Lab, true);
@@ -247,10 +249,11 @@ bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const SALOMEDSImpl_SObj
     if ((aReference=(SALOMEDSImpl_AttributeReference*)aLabel.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
       SALOMEDSImpl_AttributeTarget* aTarget = NULL;
       if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
-       aTarget->Remove(SALOMEDSImpl_Study::SObject(aLabel));
+        aTarget->Remove(SALOMEDSImpl_Study::SObject(aLabel));
     }
     SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
     if ((anAttr=(SALOMEDSImpl_AttributeIOR*)aLabel.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
+      _study->DeleteIORLabelMapItem(anAttr->Value());
     }
   }
 
@@ -280,7 +283,7 @@ bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
     if (aLocked) _study->GetProperties()->SetLocked(false);
 
     std::string Res(Att->Value());
-    string aHDFPath(Res);
+    std::string aHDFPath(Res);
 
     SALOMEDSImpl_AttributeComment* type = NULL;
     std::string DataType;
@@ -302,7 +305,7 @@ bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
 
     DefineComponentInstance (anSCO, aDriver->GetIOR());
 
-    string aHDFUrl;
+    std::string aHDFUrl;
     bool isASCII = false;
     if (HDFascii::isASCII(aHDFPath.c_str())) {
       isASCII = true;
@@ -317,26 +320,28 @@ bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
 
     char aMultifileState[2];
     char ASCIIfileState[2];
+    bool hasModuleData = false;
     try {
-      string scoid = anSCO.GetID();
+      std::string scoid = anSCO.GetID();
       hdf_file->OpenOnDisk(HDF_RDONLY);
       HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
       hdf_group->OpenOnDisk();
       HDFgroup *hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group);
       hdf_sco_group->OpenOnDisk();
+      hasModuleData = true;
 
       unsigned char* aStreamFile = NULL;
       int aStreamSize = 0;
 
       if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
-       HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
-       hdf_dataset->OpenOnDisk();
-       aStreamSize = hdf_dataset->GetSize();
-       aStreamFile  = new unsigned char[aStreamSize];
-       if(aStreamFile == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
-       hdf_dataset->ReadFromDisk(aStreamFile);
-       hdf_dataset->CloseOnDisk();
-       hdf_dataset = 0;
+        HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
+        hdf_dataset->OpenOnDisk();
+        aStreamSize = hdf_dataset->GetSize();
+        aStreamFile  = new unsigned char[aStreamSize];
+        if(aStreamFile == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
+        hdf_dataset->ReadFromDisk(aStreamFile);
+        hdf_dataset->CloseOnDisk();
+        hdf_dataset = 0;
       } else
         aStreamFile = NULL;
 
@@ -348,19 +353,19 @@ bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
       ascii_hdf_dataset->OpenOnDisk();
       ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
 
-      string aDir = SALOMEDSImpl_Tool::GetDirFromPath(Res);
+      std::string aDir = SALOMEDSImpl_Tool::GetDirFromPath(Res);
 
       bool aResult = (ASCIIfileState[0]=='A')?
-       aDriver->LoadASCII(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M'):
-       aDriver->Load(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M');
+        aDriver->LoadASCII(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M'):
+        aDriver->Load(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M');
 
       if(aStreamFile != NULL) delete []aStreamFile; 
 
       if(!aResult) {
-       RemoveAttribute( anSCO, "AttributeIOR" );
+        RemoveAttribute( anSCO, "AttributeIOR" );
 
-       _errorCode = "Can't load component";
-       throw HDFexception("Unable to load component");
+        _errorCode = "Can't load component";
+        throw HDFexception("Unable to load component");
       }
 
       //if(aDir != NULL) delete []aDir;
@@ -378,9 +383,9 @@ bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
       delete hdf_file;
 
       if (isASCII) {
-       vector<string> aFilesToRemove;
-       aFilesToRemove.push_back("hdf_from_ascii.hdf");
-       SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl),
+        std::vector<std::string> aFilesToRemove;
+        aFilesToRemove.push_back("hdf_from_ascii.hdf");
+        SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl),
                                                 aFilesToRemove, true);
       }      
     }
@@ -388,12 +393,16 @@ bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
       delete hdf_file;
 
       if (isASCII) {
-       vector<string> aFilesToRemove;
-       aFilesToRemove.push_back(aHDFUrl);
-       SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
+        std::vector<std::string> aFilesToRemove;
+        aFilesToRemove.push_back(aHDFUrl);
+        SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
       }
 
       if (aLocked) _study->GetProperties()->SetLocked(true);
+
+      if (!hasModuleData)
+       return true;
+
       _errorCode = "No persistent file";   
       return false;
     }
@@ -432,7 +441,7 @@ bool SALOMEDSImpl_StudyBuilder::Load(const SALOMEDSImpl_SObject& sco)
  */
 //============================================================================
 DF_Attribute* SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const SALOMEDSImpl_SObject& anObject, 
-                                                              const string& aTypeOfAttribute)
+                                                               const std::string& aTypeOfAttribute)
 {
   _errorCode = "";
   if(!anObject) {
@@ -455,7 +464,7 @@ DF_Attribute* SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const SALOMEDSImp
   //Add checks for TreeNode and UserID attributes  
   if (strncmp(aTypeOfAttribute.c_str(), "AttributeTreeNode",17) == 0 ) {
     
-    string aTreeNodeGUID;
+    std::string aTreeNodeGUID;
     if (strcmp(aTypeOfAttribute.c_str(), "AttributeTreeNode") == 0) {
       aTreeNodeGUID = SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID();
     } else {
@@ -495,8 +504,8 @@ DF_Attribute* SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const SALOMEDSImp
 //============================================================================
 
 bool SALOMEDSImpl_StudyBuilder::FindAttribute(const SALOMEDSImpl_SObject& anObject, 
-                                             DF_Attribute*& anAttribute, 
-                                             const string& aTypeOfAttribute)
+                                              DF_Attribute*& anAttribute, 
+                                              const std::string& aTypeOfAttribute)
 {
   _errorCode = "";
   if(!anObject) {
@@ -519,7 +528,7 @@ bool SALOMEDSImpl_StudyBuilder::FindAttribute(const SALOMEDSImpl_SObject& anObje
 //============================================================================
 
 bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const SALOMEDSImpl_SObject& anObject, 
-                                               const string& aTypeOfAttribute)
+                                                const std::string& aTypeOfAttribute)
 {
   _errorCode = "";
   CheckLocked();
@@ -529,9 +538,10 @@ bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const SALOMEDSImpl_SObject& anOb
   }
   DF_Label Lab = anObject.GetLabel();
   
-  if (aTypeOfAttribute == string("AttributeIOR")) { // Remove from IORLabel map
+  if (aTypeOfAttribute == std::string("AttributeIOR")) { // Remove from IORLabel map
     SALOMEDSImpl_AttributeIOR* anAttr = NULL;
     if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
+      _study->DeleteIORLabelMapItem(anAttr->Value());
     }
   }
 
@@ -548,7 +558,7 @@ bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const SALOMEDSImpl_SObject& anOb
  */
 //============================================================================
 bool SALOMEDSImpl_StudyBuilder::Addreference(const SALOMEDSImpl_SObject& me, 
-                                            const SALOMEDSImpl_SObject& theReferencedObject)
+                                             const SALOMEDSImpl_SObject& theReferencedObject)
 {
   _errorCode = "";
   if(!me || !theReferencedObject) {
@@ -605,7 +615,7 @@ bool SALOMEDSImpl_StudyBuilder::RemoveReference(const SALOMEDSImpl_SObject& me)
  *  Purpose  : adds a new directory with a path = thePath
  */
 //============================================================================
-bool SALOMEDSImpl_StudyBuilder::AddDirectory(const string& thePath) 
+bool SALOMEDSImpl_StudyBuilder::AddDirectory(const std::string& thePath) 
 {
   _errorCode = "";
   CheckLocked();
@@ -614,7 +624,7 @@ bool SALOMEDSImpl_StudyBuilder::AddDirectory(const string& thePath)
     return false;
   }
 
-  string aPath(thePath), aContext(""), aFatherPath;
+  std::string aPath(thePath), aContext(""), aFatherPath;
   DF_Label aLabel;
   SALOMEDSImpl_SObject anObject;
 
@@ -633,7 +643,7 @@ bool SALOMEDSImpl_StudyBuilder::AddDirectory(const string& thePath)
     aPath = _study->GetContext() + aPath;
   }
 
-  vector<string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
+  std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
   if(vs.size() == 1) 
     aFatherPath = "/";
   else {
@@ -676,7 +686,7 @@ bool SALOMEDSImpl_StudyBuilder::AddDirectory(const string& thePath)
  */
 //============================================================================
 bool SALOMEDSImpl_StudyBuilder::SetGUID(const SALOMEDSImpl_SObject& anObject, 
-                                       const string& theGUID)
+                                        const std::string& theGUID)
 {
   _errorCode = "";
   CheckLocked();
@@ -699,7 +709,7 @@ bool SALOMEDSImpl_StudyBuilder::SetGUID(const SALOMEDSImpl_SObject& anObject,
  */
 //============================================================================
 bool SALOMEDSImpl_StudyBuilder::IsGUID(const SALOMEDSImpl_SObject& anObject, 
-                                      const string& theGUID)
+                                       const std::string& theGUID)
 {
   _errorCode = "";
   if(!anObject) {
@@ -909,7 +919,7 @@ void SALOMEDSImpl_StudyBuilder::CheckLocked()
  */
 //============================================================================
 bool SALOMEDSImpl_StudyBuilder::SetName(const SALOMEDSImpl_SObject& theSO, 
-                                       const string& theValue)
+                                        const std::string& theValue)
 {
   _errorCode = "";
   CheckLocked();
@@ -930,7 +940,7 @@ bool SALOMEDSImpl_StudyBuilder::SetName(const SALOMEDSImpl_SObject& theSO,
  */
 //============================================================================
 bool SALOMEDSImpl_StudyBuilder::SetComment(const SALOMEDSImpl_SObject& theSO, 
-                                          const string& theValue)
+                                           const std::string& theValue)
 {
   _errorCode = "";
   CheckLocked();
@@ -951,7 +961,7 @@ bool SALOMEDSImpl_StudyBuilder::SetComment(const SALOMEDSImpl_SObject& theSO,
  */
 //============================================================================
 bool SALOMEDSImpl_StudyBuilder::SetIOR(const SALOMEDSImpl_SObject& theSO, 
-                                      const string& theValue)
+                                       const std::string& theValue)
 {
   _errorCode = "";
   CheckLocked();
@@ -984,14 +994,14 @@ static void Translate_persistentID_to_IOR(DF_Label& Lab, SALOMEDSImpl_Driver* dr
 
       SALOMEDSImpl_AttributeLocalID* anID = NULL;
       if ((anID=(SALOMEDSImpl_AttributeLocalID*)current.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) 
-       if (anID->Value() == FILELOCALID) continue;   //SRN: This attribute store a file name, skip it 
+        if (anID->Value() == FILELOCALID) continue;   //SRN: This attribute store a file name, skip it 
 
-      string persist_ref = Att->Value();
+      std::string persist_ref = Att->Value();
       SALOMEDSImpl_SObject so = SALOMEDSImpl_Study::SObject(current);
-      string ior_string = driver->LocalPersistentIDToIOR(so, 
-                                                        persist_ref, 
-                                                        isMultiFile, 
-                                                        isASCII);
+      std::string ior_string = driver->LocalPersistentIDToIOR(so, 
+                                                         persist_ref, 
+                                                         isMultiFile, 
+                                                         isASCII);
       SALOMEDSImpl_AttributeIOR::Set (current, ior_string); 
      
     }