using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Geometry;
using AcBr = Autodesk.AutoCAD.BoundaryRepresentation;
using Autodesk.AutoCAD.Runtime;
namespace Autodesk.AutoCAD.DatabaseServices
{
public static class PolylineExtensionMethods
{
/// <summary>
///
/// Returns true if the given Polyline forms a
/// closed boundary and its Closed property is
/// false.
///
/// The result is true if any of the
/// following conditions are true:
///
/// 1. Any segment intersects any other segment.
/// Regardless of the value of the Closed property.
///
/// 2. Any vertex lies directly on any segment.
/// or is coincident with any other vertex,
/// including zero-length segments, regardless
/// of the value of the Closed property
///
/// 3. If the first and last vertices are coincident,
/// AND the PolyLine's Closed property is false,
/// the polyline is considered effectively closed.
///
/// 4. A Closed polyline with < 3 vertices.
///
/// The test is intended to be consistent with the
/// REGION command's testing of boundary entities.
///
/// </summary>
public static bool IsEffectivelyClosed( this Polyline pline )
{
int numverts = pline.NumberOfVertices;
if( numverts < 3 )
return pline.Closed;
if( !pline.Closed && pline.StartPoint.IsEqualTo( pline.EndPoint ) )
return true;
Point3dCollection points = new Point3dCollection();
pline.IntersectWith( pline, Intersect.OnBothOperands, points,
IntPtr.Zero, IntPtr.Zero );
if( !pline.Closed )
numverts -= 2;
return points.Count != numverts;
}
}
}