删除所有布局
在下面找到了这段代码:http://forums.autodesk.com/t5/net/erasing-layouts/td-p/2669032它看起来很简单,我看不出它有什么严重的问题,但希望其他人输入。如果您有以这种方式删除绘图中所有布局的经验,您介意提供输入吗?[命令方法("EraseAllLayout")]。公共静态无效擦除所有布局(){。
。
文档文档=Application.DocumentManager.MdiActiveDocument;。
数据库db=doc.Database;。
编辑doc.Editor。
。
使用(事务tr=db.TransactionManager.StartTransaction ()) {。
。
ACAD_LAYOUT字典。
DBDicpedia layoutDict=tr.GetObject(db.LayoutDictionaryId,OpenMode.ForRead)作为DBDicpedia;。
。
//迭代字典条目,。
foreach(layoutDict中的DBDictionaryEntry de){。
字符串布局名称=de.Key;。
if(layoutName!="Model"){。
LayoutManager.Current.DeleteLayout(layoutName);//删除布局,。
}。
}。
}。
。
ed.Regen (); // 更新AutoCAD GUI以关联更改,。
}。
**** Hidden Message ***** 除了不能删除所有布局外,看起来还可以。您必须至少保留一个图纸空间布局
我所做的是添加一个带有“-template”的布局,并将其用作模板布局,然后将其克隆为新布局,然后使用if中的条件将其过滤出循环,而不是删除它
类似这样:
迭代字典条目。
foreach(布局中的DBDictionaryEntry de)
{
字符串layoutName=de.Key
如果(layoutName!=“Model”&&!layoutnam.包含(“-template”)
{
LayoutManager.Current。删除布局(layoutName);//删除布局。
}
}
不要忘记提交事务。
我知道这里的上下文是.NET,但是如果你做这样的事情,它将删除所有布局,然后创建一个新的“Layout1”作为默认布局。即使您在运行布局时处于布局中。
(foreach item (layoutlist) (vl-cmdf "._layout" "_delete" item))
当通过ReadDwgFile()方法对外部DWG执行LayoutManager操作时,我得到了一些非常奇怪的行为。对我有效的解决方案是在执行操作之前和之后将活动布局设置为modelspace。希望这能节省一些时间。据我所知,这一定是LayoutManager本身的一个错误,可能与缓存的视口或regen有关,因为只要我在打开的DWG上使用它,代码就可以完美地工作
示例:
//My code is looping through all the layouts in a DWG looking for one specific layout that I know the name of.
//All the other layouts are to be deleted.
DBDictionary layouts;
Database externalDatabase = new Database(false, true);
externalDatabase.ReadDwgFile(@"C:\Users\MyUser\Drawing.dwg", FileOpenMode.OpenForReadAndAllShare, true, "");
HostApplicationServices.WorkingDatabase = externalDatabase;
ObjectId modelSpaceId = LayoutManager.Current.GetLayoutId("Model");
using (var transaction = externalDatabase.TransactionManager.StartTransaction())
{
//In the method I use I have to set OpenMode.ForWrite
//but in most cases I guess OpenMode.ForRead would be enough
layouts = (DBDictionary)transaction.GetObject(externalDatabase.LayoutDictionaryId, OpenMode.ForWrite);
transaction.Commit();
transaction.Dispose();
}
foreach (DBDictionaryEntry layout in layouts)
{
if (String.Equals(layout.Key, "Model"))
{
continue;
}
else if (String.Equals(layout.Key, "Name Of Layout To Keep"))
{
continue;
}
else
{
LayoutManager.Current.SetCurrentLayoutId(modelSpaceId);
LayoutManager.Current.DeleteLayout(layout.Key);
LayoutManager.Current.SetCurrentLayoutId(modelSpaceId);
}
}
//Don't forget to switch HostApplicationServices.WorkingDatabase back to the original database
当我寻找与我有相同问题的人时,我在谷歌上发现了这条线索,因此如果其他人发现了这一线索,我认为在这里发布解决方案比创建自己的线索更为简洁。
页:
[1]