Salome HOME
Merge remote-tracking branch 'origin/master' into BR_LAND_COVER_MAP
[modules/hydro.git] / src / HYDROData / HYDROData_LandCover.cxx
index 815bc3b58b5fdd37680b0f084003508716d6a167..98fadcf4755e40cf719e468bf2fccc7eaf89c0c4 100644 (file)
@@ -24,8 +24,6 @@
 #include <BRepBuilderAPI_MakeFace.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
 #include <TDataStd_AsciiString.hxx>
-#include <TNaming_Builder.hxx>
-#include <TNaming_NamedShape.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Wire.hxx>
@@ -35,6 +33,9 @@
 #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>
@@ -43,6 +44,7 @@ IMPLEMENT_STANDARD_HANDLE( HYDROData_LandCover, HYDROData_Entity )
 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_LandCover, HYDROData_Entity )
 
 HYDROData_LandCover::HYDROData_LandCover()
+: HYDROData_Entity( Geom_2d )
 {
 }
 
@@ -124,13 +126,13 @@ void HYDROData_LandCover::Update()
   TCollection_AsciiString anErrorMsg;
   TopoDS_Shape aResShape = buildShape( GetPolylines(), anErrorMsg );
   
-  setShape( aResShape );
+  SetShape( aResShape );
 }
 
 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
 {
   SetReferenceObjects( thePolylines, DataTag_Polylines );
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
@@ -140,18 +142,7 @@ HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
 
 TopoDS_Shape HYDROData_LandCover::GetShape() const
 {
-  TopoDS_Shape aShape;
-
-  TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
-  if ( !aLabel.IsNull() )
-  {
-    Handle(TNaming_NamedShape) aNamedShape;
-    if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) ) {
-      aShape = aNamedShape->Get();
-    }
-  }
-
-  return aShape;
+  return HYDROData_Entity::GetShape( DataTag_Shape );
 }
 
 void HYDROData_LandCover::SetFillingColor( const QColor& theColor )
@@ -174,20 +165,19 @@ QColor HYDROData_LandCover::GetBorderColor() const
   return GetColor( DefaultBorderColor(), DataTag_BorderColor );
 }
 
-QColor HYDROData_LandCover::DefaultFillingColor()
+QColor HYDROData_LandCover::DefaultFillingColor() const
 {
   return QColor( Qt::magenta );
 }
 
-QColor HYDROData_LandCover::DefaultBorderColor()
+QColor HYDROData_LandCover::DefaultBorderColor() const
 {
   return QColor( Qt::transparent );
 }
 
-void HYDROData_LandCover::setShape( const TopoDS_Shape& theShape )
+void HYDROData_LandCover::SetShape( const TopoDS_Shape& theShape )
 {
-  TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
-  aBuilder.Generated( theShape );
+  HYDROData_Entity::SetShape( DataTag_Shape, theShape );
 }
 
 void HYDROData_LandCover::removeShape()
@@ -197,7 +187,7 @@ void HYDROData_LandCover::removeShape()
     aLabel.ForgetAllAttributes();
   }
 }
+
 TopoDS_Shape HYDROData_LandCover::buildShape( const HYDROData_SequenceOfObjects& thePolylines,
                                               TCollection_AsciiString& theErrorMsg )
 {
@@ -261,27 +251,50 @@ TopoDS_Shape HYDROData_LandCover::buildShape( const HYDROData_SequenceOfObjects&
 
   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 ";