后台打开图形
1 /// <summary> 2 /// 插入另一个dwg 3 /// </summary> 4 /// <param name="filename">文件名</param> 5 /// <param name="over">是否覆盖</param> 6 /// <returns></returns> 7 public static bool GetBlockDefFromFile(string filename, bool over) 8 { 9 Database db = HostApplicationServices.WorkingDatabase; 10 Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; 11 try 12 { 13 FileInfo fi = new FileInfo(filename); 14 string blkdefname = fi.Name; 15 if (blkdefname.Contains(".")) 16 { 17 blkdefname = blkdefname.Substring(0, blkdefname.LastIndexOf('.')); 18 } 19 bool has = Common.BlockTable(db).Has(blkdefname); 20 if ((has && over) || !has) 21 { 22 using (Database blkDb = new Database(false, true)) 23 { 24 blkDb.ReadDwgFile(filename, System.IO.FileShare.Read, true, null); 25 blkDb.CloseInput(true); 26 using (DocumentLock docLock = doc.LockDocument())//多文档要先这样,否则报至命错误 27 { 28 using (Transaction t = doc.TransactionManager.StartTransaction()) 29 { 30 ObjectId idBTR = doc.Database.Insert(blkdefname, blkDb, false); 31 t.Commit(); 32 } 33 } 34 blkDb.Dispose(); 35 36 } 37 } 38 return true; 39 } 40 catch 41 { 42 return false; 43 } 44 45 } 46 47 48 49 50 51 52 53 /// <summary> 54 /// 插入另一图的块名"blkdefname"块定义 55 /// </summary> 56 /// <param name="filename"></param> 57 /// <param name="blkdefname"></param> 58 /// <param name="over"></param>是否覆盖 59 /// <returns></returns> 60 public static bool GetBlockDefFromFile(string filename, string blkdefname, bool over) 61 { 62 try 63 { 64 bool has = Common.BlockTable(m_Database).Has(blkdefname); 65 if ((has && over) || !has) 66 { 67 using (Database blkDb = new Database(false, true)) 68 { 69 blkDb.ReadDwgFile(filename, System.IO.FileShare.Read, true, null); 70 blkDb.CloseInput(true); 71 using (DocumentLock docLock = m_Document.LockDocument())//多文档要先这样,否则报至命错误 72 { 73 using (Transaction trans = m_Database.TransactionManager.StartTransaction()) 74 { 75 ObjectIdCollection ids = new ObjectIdCollection(); 76 ids.Add(Common.GetBlock(blkdefname, blkDb).ObjectId); 77 blkDb.Wblock(m_Database, ids, new Point3d(), DuplicateRecordCloning.Replace); 78 trans.Commit(); 79 } 80 } 81 blkDb.Dispose(); 82 } 83 } 84 return true; 85 } 86 catch 87 { 88 return false; 89 } 90 91 }