如何编写代码实现从外部输入块,并插入到当前文件中
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
/// <summary>
/// 使用一个新建的database从另外一个不打开的文件引入块记录
/// ImportBlock_DB里的ReadDwgFile方法在编译调试环境下会出错,错误为:尝试读取或写入受保护的内存
/// 但在正常AutoCAD启动装载程序情况下可用
/// Version : 2008.11.11 Sieben
/// </summary>
/// <param name="fileName">另外一个文件的文件名</param>
/// <param name="blockName">引入的一个块记录名,若为空字符串"",将引入文件里面的全部图块</param>
/// <returns>成功返回true,否则返回false</returns>
static public StringCollection ImportBlock_DB(string fileName, string blockName)
{
if (!File.Exists(fileName)) return null;
Database sourceDb = new Database(false, true);
try
{
//下面的ReadDwgFile方法在编译调试环境下会出错,错误为:尝试读取或写入受保护的内存
//但在正常AutoCAD启动装载程序情况下可用.
sourceDb.ReadDwgFile(fileName, System.IO.FileShare.Read, false, "");
Database destDb = aApp.DocumentManager.MdiActiveDocument.Database;
ObjectIdCollection blockIds = new ObjectIdCollection();
StringCollection reValue = new StringCollection();
using (Transaction ctrans = sourceDb.TransactionManager.StartTransaction())
{
BlockTable cbt = (BlockTable)ctrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);
if (blockName == "")
{
foreach (ObjectId btrId in cbt)
{
BlockTableRecord cbtr = (BlockTableRecord)ctrans.GetObject(btrId, OpenMode.ForRead, false);
if (!cbtr.IsAnonymous && !cbtr.IsLayout)
{
blockIds.Add(btrId);
reValue.Add(cbtr.Name);
}
cbtr.Dispose();
}
}
else if (cbt.Has(blockName))
{
blockIds.Add(cbt[blockName]);
}
cbt.Dispose();
}
if (blockIds.Count == 0)
{
return null;
}
else
{
IdMapping mapping = new IdMapping();
sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, new IdMapping(), DuplicateRecordCloning.Replace, false);
return reValue;
}
}
catch (System.Exception ex)
{
se.WriteMessage(ex);
return null;
}
finally { sourceDb.Dispose(); }
}
/// 使用一个新建的database从另外一个不打开的文件引入块记录
/// ImportBlock_DB里的ReadDwgFile方法在编译调试环境下会出错,错误为:尝试读取或写入受保护的内存
/// 但在正常AutoCAD启动装载程序情况下可用
/// Version : 2008.11.11 Sieben
/// </summary>
/// <param name="fileName">另外一个文件的文件名</param>
/// <param name="blockName">引入的一个块记录名,若为空字符串"",将引入文件里面的全部图块</param>
/// <returns>成功返回true,否则返回false</returns>
static public StringCollection ImportBlock_DB(string fileName, string blockName)
{
if (!File.Exists(fileName)) return null;
Database sourceDb = new Database(false, true);
try
{
//下面的ReadDwgFile方法在编译调试环境下会出错,错误为:尝试读取或写入受保护的内存
//但在正常AutoCAD启动装载程序情况下可用.
sourceDb.ReadDwgFile(fileName, System.IO.FileShare.Read, false, "");
Database destDb = aApp.DocumentManager.MdiActiveDocument.Database;
ObjectIdCollection blockIds = new ObjectIdCollection();
StringCollection reValue = new StringCollection();
using (Transaction ctrans = sourceDb.TransactionManager.StartTransaction())
{
BlockTable cbt = (BlockTable)ctrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);
if (blockName == "")
{
foreach (ObjectId btrId in cbt)
{
BlockTableRecord cbtr = (BlockTableRecord)ctrans.GetObject(btrId, OpenMode.ForRead, false);
if (!cbtr.IsAnonymous && !cbtr.IsLayout)
{
blockIds.Add(btrId);
reValue.Add(cbtr.Name);
}
cbtr.Dispose();
}
}
else if (cbt.Has(blockName))
{
blockIds.Add(cbt[blockName]);
}
cbt.Dispose();
}
if (blockIds.Count == 0)
{
return null;
}
else
{
IdMapping mapping = new IdMapping();
sourceDb.WblockCloneObjects(blockIds, destDb.BlockTableId, new IdMapping(), DuplicateRecordCloning.Replace, false);
return reValue;
}
}
catch (System.Exception ex)
{
se.WriteMessage(ex);
return null;
}
finally { sourceDb.Dispose(); }
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询