class="cnblogs_code_Collapse">
C#实现在CAD图纸中插入另一个DWG图块的代码
PromptPointResult ppr = ed.GetPoint("请选择插入点:");
Point3d pt = ppr.Value;
utility.WriteToEditor(pt.ToString());
PIDBlock pidBlock = new PIDBlock();//自己定义的图块类,保存图块的路径和名称
pidBlock.Name = "sample";
pidBlock.Path = blockPath + "b_sample.dwg";using (Database blkDb = new Database(false, true))
{
//read drawing
blkDb.ReadDwgFile(pidBlock.Path, System.IO.FileShare.Read, true, null);
blkDb.CloseInput(true);
using (DocumentLock docLock = doc.LockDocument())//多文档要先这样,否则报至命错误
{
using (Transaction t = doc.TransactionManager.StartTransaction())
{
//insert it as a new block
ObjectId idBTR = doc.Database.Insert(pidBlock.Name, blkDb, false);
//create a ref to the block
BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
using (BlockReference bref = new BlockReference(pt, idBTR))
{
btr.AppendEntity(bref);
t.AddNewlyCreatedDBObject(bref, true);
}
t.Commit();
}
}
}