CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

操作当前视图 (.NET)

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

您可以使用对象的方法访问模型或图纸空间中视口的当前视图。该方法返回一个对象。您可以使用对象来操作活动视口中视图的放大倍率、位置和方向。更改对象后,使用该方法更新活动视口的当前视图。GetCurrentViewEditorGetCurrentViewViewTableRecordViewTableRecordViewTableRecordSetCurrentView

将用于操作当前视图的一些常见属性包括:

  • 中心点 - DCS坐标中视点的中心点。
  • 高度 - DCS坐标中视图的高度。增加高度以缩小;降低高度以放大。
  • 目标 - WCS 坐标中视图的目标。
  • 视图方向 - WCS 坐标中视图从目标到摄像机的矢量。
  • 视图扭曲 - 以视图弧度为单位的扭转角度。
  • 宽度 - DCS坐标中视图的宽度。增加宽度以缩小;减小宽度以放大。

VBA 代码交叉引用

AutoCAD .NET API 不提供直接操作图形当前视图的方法,例如在 ActiveX 自动化库中找到的方法。例如,如果要缩放到图形中对象的范围或图形的限制,则必须操作当前视图的 和属性。要获取图形的限制范围,请使用 Database 对象的 ,,, 和属性。WidthHeightCenterPointExtminExtmaxLimminLimmax

用于操作当前视图的函数

此示例代码是后面的示例使用的常用过程。缩放过程接受四个参数来完成缩放到边界、平移或居中图形视图以及按给定因子缩放图形视图。缩放过程要求在 WCS 坐标中提供所有坐标值。

缩放过程的参数为:

  • 最小点 - 用于定义要显示的区域的左下角的 3D 点。
  • 最大点 - 用于定义要显示的区域右上角的 3D 点。
  • 中心点 - 用于定义视图中心的 3D 点。
  • 比例因子 - 用于指定比例以增大或减小视图大小的实数。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
 
Public Sub Zoom(ByVal pMin As Point3d, ByVal pMax As Point3d, _
                ByVal pCenter As Point3d, ByVal dFactor As Double)
    '' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    Dim nCurVport As Integer = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"))

    '' Get the extents of the current space when no points 
    '' or only a center point is provided
    '' Check to see if Model space is current
    If acCurDb.TileMode = True Then
        If pMin.Equals(New Point3d()) = True And _
            pMax.Equals(New Point3d()) = True Then

            pMin = acCurDb.Extmin
            pMax = acCurDb.Extmax
        End If
    Else
        '' Check to see if Paper space is current
        If nCurVport = 1 Then
            If pMin.Equals(New Point3d()) = True And _
                pMax.Equals(New Point3d()) = True Then

                pMin = acCurDb.Pextmin
                pMax = acCurDb.Pextmax
            End If
        Else
            '' Get the extents of Model space
            If pMin.Equals(New Point3d()) = True And _
                pMax.Equals(New Point3d()) = True Then

                pMin = acCurDb.Extmin
                pMax = acCurDb.Extmax
            End If
        End If
    End If

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
        '' Get the current view
        Using acView As ViewTableRecord = acDoc.Editor.GetCurrentView()
            Dim eExtents As Extents3d

            '' Translate WCS coordinates to DCS
            Dim matWCS2DCS As Matrix3d
            matWCS2DCS = Matrix3d.PlaneToWorld(acView.ViewDirection)
            matWCS2DCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCS2DCS
            matWCS2DCS = Matrix3d.Rotation(-acView.ViewTwist, _
                                           acView.ViewDirection, _
                                           acView.Target) * matWCS2DCS

            '' If a center point is specified, define the min and max 
            '' point of the extents
            '' for Center and Scale modes
            If pCenter.DistanceTo(Point3d.Origin) <> 0 Then
                pMin = New Point3d(pCenter.X - (acView.Width / 2), _
                                   pCenter.Y - (acView.Height / 2), 0)

                pMax = New Point3d((acView.Width / 2) + pCenter.X, _
                                   (acView.Height / 2) + pCenter.Y, 0)
            End If

            '' Create an extents object using a line
            Using acLine As Line = New Line(pMin, pMax)
                eExtents = New Extents3d(acLine.Bounds.Value.MinPoint, _
                                         acLine.Bounds.Value.MaxPoint)
            End Using

            '' Calculate the ratio between the width and height of the current view
            Dim dViewRatio As Double
            dViewRatio = (acView.Width / acView.Height)

            '' Tranform the extents of the view
            matWCS2DCS = matWCS2DCS.Inverse()
            eExtents.TransformBy(matWCS2DCS)

            Dim dWidth As Double
            Dim dHeight As Double
            Dim pNewCentPt As Point2d

            '' Check to see if a center point was provided (Center and Scale modes)
            If pCenter.DistanceTo(Point3d.Origin) <> 0 Then
                dWidth = acView.Width
                dHeight = acView.Height

                If dFactor = 0 Then
                    pCenter = pCenter.TransformBy(matWCS2DCS)
                End If

                pNewCentPt = New Point2d(pCenter.X, pCenter.Y)
            Else '' Working in Window, Extents and Limits mode
                 '' Calculate the new width and height of the current view
                dWidth = eExtents.MaxPoint.X - eExtents.MinPoint.X
                dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y

                '' Get the center of the view
                pNewCentPt = New Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5), _
                                         ((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5))
            End If

            '' Check to see if the new width fits in current window
            If dWidth > (dHeight * dViewRatio) Then dHeight = dWidth / dViewRatio

            '' Resize and scale the view
            If dFactor <> 0 Then
                acView.Height = dHeight * dFactor
                acView.Width = dWidth * dFactor
            End If

            '' Set the center of the view
            acView.CenterPoint = pNewCentPt

            '' Set the current view
            acDoc.Editor.SetCurrentView(acView)
        End Using

        '' Commit the changes
        acTrans.Commit()
    End Using
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
 
