+ BRepBuilderAPI_MakeWire aMakeWire;
+
+ TopTools_ListOfShape aClosedWires;
+
+ int aNbPolylines = thePolylines.Length();
+ for ( int i = 1; i <= aNbPolylines; ++i ) {
+ Handle(HYDROData_PolylineXY) aPolyline =
+ Handle(HYDROData_PolylineXY)::DownCast( thePolylines.Value( i ) );
+
+ if ( aPolyline.IsNull() ) {
+ continue;
+ }
+
+ TopoDS_Shape aPolyShape = aPolyline->GetShape();
+ if ( aPolyShape.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 );
+ }
+ }
+ }
+
+ 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();
+ }
+ }
+ }
+ }
+ }
+
+ if ( aClosedWires.Extent() == 1 ) {
+ // make face
+ TopoDS_Wire aW = TopoDS::Wire( aClosedWires.First());
+ BRepBuilderAPI_MakeFace aMakeFace( aW );
+ aMakeFace.Build();
+ 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 );
+ 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() )
+ {
+ 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";
+ }
+ }
+ }
+ 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;
+ }
+