Salome HOME
Issue #1315 Middle point constraint problem
[modules/shaper.git] / src / Model / Model_BodyBuilder.cpp
index e8b3479d78b202275129ab0af0c1f5d21b38d82d..1d3b8a4d506d48ef85e711e995ad7c75e8568f93 100755 (executable)
@@ -18,6 +18,7 @@
 #include <TopoDS.hxx>
 #include <TopoDS_Face.hxx>
 #include <TDF_ChildIterator.hxx>
+#include <TDF_Reference.hxx>
 #include <TopTools_MapOfShape.hxx>
 #include <TopExp_Explorer.hxx>
 #include <TopTools_ListOfShape.hxx>
@@ -78,7 +79,7 @@ static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
   std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
   for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
     if (theFlag) { // disabled => make selection
-      aBuilder.Select(aPairsIter->first, aPairsIter->second);
+      aBuilder.Select(aPairsIter->second, aPairsIter->first);
     } else if (anEvol == TNaming_GENERATED) {
       aBuilder.Generated(aPairsIter->first, aPairsIter->second);
     } else if (anEvol == TNaming_MODIFY) {
@@ -88,7 +89,7 @@ static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
     } else if (anEvol == TNaming_PRIMITIVE) {
       aBuilder.Generated(aPairsIter->second);
     } else if (anEvol == TNaming_SELECTED) {
-      aBuilder.Select(aPairsIter->first, aPairsIter->second);
+      aBuilder.Select(aPairsIter->second, aPairsIter->first);
     }
   }
   // recursive call for all sub-labels
@@ -107,7 +108,8 @@ void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
   evolutionToSelectionRec(aShapeLab, theFlag);
 }
 
-void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
+void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
+                              const bool theIsStoreSameShapes)
 {
   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
   if (aData) {
@@ -122,8 +124,20 @@ void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
     if (aShape.IsNull())
       return;  // null shape inside
 
+    if(!theIsStoreSameShapes) {
+      Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
+      if(!aNS.IsNull() && !aNS->IsEmpty()) {
+        // This shape is already in document, store reference instead of shape;
+        const TDF_Label aFoundLabel = aNS->Label();
+        TDF_Reference::Set(aShapeLab, aFoundLabel);
+        aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
+        return;
+      }
+    }
+
     aBuilder.Generated(aShape);        
     // register name
+    aShapeLab.ForgetAttribute(TDF_Reference::GetID());
     if(!aBuilder.NamedShape()->IsEmpty()) {
       Handle(TDataStd_Name) anAttr;
       if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
@@ -202,11 +216,20 @@ void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theO
 
       TopoDS_Iterator aSubIter(aShapeNew);
       for(int aTag = theDecomposeSolidsTag; aSubIter.More(); aSubIter.Next()) {
+        const TopoDS_Shape& aShape = aSubIter.Value();
+        Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
+        if(!aNS.IsNull() && !aNS->IsEmpty()) {
+          // This shape is already in document, don't add it.
+          continue;
+        }
         TNaming_Builder aSubBuilder(aShapeLab.FindChild(aTag++));
         aSubBuilder.Generated(aSubIter.Value());
         if(!aName.IsEmpty()) {
+          TCollection_AsciiString aShapeType = aShape.ShapeType() == TopAbs_EDGE ? "_Edge_" :
+                                               aShape.ShapeType() == TopAbs_FACE ? "_Face_" :
+                                               aShape.ShapeType() == TopAbs_SOLID ? "_Solid_" : "_Shape_";
           std::string aSolidName = 
-            (aName + "_Solid_" + TCollection_AsciiString(aTag - theDecomposeSolidsTag)).ToCString(); 
+            (aName + aShapeType + TCollection_AsciiString(aTag - theDecomposeSolidsTag)).ToCString(); 
           std::shared_ptr<Model_Document> aDoc = 
             std::dynamic_pointer_cast<Model_Document>(document());
           aDoc->addNamingName(aSubBuilder.NamedShape()->Label(), aSolidName);
@@ -762,7 +785,11 @@ std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
 {
   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
   if (aData) {
-    TDF_Label& aShapeLab = aData->shapeLab();
+    TDF_Label aShapeLab = aData->shapeLab();
+    Handle(TDF_Reference) aRef;
+    if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
+      aShapeLab = aRef->Get();
+    }
     Handle(TNaming_NamedShape) aName;
     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
       TopoDS_Shape aShape = aName->Get();