Salome HOME
merge master
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineOperator.cxx
index 612d24f985e09c6dc764661df0e5ce9cfe4b2ada..b991bf7f6992a0728f13c55e2e93e11940eb3cea 100644 (file)
 #include <HYDROData_PolylineOperator.h>
 #include <HYDROData_Document.h>
 #include <HYDROData_TopoCurve.h>
+#include <HYDROData_Object.h>
 
+#ifndef LIGHT_MODE
 #include <CurveCreator_Utils.hxx>
+#endif
 
 #include <BRepAdaptor_Curve.hxx>
 #include <BRep_Builder.hxx>
@@ -44,7 +47,9 @@
 #include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
-#include <QString>
+#include <QStringList>
+#include <QColor>
+#include <Geom_BSplineCurve.hxx>
 
 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
 {
@@ -64,6 +69,11 @@ bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theD
                                         const gp_Pnt2d& thePoint,
                                         double theTolerance ) const
 {
+  if (thePolyline.IsNull())
+  {
+    return false;
+  }
+
   std::vector<gp_Pnt2d> aPointsList( 1 );
   aPointsList[0] = thePoint;
   std::vector<TopoDS_Wire> aCurves;
@@ -72,8 +82,9 @@ bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theD
   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->GetName(), aCurvesList, true );
+    Split( aCurves[i], thePoint, theTolerance, aCurvesList );
+    bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(),
+      aCurvesList, true, thePolyline->GetWireColor() );
     isOK = isOK && isLocalOK;
   }
   return isOK;
@@ -82,11 +93,17 @@ bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theD
 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
               const Handle( HYDROData_PolylineXY )& thePolyline,
               const Handle( HYDROData_PolylineXY )& theTool,
-              double theTolerance ) const
+              double theTolerance,
+              bool& theIsIntersected) const
 {
+  if (thePolyline.IsNull() || theTool.IsNull())
+  {
+    return false;
+  }
+
   HYDROData_SequenceOfObjects aSeq;
   aSeq.Append( theTool );
-  return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
+  return split( theDoc, thePolyline, aSeq, theTolerance, -1, theIsIntersected);
 }
 
 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
@@ -97,7 +114,8 @@ bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theD
   for( int i=f; i<=l; i++ )
   {
     Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
-    if( !split( theDoc, aPolyline, thePolylines, theTolerance, i ) )
+    bool isIntersected;
+    if( !split( theDoc, aPolyline, thePolylines, theTolerance, i, isIntersected) )
       return false;
   }
   return true;
@@ -145,7 +163,10 @@ bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theD
 
   std::vector<TopoDS_Shape> aPolylines(1);
   aPolylines[0] = aWireSet;
-  CreatePolylines(theDoc, theName, aPolylines, false);
+  QString aName = theName;
+  if( aName.isEmpty() )
+    aName = "merged";
+  CreatePolylines( theDoc, aName, aPolylines, true, QColor() );
   return true;
 }
 
@@ -153,8 +174,16 @@ bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theD
                                         const Handle( HYDROData_PolylineXY )& thePolyline,
                                         const HYDROData_SequenceOfObjects& theTools,
                                         double theTolerance,
-                                        int theIgnoreIndex ) const
+                                        int theIgnoreIndex,
+                                        bool& theIsIntersected) const
 {
+  theIsIntersected = false;
+
+  if (thePolyline.IsNull())
+  {
+    return false;
+  }
+
   std::vector<TopoDS_Wire> aCurves;
   GetWires(thePolyline, aCurves);
   std::vector<TopoDS_Wire> aToolCurves;
@@ -163,11 +192,19 @@ bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theD
     {
       Handle( HYDROData_PolylineXY ) aToolPolyline = 
         Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
-      std::vector<TopoDS_Wire> aTCurves;
-      GetWires(aToolPolyline, aTCurves);
-      append( aToolCurves, aTCurves);
+      if (!aToolPolyline.IsNull())
+      {
+        std::vector<TopoDS_Wire> aTCurves;
+        GetWires(aToolPolyline, aTCurves);
+        append( aToolCurves, aTCurves);
+      }
     }
 
+  if (aToolCurves.empty())
+  {
+    return false;
+  }
+
   const int aPSCount = aCurves.size();
   const int aTSCount = aToolCurves.size();
   std::vector<TopoDS_Shape> aResult;
@@ -185,19 +222,19 @@ bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theD
       aCurve.Intersect(aToolCurves[aTSI], aParams);
     }
 
