using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using App = Autodesk.AutoCAD.ApplicationServices.Application;
#pragma warning disable 0618
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(ChangeSegmentWidth.MyCommands))]
namespace ChangeSegmentWidth
{
public class MyCommands
{
static double def = 0.0;
[CommandMethod("ChgWidth")]
public void Width()
{
Document doc = App.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nВыберите сегменты полилинии");
peo.SetRejectMessage("\nЭто не полилиния!");
peo.AddAllowedClass(typeof(Polyline), true);
PromptDoubleOptions pdo = new PromptDoubleOptions("\nШирина сегмента полилинии");
pdo.DefaultValue = def;
PromptDoubleResult res = ed.GetDouble(pdo);
double width = res.Value;
def = res.Value;
while (true)
{
PromptEntityResult sel = ed.GetEntity(peo);
if (sel.Status == PromptStatus.OK)
{
using (Polyline pl = sel.ObjectId.Open(OpenMode.ForWrite) as Polyline)
{
Matrix3d u2w = ed.CurrentUserCoordinateSystem;
Vector3d viewDir = ed.GetCurrentView().ViewDirection;
Point3d pW = pl.GetClosestPointTo(sel.PickedPoint.TransformBy(u2w), viewDir, false);
int n = (int)(pl.GetParameterAtPoint(pW) + 1e-10);
if (pl.GetStartWidthAt(n) > 1e-10)
{
pl.SetStartWidthAt(n, 0.0); pl.SetEndWidthAt(n, 0.0);
}
else
{
pl.SetStartWidthAt(n, width); pl.SetEndWidthAt(n, width);
}
}
}
else break;
}
}
}
}