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;
[assembly: CommandClass(typeof(GetVerts.PlineUtils))]
namespace GetVerts
{
public class PlineUtils
{
public static Dictionary<int, Point3d> GetPlinePoints(ObjectId id)
{
Dictionary<int, Point3d> dic = new Dictionary<int, Point3d>();
using (Curve cv = id.Open(OpenMode.ForRead, true) as Curve) {
if (cv == null || (!(cv is Polyline) && !(cv is Polyline2d) && !(cv is Polyline3d)))
throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NotThatKindOfClass);
int startParam = Convert.ToInt32(cv.StartParam);
int endParam = Convert.ToInt32(cv.EndParam - (cv.Closed ? 1.0 : 0.0));
for (int i = startParam; i <= endParam; i++) {
dic.Add(i, cv.GetPointAtParameter(i));
}
}
return dic;
}
// Для тестирования
[CommandMethod("TestPoly", CommandFlags.Modal)]
public void TestPoly()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
PromptEntityOptions pr = new PromptEntityOptions("\nВыберите полилинию");
pr.SetRejectMessage("Это не полилиния");
pr.AddAllowedClass(typeof(Polyline), false);
pr.AddAllowedClass(typeof(Polyline2d), false);
pr.AddAllowedClass(typeof(Polyline3d), false);
PromptEntityResult rs = ed.GetEntity(pr);
if (rs.Status == PromptStatus.OK) {
try {
Dictionary<int, Point3d> dic = GetPlinePoints(rs.ObjectId);
foreach (KeyValuePair<int, Point3d> el in dic) {
ed.WriteMessage("\nVert:{0} ({1})", el.Key, el.Value);
}
}
catch (System.Exception ex){
ed.WriteMessage("{0}", ex);
}
}
}
}
}