LineDemo
[CommandMethod("LineDemo")]
public void LineDemo()
{
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 = pointStart;
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;
}
bool isContinue = true;
while (isContinue)
{
if (lineList.Count > 1)
{
ppr = ed.GetPoint("\n 指定下一点或 [闭合(C)/放弃(U)]:", pointPre, new string[] { "C", "U" });
}
else
{
ppr = ed.GetPoint("\n 指定下一点或 [放弃(U)]:", pointPre, new string[] { "U" });
}
Point3d pointNext = new Point3d();
if (ppr.Status == PromptStatus.Cancel)
{
return;
}
if (ppr.Status == PromptStatus.None)
{
return;
}
if (ppr.Status == PromptStatus.OK)
{
pointNext = ppr.Value;
ObjectId lId = db.AddLineToModelSpace(pointPre, pointNext);
lineList.Add(lId);
pointPre = pointNext;
}
if (ppr.Status == PromptStatus.Keyword)
{
switch (ppr.StringResult)
{
case "U":
if (lineList.Count == 0)
{
pointStart = new Point3d(100, 100, 0);
pointPre = pointStart;
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":
ObjectId i = db.AddLineToModelSpace(pointStart, pointPre);
lineList.Add(i);
isContinue = false;
break;
case " ":
isContinue = false;
break;
default:
break;
}
}
}
}
获取点
public static PromptPointResult GetPoint2(this Editor ed, string promptStr)
{
PromptPointOptions ppo = new PromptPointOptions(promptStr);
ppo.AllowNone = true;
PromptPointResult ppr = ed.GetPoint(ppo);
return ppr;
}
获取点或关键字
public static PromptPointResult GetPoint(this Editor ed, string prompt, Point3d pointBase, params string[] keyWord)
{
PromptPointOptions ppo = new PromptPointOptions(prompt);
ppo.AllowNone = true;
for (int i = 0; i < keyWord.Length; i++)
{
ppo.Keywords.Add(keyWord[i]);
}
ppo.AppendKeywordsToMessage = false;
ppo.BasePoint = pointBase;
ppo.UseBasePoint = true;
PromptPointResult ppr = ed.GetPoint(ppo);
return ppr;
}
添加直线到图形数据库
public static ObjectId AddLineToModelSpace(this Database db, Point3d startPoint, Point3d endPoint)
{
return db.AddEntityToModelSpace(new Line(startPoint, endPoint));
}
将图形对象添加到图形文件中
public static ObjectId AddEntityToModelSpace(this Database db, Entity entity)
{
ObjectId entityId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entityId = btr.AppendEntity(entity);
trans.AddNewlyCreatedDBObject(entity, true);
trans.Commit();
}
return entityId;
}