Salome HOME
debug salome tests
[modules/hydro.git] / src / HYDROData / HYDROData_LandCover.cxx
index b588c1faa7b9c6878d298db512696ba779c76d9e..221cc2f1e90c623cb5dd6a3ed47df05389c57073 100644 (file)
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Wire.hxx>
 #include <TopoDS_Face.hxx>
+#include <TopExp_Explorer.hxx>
 #include <TopTools_ListOfShape.hxx>
 #include <TopTools_ListIteratorOfListOfShape.hxx>
 #include <NCollection_IncAllocator.hxx>
 #include <BOPAlgo_BOP.hxx>
+#include <ShapeAnalysis_Wire.hxx>
+#include <Precision.hxx>
+#include <TopTools_SequenceOfShape.hxx>
 
 #include <QColor>
 #include <QStringList>
 
-IMPLEMENT_STANDARD_HANDLE( HYDROData_LandCover, HYDROData_Entity )
 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_LandCover, HYDROData_Entity )
 
 HYDROData_LandCover::HYDROData_LandCover()
+: HYDROData_Entity( Geom_2d )
 {
 }
 
@@ -96,7 +100,9 @@ bool HYDROData_LandCover::IsHas2dPrs() const
 void HYDROData_LandCover::SetStricklerType( const QString& theType )
 {
   TCollection_AsciiString anAsciiStr( theType.toStdString().c_str() );
-  TDataStd_AsciiString::Set( myLab.FindChild( DataTag_StricklerType ), anAsciiStr );
+  Handle(TDataStd_AsciiString) anAttr = TDataStd_AsciiString::Set( myLab.FindChild( DataTag_StricklerType ), anAsciiStr );
+  anAttr->SetID(TDataStd_AsciiString::GetID());
+
 }
 
 QString HYDROData_LandCover::GetStricklerType() const
@@ -120,7 +126,8 @@ void HYDROData_LandCover::Update()
   
   removeShape();
 
-  TopoDS_Shape aResShape = buildShape();
+  TCollection_AsciiString anErrorMsg;
+  TopoDS_Shape aResShape = buildShape( GetPolylines(), anErrorMsg );
   
   setShape( aResShape );
 }
@@ -128,7 +135,7 @@ void HYDROData_LandCover::Update()
 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
 {
   SetReferenceObjects( thePolylines, DataTag_Polylines );
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
@@ -195,43 +202,63 @@ void HYDROData_LandCover::removeShape()
     aLabel.ForgetAllAttributes();
   }
 }
