【转载】将dwg作为块插入及从新定义块并更新显示
本帖最后由 qq1254582201 于 2024-11-4 16:44 编辑public void ReplaceBlock2()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
string blockName = "TEST";
Database blkDb = new Database(false, true);
blkDb.ReadDwgFile(@"C:\\Temp\\TEST.dwg", System.IO.FileShare.Read, true, "");
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
BlockTable blockTable = Tx.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
ObjectId btrId = db.Insert(blockName, blkDb, true);
if (btrId != ObjectId.Null)
{
BlockTableRecord btr = Tx.GetObject(btrId, OpenMode.ForRead, false, true);
ObjectIdCollection brefIds = btr.GetBlockReferenceIds(false, true);
foreach (ObjectId id in brefIds)
{
BlockReference bref = Tx.GetObject(id, OpenMode.ForWrite, false, true);
bref.RecordGraphicsModified(true);
}
}
Tx.Commit();
}
blkDb.Dispose();
}插入外部CAD作为块,并更新显示
public void ReplaceBlock1()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
string blockName = "TEST";
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
BlockTable blockTable = Tx.GetObject(db.BlockTableId, OpenMode.ForRead, false, true);
if ((blockTable.Has(blockName)))
{
BlockTableRecord btr = Tx.GetObject(blockTable, OpenMode.ForWrite, false, true);
// Erase all entities in btr
foreach (ObjectId id in btr)
{
Entity ent = id.GetObject(OpenMode.ForWrite);
ent.Erase();
}
// Add new entities to btr
Line aLine = new Line(new Point3d(50, 50, 0), new Point3d(100, 100, 0));
btr.AppendEntity(aLine);
Tx.AddNewlyCreatedDBObject(aLine, true);
ObjectIdCollection brefIds = btr.GetBlockReferenceIds(false, true);
// Update blockrefs to display new graphics
foreach (ObjectId id in brefIds)
{
BlockReference bref = Tx.GetObject(id, OpenMode.ForWrite, false, true);
bref.RecordGraphicsModified(true);
}
Tx.Commit();
}
}
}
修改块对象后更新既有块对象内的显示
本帖最后由 你有种再说一遍 于 2024-11-4 19:16 编辑
你这个例子不好,
插入前要激活数据库,不然会产生文字偏移.
https://www.cnblogs.com/JJBox/p/10226119.html
在IFox提供的事务栈内封装了一个处理,
trans.Task(()=>{
//TODO
db.Insert(..)
});
来处理
谢谢楼主分享! 上个图看看效果啊。
页:
[1]