Salome HOME
17.12.2013. Added Partition algorithm (draft version).
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.cxx
index a8cb4c0f7c3e837ac5e3e2f5eeed23579e76f7d7..eaea172a650df3981ce37149b8c72b573c54743c 100755 (executable)
 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
 #include <TDataStd_RealList.hxx>
 
+#include <TopoDS_Iterator.hxx>
 #include <TopTools_ListIteratorOfListOfShape.hxx>
-
+#include <TopTools_HSequenceOfShape.hxx>
+#include <ShapeAnalysis_FreeBounds.hxx>
+#include <TopoDS.hxx>
+#include <QColor>
 #include <QPainterPath>
 #include <QVariant>
 
 #define PYTHON_POLYLINEXY_ID "KIND_POLYLINEXY"
 
+const double LOCAL_SELECTION_TOLERANCE = 0.0001;
+
 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
 
@@ -105,6 +111,11 @@ QVariant HYDROData_PolylineXY::GetDataVariant()
   return aVarData;
 }
 
+QColor HYDROData_PolylineXY::DefaultWireColor()
+{
+  return QColor( Qt::red );
+}
+
 TopoDS_Shape HYDROData_PolylineXY::GetShape()
 {
   return getPolylineShape();
@@ -138,13 +149,16 @@ TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&
       gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
       gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), aLastPoint.Z() );
 
+      if ( aPnt1.IsEqual( aPnt2, LOCAL_SELECTION_TOLERANCE ) )
+        continue;
+
       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
       aMakeWire.Add( anEdge );
     }
   }
   else //if( theType == PolylineSection::SECTION_SPLINE )
   {
-    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed );
+    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
 
     TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
     aMakeWire.Add( anEdge );
@@ -183,7 +197,7 @@ void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&
   }
   else
   {
-    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed );
+    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
     aBSpline.ComputePath( thePath );
   }
 }
@@ -221,12 +235,37 @@ void HYDROData_PolylineXY::Update()
     }
 
     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
-    aSectionWiresList.Append( aSectionWire );
-    aMakeWire.Add( aSectionWire );
+    if ( !aSectionWire.IsNull() ) {
+      aSectionWiresList.Append( aSectionWire );
+      aMakeWire.Add( aSectionWire );
+    }
   }
+// all input wires in the <aSectionWiresList>
+  Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
+  Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
+  TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
+  for(;it.More();it.Next()) {
+       TopoDS_Iterator it2(it.Value());
+       for(;it2.More();it2.Next()) 
+          aSeqEdges->Append(it2.Value());
+  }
+   BRep_Builder aBB;
+   TopoDS_Compound aCmp;
+   aBB.MakeCompound(aCmp);
+   if(aSeqEdges->Length() >1) {
+     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,Precision::Confusion(),Standard_True,aSeqWires);
+     for (Standard_Integer i = 1; i <= aSeqWires->Length();i++) {
+       const TopoDS_Shape& aS1 = aSeqWires->Value(i);
+       aBB.Add(aCmp, aS1);
+        }
+   } else if (aSeqEdges->Length() == 1) {
+          BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
+         if (mkWire.IsDone())
+                 aBB.Add(aCmp, mkWire.Wire());
+   }
 
-  TopoDS_Shape aShape;
-
+  
+/*
   if ( aMakeWire.IsDone() )
   {
     aShape = aMakeWire.Shape();
@@ -247,8 +286,8 @@ void HYDROData_PolylineXY::Update()
     
     aShape = aCompound;
   }
-
-  setPolylineShape( aShape );
+*/
+  setPolylineShape( aCmp );
 }
 
 /**
@@ -296,6 +335,9 @@ double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
       const Point& aSectPoint = aSectPointsList.Value( i );
       aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
       aPrevPoint = aSectPoint;
+
+      if ( thePointIndex == i - 1 )
+        break;
     }
   }
   else
@@ -315,7 +357,7 @@ double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
         aPointToTest = aPoint;
     }
 
-    HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed );
+    HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
 
     Quantity_Parameter aFirstParam = aBSpline.Curve()->FirstParameter();
     Quantity_Parameter aSecondParam = aBSpline.Curve()->LastParameter();