Salome HOME
Bug #334: Different behavior in Edit calculation case input panel.
[modules/hydro.git] / src / HYDROData / HYDROData_SplitToZonesTool.cxx
index a4312ab4671110e8e16b579f9c26ffe467621b7f..d076706fadf98920b6266bf9f770ddc7d9932e80 100644 (file)
 #include <TopoDS_Compound.hxx>
 #include <TopoDS_Wire.hxx>
 #include <TopoDS_Edge.hxx>
-#include <TopoDS_Iterator.hxx>
 #include <BRepCheck_Analyzer.hxx>
-#include <NCollection_IncAllocator.hxx>
-#include <BOPAlgo_Builder.hxx>
-#include <BOPAlgo_PaveFiller.hxx>
-#include <NCollection_Map.hxx>
-#include <NCollection_DataMap.hxx>
 #include <TopTools_ListOfShape.hxx>
 #include <TopTools_ListIteratorOfListOfShape.hxx>
-#include <TopTools_ShapeMapHasher.hxx>
 #include <gp_Pln.hxx>
 #include <BRepGProp.hxx>
 #include <GProp_GProps.hxx>
 #include <Geom_Plane.hxx>
 #include <BRepBuilderAPI_FindPlane.hxx>
 
-
-typedef NCollection_Map<TopoDS_Shape, TopTools_ShapeMapHasher> HYDROData_MapOfShape;
-typedef HYDROData_MapOfShape::Iterator HYDROData_MapIteratorOfMapOfShape;
-typedef NCollection_DataMap<TopoDS_Shape, TopTools_ListOfShape, TopTools_ShapeMapHasher> HYDROData_DataMapOfShapeListOfShape;
-typedef HYDROData_DataMapOfShapeListOfShape::Iterator HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape;
-typedef NCollection_DataMap<TopoDS_Shape,  QStringList, TopTools_ShapeMapHasher> HYDROData_DataMapOfShapeListOfString;
-typedef HYDROData_DataMapOfShapeListOfString::Iterator HYDROData_DataMapIteratorOfDataMapOfShapeListOfString;
-#undef _NCollection_MapHasher
-
 //#define DEB_SPLIT_TO_ZONES 1
-#ifdef DEB_SPLIT_TO_ZONES
+//#define DEB_SPLIT_TO_ZONES_CHECK_PARTITION 1
+#if (defined (DEB_SPLIT_TO_ZONES) || defined(DEB_SPLIT_TO_ZONES_CHECK_PARTITION))
 #include <BRepTools.hxx>
+static TCollection_AsciiString fileNameBefore("BeforeTranslation");
 #endif
+
 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
 {
   TopoDS_Face aResFace;
@@ -88,6 +75,38 @@ Standard_Boolean buildLimFace(const TopoDS_Wire& theBndWire, TopoDS_Face& outFac
   }
   return Standard_False;
 }
+//======================================================================================
+
+void HYDROData_SplitToZonesTool::SetFileNames(const QString& theNameBefore, const QString& theNameAfter)
+{
+#ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
+  if(!theNameBefore.isNull() && !theNameBefore.isEmpty())
+  {
+    TCollection_AsciiString aName (theNameBefore.toStdString().data());
+       fileNameBefore = aName;
+  }
+  if(!theNameAfter.isNull() && !theNameAfter.isEmpty())
+  {
+    TCollection_AsciiString aName (theNameAfter.toStdString().data());
+    HYDROData_Transform::SetFileName (aName);
+  }
+#endif
+}
+//======================================================================================
+Standard_Integer HYDROData_SplitToZonesTool::SplitFaces(const TopoDS_Compound& theComp, 
+                                                                                                               HYDROData_Transform& theTool)
+{
+  theTool.Clear();
+  theTool.SetArgument(theComp);
+  theTool.Detect();
+  const Standard_Boolean bToTransform = theTool.ToTransform();  
+  theTool.SetToTransform(bToTransform);  
+  theTool.Perform();
+  Standard_Integer anErr = theTool.ErrorStatus();
+  return anErr;
+}
+
+//======================================================================================
 HYDROData_SplitToZonesTool::SplitDataList
   HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects&  theObjectList,
                                      const HYDROData_SequenceOfObjects&  theGroupsList,
