从样板图形添加块-WblockCloneObjects-eWasOpenForNotify
我正在尝试使用WblockCloneObjects从模板绘图中添加块,但不断收到eWasOpenForNotify
错误。
///
/// Checks if the block definition is in the active drawing, if not, pull it from the template and add it
///
/// Name of the block.
///
public static ObjectId AddBlockDef(String blockName)
{
using (Transaction acTr = Active.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)acTr.GetObject(Active.Database.BlockTableId, OpenMode.ForNotify);
ObjectId blockId;
if (!bt.Has(blockName))
{
ObjectIdCollection ids = new ObjectIdCollection();
// get the template drawing
using (Database dwtDb = new Database(false, true))
{
string dllPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
String dwtPath =dllPath + @"\AID-Template.dwt";
dwtDb.ReadDwgFile(dwtPath, System.IO.FileShare.ReadWrite, true, "");
using (Transaction tr = dwtDb.TransactionManager.StartTransaction())
{
BlockTable dwtBt = (BlockTable)tr.GetObject(dwtDb.BlockTableId, OpenMode.ForRead);
// does it exist
if (dwtBt.Has(blockName))
{
ids.Add(dwtBt);
}
tr.Commit();
}
}
if (ids.Count > 0)
// add it to active drawing
{
IdMapping iMap = new IdMapping();
Active.Database.WblockCloneObjects(ids, Active.Database.BlockTableId, iMap,
DuplicateRecordCloning.Ignore, false); //<-- eWasOpenForNotify Error here
}
else
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("\nBlock: " + blockName +" not found.");
}
}
blockId = acTr.GetObject(bt, OpenMode.ForRead).ObjectId;
acTr.Commit();
return blockId;
}
}
有什么想法吗?
我从这里获得了代码的基础。也许我以错误的方式去做了?理论上,模板 dwt 文件将位于支持路径中。WblockCloneObjects看起来确实是一种绕开的方式,但我知道什么......
**** Hidden Message ***** 嗨,
只要快速查看一下代码中的异常类型,就可以注意到:
block table Bt =(block table)acTr。GetObject(活动。Database.BlockTableId,OpenMode。for notify); 将读取模式更改为读/写或读取时,它仍然会崩溃。
显然很活跃。当数据库上有任何事务处于打开状态时,无法调用Database.WblockCloneObjects。我进行了重构,将WblockCloneObjects从任何事务中取出,代码又开始工作了。
页:
[1]