重命名布局选项卡 autocad c# .net
Renaming layout tab autocad c# .net
以下代码旨在重命名 autocad 中的布局选项卡。它总是重命名前 2 个布局选项卡。当我在 运行 程序之前添加更多选项卡时,它会重命名所有选项卡但会跳过 layout5。当我再次 运行 程序时,它会重新编号(包括 layout5)。
如果之后我添加更多布局,我会收到一个致命错误:Unhandeld Acces Violation Reading 0xffffffff exception at 1315DFE3h
有人可以指出正确的方向或者可以告诉我我做错了什么吗?提前致谢。
public class LayoutHernummeren
{
[CommandMethod("LayRenum")]
public void CmdLayRenum()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor edt = doc.Editor;
Database db = doc.Database;
string oldName;
string newName;
bool model = true;
try
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDict = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
foreach (DBDictionaryEntry layoutEntry in layoutDict)
{
Layout layout = trans.GetObject((ObjectId)layoutEntry.Value, OpenMode.ForRead) as Layout;
layout.UpgradeOpen();
oldName = layout.LayoutName;
newName = "blad" + layout.TabOrder.ToString("00");
if (oldName != newName)
{
if (layout.ModelType != model)
{
LayoutManager.Current.RenameLayout(oldName, newName);
}
}
}
trans.Commit();
}
}
catch(System.Exception ex)
{
edt.WriteMessage("\nError >> " + ex.Message);
}
}
}
看来我们需要遍历所有布局才能先刷新 TabOrder。
这是一个例子。
[CommandMethod("LayRenum")]
public void CmdLayRenum()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var edt = doc.Editor;
var db = doc.Database;
var layoutMgr = LayoutManager.Current;
try
{
using (var trans = db.TransactionManager.StartTransaction())
{
var layoutDict = (DBDictionary)trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
var layouts = layoutDict
.Cast<System.Collections.DictionaryEntry>()
.Select(entry => (Layout)trans.GetObject((ObjectId)entry.Value, OpenMode.ForWrite))
.OrderBy(layout => layout.TabOrder)
.ToArray();
for (int i = 1; i < layouts.Length; i++)
{
var layout = layouts[i];
LayoutManager.Current.RenameLayout(layout.LayoutName, $"blad{i:00}");
}
trans.Commit();
}
}
catch (System.Exception ex)
{
edt.WriteMessage("\nError >> " + ex.Message);
}
}
以下代码旨在重命名 autocad 中的布局选项卡。它总是重命名前 2 个布局选项卡。当我在 运行 程序之前添加更多选项卡时,它会重命名所有选项卡但会跳过 layout5。当我再次 运行 程序时,它会重新编号(包括 layout5)。 如果之后我添加更多布局,我会收到一个致命错误:Unhandeld Acces Violation Reading 0xffffffff exception at 1315DFE3h
有人可以指出正确的方向或者可以告诉我我做错了什么吗?提前致谢。
public class LayoutHernummeren
{
[CommandMethod("LayRenum")]
public void CmdLayRenum()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor edt = doc.Editor;
Database db = doc.Database;
string oldName;
string newName;
bool model = true;
try
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDict = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
foreach (DBDictionaryEntry layoutEntry in layoutDict)
{
Layout layout = trans.GetObject((ObjectId)layoutEntry.Value, OpenMode.ForRead) as Layout;
layout.UpgradeOpen();
oldName = layout.LayoutName;
newName = "blad" + layout.TabOrder.ToString("00");
if (oldName != newName)
{
if (layout.ModelType != model)
{
LayoutManager.Current.RenameLayout(oldName, newName);
}
}
}
trans.Commit();
}
}
catch(System.Exception ex)
{
edt.WriteMessage("\nError >> " + ex.Message);
}
}
}
看来我们需要遍历所有布局才能先刷新 TabOrder。 这是一个例子。
[CommandMethod("LayRenum")]
public void CmdLayRenum()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var edt = doc.Editor;
var db = doc.Database;
var layoutMgr = LayoutManager.Current;
try
{
using (var trans = db.TransactionManager.StartTransaction())
{
var layoutDict = (DBDictionary)trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
var layouts = layoutDict
.Cast<System.Collections.DictionaryEntry>()
.Select(entry => (Layout)trans.GetObject((ObjectId)entry.Value, OpenMode.ForWrite))
.OrderBy(layout => layout.TabOrder)
.ToArray();
for (int i = 1; i < layouts.Length; i++)
{
var layout = layouts[i];
LayoutManager.Current.RenameLayout(layout.LayoutName, $"blad{i:00}");
}
trans.Commit();
}
}
catch (System.Exception ex)
{
edt.WriteMessage("\nError >> " + ex.Message);
}
}