对于.Net中的Autocad,我有一个包含dwg文件的文件夹,我想删除除“Model”和“Layout1”之外的每个文件中的布局。我在Autocad中使用此代码打开文件:
public void RemoveLayout(Transaction transaction, Database database, ObjectId layoutId)
{
if (!layoutId.IsErased)
{
Layout layout = transaction.GetObject(layoutId, OpenMode.ForRead) as Layout;
LayoutManager.Current.DeleteLayout(layout.LayoutName);
}
}
但是,当我尝试打开文件夹中的文档并将文档设置为活动时:
Document doc = Application.DocumentManager.Open(file, false);
Application.DocumentManager.MdiActiveDocument = doc;
LayoutManager.current
仍然指向我的第一个文档,因此我无法使用以下代码删除所需的布局:
LayoutManager.Current.DeleteLayout(layout.LayoutName);
答案 0 :(得分:0)
您需要在命令标志中添加Session
。并且需要明确锁定文档:
[CommandMethod("YOURCOMMAND", CommandFlags.Session)]
public void YourCommand()
{
[..]
Document doc = Application.DocumentManager.Open(file);
// No need here to set MdiActiveDocument, it's done by the Open method.
using (doc.LockDocument())
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
LayoutManager.Current.DeleteLayout("Your layout name");
tr.Commit();
}
}