using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(TestSelectCP.MyCommands))]
namespace TestSelectCP
{
public class MyCommands
{
const double FUZZ = 0.1;
[CommandMethod("TestCrossingPolygonSelection")]
public void TestCrossingPolygonSelectionCmd()
{
Document adoc = Application.DocumentManager.MdiActiveDocument;
Editor ed = adoc.Editor;
PromptEntityOptions plineOpt = new PromptEntityOptions
("\nВыберите полилинию-контур: ");
plineOpt.SetRejectMessage("\nЭто не полилиния!");
plineOpt.AddAllowedClass(typeof(Polyline), true);
PromptEntityResult plineRes = ed.GetEntity(plineOpt);
if (plineRes.Status != PromptStatus.OK) return;
Point3dCollection pts = new Point3dCollection();
#pragma warning disable CS0618 // Type or member is obsolete
using (Polyline pline = plineRes.ObjectId.Open(OpenMode.ForRead) as Polyline)
#pragma warning restore CS0618 // Type or member is obsolete
{
for (int i = 0; i < pline.NumberOfVertices; i++)
{
Point3d p = pline.GetPoint3dAt(i);
// if (i == 0 || pts[i-1].DistanceTo(p) >= FUZZ) <--- Исправлено по наводке Дмитрия Загорулькина
if (pts.Count == 0 || pts[pts.Count-1].DistanceTo(p) >= FUZZ)
pts.Add(p);
}
}
// Повторять последнюю точку не нужно!!!
if (pts[0].DistanceTo(pts[pts.Count - 1]) < FUZZ)
{
pts.RemoveAt(pts.Count - 1);
}
//double dist = 1e+32;
//int iMindist = -1;
//for (int i = 0; i < pts.Count; i++)
//{
// double dist1 = pts[i].DistanceTo(pts[(i + 1) % pts.Count]);
// if (dist1 < dist) {
// dist = dist1; iMindist = i;
// }
//}
//ed.WriteMessage($"\ndist={dist} iMindist={iMindist}");
PromptSelectionResult selRes = ed.SelectCrossingPolygon (pts);
if (selRes.Status == PromptStatus.Error)
{
short code = (short)Application.GetSystemVariable("ERRNO");
Application.ShowAlertDialog($"Ошибка: {code}");
return;
}
}
}
}