namespace Rivilis
{
public class TestSelfCrossing
{
[CommandMethod("TestMPoly", CommandFlags.Modal)]
public void TestCross()
{
Document adoc = Application.DocumentManager.MdiActiveDocument;
Database db = adoc.Database;
Editor ed = adoc.Editor;
PromptEntityOptions entOpt = new PromptEntityOptions("\nВыберите полилинию: ");
entOpt.SetRejectMessage("Это не полилиния!");
entOpt.AddAllowedClass(typeof(Polyline), true);
PromptEntityResult entRes = ed.GetEntity(entOpt);
if (entRes.Status != PromptStatus.OK) return;
Point2dCollection polyPts = new Point2dCollection();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Polyline pline = tr.GetObject(entRes.ObjectId, OpenMode.ForRead) as Polyline;
Point3dCollection gripPts = new Point3dCollection();
pline.GetGripPoints(gripPts, new IntegerCollection(), new IntegerCollection());
foreach (Point3d pt in gripPts)
{
polyPts.Add(new Point2d(pt.X, pt.Y));
}
tr.Commit();
}
PromptPointResult ptRes = ed.GetPoint("\nУкажите точку: ");
if (ptRes.Status != PromptStatus.OK) return;
Point2dCollection testPrs = new Point2dCollection();
testPrs.Add(new Point2d(ptRes.Value.X, ptRes.Value.Y));
bool[] testRes = PolygonPointsTest(polyPts, testPrs);
Application.ShowAlertDialog(testRes[0] ? "Точка внутри контура" : "Точка вне контура");
}
public static bool[] PolygonPointsTest(Point2dCollection polygonPts, Point2dCollection testPts)
{
List<bool> testRes = new List<bool>();
using (MPolygon mPoly = new MPolygon())
{
MPolygonLoop mpLoop = new MPolygonLoop();
foreach (Point2d pt in polygonPts)
{
mpLoop.Add(new BulgeVertex(pt, 0.0));
}
// Для того чтобы получить нормальный контур, нужно добавить первую точку
mpLoop.Add(new BulgeVertex(polygonPts[0], 0.0));
mPoly.AppendMPolygonLoop(mpLoop, false, Tolerance.Global.EqualPoint);
foreach (Point2d pt in testPts)
{
IntegerCollection intCol = mPoly.IsPointInsideMPolygon(new Point3d(pt.X, pt.Y, 0.0), Tolerance.Global.EqualPoint);
testRes.Add(intCol.Count > 0);
}
}
return testRes.ToArray();
}
}
}