Salome HOME
comments and debug traces on split polylines
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineOperator.cxx
index 41d0f7c831cb4731c3ae87b0c29783cb09de7814..01537030037778b25b120f97755d41ffb34c6119 100644 (file)
@@ -53,7 +53,7 @@
 #include <TopTools_IndexedMapOfShape.hxx>
 #include <BRepLib_MakeWire.hxx>
 
-//#define _DEVDEBUG_
+#define _DEVDEBUG_
 #include "HYDRO_trace.hxx"
 #include <BRepTools.hxx>
 #include <sstream>
@@ -86,13 +86,40 @@ bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theD
   std::vector<TopoDS_Wire> aCurves;
   GetWires(thePolyline, aCurves);
   bool isOK = true;
+
+  std::vector<QColor> aSectColors;
+  int nbSec = thePolyline->NbSections();
+  for (int i = 0; i < nbSec; i++)
+  {
+    QColor aColor;
+    thePolyline->GetSectionColor(i, aColor);
+    aSectColors.push_back(aColor);
+  }
+
+  bool bsetColor = aCurves.size() == aSectColors.size();
   for( int i=0, n=aCurves.size(); i<n; i++ )
   {
     std::vector<TopoDS_Shape> aCurvesList;
     Split( aCurves[i], thePoint, theTolerance, aCurvesList );
-    bool isLocalOK = CreatePolylines( theDoc, thePolyline, aCurvesList, true );
+    NCollection_IndexedDataMap<Handle(HYDROData_PolylineXY), TopoDS_Shape> outPoly2Sh;
+    bool isLocalOK = CreatePolylines( theDoc, thePolyline, aCurvesList, true, outPoly2Sh );
     isOK = isOK && isLocalOK;
+    if (bsetColor)
+    {
+      QColor aColor = aSectColors[i];
+      if( aColor.isValid() )
+      {
+        for (int j=1;j<=outPoly2Sh.Extent();j++)
+        {
+          const Handle(HYDROData_PolylineXY)& Poly = outPoly2Sh.FindKey(j); 
+          int nbSec = Poly->NbSections();
+          for (int k = 0; k < nbSec; k++)
+            Poly->SetSectionColor(k, aColor);
+        }
+      }
+    }
   }
+
   return isOK;
 }
 
@@ -146,7 +173,7 @@ bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theD
     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
     NCollection_Sequence<bool>                              aSectClosures;
-    aPolyline->GetSections( aSectNames, aSectTypes, aSectClosures );
+    aPolyline->GetSections( aSectNames, aSectTypes, aSectClosures );    
 
     for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
     {
@@ -155,7 +182,12 @@ bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theD
       bool aSectClosure = aSectClosures.Value( i );
       aNewPolyline->AddSection(aSectName, aSectType, aSectClosure);
       HYDROData_PolylineXY::PointsList aPointsList = aPolyline->GetPoints(i-1, false);
-      aNewPolyline->SetPoints(ins++, aPointsList);
+      QColor sectColor; 
+      aPolyline->GetSectionColor(i-1, sectColor);
+      aNewPolyline->SetPoints(ins, aPointsList);
+      if (sectColor.isValid())
+        aNewPolyline->SetSectionColor(ins, sectColor);
+      ins++;
     }
   }
   QString aName = theName;
@@ -212,15 +244,32 @@ bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theD
     return false;
   }
 
+  std::vector<QColor> aSectColors;
+  int nbSec = thePolyline->NbSections();
+
   const int aPSCount = aCurves.size();
   const int aTSCount = aToolCurves.size();
   std::vector<TopoDS_Shape> aResult;
+
+  bool bSetColor = aPSCount == nbSec;
+  if (bSetColor)
+  {
+    for (int i = 0; i < nbSec; i++)
+    {
+      QColor aColor;
+      thePolyline->GetSectionColor(i, aColor);
+      aSectColors.push_back(aColor);
+    }
+  }
+  NCollection_DataMap <TopoDS_Shape, QColor, TopTools_ShapeMapHasher> W2Color;
+
   for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
   {
     HYDROData_TopoCurve aCurve;
     DEBTRACE("Initialize curve " << aPSI);
-    if (!aCurve.Initialize(aCurves[aPSI]))
+    if (!aCurve.Initialize(aCurves[aPSI]))    // empty, non manifold (or internal bug!)
     {
+      DEBTRACE("discarded curve (empty or non manifold)!");
       continue;
     }
 
@@ -238,16 +287,35 @@ bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theD
       aSplitCurves.end();
     for (int iw=0; aCIt != aLastCIt; ++aCIt, iw++)
     {
-      std::stringstream brepName;
-      brepName << "theSplitWire_";
-      brepName << iw;
-      brepName << ".brep";
-      BRepTools::Write(aCIt->Wire() , brepName.str().c_str() );
-      aResult.push_back(aCIt->Wire());
+      //std::stringstream brepName;
+      //brepName << "theSplitWire_";
+      //brepName << iw;
+      //brepName << ".brep";
+      //BRepTools::Write(aCIt->Wire() , brepName.str().c_str() );
+      const TopoDS_Wire& aW = aCIt->Wire();
+      if (bSetColor)
+        W2Color.Bind(aW, aSectColors[aPSI]);
+      aResult.push_back(aW);
     }
   }
 
-  CreatePolylines( theDoc, thePolyline, aResult, true );
+  NCollection_IndexedDataMap<Handle(HYDROData_PolylineXY), TopoDS_Shape> outPoly2Sh;
+  CreatePolylines( theDoc, thePolyline, aResult, true, outPoly2Sh );
+  
+  for (int j=1;j<=outPoly2Sh.Extent();j++)
+  {
+    const Handle(HYDROData_PolylineXY)& Poly = outPoly2Sh.FindKey(j); 
+    const TopoDS_Shape& Sh = outPoly2Sh.FindFromIndex(j); 
+
+    int nbSec = Poly->NbSections();
+    for (int k = 0; k < nbSec; k++)
+    {
+      const QColor* color = W2Color.Seek(Sh);
+      if (color)
+        Poly->SetSectionColor(k, *color);
+    }
+  }
+  
   return true;
 }
 
@@ -301,7 +369,8 @@ void HYDROData_PolylineOperator::Split(
 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
                                                   const Handle( HYDROData_PolylineXY )& theOldPolyline,
                                                   const std::vector<TopoDS_Shape>& theShapes,
-                                                  bool isUseIndices )
+                                                  bool isUseIndices,
+                                                  NCollection_IndexedDataMap<Handle(HYDROData_PolylineXY), TopoDS_Shape>& outPoly2Sh)
 {
   if( theDoc.IsNull() )
     return false;
@@ -309,7 +378,6 @@ bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Docume
   if ( theOldPolyline.IsNull() )
     return false;
   const QString& theNamePrefix = theOldPolyline->GetName();
-  const QColor& theColor = theOldPolyline->GetWireColor();
 
   int n = theShapes.size();
   DEBTRACE("theShapes.size() "<< n);
@@ -338,8 +406,7 @@ bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Docume
       aPolyline->SetName( theNamePrefix );
     }
 
-    if( theColor.isValid() )
-      aPolyline->SetWireColor( theColor );
+    outPoly2Sh.Add(aPolyline, theShapes[i]);
   }
   return true;
 }