Salome HOME
Fixes for the issue #2375 model with dump/import script
[modules/shaper.git] / src / Model / Model_ResultConstruction.cpp
index b60c6f01263dd644228cf557111a6713a4661edf..0c2e4cac995eb1d078896eaf3390e00e1de6b19b 100644 (file)
 #include <Model_Data.h>
 #include <ModelAPI_CompositeFeature.h>
 #include <GeomAlgoAPI_SketchBuilder.h>
-#include <Model_SelectionNaming.h>
 #include <ModelAPI_Events.h>
-#include <Config_PropManager.h>
+#include <Model_Document.h>
 #include <GeomAPI_PlanarEdges.h>
 #include <GeomAPI_Shape.h>
 #include <Events_Loop.h>
 
-#include <TDF_Reference.hxx>
-#include <TDF_ChildIterator.hxx>
 #include <TDF_ChildIDIterator.hxx>
 #include <TNaming_NamedShape.hxx>
 #include <TNaming_Builder.hxx>
-#include <TDataStd_Integer.hxx>
 #include <TDataStd_IntPackedMap.hxx>
 #include <TDataStd_Name.hxx>
 #include <TDataStd_UAttribute.hxx>
-#include <TColStd_MapOfTransient.hxx>
-#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
-#include <BRep_Tool.hxx>
 #include <BRep_Builder.hxx>
 #include <TopoDS.hxx>
-#include <TopoDS_Shape.hxx>
 #include <TopoDS_Edge.hxx>
 #include <TopoDS_Vertex.hxx>
 #include <TopoDS_ListOfShape.hxx>
 #include <TopExp_Explorer.hxx>
-#include <TNaming_Tool.hxx>
-#include <Precision.hxx>
 #include <TopTools_MapOfShape.hxx>
+#include <NCollection_IndexedDataMap.hxx>
+
+#include <algorithm>
+
 
 // identifier of the infinite result
 Standard_GUID kIS_INFINITE("dea8cc5a-53f2-49c1-94e8-a947bed20a9f");
@@ -72,8 +66,10 @@ void Model_ResultConstruction::setShape(std::shared_ptr<GeomAPI_Shape> theShape)
   if (myShape != theShape) {
     if (!isInfinite())
       storeShape(theShape);
-    static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
-    ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
+    if (!theShape.get() || !theShape->isEqual(myShape)) {
+      static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+      ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
+    }
     myShape = theShape;
   }
 }
@@ -83,11 +79,31 @@ std::shared_ptr<GeomAPI_Shape> Model_ResultConstruction::shape()
   return myShape;
 }
 
