[CommandMethod("AddPoly")]
public void AddPoly()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
double width = 0;
short colorIndex = 0;
int index = 2;
ObjectId polyEntId = ObjectId.Null;
PromptPointOptions optPoint = new PromptPointOptions("\n请输入第一个点<100,200>");
optPoint.AllowNone = true;
PromptPointResult resPoint = ed.GetPoint(optPoint);
if (resPoint.Status == PromptStatus.Cancel)
return;
Point3d ptStart;
if (resPoint.Status == PromptStatus.None)
ptStart = new Point3d(100, 200, 0);
else
ptStart = resPoint.Value;
Point3d ptPrevious = ptStart;
PromptPointOptions optPtKey = new PromptPointOptions("\n请输入下一个点或[线宽(W)/颜色(C)/完成(O)]<O>");
optPtKey.Keywords.Add("W");
optPtKey.Keywords.Add("C");
optPtKey.Keywords.Add("O");
optPtKey.Keywords.Default = "O";
optPtKey.UseBasePoint = true;
optPtKey.BasePoint = ptPrevious;
optPtKey.AppendKeywordsToMessage = false;
PromptPointResult resKey = ed.GetPoint(optPtKey);
while (resKey.Status == PromptStatus.OK || resKey.Status == PromptStatus.Keyword)
{
Point3d ptNext = new Point3d();
if (resKey.Status == PromptStatus.Keyword)
{
switch (resKey.StringResult)
{
case "W":
width = GetWidth();
break;
case "C":
colorIndex = GetColorIndex();
break;
case "O":
return;
default:
ed.WriteMessage("\n输入了无效关键字");
break;
}
}
else
{
ptNext = resKey.Value;
if (index == 2)
{
Point2d pt1 = new Point2d(ptPrevious[0], ptPrevious[1]);
Point2d pt2 = new Point2d(ptNext[0], ptNext[1]);
Polyline polyEnt = new Polyline();
polyEnt.AddVertexAt(0, pt1, 0, width, width);
polyEnt.AddVertexAt(1, pt2, 0, width, width);
polyEnt.Color = Color.FromColorIndex(ColorMethod.ByColor, colorIndex);
polyEntId = db.AddToModelSpace(polyEnt);
}
else
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Polyline polyEnt = trans.GetObject(polyEntId, OpenMode.ForWrite) as Polyline;
if (polyEnt != null)
{
Point2d ptCurrent = new Point2d(ptNext[0], ptNext[1]);
polyEnt.AddVertexAt(index - 1, ptCurrent, 0, width, width);
polyEnt.Color = Color.FromColorIndex(ColorMethod.ByColor, colorIndex);
polyEnt.ConstantWidth = width;
}
trans.Commit();
}
}
index++;
}
ptPrevious = ptNext;
optPtKey.BasePoint = ptPrevious;
resKey = ed.GetPoint(optPtKey);
}
}
public double GetWidth()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptDoubleOptions optDou = new PromptDoubleOptions("\n请输入线宽");
optDou.AllowNegative = false;
optDou.DefaultValue = 0;
PromptDoubleResult resDou = ed.GetDouble(optDou);
if (resDou.Status == PromptStatus.OK)
{
Double width = resDou.Value;
return width;
}
else
return 0;
}
public short GetColorIndex()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptIntegerOptions optInt = new PromptIntegerOptions("\n请输入颜色索引值(0~256)");
optInt.DefaultValue = 0;
PromptIntegerResult resInt = ed.GetInteger(optInt);
if (resInt.Status == PromptStatus.OK)
{
short colorIndex = (short)resInt.Value;
if (colorIndex > 256 | colorIndex < 0)
return 0;
else
return colorIndex;
}
else
return 0;
}