@@ -98,7 +117,7 @@ HYDROData_SplitToZonesTool::SplitDataList
   // Preparation. 
   // Collect the object shapes to split. InputDataList will contain elements which will hold shape & name_of_shape.
   SplitDataList anInputSplitDataList;
-  TCollection_AsciiString aNam("Faces_");
+  //TCollection_AsciiString aNam("Faces_");
   for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
   {
     Handle(HYDROData_Object) aGeomObj = 
@@ -129,7 +148,7 @@ HYDROData_SplitToZonesTool::SplitDataList
   }
   //
   SplitDataList anInputGroupList;
-  for( int anIndex = 1; anIndex <= theGroupsList.Length(); anIndex++ )
+  for( int anIndex = 1, aLength = theGroupsList.Length(); anIndex <=aLength; anIndex++ )
   {
     Handle(HYDROData_ShapesGroup) aGeomGroup = 
       Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
@@ -151,20 +170,28 @@ HYDROData_SplitToZonesTool::SplitDataList
             SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
             anInputGroupList.append( aSplitData );
           }
-          }
+               }
       } else {
         SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
         anInputGroupList.append( aSplitData );
+#ifdef DEB_SPLIT_TO_ZONES
+               QString aStr = aSplitData.ObjectNames.join(" "); 
+               cout << "Group# = " <<anIndex <<" Nmb in grp = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;   
+#endif
       }
-    }
+       }
   }
   // If only one shape is given we don't split it 
   // algorithm just returns the unpacked input data
-  if(theObjectList.Size() == 1)  {
-       anOutputSplitDataList.append(anInputSplitDataList); 
-       if(!theGroupsList.IsEmpty() ) 
-         anOutputSplitDataList.append(anInputGroupList);       
-    return anOutputSplitDataList;
+  bool limplus1Object(false);
+  if(theObjectList.Size() == 1 )  {
+       if(thePolyline.IsNull()) {
+         anOutputSplitDataList.append(anInputSplitDataList); 
+         if(!theGroupsList.IsEmpty() ) 
+           anOutputSplitDataList.append(anInputGroupList);     
+      return anOutputSplitDataList;
+       } else
+               limplus1Object = true;// size =1 && hasLimits
   }
   HYDROData_DataMapOfShapeListOfString aDM3;
   if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
@@ -174,12 +201,9 @@ HYDROData_SplitToZonesTool::SplitDataList
       aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
     }
   }
-  // Step 1. Prepare Partition structures.
-  Handle(NCollection_BaseAllocator) pA1 = new NCollection_IncAllocator, pA2 = new NCollection_IncAllocator;
-  BOPAlgo_PaveFiller* aPaveFiller      = new BOPAlgo_PaveFiller(pA1);
-  BOPAlgo_Builder* aBuilder            = new BOPAlgo_Builder(pA2);
+  // Step 1. Prepare Partition structures. 
+  TopoDS_Shape aResult;
   BOPCol_ListOfShape aLS;  
-
   QStringList aListOfNames;
   TopoDS_Compound aCmp;
   BRep_Builder aBB;
@@ -192,47 +216,18 @@ HYDROData_SplitToZonesTool::SplitDataList
     //TCollection_AsciiString aName = aNam + i + ".brep";
     //BRepTools::Write(aSh, aName.ToCString());
   }
