CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

标识和操作活动视区 (.NET)

2023-1-1 16:07| 发布者: admin| 查看: 692| 评论: 0|来自: AutoCAD

活动视口在“视口”表中由名为“*Active”的记录表示,该记录不是唯一的名称,因为当前显示在“模型”选项卡上的所有平铺视口都命名为“*Active”。显示的每个平铺视口都分配有一个编号。活动视口的编号可通过以下方式获得:

  • 检索 CVPORT 系统变量的值
  • 使用编辑器对象的属性获取活动视区的对象 ID,然后打开视口对象以访问其 Number 属性ActiveViewportId

拥有活动视口后,您可以控制其显示属性,为视口启用绘图辅助工具(如网格和捕捉)以及视口本身的大小。平铺视口由两个角点定义:左下角和右上角。属性表示视区在显示器上的图形位置。LowerLeftCornerUpperRightCorner

单个平铺视口配置的左下角为 (0,0),右上角为 (1,1)。绘图窗口的左下角始终由点 (0,0) 表示,而右上角由 (1,1) 表示,无论“模型”选项卡上的平铺视口数量如何。当显示多个平铺视口时,左下角和右上角将有所不同,但一个视口的左下角为 (0,0),另一个视区的右上角为 (1,1)

这些属性定义如下(以四向拆分为例):

在此示例中:

  • 视口 1 - 左下角 = (0, .5), 右上角 = (.5, 1)
  • 视口 2 - 左下角 = (.5, .5), 右上角 = (1, 1)
  • 视口 3-左下角 = (0, 0), 右上角 = (.5, .5)
  • 视口 4-左下角 = (.5, 0), 右上角 = (1, .5)

创建具有两个水平窗口的新平铺视口配置

