将外部图块插入当前图形(c#代码)
在使用C#开发AutoCad工具的过程中,需要将外部定义好的dwg图块文件调入到当前文件中,在网上查阅了许多相关资料,所涉及的并不多,整理起来如下:
1 public ObjectId InsertBlock(string fileName)
2 {
3 ObjectId blockId;
4 //图形数据库读取外部图块
5 Database blockDatabase = new Database(false, true);
6 blockDatabase.ReadDwgFile(fileName, System.IO.FileShare.Read, false, string.Empty);
7 blockDatabase.CloseInput(true);
8
9 Database db = HostApplicationServices.WorkingDatabase;
10 using(Transaction trans = db.TransactionManager.StartTransaction())
11 {
12 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
13 string blockName = SymbolUtilityServices.GetBlockNameFromInsertPathName(fileName);
14 //将外部图块插入到当前模型空间
15 blockId = db.Insert(blockName, blockDatabase, false);
16 trans.Commit();
17 }
18 return blockId;
19 }
2 {
3 ObjectId blockId;
4 //图形数据库读取外部图块
5 Database blockDatabase = new Database(false, true);
6 blockDatabase.ReadDwgFile(fileName, System.IO.FileShare.Read, false, string.Empty);
7 blockDatabase.CloseInput(true);
8
9 Database db = HostApplicationServices.WorkingDatabase;
10 using(Transaction trans = db.TransactionManager.StartTransaction())
11 {
12 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
13 string blockName = SymbolUtilityServices.GetBlockNameFromInsertPathName(fileName);
14 //将外部图块插入到当前模型空间
15 blockId = db.Insert(blockName, blockDatabase, false);
16 trans.Commit();
17 }
18 return blockId;
19 }