-  HYDROData_Transform sTool;
-  sTool.Clear();
-  sTool.SetArgument(aCmp);
-  sTool.Detect();
-  const Standard_Boolean bToTransform = sTool.ToTransform();
-  Standard_Integer anErr(0);  
-  sTool.SetToTransform(bToTransform);  
-  sTool.Perform();
-  anErr = sTool.ErrorStatus();
-  if(anErr)
-    return anOutputSplitDataList;
-  const TopoDS_Shape& aResult = sTool.Shape();
-  /* 
-  else {  
-  BOP ==
-  aPaveFiller->SetArguments(aLS);
-  aPaveFiller->Perform();
-  anErr = aPaveFiller->ErrorStatus();
-  if(anErr) 
-    return anOutputSplitDataList;
-  BOPDS_PDS pDS = aPaveFiller->PDS();
-  if (!pDS) 
-    return anOutputSplitDataList;
-  aBuilder->Clear();
-
-  // Step 2. Split faces 
-  BOPCol_ListIteratorOfListOfShape anIt(aLS);
-  for (; anIt.More(); anIt.Next()) {
-    const TopoDS_Shape& aS = anIt.Value();
-    aBuilder->AddArgument(aS);
-  }
-  aBuilder->PerformWithFiller(*aPaveFiller);
-  anErr = aBuilder->ErrorStatus();
+#ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
+  TCollection_AsciiString aNameBefore = fileNameBefore + ".brep";
+  BRepTools::Write(aCmp, aNameBefore.ToCString());
+#endif
 
-  ////
-  if(anErr) 
+  HYDROData_DataMapOfShapeListOfShape aDM1;
+  if(anInputSplitDataList.size() > 1) {
+  HYDROData_Transform splitTool; 
+  const Standard_Integer anErr = SplitFaces(aCmp, splitTool);    
+  if(anErr)
     return anOutputSplitDataList;
-  aResult = aBuilder->Shape();
-  } */
-
+  aResult = splitTool.Shape();
   if (aResult.IsNull()) 
     return anOutputSplitDataList;
   BRepCheck_Analyzer aCheck (aResult);
@@ -248,13 +243,13 @@ HYDROData_SplitToZonesTool::SplitDataList
 #endif
   
   // Step 3. Collect history  
-  HYDROData_DataMapOfShapeListOfShape aDM1;
+  //HYDROData_DataMapOfShapeListOfShape aDM1;
   BOPCol_ListIteratorOfListOfShape anIt(aLS); 
   //TCollection_AsciiString aNamM ("EdgM_");
   //TCollection_AsciiString aNamG ("EdgG_");
   for (int i =1;anIt.More();anIt.Next(),i++) {
        Standard_Boolean foundF(Standard_False);
-    const TopTools_ListOfShape& aListOfNew = /*aBuilder->*/sTool.Modified(anIt.Value());
+    const TopTools_ListOfShape& aListOfNew = splitTool.Modified(anIt.Value());
        if(!aListOfNew.IsEmpty())
          foundF = Standard_True;
     TopTools_ListOfShape aList;
@@ -262,7 +257,7 @@ HYDROData_SplitToZonesTool::SplitDataList
     for(;it.More();it.Next())       
       aList.Append(it.Value());
     // Bug in History: partition should give only modified entities! => temporary solution is used
-    const TopTools_ListOfShape& aListOfGen = /*aBuilder->*/sTool.Generated(anIt.Value());
+    const TopTools_ListOfShape& aListOfGen = splitTool.Generated(anIt.Value());
        if(!aListOfGen.IsEmpty())
          foundF = Standard_True;
        it.Initialize(aListOfGen);    
@@ -271,13 +266,15 @@ HYDROData_SplitToZonesTool::SplitDataList
     if(!foundF) // face is not modified
          aList.Append (anIt.Value());
        aDM1.Bind(anIt.Value(), aList);
-       //TCollection_AsciiString aName;
+#ifdef DEB_SPLIT_TO_ZONES
+       TCollection_AsciiString aName;
+#endif
        if(!anInputGroupList.isEmpty() ) { // 1
-      aList.Clear();
          TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
          for (int j =1;exp.More();exp.Next(),j++) {
+        aList.Clear();
            Standard_Boolean foundE(Standard_False);
-           const TopTools_ListOfShape& aListM = /*aBuilder->*/sTool.Modified(exp.Current());   
+           const TopTools_ListOfShape& aListM = splitTool.Modified(exp.Current());     
                //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
                if(aListM.Extent()) foundE = Standard_True;
                it.Initialize(aListM);    
@@ -286,7 +283,7 @@ HYDROData_SplitToZonesTool::SplitDataList
                  //aName = aNamM + i + j +k +".brep";
                  //BRepTools::Write(it.Value(),aName.ToCString());
                }
-               const TopTools_ListOfShape& aListG = /*aBuilder->*/sTool.Generated(exp.Current());
+               const TopTools_ListOfShape& aListG = splitTool.Generated(exp.Current());
                if(aListG.Extent()) foundE = Standard_True;
                it.Initialize(aListG);    
                for(int k=1;it.More();it.Next(),k++)   {  
@@ -295,23 +292,28 @@ HYDROData_SplitToZonesTool::SplitDataList
                  //BRepTools::Write(it.Value(),aName.ToCString());
                }
                //cout << "NB_EDGE = " << aList.Extent() <<endl;
-               if(!foundE) //{
+               if(!foundE) {
                  aList.Append (exp.Current());
-               //aName = aNamG + i + j +".brep";
-               //BRepTools::Write(exp.Current(),aName.ToCString());
-               //}
+#ifdef DEB_SPLIT_TO_ZONES
+                 aName = aNamG + i + j +".brep";
+                 BRepTools::Write(exp.Current(),aName.ToCString());
+                 cout << aName.ToCString()<< " = " << exp.Current().TShape() <<endl;
+#endif
+               }
                aDM1.Bind(exp.Current(), aList);
-         }      
+         }
        }
   }
