CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

使平铺视口成为当前视口 (.NET)

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

在当前视口中输入点并选择对象。要使视口成为当前视口,请使用 CVPORT 系统变量,并按要设置当前视口的视口编号指定视口。

您可以循环访问现有视区以查找特定视区。为此,请使用属性标识名称为“*Active”的视口表记录。Name

拆分视口,然后循环访问窗口

本示例将活动视口拆分为两个水平窗口。然后,它循环访问图形中的所有平铺视口,并显示视口名称以及每个视口的左下角和右上角。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("SplitAndIterateModelViewports")> _
Public Sub SplitAndIterateModelViewports()
    '' 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 write
        Dim acVportTbl As ViewportTable
        acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId, _
                                       OpenMode.ForWrite)

        '' Open the active viewport for write
        Dim acVportTblRec As ViewportTableRecord
        acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, _
                                          OpenMode.ForWrite)

        Using acVportTblRecNew As ViewportTableRecord = New ViewportTableRecord()

            '' Add the new viewport to the Viewport table and the transaction
            acVportTbl.Add(acVportTblRecNew)
            acTrans.AddNewlyCreatedDBObject(acVportTblRecNew, True)

            '' Assign the name '*Active' to the new Viewport
            acVportTblRecNew.Name = "*Active"

            '' Use the existing lower left corner for the new viewport
            acVportTblRecNew.LowerLeftCorner = acVportTblRec.LowerLeftCorner

            '' Get half the X of the existing upper corner
            acVportTblRecNew.UpperRightCorner = New Point2d(acVportTblRec.UpperRightCorner.X, _
                                                            acVportTblRec.LowerLeftCorner.Y + _
                                                            ((acVportTblRec.UpperRightCorner.Y - _
                                                              acVportTblRec.LowerLeftCorner.Y) / 2))

            '' Recalculate the corner of the active viewport
            acVportTblRec.LowerLeftCorner = New Point2d(acVportTblRec.LowerLeftCorner.X, _
                                                        acVportTblRecNew.UpperRightCorner.Y)

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

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

                If (acVportTblRecCur.Name = "*Active") Then
                    Application.SetSystemVariable("CVPORT", acVportTblRecCur.Number)

                    Application.ShowAlertDialog("Viewport: " & acVportTblRecCur.Number & _
                                                " is now active." & _
                                                vbLf & "Lower left corner: " & _
                                                acVportTblRecCur.LowerLeftCorner.X & ", " & _
                                                acVportTblRecCur.LowerLeftCorner.Y & _
                                                vbLf & "Upper right corner: " & _
                                                acVportTblRecCur.UpperRightCorner.X & ", " & _
                                                acVportTblRecCur.UpperRightCorner.Y)
                End If
            Next
        End Using

        '' Commit the changes and dispose of the transaction
        acTrans.Commit()
    End Using
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("SplitAndIterateModelViewports")]
public static void SplitAndIterateModelViewports()
{
    // 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 write
        ViewportTable acVportTbl;
        acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId,
                                        OpenMode.ForWrite) as ViewportTable;

        // Open the active viewport for write
        ViewportTableRecord acVportTblRec;
        acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                                            OpenMode.ForWrite) as ViewportTableRecord;

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

            // Assign the name '*Active' to the new Viewport
            acVportTblRecNew.Name = "*Active";

            // Use the existing lower left corner for the new viewport
            acVportTblRecNew.LowerLeftCorner = acVportTblRec.LowerLeftCorner;

            // Get half the X of the existing upper corner
            acVportTblRecNew.UpperRightCorner = new Point2d(acVportTblRec.UpperRightCorner.X,
                                                            acVportTblRec.LowerLeftCorner.Y +
                                                            ((acVportTblRec.UpperRightCorner.Y -
                                                                acVportTblRec.LowerLeftCorner.Y) / 2));

            // Recalculate the corner of the active viewport
            acVportTblRec.LowerLeftCorner = new Point2d(acVportTblRec.LowerLeftCorner.X,
                                                        acVportTblRecNew.UpperRightCorner.Y);

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

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

                if (acVportTblRecCur.Name == "*Active")
                {
                    Application.SetSystemVariable("CVPORT", acVportTblRecCur.Number);

                    Application.ShowAlertDialog("Viewport: " + acVportTblRecCur.Number +
                                                " is now active." +
                                                "\nLower left corner: " +
                                                acVportTblRecCur.LowerLeftCorner.X + ", " +
                                                acVportTblRecCur.LowerLeftCorner.Y +
                                                "\nUpper right corner: " +
                                                acVportTblRecCur.UpperRightCorner.X + ", " +
                                                acVportTblRecCur.UpperRightCorner.Y);
                }
            }
        }

        // Commit the changes and dispose of the transaction
        acTrans.Commit();
    }
}

VBA/ActiveX 代码参考

Sub SplitandInterateModelViewports()
    ' Get the active viewport
    Dim vportObj As AcadViewport
    Set vportObj = ThisDrawing.ActiveViewport
 
    ' Split the viewport into 2 windows
    vportObj.Split acViewport2Horizontal
 
    ' Iterate through the viewports,
    ' highlighting each viewport and displaying
    ' the upper right and lower left corners
    ' for each.
    Dim vport As AcadViewport
    Dim LLCorner As Variant
    Dim URCorner As Variant
 
    For Each vport In ThisDrawing.Viewports
        ThisDrawing.ActiveViewport = vport
        LLCorner = vport.LowerLeftCorner
        URCorner = vport.UpperRightCorner
        MsgBox "Viewport: " & vport.Name & " is now active." & _
               vbCrLf & "Lower left corner: " & _
               LLCorner(0) & ", " & LLCorner(1) & vbCrLf & _
               "Upper right corner: " & _
               URCorner(0) & ", " & URCorner(1)
    Next vport
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

2024年新出cad图库素材

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

GMT+8, 2024-5-7 03:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部