如何将此列表限制为仅包含 AutoCAD 中的图纸空间布局?

问题描述

这是在命令行窗口中显示布局列表的简单命令:

public void TestVP()
{
    try
    {
        _AcAp.Document acDoc = _AcAp.Application.DocumentManager.MdiActiveDocument;
        _AcDb.Database acCurDb = acDoc.Database;

        _AcEd.Editor editor = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
        using (_AcDb.Transaction acTrans = _AcDb.Hostapplicationservices.WorkingDatabase.TransactionManager.StartTransaction())
        {
            _AcDb.BlockTable acBlkTbl;
            acBlkTbl = acTrans.Getobject(acCurDb.BlockTableId,_AcDb.OpenMode.ForRead) as _AcDb.BlockTable;

            foreach(_AcDb.ObjectId id in acBlkTbl)
            {
                _AcDb.BlockTableRecord btRecord = (_AcDb.BlockTableRecord)acTrans.Getobject(id,_AcDb.OpenMode.ForRead);
                if(btRecord.IsLayout)
                {
                    _AcDb.Layout acLayout = (_AcDb.Layout)acTrans.Getobject(btRecord.LayoutId,_AcDb.OpenMode.ForRead);
                    editor.WriteMessage(acLayout.LayoutName + "\n");
                }
            }

            acTrans.Commit();
        }
    }
    catch (System.Exception ex)
    {
        _AcAp.Application.ShowAlertDialog(
            string.Format("\nError: {0}\nStacktrace: {1}",ex.Message,ex.StackTrace));
    }
}

目前它还包括模型布局。我不想这样。如何将布局列表限制为图纸空间布局?


即使我像这样使用 LayoutManager

using (_AcDb.Transaction acTrans = _AcDb.Hostapplicationservices.WorkingDatabase.TransactionManager.StartTransaction())
{
    _AcDb.DBDictionary layoutDic = acTrans.Getobject(acCurDb.LayoutDictionaryId,_AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;
    foreach(_AcDb.DBDictionaryEntry entry in layoutDic)
    {
        _AcDb.ObjectId layoutId = entry.Value;
        _AcDb.Layout layout = acTrans.Getobject(layoutId,_AcDb.OpenMode.ForRead) as _AcDb.Layout;
        editor.WriteMessage(String.Format("{0}--> {1}",Environment.NewLine,layout.LayoutName));
    }

    acTrans.Commit();
}

我仍然得到:

Active Layout is : Model Number of Layouts is : 3
--> Layout1
--> Layout2
--> Model

我不想针对“Model”的布局名称进行测试。

解决方法

这是我想出来的:

public void TestVP()
{
    try
    {
        _AcAp.Document acDoc = _AcAp.Application.DocumentManager.MdiActiveDocument;
        _AcDb.Database acCurDb = acDoc.Database;
        _AcEd.Editor editor = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
        _AcDb.LayoutManager layoutMgr = _AcDb.LayoutManager.Current;

        editor.WriteMessage(String.Format("{0}Active Layout is : {1}",Environment.NewLine,layoutMgr.CurrentLayout));
        editor.WriteMessage(String.Format("{0}Number of Layouts is : {1}",layoutMgr.LayoutCount));

        using (_AcDb.Transaction acTrans = _AcDb.HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
        {
            _AcDb.BlockTable bt = acCurDb.BlockTableId.GetObject(_AcDb.OpenMode.ForRead) as _AcDb.BlockTable;
            _AcDb.BlockTableRecord ms = acTrans.GetObject(bt[_AcDb.BlockTableRecord.ModelSpace],_AcDb.OpenMode.ForRead) as _AcDb.BlockTableRecord;
            _AcDb.DBDictionary layoutDic = acTrans.GetObject(acCurDb.LayoutDictionaryId,_AcDb.OpenMode.ForRead) as _AcDb.DBDictionary;
            foreach(_AcDb.DBDictionaryEntry entry in layoutDic)
            {
                _AcDb.ObjectId layoutId = entry.Value;
                _AcDb.Layout layout = acTrans.GetObject(layoutId,_AcDb.OpenMode.ForRead) as _AcDb.Layout;
                editor.WriteMessage(String.Format("{0}--> {1}",layout.LayoutName));

                _AcDb.BlockTableRecord layoutBlkTbl = acTrans.GetObject(layout.BlockTableRecordId,_AcDb.OpenMode.ForRead) as _AcDb.BlockTableRecord;
                if(layoutBlkTbl.Id != ms.Id)
                    editor.WriteMessage(" (PaperSpace)");
                
            }

            acTrans.Commit();
        }
    }
    catch (System.Exception ex)
    {
        _AcAp.Application.ShowAlertDialog(
            string.Format("\nError: {0}\nStacktrace: {1}",ex.Message,ex.StackTrace));
    }
}

我得到的输出是:

Active Layout is : Model
Number of Layouts is : 3
--> Layout1 (PaperSpace)
--> Layout2 (PaperSpace)
--> Model

我只是将 LayoutIdModel Space 对象 Idcase 进行比较。可能有更简单的方法。但至少我现在可以将我的列表限制为仅纸张空间布局。