CAD开发 AutoCAD事务
AutoCAD事务(Transaction)是AutoCAD .NET API中的一种机制,用于管理对图形数据库的修改。它允许开发者将一系列操作封装为一个事务,确保这些操作要么全部成功,要么全部失败,从而维护数据的一致性。流程大致为:获取文档--获取文档的数据库--开启事务--修改操作(添加、删除、修改等等)--提交事务。
开启事务:Transaction trans = db.TransactionManager.StartTransaction()
各种操作:增、删、改、查询等等
提交事务:trans.Commit()
撤销事务: trans.Abort()
//向当前文档中添加一条线
Document doc = Application.DocumentManager.MdiActiveDocument;//固定格式:获取当前文档
Database db = doc.Database;//获取当前数据库
using (Transaction trans = db.TransactionManager.StartTransaction())//设置事务
{
BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); // 以写入方式打开模型空间
Point3d point1 = new Point3d(0, 0, 0);
Point3d point2 = new Point3d(0, 1, 0);
Line line = new Line(point1, point2);//建立一条线
modelSpace.AppendEntity(line);//把线添加到模型空间
trans.AddNewlyCreatedDBObject(line, true);//把线添加到事务中
trans.Commit(); // 提交事务
}
posted on 2025-11-15 05:01 sswsswssw1996 阅读(0) 评论(0) 收藏 举报
浙公网安备 33010602011771号