[CommandMethod("InsertOut")] //插入外面的块文件 public void InsertOut() { Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; PromptPointResult ppr = ed.GetPoint("选择插入点"); Point3d pt = ppr.Value;//获取到插入点 //utility.WriteToEditor(pt.ToString()); string blockPath = @"E:\sguozengWork\德兴市管线数据\pipe\R18\Dwg\well_symbol\变压器.dwg"; using (Database blkDb = new Database(false, true)) { //read drawing blkDb.ReadDwgFile(blockPath, System.IO.FileShare.Read, true, null); blkDb.CloseInput(true); using (DocumentLock docLock = doc.LockDocument())//多文档要先这样,否则报至命错误 { using (Transaction t = doc.TransactionManager.StartTransaction()) { string name = "aa";//aa是不与blockPath文件中的任何块重名的字符串 //insert it as a new block ObjectId idBTR = doc.Database.Insert(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)) //pt是一个Point3D坐标,这里是插入进当前dwg文件中 { btr.AppendEntity(bref); t.AddNewlyCreatedDBObject(bref, true); } t.Commit(); } } } }