Cad直线命令
[CommandMethod("Line1")]
public void Line1()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
}
声明一个直线的集合对象
List<ObjectId> lineList = new List<ObjectId>();
声明一个预备的对象
Point3d pointStart = new Point3d(100, 100, 0);
Point3d pointPre = new Point3d(100, 100, 0);
PromptPointResult ppr = ed.GetPoint2("\n指定第一个点:");
if (ppr.Status == PromptStatus.Cancel)
{
return;
}
if (ppr.Status == PromptStatus.None)
{
pointPre = pointStart;
}
if (ppr.Status == PromptStatus.OK)
{
pointStart = ppr.Value;
pointPre = pointStart;
}
LineJig lineJig;
PromptResult pr;
判断循环是否继续
bool isContinue = true;
while (isContinue)
{
//只要有两条直线就闭合
if (lineList.Count > 1)
{
lineJig = new LineJig(pointPre, "\n指定下一点或 [闭合(C)/放弃(U)]:",new string[] { "C","U"});
pr = ed.Drag(lineJig);
}
else
{
lineJig = new LineJig(pointPre, "\n指定下一点或 [放弃(U)]:", new string[] { "U" });
pr = ed.Drag(lineJig);
}
//Point3d pointNext;
if (pr.Status == PromptStatus.Cancel)
{
return;
}
if (pr.Status == PromptStatus.None)
{
return;
}
if (pr.Status == PromptStatus.OK)
{
Line line = (Line)lineJig.GetEntity();
pointPre = line.EndPoint;
ObjectId lId = db.AddEntityToModelSpace(line);
lineList.Add(lId);
}
if (pr.Status == PromptStatus.Keyword)
{
switch (pr.StringResult)
{
case "U":
if (lineList.Count == 0)
{
pointStart = new Point3d(100, 100, 0);
pointPre = new Point3d(100, 100, 0);
ppr = ed.GetPoint2("\n指定第一个点:");
if (ppr.Status == PromptStatus.Cancel)
{
return;
}
if (ppr.Status == PromptStatus.None)
{
pointPre = pointStart;
}
if (ppr.Status == PromptStatus.OK)
{
pointStart = ppr.Value;
pointPre = pointStart;
}
}
else if (lineList.Count > 0)
{
int count = lineList.Count;
ObjectId lId = lineList.ElementAt(count - 1);
pointPre = this.GetLineStartPoint(lId);
lineList.RemoveAt(count - 1);
lId.EraseEntity();
}
break;
case "C":
lineList.Add(db.AddLineToModelSpace(pointPre, pointStart));
isContinue = false;
//return;
break;
case " ":
isContinue = false;
break;
default:
break;
}
}
}