using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcGe = Autodesk.AutoCAD.Geometry;
#pragma warning disable 0618
[assembly: CommandClass(typeof(MakeContour.MyCommands))]
namespace MakeContour
{
public class MyCommands
{
[CommandMethod("MakeContour")]
public void MyCommand()
{
AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
AcDb.Database db = doc.Database;
AcEd.Editor ed = doc.Editor;
AcEd.PromptPointOptions pr =
new AcEd.PromptPointOptions("\nУкажите вершину контура: ");
AcDb.ObjectId idPoly;
AcGe.Point3d lastPt = AcGe.Point3d.Origin;
using (AcDb.Polyline poly = new AcDb.Polyline())
{
using (AcDb.BlockTableRecord space =
db.CurrentSpaceId.Open(AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord)
{
idPoly = space.AppendEntity(poly);
}
}
int nVert = 0;
while (true)
{
pr.BasePoint = lastPt;
pr.UseBasePoint = true;
pr.UseDashedLine = true;
if (nVert >= 3)
{
pr.AllowNone = true;
pr.SetMessageAndKeywords(
"\nУкажите вершину контура [Отменить] (ENTER - завершение): ", "U");
} else if (nVert >= 1)
{
pr.SetMessageAndKeywords(
"\nУкажите вершину контура [Отменить]: ", "U");
}
else
{
pr.UseBasePoint = false;
pr.Message = "\nУкажите вершину контура: ";
pr.Keywords.Clear();
}
AcEd.PromptPointResult res = ed.GetPoint(pr);
if (res.Status == AcEd.PromptStatus.Keyword)
{
if (res.StringResult == "U" && nVert > 0)
{
using (AcDb.Polyline poly = idPoly.Open(AcDb.OpenMode.ForWrite) as AcDb.Polyline)
{
try
{
if (nVert == 1)
{
poly.Reset(true, 0);
}
else
{
poly.RemoveVertexAt(poly.NumberOfVertices - 1);
lastPt = poly.EndPoint;
}
} catch { }
--nVert;
}
}
}
else if (res.Status == AcEd.PromptStatus.Cancel)
{
using (AcDb.Polyline poly = idPoly.Open(AcDb.OpenMode.ForWrite) as AcDb.Polyline)
{
poly.Erase();
break;
}
}
else if (res.Status == AcEd.PromptStatus.None)
{
break;
}
else
{
lastPt = res.Value;
using (AcDb.Polyline poly = idPoly.Open(AcDb.OpenMode.ForWrite) as AcDb.Polyline)
{
poly.AddVertexAt(poly.NumberOfVertices,
new AcGe.Point2d(res.Value.X, res.Value.Y), 0, 0, 0);
}
nVert++;
}
}
// Полилинию, у которой меньше двух вершин удаляем
using (AcDb.Polyline poly = idPoly.Open(AcDb.OpenMode.ForWrite) as AcDb.Polyline)
{
try {
if (poly.NumberOfVertices < 2) poly.Erase();
}
catch { }
}
}
}
}