using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
#pragma warning disable 0618
[assembly: CommandClass(typeof(Rivilis.TestPolygon))]
namespace Rivilis
{
public class TestPolygon
{
[CommandMethod("TP", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void MyCommand()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "\nВыберите полилинии: ";
pso.MessageForRemoval = "\nУдалите полилинии: ";
TypedValue[] filList = new TypedValue[1] {
new TypedValue((int)DxfCode.Start, "LWPOLYLINE")
};
SelectionFilter sf = new SelectionFilter(filList);
PromptSelectionResult psr = ed.GetSelection(pso, sf);
if (psr.Status == PromptStatus.OK) {
foreach (ObjectId id in psr.Value.GetObjectIds()) {
var inds = GetVertexiesDesc(id);
int i = 0; // Считаем количество выпуклых углов
foreach (var ind in inds) if (ind.Value) i++;
ed.WriteMessage("\nВ полилинии {0} выпуклых углов {1}",
id.Handle.ToString(), i);
}
}
}
/// <summary>
/// Получаем коллекцию пар (номер вершины) - (флаг выпуклости вершины)
/// </summary>
/// <param name="id">ObjectId полилинии.</param>
/// <returns></returns>
public static Dictionary<int, bool> GetVertexiesDesc(ObjectId id)
{
var inds = new Dictionary<int, bool>();
using (Polyline poly = id.Open(OpenMode.ForWrite) as Polyline) {
double s = poly.Area;
for (int i = 0; i < poly.NumberOfVertices; i++) {
double bulge = poly.GetBulgeAt(i);
Point2d p = poly.GetPoint2dAt(i);
poly.RemoveVertexAt(i); // Удаляем вершину
double s1 = poly.Area;
poly.AddVertexAt(i, p, bulge, 0, 0); // Возвращаем вершину
if (s > s1) inds.Add(i, true); else inds.Add(i, false);
}
poly.Cancel();
}
return inds;
}
}
}