-TopoDS_Shape HYDROData_LandCover::buildShape() const
+
+TopoDS_Shape HYDROData_LandCover::buildShape( const HYDROData_SequenceOfObjects& thePolylines,
+                                              TCollection_AsciiString& theErrorMsg )
 {
+  theErrorMsg.Clear();
   TopoDS_Shape aResShape;
 
   BRepBuilderAPI_MakeWire aMakeWire;
   
   TopTools_ListOfShape aClosedWires;
 
-  HYDROData_SequenceOfObjects aRefPolylines = GetPolylines();
-  for ( int i = 1, n = aRefPolylines.Length(); i <= n; ++i ) {
+  int aNbPolylines = thePolylines.Length();
+  for ( int i = 1; i <= aNbPolylines; ++i ) {
     Handle(HYDROData_PolylineXY) aPolyline = 
-      Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
+      Handle(HYDROData_PolylineXY)::DownCast( thePolylines.Value( i ) );
     
     if ( aPolyline.IsNull() ) {
       continue;
     }
 
     TopoDS_Shape aPolyShape = aPolyline->GetShape();
-    if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
+    if ( aPolyShape.IsNull() ) {
       continue;
     }
 
-    const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
-    if ( aPolylineWire.IsNull() ) {
-      continue;
+    // Extract polyline wire(s)
+    TopTools_ListOfShape aPolylineWires;
+      
+    if ( aPolyShape.ShapeType() == TopAbs_WIRE ) {
+      const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
+      if ( !aPolylineWire.IsNull() ) {
+        aPolylineWires.Append( aPolylineWire );
+      }
+    } else if ( aPolyShape.ShapeType() == TopAbs_COMPOUND ) {
+      TopExp_Explorer anExp( aPolyShape, TopAbs_WIRE );
+      for (; anExp.More(); anExp.Next() ) {
+        if(!anExp.Current().IsNull()) {
+          const TopoDS_Wire& aWire = TopoDS::Wire( anExp.Current() );
+          aPolylineWires.Append( aWire );
+        }
+      }
     }
-
-    if ( aPolylineWire.Closed() ) {
-      aClosedWires.Append( aPolylineWire );
-    } else {
-      aMakeWire.Add( aPolylineWire );
-      aMakeWire.Build();
-      if ( aMakeWire.IsDone() ) {
-        if ( aMakeWire.Wire().Closed() ) {
-          aClosedWires.Append( aMakeWire.Wire() );
-          aMakeWire = BRepBuilderAPI_MakeWire();
+    
+    TopTools_ListIteratorOfListOfShape anIt( aPolylineWires );
+    for ( ; anIt.More(); anIt.Next() ) {
+      TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
+      
+      if ( aWire.Closed() ) {
+        aClosedWires.Append( aWire );
+      } else {
+        aMakeWire.Add( aWire );
+        aMakeWire.Build();
+        if ( aMakeWire.IsDone() ) {
+          if ( aMakeWire.Wire().Closed() ) {
+            aClosedWires.Append( aMakeWire.Wire() );
+            aMakeWire = BRepBuilderAPI_MakeWire();
+          }
         }
       }
     }
@@ -239,27 +266,54 @@ TopoDS_Shape HYDROData_LandCover::buildShape() const
 
   if ( aClosedWires.Extent() == 1 ) {
     // make face
-    BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( aClosedWires.First() ) );
+    TopoDS_Wire aW = TopoDS::Wire( aClosedWires.First());
+    BRepBuilderAPI_MakeFace aMakeFace( aW );
     aMakeFace.Build();
-    if( aMakeFace.IsDone() ) {
-      aResShape = aMakeFace.Face();
+    if( aMakeFace.IsDone() ) 
+    {
+      Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
+      if (!aSAW->CheckSelfIntersection())
+        aResShape = aMakeFace.Face();
+      else
+        theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire have been detected";
     }
   } else if ( aClosedWires.Extent() > 1 ) {
     // make compound
     BRep_Builder aBuilder;
     TopoDS_Compound aCompound;
     aBuilder.MakeCompound( aCompound );
-
+    TopTools_SequenceOfShape aSeq;
     TopTools_ListIteratorOfListOfShape aWiresIter( aClosedWires );
-    for ( ; aWiresIter.More(); aWiresIter.Next() ) {
-      BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( aWiresIter.Value() ) );
+    bool anErrStat = false;
+    for ( ; aWiresIter.More() && !anErrStat; aWiresIter.Next() )
+    {
+      TopoDS_Wire aW = TopoDS::Wire( aWiresIter.Value() );
+      BRepBuilderAPI_MakeFace aMakeFace( aW );
       aMakeFace.Build();
-      if( aMakeFace.IsDone() ) {
-        aBuilder.Add( aCompound, aMakeFace.Face() );
+      if( aMakeFace.IsDone() ) 
+      {
+        Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
+        if (!aSAW->CheckSelfIntersection())
+          aSeq.Append( aMakeFace.Face() );
+        else
+        {
+          anErrStat = true;
+          theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire(s) have been detected";
+        }
       }
     }
-
-    aResShape = aCompound;
+    if (!anErrStat)
+    {
+      for (int i = 1; i <= aSeq.Length(); i++)
+        aBuilder.Add( aCompound, aSeq(i) );
+      aResShape = aCompound;
+    }
+    else
+      aResShape = TopoDS_Shape();
+  } else if ( aNbPolylines > 0 ) {
+    TCollection_AsciiString aSourceName = aNbPolylines > 1 ? "polylines" : "polyline";
+    theErrorMsg = "Can't build closed contour on the given ";
+    theErrorMsg += aSourceName;
   }
 
   ///< \TODO to be reimplemented