-    std::deque<HYDROData_TopoCurve> aSplittedCurves;
-    aCurve.Cut(aParams, aSplittedCurves);
+    std::deque<HYDROData_TopoCurve> aSplitCurves;
+    theIsIntersected |= aCurve.Cut(aParams, aSplitCurves);
     std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
-      aSplittedCurves.begin();
+      aSplitCurves.begin();
     std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
-      aSplittedCurves.end();
+      aSplitCurves.end();
     for (; aCIt != aLastCIt; ++aCIt)
     {
       aResult.push_back(aCIt->Wire());
     }
   }
 
-  CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
+  CreatePolylines( theDoc, thePolyline->GetName(), aResult, true, thePolyline->GetWireColor() );
   return true;
 }
 
@@ -249,7 +286,8 @@ void HYDROData_PolylineOperator::Split(
 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
                                                   const QString& theNamePrefix,
                                                   const std::vector<TopoDS_Shape>& theShapes,
-                                                  bool isUseIndices )
+                                                  bool isUseIndices,
+                                                  const QColor& theColor )
 {
   if( theDoc.IsNull() )
     return false;
@@ -268,14 +306,20 @@ bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Docume
     if( isUseIndices )
     {
       QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
-      if( theDoc->FindObjectByName( aNewName ).IsNull() )  // the object with such a name is not found
-        aPolyline->SetName( aNewName );
-      anIndex++;
+      while( !theDoc->FindObjectByName( aNewName ).IsNull() )  // the object with such a name is not found
+      {
+        anIndex++;
+        aNewName = theNamePrefix + "_" + QString::number( anIndex );
+      }
+      aPolyline->SetName( aNewName );
     }
     else
     {
       aPolyline->SetName( theNamePrefix );
     }
+
+    if( theColor.isValid() )
+      aPolyline->SetWireColor( theColor );
   }
   return true;
 }
@@ -303,7 +347,9 @@ double HYDROData_PolylineOperator::ReduceDeflection(
   }
   Handle(Geom_BSplineCurve) aBSpline2;
   const bool isClosed = theCurve.IsClosed();
+#ifndef LIGHT_MODE
   if (!CurveCreator_Utils::constructBSpline(aPs2, isClosed, aBSpline2))
+#endif
   {
     return -1;
   }
@@ -379,3 +425,43 @@ double HYDROData_PolylineOperator::ReduceDeflection(
   }
   return aMaxDefl;
 }
+
+bool HYDROData_PolylineOperator::Extract( const Handle(HYDROData_Document)& theDocument,
+                                          const Handle(HYDROData_Object)& theObject )
+{
+  if( theObject.IsNull() || theDocument.IsNull() )
+    return false;
+
+  QList<TopoDS_Shape> aBoundShapes;
+  QStringList aBoundNames;
+
+  theObject->GetBoundaries( aBoundShapes, aBoundNames );
+
+  for( int i=0, n=aBoundShapes.size(); i<n; i++ )
+  {
+    TopoDS_Shape aShape = aBoundShapes[i];
+    if( aShape.IsNull() )
+      continue;
+
+    QString aBoundName = i<aBoundNames.size() ? aBoundNames[i] : "";
+
+    Handle( HYDROData_PolylineXY ) aPolyline = 
+      Handle( HYDROData_PolylineXY )::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
+    
+    if( aPolyline.IsNull() )
+      return false;
+
+    aPolyline->SetShape( aShape );
+
+    int anIndex = 0;
+    QString aName = aBoundName;
+    while( !theDocument->FindObjectByName( aName ).IsNull() )
+    {
+      anIndex++;
+      aName = aBoundName + "_" + QString::number( anIndex );
+    }
+    aPolyline->SetName( aName );
+  }
+
+  return true;
+}