-
+  } else 
+         aResult = anInputSplitDataList.at(0).Shape; // get single input shape
   // aDM2: NewShape ==> ListOfOldShapes
   HYDROData_DataMapOfShapeListOfShape aDM2;
   // make limiting face
   HYDROData_DataMapOfShapeListOfShape aDM4;
   Standard_Boolean hasLimits(Standard_False);
-  if (! thePolyline.IsNull()) {         
-    //const TopoDS_Shape aShape = thePolyline->GetShape();     
+  QString aBndName;
+  HYDROData_MapOfShape aBndView;
+  if (! thePolyline.IsNull()) {    
     const TopoDS_Wire aBndWire = TopoDS::Wire(thePolyline->GetShape());
     if(!aBndWire.IsNull()) {
       TopoDS_Face limFace;       
@@ -325,15 +327,21 @@ HYDROData_SplitToZonesTool::SplitDataList
 #ifdef DEB_SPLIT_TO_ZONES
                    BRepTools::Write(aComResult,"CommonV.brep");
 #endif 
-
-                   hasLimits = Standard_True; // DM2 should filled here
+                       aBndName = thePolyline->GetName();
+                   hasLimits = Standard_True; // DM2 should be filled here
+                       TopExp_Explorer exp (limFace, TopAbs_EDGE);
+                       for (int i =1;exp.More();exp.Next(),i++) {
+              const TopoDS_Shape& anEdge = exp.Current();
+                         if(anEdge.IsNull()) continue;
+                         aBndView.Add(anEdge);
+                       }
                    HYDROData_MapOfShape aView;
-                   TopExp_Explorer exp (aResult, TopAbs_FACE);
-                   for (int i =1;exp.More();exp.Next(),i++) {
+                       exp.Init (aResult, TopAbs_FACE);
+                       for (int i =1;exp.More();exp.Next(),i++) {
               const TopoDS_Shape& aFace = exp.Current();
                      if(!aFace.IsNull()) {
                        const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
-                         //cout << "Modified: " << aListOfNew.Extent() <<endl;                 
+                           //cout << "Modified: " << aListOfNew.Extent() <<endl;                       
                 const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);                       
                            if(!aListOfNew.IsEmpty()) {
                      aDM4.Bind(aFace, aListOfNew);
@@ -403,9 +411,29 @@ HYDROData_SplitToZonesTool::SplitDataList
                   }
                 }
                 //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
+                if(limplus1Object) {
+                  // fill DM1 (old - new) and DM2 (new - old)
+                  HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape mIt(aDM4);
+           for(;mIt.More();mIt.Next()) {
+                        const TopoDS_Shape& aKey = mIt.Key();//old
+                        TopTools_ListOfShape aList;
+                        aList.Append(aKey);
+                        const TopTools_ListOfShape& aListOfNew = mIt.Value();
+                        aDM1.Bind(aKey, aListOfNew);
+                        TopTools_ListIteratorOfListOfShape it(aListOfNew);
+                        for(;it.More();it.Next()) {
+                          if(!aDM2.IsBound(it.Value()))
+                                aDM2.Bind(it.Value(), aList);
+                          else {
+                                TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
+                                aList.Prepend(aKey);
+                          }
+                        }
+                  }
+                } else {
          HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1); 
                 //DM4 contains Old - New after common op. DM1: old - new after Split op.
-         for(;aMIt.More();aMIt.Next()) {
+                for(;aMIt.More();aMIt.Next()) {
            const TopoDS_Shape& aKey = aMIt.Key();
            TopTools_ListOfShape aList;
            aList.Append(aKey);
@@ -426,8 +454,9 @@ HYDROData_SplitToZonesTool::SplitDataList
                                  }
                                }
                         }
-                  }//
-                 }
+                  }
+                }
+                }
                 } else {
                         hasLimits = Standard_False;
 #ifdef DEB_SPLIT_TO_ZONES
@@ -437,7 +466,7 @@ HYDROData_SplitToZonesTool::SplitDataList
           }
          }
        }
