Salome HOME
0014047: EDF PAL 334 : Problem to select merged face with Create group window
[modules/geom.git] / src / GEOM / GEOM_Engine.cxx
index 3ac30e5ab278290a3290f74e1b87562b62897be4..5693abcebe0a1d7ba7d755acff8f016605beb9bc 100644 (file)
@@ -116,6 +116,25 @@ GEOM_Engine::GEOM_Engine()
   _UndoLimit = 10;
 }
 
+/*!
+ *  Destructor
+ */
+GEOM_Engine::~GEOM_Engine()
+{ 
+  GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
+  for(; It.More(); It.Next()) 
+    {
+      RemoveObject(Handle(GEOM_Object)::DownCast(It.Value()));
+    }
+
+  //Close all documents not closed
+  for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
+    Close(anItr.Key());
+
+  _mapIDDocument.Clear();
+  _objects.Clear();
+}
+
 //=============================================================================
 /*!
  *  GetDocument
@@ -176,18 +195,33 @@ Handle(GEOM_Object) GEOM_Engine::GetObject(int theDocID, char* theEntry)
 //=============================================================================
 Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
 {
-    Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
-    Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
+  Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
+  Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
+
+  // NPAL18604: use existing label to decrease memory usage,
+  //            if this label has been freed (object deleted)
+  bool useExisting = false;
+  TDF_Label aChild;
+  if (!_lastCleared.IsNull()) {
+    if (_lastCleared.Root() == aDoc->Main().Root()) {
+      useExisting = true;
+      aChild = _lastCleared;
+      _lastCleared.Nullify();
+    }
+  }
+  if (!useExisting) {
+    // create new label
+    aChild = TDF_TagSource::NewChild(aDoc->Main());
+  }
 
-    TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
-    Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
+  Handle(GEOM_Object) anObject = new GEOM_Object(aChild, theType);
 
-    //Put an object in the map of created objects
-    TCollection_AsciiString anID = BuildIDFromObject(anObject);
-    if(_objects.IsBound(anID)) _objects.UnBind(anID);
-    _objects.Bind(anID, anObject);
+  //Put an object in the map of created objects
+  TCollection_AsciiString anID = BuildIDFromObject(anObject);
+  if(_objects.IsBound(anID)) _objects.UnBind(anID);
+  _objects.Bind(anID, anObject);
 
-    return anObject;
+  return anObject;
 }
 
 //=============================================================================
@@ -204,7 +238,21 @@ Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
   Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
   Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
 
-  TDF_Label aChild = TDF_TagSource::NewChild(aDoc->Main());
+  // NPAL18604: use existing label to decrease memory usage,
+  //            if this label has been freed (object deleted)
+  bool useExisting = false;
+  TDF_Label aChild;
+  if (!_lastCleared.IsNull()) {
+    if (_lastCleared.Root() == aDoc->Main().Root()) {
+      useExisting = true;
+      aChild = _lastCleared;
+      _lastCleared.Nullify();
+    }
+  }
+  if (!useExisting) {
+    // create new label
+    aChild = TDF_TagSource::NewChild(aDoc->Main());
+  }
 
   Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
   Handle(GEOM_Object) anObject = new GEOM_Object(aChild, 28); //28 is SUBSHAPE type
@@ -258,22 +306,23 @@ Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
 //=============================================================================
 bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
 {
-  if(!theObject) return false;
+  if (!theObject) return false;
 
   //Remove an object from the map of available objects
   TCollection_AsciiString anID = BuildIDFromObject(theObject);
-  if(_objects.IsBound(anID)) _objects.UnBind(anID);
+  if (_objects.IsBound(anID)) _objects.UnBind(anID);
 
   int nb = theObject->GetNbFunctions();
   Handle(TDataStd_TreeNode) aNode;
-  for(int i = 1; i<=nb; i++) {
+  for (int i = 1; i<=nb; i++) {
     Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
-    if(aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode)) 
+    if (aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode)) 
       aNode->Remove();
   }
 
   TDF_Label aLabel = theObject->GetEntry();
   aLabel.ForgetAllAttributes(Standard_True);
+  _lastCleared = aLabel;
 
   theObject.Nullify();
 
@@ -344,20 +393,22 @@ bool GEOM_Engine::Load(int theDocID, char* theFileName)
 //=============================================================================
 void GEOM_Engine::Close(int theDocID)
 {
-  if(_mapIDDocument.IsBound(theDocID)) {
+  if (_mapIDDocument.IsBound(theDocID)) {
     Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
 
     //Remove all GEOM Objects associated to the given document
     TColStd_SequenceOfAsciiString aSeq;
-    GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It(_objects);
-    for(; It.More(); It.Next()) {
-      TCollection_AsciiString anObjID(It.Key());
+    GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
+    for (; It.More(); It.Next()) {
+      TCollection_AsciiString anObjID (It.Key());
       Standard_Integer anID = ExtractDocID(anObjID);
-      if(theDocID == anID) aSeq.Append(It.Key());
+      if (theDocID == anID) aSeq.Append(It.Key());
     }
-    for(Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
+    for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
 
-   _mapIDDocument.UnBind(theDocID);
+    _lastCleared.Nullify();
+
+    _mapIDDocument.UnBind(theDocID);
     _OCAFApp->Close(aDoc);
     aDoc.Nullify();
   }
@@ -368,35 +419,35 @@ void GEOM_Engine::Close(int theDocID)
  *  DumpPython
  */
 //=============================================================================
-TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID, 
+TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
                                                Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
-                                               bool isPublished, 
+                                               bool isPublished,
                                                bool& aValidScript)
 {
   TCollection_AsciiString aScript;
   Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
-  
-  if(aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
+
+  if (aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
+
   aScript = "import geompy\n";
   aScript += "import math\n";
   aScript += "import SALOMEDS\n\n";
   aScript += "def RebuildData(theStudy):";
   aScript += "\n\tgeompy.init_geom(theStudy)";
-  
+
   Standard_Integer posToInertGlobalVars = aScript.Length() + 1;
 
   Handle(TDataStd_TreeNode) aNode, aRoot;
   Handle(GEOM_Function) aFunction;
   TColStd_MapOfTransient aMap;
 
-  if(aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
+  if (aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
     TDataStd_ChildNodeIterator Itr(aRoot);
-    for(; Itr.More(); Itr.Next()) {
+    for (; Itr.More(); Itr.Next()) {
       aNode = Itr.Value();
       aFunction = GEOM_Function::GetFunction(aNode->Label());
-      if(aFunction.IsNull()) {
-       cout << "Null function !!!!" << endl;
+      if (aFunction.IsNull()) {
+       MESSAGE ( "Null function !!!!" );
        continue;
       }
       ProcessFunction(aFunction, aScript, aMap);
@@ -431,12 +482,12 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
   //Replace entries by the names
   TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_"),
     allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
-  if(aLen == 0) anUpdatedScript = aScript;
+  if (aLen == 0) anUpdatedScript = aScript;
 
-  for(Standard_Integer i = 1; i <= aLen; i+=2) {
+  for (Standard_Integer i = 1; i <= aLen; i+=2) {
     anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
     anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
-    if(theObjectNames.IsBound(anEntry)) {
+    if (theObjectNames.IsBound(anEntry)) {
       aName = theObjectNames.Find(anEntry);
       // check validity of aName
       bool isValidName = true;
@@ -479,8 +530,9 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
   }
 
   //Add final part of the script
-  if(aLen && aSeq->Value(aLen) < aScriptLength)  anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
+  if (aLen && aSeq->Value(aLen) < aScriptLength)
+    anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength); // mkr : IPAL11865
+
   // ouv : NPAL12872
   for (anEntryToNameIt.Initialize( theObjectNames );
        anEntryToNameIt.More();
@@ -518,7 +570,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
   // Make script to publish in study
   if ( isPublished )
   {
-    map< int, string > anEntryToCommandMap; // sort publishing commands by object entry
+    std::map< int, std::string > anEntryToCommandMap; // sort publishing commands by object entry
     for (anEntryToNameIt.Initialize( theObjectNames );
          anEntryToNameIt.More();
          anEntryToNameIt.Next())
@@ -551,17 +603,18 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
       // bind a command to the last digit of the entry
       int tag =
         aEntry.SubString( aEntry.SearchFromEnd(":")+1, aEntry.Length() ).IntegerValue();
-      anEntryToCommandMap.insert( make_pair( tag, aCommand.ToCString() ));
+      anEntryToCommandMap.insert( std::make_pair( tag, aCommand.ToCString() ));
     }
 
     // add publishing commands to the script
-    map< int, string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
+    std::map< int, std::string >::iterator anEntryToCommand = anEntryToCommandMap.begin();
     for ( ; anEntryToCommand != anEntryToCommandMap.end(); ++anEntryToCommand ) {
       anUpdatedScript += (char*)anEntryToCommand->second.c_str();
     }
   }
 
-  anUpdatedScript += "\n\tpass\n";
+  //anUpdatedScript += "\n\tpass\n";
+  anUpdatedScript += "\n";
   aValidScript = true;
 
   // fill _studyEntry2NameMap and build globalVars
@@ -582,7 +635,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
     globalVars.Insert( 1, "\n\tglobal " );
     anUpdatedScript.Insert( posToInertGlobalVars, globalVars );
   }
-  
+
   return anUpdatedScript;
 }
 
@@ -663,7 +716,7 @@ Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theStrin
   Standard_Integer aLen = theString.Length();
   Standard_Boolean isFound = Standard_False;
 
-  char* arr = theString.ToCString();
+  const char* arr = theString.ToCString();
   Standard_Integer i = 0, j;
 
   while(i < aLen) {