如何在不添加到数据库的情况下在 AutoCAD 绘图区域上显示图元

本文关键字:区域 绘图 显示图 图元 AutoCAD 显示 添加 数据库 情况下 | 更新日期: 2023-09-27 18:31:48

我想在绘图区域上显示图元作为用户的预览,然后如果用户接受程序,则将图元添加到数据库或进行一些修改。

我习惯于使用事务并提交实体出现的事务,如果我可以在提交事务之前使实体出现

using (Transaction tr = db.TransactionManager.StartTransaction())
{
    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
    BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
    int i = poly2TxtSetting.currentFormat.IndexFormat.startWith;
    List<ObjectId> ListTextId = new List<ObjectId>();
    List<ObjectId> ListPointId = new List<ObjectId>();
    foreach (var po in Points)
    {
        i += poly2TxtSetting.currentFormat.IndexFormat.step;
        DBText dtext = new DBText();
        dtext.TextString = i.tostring();
        dtext.Position = po;
        dtext.SetDatabaseDefaults();
        DBPoint point = new DBPoint(po);
        btr.AppendEntity(dtext);
        tr.AddNewlyCreatedDBObject(dtext, true);
        btr.AppendEntity(point);
        tr.AddNewlyCreatedDBObject(point, true);
    }
    tr.Commit();
}

如何在不添加到数据库的情况下在 AutoCAD 绘图区域上显示图元

如果要在 AutoCAD 模型空间中显示模型,则有两个选项。

1)将其插入数据库。2) 将其添加到瞬态管理器中。

我认为你需要的是第二种选择。

搜索瞬态图形。

检查下面的代码,这将有助于你。

Solid3d solid=new Solid(0);
solid.CreateSphere(10);
TransientManager.CurrentTransientManager.AddTransient(solid, TransientDrawingMode.Main, 128, new IntegerCollection());

这将在原点上显示半径=10 的球体;

您可以等待图形刷新:

 tr.TransactionManager.QueueForGraphicsFlush();

然后提示输入,以便用户有时间查看更新:

PromptKeywordOptions pko = new PromptKeywordOptions("'nKeep Changes?");
pko.AllowNone = true;
pko.Keywords.Add("Y");
pko.Keywords.Add("N");
pko.Keywords.Default = "Y";
PromptResult pkr = ed.GetKeywords(pko);
if (pkr.StringResult == "Y") {
    tr.Commit();
} else {
    tr.Abort();
}

此链接提供了使用此技术的示例应用程序。