var doc = Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var db = doc.Database;
using (var docLock = doc.LockDocument())
{
using (var tr = db.TransactionManager.StartTransaction())
{
var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
var btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
var txt = new DBText { TextString = "Hello World" };
btr.AppendEntity(txt); // 添加图元对象到数据库 + BlockTableRecord
tr.AddNewlyCreatedDBObject(txt, true);
// 如果没有这行,或者 add是false
// 如果后面有tr.commit() ,那么图元依然会添加进数据库, 但是有问题,不能选择t
// // 如果漏了 tr.AddNewlyCreatedDBObject(txt, true); 可以修复
//foreach (var id in btr)
//{
// var ent = tr.GetObject(id, OpenMode.ForWrite);
// tr.AddNewlyCreatedDBObject(ent, true); // make avaiable
// //ent.Erase(); // delete
// Console.WriteLine($"{ent}");
//}
// 如果用 tr.abort 那么数据会丢弃
tr.Commit();
}
}
网友评论