Salome HOME
refs #567: HYDROData_LandCover::buildShape() method returns the error message now.
[modules/hydro.git] / src / HYDROData / HYDROData_LandCover.cxx
index b588c1faa7b9c6878d298db512696ba779c76d9e..815bc3b58b5fdd37680b0f084003508716d6a167 100644 (file)
@@ -30,6 +30,7 @@
 #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>
@@ -120,7 +121,8 @@ void HYDROData_LandCover::Update()
   
   removeShape();
 
-  TopoDS_Shape aResShape = buildShape();
+  TCollection_AsciiString anErrorMsg;
+  TopoDS_Shape aResShape = buildShape( GetPolylines(), anErrorMsg );
   
   setShape( aResShape );
 }
@@ -196,42 +198,62 @@ void HYDROData_LandCover::removeShape()
   }
 }
  
-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();
+          }
         }
       }
     }
@@ -260,6 +282,10 @@ TopoDS_Shape HYDROData_LandCover::buildShape() const
     }
 
     aResShape = aCompound;
+  } 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