Salome HOME
Issue #1837: make all files with UNIX end lines
[modules/shaper.git] / src / Model / Model_BodyBuilder.cpp
index 9d560ba23e14b9e22eb86ccd8afc8b9ebff8b071..c8d20dbeee3044eb9e378a575cdf81e5110b781f 100755 (executable)
@@ -8,7 +8,6 @@
 
 #include <Model_Data.h>
 #include <Model_Document.h>
-#include <ModelAPI_AttributeIntArray.h>
 #include <TNaming_Builder.hxx>
 #include <TNaming_NamedShape.hxx>
 #include <TNaming_Iterator.hxx>
@@ -67,9 +66,10 @@ static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
     } else {
       TDataStd_Integer::Set(theLab, anEvolution);
     }
-
+    
     for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
-      aShapePairs.push_back(std::pair<TopoDS_Shape, TopoDS_Shape>
+      // iterator goes in reversed order relatively to the Builder, to, make the list reversed
+      aShapePairs.push_front(std::pair<TopoDS_Shape, TopoDS_Shape>
         (anIter.OldShape(), anIter.NewShape()));
     }
   }
@@ -79,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) {
@@ -89,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
@@ -124,18 +124,20 @@ void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
     if (aShape.IsNull())
       return;  // null shape inside
 
-    /*if(!theIsStoreSameShapes) {
+    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)) {
@@ -214,23 +216,35 @@ 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();
+        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);
           TDataStd_Name::Set(aSubBuilder.NamedShape()->Label(), aSolidName.c_str());
         }
       }
+    } else if(!aBuilder.NamedShape()->IsEmpty()) {
+      Handle(TDataStd_Name) anAttr;
+      if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
+        std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
+        if(!aName.empty()) {
+          std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
+          aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
+        }
+      }
     }
   }
 }
@@ -280,10 +294,9 @@ TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
 
 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
 {
-  std::string aName = data()->name() + "/" + theName; 
   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
-  aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), aName);
-  TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
+  //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
+  TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
 }
 void Model_BodyBuilder::generated(
   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
@@ -302,6 +315,22 @@ void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldSh
   builder(theTag)->Generated(anOldShape, aNewShape);
   if(!theName.empty()) 
     buildName(theTag, theName);
+  TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
+  if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
+    TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
+    const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
+    int aTag = 1;
+    std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
+    for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
+      TDF_Label aChildLabel = aLabel.FindChild(aTag);
+      TNaming_Builder aBuilder(aChildLabel);
+      aBuilder.Generated(anOldShape, anExp.Current());
+      TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
+      //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
+      TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
+      aTag++;
+    }
+  }
 }
 
 
@@ -350,13 +379,14 @@ void Model_BodyBuilder::loadAndOrientModifiedShapes (
   GeomAPI_DataMapOfShapeShape& theSubShapes,
   const bool theIsStoreSeparate)
 {
+  int anIndex = 1;
   int aTag = theTag;
+  bool isBuilt = !theName.empty();
   std::string aName = theName;
   std::ostringstream aStream;
-  int anIndex = 1;
+  GeomShapePtr aResultShape = shape();
   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
   TopTools_MapOfShape aView;
-  bool isBuilt = theName.empty();
   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
@@ -367,14 +397,16 @@ void Model_BodyBuilder::loadAndOrientModifiedShapes (
     theMS->modified(aRShape, aList);
     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
     for (; anIt != aLast; anIt++) {
-      TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();    
+      TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
       if (theSubShapes.isBound(*anIt)) {
         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
       }
-      if (!aRoot.IsSame (aNewShape)) {
+      GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
+      aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
+      if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape)) {
         builder(aTag)->Modify(aRoot,aNewShape);
-        if(!isBuilt) {
+        if(isBuilt) {
           if(theIsStoreSeparate) {
             aStream.str(std::string());
             aStream.clear();
@@ -401,7 +433,7 @@ void Model_BodyBuilder::loadAndOrientGeneratedShapes (
 {
   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
   TopTools_MapOfShape aView;
-  bool isBuilt = theName.empty();
+  bool isBuilt = !theName.empty();
   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
@@ -412,15 +444,30 @@ void Model_BodyBuilder::loadAndOrientGeneratedShapes (
     theMS->generated(aRShape, aList);
     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
     for (; anIt != aLast; anIt++) {
-      TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();    
+      TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
       if (theSubShapes.isBound(*anIt)) {
         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
       }
       if (!aRoot.IsSame (aNewShape)) {
         builder(theTag)->Generated(aRoot,aNewShape);
-        if(!isBuilt) 
-          buildName(theTag, theName);  
+        if(isBuilt)
+          buildName(theTag, theName);
+      }
+      TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
+      if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
+        TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
+        const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
+        int aTag = 1;
+        std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
+        for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
+          TDF_Label aChildLabel = aLabel.FindChild(aTag);
+          TNaming_Builder aBuilder(aChildLabel);
+          aBuilder.Generated(aRoot, anExp.Current());
+          TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
+          TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
+          aTag++;
+        }
       }
     }
   }
@@ -672,23 +719,6 @@ void Model_BodyBuilder::loadDisconnectedEdges(
     }
   }
 
-  /*  TopTools_IndexedDataMapOfShapeListOfShape aDM;
-  TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aDM);
-  for(int i=1; i <= aDM.Extent(); i++) {
-  if(aDM.FindFromIndex(i).Extent() > 1) continue;
-  if (BRep_Tool::Degenerated(TopoDS::Edge(aDM.FindKey(i))))
-  continue;
-  builder(theTag)->Generated(aDM.FindKey(i));
-  TCollection_AsciiString aStr(theTag);
-  std::string aName = theName + aStr.ToCString();
-  buildName(theTag, aName);
-  #ifdef DEB_IMPORT
-  aName +=  + ".brep";
-  BRepTools::Write(aDM.FindKey(i), aName.c_str());
-  #endif
-  theTag++;
-  }
-  */
   TopTools_MapOfShape anEdgesToDelete;
   TopExp_Explorer anEx(aShape,TopAbs_EDGE); 
   std::string aName;
@@ -781,10 +811,10 @@ 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();
-    /*Handle(TDF_Reference) aRef;
+    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();