Salome HOME
Import/Export // API revision. p4
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineOperator.cxx
index e8a7eec82937ebdb4ef51b491f27025a618bfbc6..757805c153a54f82611f58dbcb8cdc296f1fbeac 100644 (file)
 #include <HYDROData_Document.h>
 #include <BRepBuilderAPI_MakeEdge2d.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
+#include <TopoDS.hxx>
 #include <TopoDS_Edge.hxx>
 #include <TopoDS_Wire.hxx>
+#include <TopExp_Explorer.hxx>
+#include <QString>
 
 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
 {
@@ -36,126 +39,183 @@ template<class T> void append( std::vector<T>& theList, const std::vector<T>& th
     theList[i] = theList2[j];
 }
 
-
 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
-                                        const TCollection_AsciiString& theNamePrefix,
                                         const Handle( HYDROData_PolylineXY )& thePolyline,
-                                        const gp_Pnt2d& thePoint ) const
+                                        const gp_Pnt2d& thePoint,
+                                        double theTolerance ) const
 {
   std::vector<gp_Pnt2d> aPointsList( 1 );
   aPointsList[0] = thePoint;
-  std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( thePolyline );
+  std::vector<TopoDS_Wire> aCurves = GetWires( thePolyline );
   bool isOK = true;
   for( int i=0, n=aCurves.size(); i<n; i++ )
   {
-    std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( aCurves[i], aPointsList );
-    bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
+    std::vector<TopoDS_Shape> aCurvesList = Split( aCurves[i], thePoint, theTolerance );
+    bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
     isOK = isOK && isLocalOK;
   }
   return isOK;
 }
 
 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
-                                        const TCollection_AsciiString& theNamePrefix,
-                                        const Handle( HYDROData_PolylineXY )& thePolyline,
-                                        const Handle( HYDROData_PolylineXY )& theTool ) const
+              const Handle( HYDROData_PolylineXY )& thePolyline,
+              const Handle( HYDROData_PolylineXY )& theTool,
+              double theTolerance ) const
 {
-  std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( thePolyline );
-  std::vector<Handle( Geom2d_Curve )> aToolCurves = GetCurves( theTool );
-  bool isOK = true;
-  for( int i=0, n=aCurves.size(); i<n; i++ )
-    for( int j=0, m=aToolCurves.size(); j<m; j++ )
-    {
-      std::vector<gp_Pnt2d> aPointsList = Intersection( aCurves[i], aToolCurves[j] );
-      std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( aCurves[i], aPointsList );
-      bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
-      isOK = isOK && isLocalOK;
-    }
-  return isOK;
+  HYDROData_SequenceOfObjects aSeq;
+  aSeq.Append( theTool );
+  return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
 }
 
 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
-                                        const TCollection_AsciiString& theNamePrefix,
-                                        const HYDROData_SequenceOfObjects& thePolylines )
+                                        const HYDROData_SequenceOfObjects& thePolylines,
+                                        double theTolerance )
 {
   int f = thePolylines.Lower(), l = thePolylines.Upper();
-  bool isOK = true;
-  std::vector<Handle( Geom2d_Curve )> anAllCurves;
   for( int i=f; i<=l; i++ )
   {
     Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
-    std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( aPolyline );
-    append( anAllCurves, aCurves );
+    if( !split( theDoc, aPolyline, thePolylines, theTolerance, i ) )
+      return false;
   }
+  return true;
+}
+
+bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
+                                        const QString& theName,
+                                        const HYDROData_SequenceOfObjects& thePolylines,
+                                        bool isConnectByNewSegment,
+                                        double theTolerance )
+{
+  TopoDS_Shape aMergedPolyline;
+
+  int f = thePolylines.Lower(), l = thePolylines.Upper();
+  for( int i=f; i<=l; i++ )
+  {
+    Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
+    std::vector<TopoDS_Wire> aCurves = GetWires( aPolyline );
+    for( int j=0, m=aCurves.size(); j<m; j++ )
+      if( !Merge( aMergedPolyline, aCurves[j], isConnectByNewSegment, theTolerance ) )
+        return false;
+  }
+
+  std::vector<TopoDS_Shape> aShapes( 1 );
+  aShapes[0] = aMergedPolyline;
+  CreatePolylines( theDoc, theName, aShapes, false );
+
+  return true;
+}
+
+bool HYDROData_PolylineOperator::Merge( TopoDS_Shape& theShape, const TopoDS_Wire& theWire, 
+                                        bool isConnectByNewSegment, double theTolerance )
+{
+  //TODO
+  return true;
+}
+
+bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
+                                        const Handle( HYDROData_PolylineXY )& thePolyline,
+                                        const HYDROData_SequenceOfObjects& theTools,
+                                        double theTolerance,
+                                        int theIgnoreIndex ) const
+{
+  std::vector<TopoDS_Wire> aCurves = GetWires( thePolyline );
+  std::vector<TopoDS_Wire> aToolCurves;
+  for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
+    if( i!=theIgnoreIndex )
+    {
+      Handle( HYDROData_PolylineXY ) aToolPolyline = 
+        Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
+      append( aToolCurves, GetWires( aToolPolyline ) );
+    }
 
-  for( int i=0, n=anAllCurves.size(); i<n; i++ )
+  bool isOK = true;
+
+  int n = aCurves.size();
+  std::vector<TopoDS_Shape> aResults( n );
+  for( int i=0; i<n; i++ )
+    aResults[i] = aCurves[i];
+
+  for( int j=0, m=aToolCurves.size(); j<m; j++ )
   {
-    std::vector<gp_Pnt2d> aCompletePointsList;
-    for( int j=0; j<n; j++ )
+    std::vector<TopoDS_Shape> aNewResults;
+    for( int k=0, q=aResults.size(); k<q; k++ )
     {
-      if( i==j )
-        continue;
-      std::vector<gp_Pnt2d> aPointsList = Intersection( anAllCurves[i], anAllCurves[j] );
-      append( aCompletePointsList, aPointsList );
+      std::vector<TopoDS_Shape> aCurvesList = Split( TopoDS::Wire( aResults[k] ), aToolCurves[j], theTolerance );
+      append( aNewResults, aCurvesList );
     }
-    std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( anAllCurves[i], aCompletePointsList );
-    bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
-    isOK = isOK && isLocalOK;
+    aResults = aNewResults;
   }
+
+  CreatePolylines( theDoc, thePolyline->GetName(), aResults, true );
+
   return isOK;
 }
 
-bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
-                                        const TCollection_AsciiString& theName,
-                                        const HYDROData_SequenceOfObjects& thePolylines )
+std::vector<TopoDS_Wire> HYDROData_PolylineOperator::GetWires( const Handle( HYDROData_PolylineXY )& thePolyline )
 {
-  //TODO
-  return true;
+  std::vector<TopoDS_Wire> aResult;
+
+  TopoDS_Shape aShape = thePolyline->GetShape();
+
+  if( aShape.ShapeType()==TopAbs_WIRE )
+  {
+    aResult.push_back( TopoDS::Wire( aShape ) );
+  }
+  else
+  {
+    TopExp_Explorer anExp( aShape, TopAbs_WIRE );
+    for( ; anExp.More(); anExp.Next() )
+    {
+      aResult.push_back( TopoDS::Wire( anExp.Current() ) );
+    }
+  }
+  return aResult;
 }
 
-std::vector<Handle( Geom2d_Curve )> HYDROData_PolylineOperator::GetCurves( const Handle( HYDROData_PolylineXY )& thePolyline )
+std::vector<TopoDS_Shape> HYDROData_PolylineOperator::Split( const TopoDS_Wire& theWire,
+                                                             const gp_Pnt2d& thePoint,
+                                                             double theTolerance )
 {
-  std::vector<Handle( Geom2d_Curve )> aResult;
+  std::vector<TopoDS_Shape> aResult;
   //TODO
   return aResult;
 }
 
-std::vector<gp_Pnt2d> HYDROData_PolylineOperator::Intersection( const Handle( Geom2d_Curve )& theCurve,
-                                                                const Handle( Geom2d_Curve )& theTool )
+std::vector<TopoDS_Shape> HYDROData_PolylineOperator::Split( const TopoDS_Wire& theWire,
+                                                             const TopoDS_Wire& theTool,
+                                                             double theTolerance )
 {
-  std::vector<gp_Pnt2d> aResult;
+  std::vector<TopoDS_Shape> aResult;
   //TODO
   return aResult;
 }
 
-std::vector<Handle( Geom2d_Curve )> HYDROData_PolylineOperator::Split( const Handle( Geom2d_Curve )& theCurve,
-                                                                       const std::vector<gp_Pnt2d>& thePoints )
+std::vector<TopoDS_Shape> HYDROData_PolylineOperator::Split( const std::vector<TopoDS_Wire>& theWires,
+                                                             double theTolerance )
 {
-  std::vector<Handle( Geom2d_Curve )> aResult;
+  std::vector<TopoDS_Shape> aResult;
   //TODO
   return aResult;
 }
 
 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
-                                                  const TCollection_AsciiString& theNamePrefix,
-                                                  const std::vector<Handle( Geom2d_Curve )>& theCurves )
+                                                  const QString& theNamePrefix,
+                                                  const std::vector<TopoDS_Shape>& theShapes,
+                                                  bool isUseIndices )
 {
   if( theDoc.IsNull() )
     return false;
 
-  int n = theCurves.size();
+  int n = theShapes.size();
   for( int i=0; i<n; i++ )
   {
-    TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge2d( theCurves[i] ).Edge();
-    BRepBuilderAPI_MakeWire aMakeWire;
-    aMakeWire.Add( anEdge );
-
     Handle( HYDROData_PolylineXY ) aPolyline = 
       Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
     if( aPolyline.IsNull() )
       return false;
 
-    aPolyline->SetShape( aMakeWire.Wire() );
+    aPolyline->SetShape( theShapes[i] );
     //TODO: set name
   }
   return true;