- 积分
- 2848
- 明经币
- 个
- 注册时间
- 2004-7-28
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2013-4-2 11:19:24
|
显示全部楼层
- /// <summary>
- /// 在当前图形中导入外部图形中的块,返回块名称
- /// </summary>
- /// <param name="fileName">外部DWG图形文件(绝对路径)</param>
- /// <param name="blockName">待插入的外部图形中的块名称</param>
- /// <param name="bReplace">是否覆盖当前图形中的同名块</param>
- /// <returns>返回插入到当前文档中的块Id</returns>
- public static ObjectId m_ImportBlock(string fileName, string blockName, bool bReplace)
- {
- ObjectId destId = mFun.m_GetBlockId(blockName);
- if (destId.IsNull)
- {
- using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())//记住一定要先锁定文档
- {
- using (Database sourceDb = new Database(false, false))
- {
- sourceDb.ReadDwgFile(fileName.Trim(), FileShare.Read, true, null);
- //destId = mCommands.m_db.Insert(blockName, sourceDb, false);//错误?!?!块自参照???!!!
- destId = m_ImportBlock(sourceDb, blockName, bReplace);
- sourceDb.CloseInput(true);
- }
- }
- }
- return destId;
- }
- /// <summary>
- /// 从原数据库中导入具有指定块名称的块
- /// </summary>
- /// <param name="sourceDb">原数据库</param>
- /// <param name="blockName">要导入的块的名称</param>
- /// <param name="bReplace">是否覆盖同名块</param>
- /// <returns></returns>
- public static ObjectId m_ImportBlock(Database sourceDb, string blockName, bool bReplace)
- {
- ObjectId destId = ObjectId.Null;
- using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
- {
- using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction())
- {
- BlockTable sourceBt = sourceTr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead) as BlockTable;
- if (sourceBt.Has(blockName))
- {
- ObjectId sourceId = sourceBt[blockName];
- IdMapping idMap = new IdMapping();
- sourceDb.WblockCloneObjects(
- new ObjectIdCollection(new ObjectId[] { sourceId }),
- HostApplicationServices.WorkingDatabase.BlockTableId,
- idMap,
- bReplace == true ? DuplicateRecordCloning.Replace : DuplicateRecordCloning.MangleName,
- false);
- destId = idMap[sourceId].Value;
- }
- sourceTr.Commit();
- }
- }
- return destId;
- }
|
|