当通过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
当我寻找与我有相同问题的人时,我在谷歌上发现了这条线索,因此如果其他人发现了这一线索,我认为在这里发布解决方案比创建自己的线索更为简洁。 |