-  }
+  }// end limits processing
   if(!hasLimits) {
     HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
     for(;aMIt.More();aMIt.Next()) {
@@ -457,8 +486,9 @@ HYDROData_SplitToZonesTool::SplitDataList
        }
   }
   // Step 4. Fill output structure.
-  //TCollection_AsciiString aNam4 ("SC_");
-  
+#ifdef DEB_SPLIT_TO_ZONES
+  TCollection_AsciiString aNam4 ("SC_");
+#endif  
   HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
   for(int i =1;aMIt.More();aMIt.Next(),i++) {
     SplitData aDestSplitData;
@@ -466,27 +496,42 @@ HYDROData_SplitToZonesTool::SplitDataList
     aDestSplitData.Shape = aKey;
     if(aKey.ShapeType() == TopAbs_FACE)
       aDestSplitData.Type = SplitData::Data_Zone;
-    else
+       else {
       aDestSplitData.Type = SplitData::Data_Edge;
 #ifdef DEB_SPLIT_TO_ZONES
-         //TCollection_AsciiString aName = aNam4 + i + ".brep";
-         //BRepTools::Write(aKey,aName.ToCString());
+         TCollection_AsciiString aName = aNam4 + i + ".brep";
+         BRepTools::Write(aKey,aName.ToCString());
 #endif 
-
+       }
 
     QStringList aListOfNames; // names processing
     const TopTools_ListOfShape& aListOfOld = aMIt.Value();
     TopTools_ListIteratorOfListOfShape it(aListOfOld);
-       for(;it.More();it.Next()) {     
+       for(int j =1;it.More();it.Next(),j++) { 
          const TopoDS_Shape& aSh = it.Value(); //old
          if(aDM3.IsBound(aSh)) {
-           const QStringList& ObjectNames = aDM3.Find(aSh);
+           const QStringList& ObjectNames = aDM3.Find(aSh);    
                aListOfNames.append(ObjectNames);
-         }      
+#ifdef DEB_SPLIT_TO_ZONES
+               TCollection_AsciiString aName = aNam4 + i +"_" + j + ".brep";
+           BRepTools::Write(aSh ,aName.ToCString());           
+#endif 
+         }     else {
+                 if(aBndView.Contains(aSh) && hasLimits) {                     
+                       aListOfNames.append(aBndName);
+#ifdef DEB_SPLIT_TO_ZONES
+                       cout << " BndName = "<<aBndName.toStdString() <<endl; 
+#endif
+                 }
+         }
        }
 
        aDestSplitData.ObjectNames = aListOfNames;  
-       anOutputSplitDataList.append(aDestSplitData);
+       anOutputSplitDataList.append(aDestSplitData);   
+#ifdef DEB_SPLIT_TO_ZONES
+       QString aStr = aDestSplitData.ObjectNames.join(" "); 
+       cout << "New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size() <<endl; 
+#endif
   }
  
   return anOutputSplitDataList;