static void Zoom(Point3d pMin, Point3d pMax, Point3d pCenter, double dFactor)
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    int nCurVport = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"));

    // Get the extents of the current space when no points 
    // or only a center point is provided
    // Check to see if Model space is current
    if (acCurDb.TileMode == true)
    {
        if (pMin.Equals(new Point3d()) == true && 
            pMax.Equals(new Point3d()) == true)
        {
            pMin = acCurDb.Extmin;
            pMax = acCurDb.Extmax;
        }
    }
    else
    {
        // Check to see if Paper space is current
        if (nCurVport == 1)
        {
            // Get the extents of Paper space
            if (pMin.Equals(new Point3d()) == true && 
                pMax.Equals(new Point3d()) == true)
            {
                pMin = acCurDb.Pextmin;
                pMax = acCurDb.Pextmax;
            }
        }
        else
        {
            // Get the extents of Model space
            if (pMin.Equals(new Point3d()) == true && 
                pMax.Equals(new Point3d()) == true)
            {
                pMin = acCurDb.Extmin;
                pMax = acCurDb.Extmax;
            }
        }
    }

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Get the current view
        using (ViewTableRecord acView = acDoc.Editor.GetCurrentView())
        {
            Extents3d eExtents;

            // Translate WCS coordinates to DCS
            Matrix3d matWCS2DCS;
            matWCS2DCS = Matrix3d.PlaneToWorld(acView.ViewDirection);
            matWCS2DCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCS2DCS;
            matWCS2DCS = Matrix3d.Rotation(-acView.ViewTwist, 
                                            acView.ViewDirection, 
                                            acView.Target) * matWCS2DCS;

            // If a center point is specified, define the min and max 
            // point of the extents
            // for Center and Scale modes
            if (pCenter.DistanceTo(Point3d.Origin) != 0)
            {
                pMin = new Point3d(pCenter.X - (acView.Width / 2),
                                    pCenter.Y - (acView.Height / 2), 0);

                pMax = new Point3d((acView.Width / 2) + pCenter.X,
                                    (acView.Height / 2) + pCenter.Y, 0);
            }

            // Create an extents object using a line
            using (Line acLine = new Line(pMin, pMax))
            {
                eExtents = new Extents3d(acLine.Bounds.Value.MinPoint,
                                            acLine.Bounds.Value.MaxPoint);
            }

            // Calculate the ratio between the width and height of the current view
            double dViewRatio;
            dViewRatio = (acView.Width / acView.Height);

            // Tranform the extents of the view
            matWCS2DCS = matWCS2DCS.Inverse();
            eExtents.TransformBy(matWCS2DCS);

            double dWidth;
            double dHeight;
            Point2d pNewCentPt;

            // Check to see if a center point was provided (Center and Scale modes)
            if (pCenter.DistanceTo(Point3d.Origin) != 0)
            {
                dWidth = acView.Width;
                dHeight = acView.Height;

                if (dFactor == 0)
                {
                    pCenter = pCenter.TransformBy(matWCS2DCS);
                }

                pNewCentPt = new Point2d(pCenter.X, pCenter.Y);
            }
            else // Working in Window, Extents and Limits mode
            {
                // Calculate the new width and height of the current view
                dWidth = eExtents.MaxPoint.X - eExtents.MinPoint.X;
                dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y;

                // Get the center of the view
                pNewCentPt = new Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5),
                                            ((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5));
            }

            // Check to see if the new width fits in current window
            if (dWidth > (dHeight * dViewRatio)) dHeight = dWidth / dViewRatio;

            // Resize and scale the view
            if (dFactor != 0)
            {
                acView.Height = dHeight * dFactor;
                acView.Width = dWidth * dFactor;
            }

            // Set the center of the view
            acView.CenterPoint = pNewCentPt;

            // Set the current view
            acDoc.Editor.SetCurrentView(acView);
        }

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

路过

雷人

握手

鲜花

鸡蛋

最新评论

 VBA & VB.NET开发基础与实例教程

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

GMT+8, 2024-5-6 23:56

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部