+static std::string shortName(
+  std::shared_ptr<ModelAPI_ResultConstruction>& theConstr)
+{
+  std::string aName = theConstr->data()->name();
+  // remove "-", "/" and "&" command-symbols
+  aName.erase(std::remove(aName.begin(), aName.end(), '-'), aName.end());
+  aName.erase(std::remove(aName.begin(), aName.end(), '/'), aName.end());
+  aName.erase(std::remove(aName.begin(), aName.end(), '&'), aName.end());
+  // remove the last 's', 'e', 'f' and 'r' symbols:
+  // they are used as markers of start/end/forward/rewersed indicators
+  static const std::string aSyms("sefr");
+  std::string::iterator aSuffix = aName.end() - 1;
+  while(aSyms.find(*aSuffix) != std::string::npos) {
+    --aSuffix;
+  }
+  aName.erase(aSuffix + 1, aName.end());
+  return aName;
+}
+
+
 bool Model_ResultConstruction::updateShape()
 {
   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
   if (aData && aData->isValid()) {
-    TDF_Label& aShapeLab = aData->shapeLab();
+    TDF_Label aShapeLab = aData->shapeLab();
     Handle(TNaming_NamedShape) aNS;
     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
       TopoDS_Shape aShape = aNS->Get();
@@ -106,7 +122,6 @@ Model_ResultConstruction::Model_ResultConstruction()
 {
 }
 
-
 bool Model_ResultConstruction::isInHistory()
 {
   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
@@ -152,7 +167,7 @@ int Model_ResultConstruction::facesNum(const bool theUpdateNaming)
   int aResult = 0;
   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
   if (aData.get() && aData->isValid()) {
-    TDF_Label& aShapeLab = aData->shapeLab();
+    TDF_Label aShapeLab = aData->shapeLab();
     TDF_ChildIDIterator anOldIter(aShapeLab, TDataStd_IntPackedMap::GetID());
     for (; anOldIter.More(); anOldIter.Next()) {
       aResult++;
@@ -167,7 +182,7 @@ std::shared_ptr<GeomAPI_Face> Model_ResultConstruction::face(const int theIndex)
   int anIndex = 0;
   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
   if (aData.get() && aData->isValid()) {
-    TDF_Label& aShapeLab = aData->shapeLab();
+    TDF_Label aShapeLab = aData->shapeLab();
     TDF_ChildIDIterator anOldIter(aShapeLab, TDataStd_IntPackedMap::GetID());
     for (; anOldIter.More(); anOldIter.Next()) {
       if (anIndex == theIndex) {
@@ -193,7 +208,7 @@ void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShap
   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
   if (aData && aData->isValid()) {
     std::string aMyName = data()->name();
-    TDF_Label& aShapeLab = aData->shapeLab();
+    TDF_Label aShapeLab = aData->shapeLab();
     if (!theShape.get() || theShape->isNull()) {
       aShapeLab.ForgetAllAttributes();
       TDataStd_Name::Set(aShapeLab, aMyName.c_str()); // restore name forgotten
@@ -215,10 +230,10 @@ void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShap
 
       TopExp_Explorer anExp(aShape, TopAbs_VERTEX);
       for(int anIndex = 1; anExp.More(); anExp.Next(), anIndex++) {
-        TDF_Label aSubLab = aShapeLab.FindChild(anIndex);;
+        TDF_Label aSubLab = aShapeLab.FindChild(anIndex);
         TNaming_Builder aBuilder(aSubLab);
         aBuilder.Generated(anExp.Current());
-        std::string aVertexName = anIndex == 1 ? "StartVertex" : "EndVertex";
+        std::string aVertexName = aMyName + "_" + (anIndex == 1 ? "StartVertex" : "EndVertex");
         TDataStd_Name::Set(aSubLab, aVertexName.c_str());
         aMyDoc->addNamingName(aSubLab, aVertexName);
       }
@@ -253,7 +268,7 @@ void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShap
             Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
             aCurvesIndices.Bind(aCurve, a);
             anEdgeIndices.Bind(a, anEdge);
-            aComponentsNames[a] = Model_SelectionNaming::shortName(aConstr);
+            aComponentsNames[a] = shortName(aConstr);
           }
         }
       }
@@ -261,14 +276,15 @@ void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShap
       std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
       GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
         aWirePtr->norm(), aWirePtr, aFaces);
-      NCollection_DataMap<TopoDS_Face, TColStd_ListOfInteger> aNewIndices; // edges indices
+      // order is important to store faces in the same order if sketch is created from scratch
+      NCollection_IndexedDataMap<TopoDS_Face, TColStd_ListOfInteger> aNewIndices; // edges indices
       std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aFIter = aFaces.begin();
       for (; aFIter != aFaces.end(); aFIter++) {
         std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(*aFIter));
         // put them to a label, trying to keep the same faces on the same labels
         if (aFace.get() && !aFace->isNull()) {
           TopoDS_Face aTopoFace = TopoDS::Face(aFace->impl<TopoDS_Shape>());
-          aNewIndices.Bind(aTopoFace, TColStd_ListOfInteger());
+          aNewIndices.Add(aTopoFace, TColStd_ListOfInteger());
           // keep new indices of sub-elements used in this face
           for (TopExp_Explorer anEdges(aTopoFace, TopAbs_EDGE); anEdges.More(); anEdges.Next()) {
             TopoDS_Edge anEdge = TopoDS::Edge(anEdges.Current());
@@ -278,7 +294,7 @@ void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShap
               int anIndex = aCurvesIndices.Find(aCurve);
               if ((aFirst > aLast) != (anEdge.Orientation() == TopAbs_REVERSED))
                 anIndex = -anIndex;
-              aNewIndices.ChangeFind(aTopoFace).Append(anIndex);
+              aNewIndices.ChangeFromKey(aTopoFace).Append(anIndex);
             }
           }
         }
@@ -286,7 +302,8 @@ void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShap
       NCollection_DataMap<int, TopoDS_Face> aFacesOrder; // faces -> tag where they must be set
       NCollection_List<TopoDS_Face> anUnorderedFaces; // faces that may be located at any index
       // searching for the best new candidate to old location
-      NCollection_DataMap<TopoDS_Face, TColStd_ListOfInteger>::Iterator aNewIter(aNewIndices);
+      NCollection_IndexedDataMap<TopoDS_Face, TColStd_ListOfInteger>::Iterator
+        aNewIter(aNewIndices);
       for (; aNewIter.More(); aNewIter.Next()) {
         double aBestFound = 0, aBestNotFound = 1.e+100;
         int aBestTag = 0;
@@ -355,28 +372,29 @@ void Model_ResultConstruction::storeShape(std::shared_ptr<GeomAPI_Shape> theShap
           aFaceBuilder.Generated(aFaceToPut);
           // store also indices of the new face edges
           Handle(TDataStd_IntPackedMap) aNewMap = TDataStd_IntPackedMap::Set(aLab);
-          const TColStd_ListOfInteger& aNewInd = aNewIndices.Find(aFaceToPut);
+          const TColStd_ListOfInteger& aNewInd = aNewIndices.FindFromKey(aFaceToPut);
           std::stringstream aName;
           aName<<"Face";
           TopExp_Explorer aPutEdges(aFaceToPut, TopAbs_EDGE);
           TNaming_Builder* anEdgesBuilder = 0;
           for(TColStd_ListOfInteger::Iterator anIter(aNewInd); anIter.More(); anIter.Next()) {
             int anIndex = anIter.Value();
+            int aModIndex = anIndex > 0 ? anIndex : -anIndex;
             aNewMap->Add(anIndex);
-            aName<<"-"<<aComponentsNames[anIndex > 0 ? anIndex : -anIndex];
+            aName<<"-"<<aComponentsNames[aModIndex];
             if (anIter.Value() > 0)
               aName<<"f";
             else
               aName<<"r";
             // collect all edges of the face which are modified in sub-label of the face
-            if (anEdgeIndices.IsBound(anIndex) &&
-                !aFaceEdges.Contains(anEdgeIndices.Find(anIndex))) {
+            if (anEdgeIndices.IsBound(aModIndex) &&
+                !aFaceEdges.Contains(anEdgeIndices.Find(aModIndex))) {
               if (!anEdgesBuilder) {
                 TDF_Label anEdgesLabel = aLab.FindChild(1);
                 anEdgesBuilder = new TNaming_Builder(anEdgesLabel);
                 TDataStd_Name::Set(anEdgesLabel, "SubEdge");
               }
-              anEdgesBuilder->Modify(anEdgeIndices.Find(anIndex), aPutEdges.Current());
+              anEdgesBuilder->Modify(anEdgeIndices.Find(aModIndex), aPutEdges.Current());
             }
             aPutEdges.Next();
           }