以下示例创建一个两个水平视口作为命名视口配置,并重新定义活动显示。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("CreateModelViewport")> _
Public Sub CreateModelViewport()
    '' Get the current database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

        '' Open the Viewport table for read
        Dim acVportTbl As ViewportTable
        acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId, OpenMode.ForRead)

        '' Check to see if the named view 'TEST_VIEWPORT' exists
        If (acVportTbl.Has("TEST_VIEWPORT") = False) Then
            '' Open the View table for write
            acTrans.GetObject(acCurDb.ViewportTableId, OpenMode.ForWrite)

            '' Add the new viewport to the Viewport table and the transaction
            Using acVportTblRecLwr As ViewportTableRecord = New ViewportTableRecord()
                acVportTbl.Add(acVportTblRecLwr)
                acTrans.AddNewlyCreatedDBObject(acVportTblRecLwr, True)

                '' Name the new viewport 'TEST_VIEWPORT' and assign it to be
                '' the lower half of the drawing window
                acVportTblRecLwr.Name = "TEST_VIEWPORT"
                acVportTblRecLwr.LowerLeftCorner = New Point2d(0, 0)
                acVportTblRecLwr.UpperRightCorner = New Point2d(1, 0.5)

                '' Add the new viewport to the Viewport table and the transaction
                Using acVportTblRecUpr As ViewportTableRecord = New ViewportTableRecord()
                    acVportTbl.Add(acVportTblRecUpr)
                    acTrans.AddNewlyCreatedDBObject(acVportTblRecUpr, True)

                    '' Name the new viewport 'TEST_VIEWPORT' and assign it to be
                    '' the upper half of the drawing window
                    acVportTblRecUpr.Name = "TEST_VIEWPORT"
                    acVportTblRecUpr.LowerLeftCorner = New Point2d(0, 0.5)
                    acVportTblRecUpr.UpperRightCorner = New Point2d(1, 1)

                    '' To assign the new viewports as the active viewports, the 
                    '' viewports named '*Active' need to be removed and recreated
                    '' based on 'TEST_VIEWPORT'.

                    '' Step through each object in the symbol table
                    For Each acObjId As ObjectId In acVportTbl
                        '' Open the object for read
                        Dim acVportTblRec As ViewportTableRecord
                        acVportTblRec = acTrans.GetObject(acObjId, _
                                                          OpenMode.ForRead)

                        '' See if it is one of the active viewports, and if so erase it
                        If (acVportTblRec.Name = "*Active") Then
                            acTrans.GetObject(acObjId, OpenMode.ForWrite)
                            acVportTblRec.Erase()
                        End If
                    Next

                    '' Clone the new viewports as the active viewports
                    For Each acObjId As ObjectId In acVportTbl
                        '' Open the object for read
                        Dim acVportTblRec As ViewportTableRecord
                        acVportTblRec = acTrans.GetObject(acObjId, _
                                                          OpenMode.ForRead)

                        '' See if it is one of the active viewports, and if so erase it
                        If (acVportTblRec.Name = "TEST_VIEWPORT") Then
                            Dim acVportTblRecClone As ViewportTableRecord
                            acVportTblRecClone = acVportTblRec.Clone()

                            '' Add the new viewport to the Viewport table and the transaction
                            acVportTbl.Add(acVportTblRecClone)
                            acVportTblRecClone.Name = "*Active"
                            acTrans.AddNewlyCreatedDBObject(acVportTblRecClone, True)
                        End If
                    Next

                    '' Update the display with the new tiled viewports arrangement
                    acDoc.Editor.UpdateTiledViewportsFromDatabase()
                End Using
            End Using

            '' Commit the changes
            acTrans.Commit()
        End If

        '' Dispose of the transaction
    End Using
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("CreateModelViewport")]
public static void CreateModelViewport()
{
    // Get the current database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Viewport table for read
        ViewportTable acVportTbl;
        acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId,
                                        OpenMode.ForRead) as ViewportTable;

        // Check to see if the named view 'TEST_VIEWPORT' exists
        if (acVportTbl.Has("TEST_VIEWPORT") == false)
        {
            // Open the View table for write
            acTrans.GetObject(acCurDb.ViewportTableId, OpenMode.ForWrite);

            // Add the new viewport to the Viewport table and the transaction
            using (ViewportTableRecord acVportTblRecLwr = new ViewportTableRecord())
            {
                acVportTbl.Add(acVportTblRecLwr);
                acTrans.AddNewlyCreatedDBObject(acVportTblRecLwr, true);

                // Name the new viewport 'TEST_VIEWPORT' and assign it to be
                // the lower half of the drawing window
                acVportTblRecLwr.Name = "TEST_VIEWPORT";
                acVportTblRecLwr.LowerLeftCorner = new Point2d(0, 0);
                acVportTblRecLwr.UpperRightCorner = new Point2d(1, 0.5);

                // Add the new viewport to the Viewport table and the transaction
                using (ViewportTableRecord acVportTblRecUpr = new ViewportTableRecord())
                {
                    acVportTbl.Add(acVportTblRecUpr);
                    acTrans.AddNewlyCreatedDBObject(acVportTblRecUpr, true);

                    // Name the new viewport 'TEST_VIEWPORT' and assign it to be
                    // the upper half of the drawing window
                    acVportTblRecUpr.Name = "TEST_VIEWPORT";
                    acVportTblRecUpr.LowerLeftCorner = new Point2d(0, 0.5);
                    acVportTblRecUpr.UpperRightCorner = new Point2d(1, 1);

                    // To assign the new viewports as the active viewports, the 
                    // viewports named '*Active' need to be removed and recreated
                    // based on 'TEST_VIEWPORT'.

                    // Step through each object in the symbol table
                    foreach (ObjectId acObjId in acVportTbl)
                    {
                        // Open the object for read
                        ViewportTableRecord acVportTblRec;
                        acVportTblRec = acTrans.GetObject(acObjId,
                                                            OpenMode.ForRead) as ViewportTableRecord;

                        // See if it is one of the active viewports, and if so erase it
                        if (acVportTblRec.Name == "*Active")
                        {
                            acTrans.GetObject(acObjId, OpenMode.ForWrite);
                            acVportTblRec.Erase();
                        }
                    }

                    // Clone the new viewports as the active viewports
                    foreach (ObjectId acObjId in acVportTbl)
                    {
                        // Open the object for read
                        ViewportTableRecord acVportTblRec;
                        acVportTblRec = acTrans.GetObject(acObjId,
                                                            OpenMode.ForRead) as ViewportTableRecord;

                        // See if it is one of the active viewports, and if so erase it
                        if (acVportTblRec.Name == "TEST_VIEWPORT")
                        {
                            ViewportTableRecord acVportTblRecClone;
                            acVportTblRecClone = acVportTblRec.Clone() as ViewportTableRecord;

                            // Add the new viewport to the Viewport table and the transaction
                            acVportTbl.Add(acVportTblRecClone);
                            acVportTblRecClone.Name = "*Active";
                            acTrans.AddNewlyCreatedDBObject(acVportTblRecClone, true);
                        }
                    }

                    // Update the display with the new tiled viewports arrangement
                    acDoc.Editor.UpdateTiledViewportsFromDatabase();
                }
            }

            // Commit the changes
            acTrans.Commit();
        }

        // Dispose of the transaction
    }
}

VBA/ActiveX 代码参考

Sub CreateModelViewport()
    ' Create a new viewport
    Dim vportObj As AcadViewport
    Set vportObj = ThisDrawing.Viewports.Add("TEST_VIEWPORT")
 
    ' Split vportObj into 2 horizontal windows
    vportObj.Split acViewport2Horizontal
 
    ' Now set vportObj to be the active viewport
    ThisDrawing.ActiveViewport = vportObj
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

2024年新出cad图库素材

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-7 06:23

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部