后台打开图形

 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         }

 

转载于:https://www.cnblogs.com/luny147258/p/4638554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值