CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

转换坐标 (.NET)

2023-1-1 11:13| 发布者: admin| 查看: 567| 评论: 0|来自: AutoCAD

该方法可以将点或位移从一个坐标系转换为另一个坐标系。您可以使用该方法指定要从哪个坐标系转换到哪个坐标系以及要转换到哪个坐标系。该方法需要满足以下条件:TransformByAlignCoordinateSystemAlignCoordinateSystem

  • 要从中转换的坐标系的原点
  • 三个 3D 矢量,表示要从中平移的坐标系的 X、Y 和 X 轴
  • 要平移到的坐标系的原点
  • 三个 3D 矢量,表示要平移到的坐标系的 X、Y 和 X 轴
WCS系列

世界坐标系:参考坐标系。所有其他坐标系都是相对于 WCS 定义的,WCS 永远不会更改。相对于 WCS 测量的值在对其他坐标系的更改中是稳定的。除非另有说明,否则传入和传出 .NET API 中的方法和属性的所有点都在 WCS 中表示。

UCS系统

用户坐标系 (UCS):工作坐标系。用户指定 UCS 以简化绘图任务。传递给AutoCAD命令的所有点(包括从AutoLISP例程和外部函数返回的点)都是当前UCS中的点(除非用户在命令提示符下以*开头)。如果希望应用程序将 WCS、OCS 或 DCS 中的坐标发送到 AutoCAD 命令,则必须首先通过调用转换命令来将它们转换为 UCS,然后使用表示坐标值的方法转换 Point3d 或 Point 2d 对象。TransformBy

法 团

对象坐标系(也称为实体坐标系或 ECS):由某些方法和属性为 Polyline2d 和 Polyline 对象指定的点值在此坐标系中表示,相对于对象。这些点通常根据对象的预期用途转换为 WCS、当前 UCS 或当前 DCS。相反,WCS、UCS 或 DCS 中的点必须先转换为 OCS,然后才能通过相同的属性将其写入数据库。

在将坐标转换为 OCS 或从 OCS 转换坐标时,必须考虑 OCS 的法线。

DCS系统

显示坐标系:对象在显示之前变换的坐标系。DCS 的原点是存储在 AutoCAD 系统变量 TARGET 中的点,其 Z 轴是查看方向。换言之,视口始终是其DCS的平面视图。这些坐标可用于确定向用户显示某些内容的位置。

PSDCS公司

图纸空间 DCS:此坐标系只能转换为模型空间视口的 DCS 或从模型空间视口转换。这实质上是一种 2D 转换,其中 XY 坐标始终是缩放的。因此,它可用于查找两个坐标系之间的比例因子。PSDCS 只能转换为模型空间视口。

将 OCS 坐标转换为 WCS 坐标

本示例在模型空间中创建折线。然后,折线的第一个折点将同时显示在 OCS 和 WCS 坐标中。

VB.NET

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

    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
        '' Open the Block table for read
        Dim acBlkTbl As BlockTable
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
                                     OpenMode.ForRead)

        '' Open the Block table record Model space for write
        Dim acBlkTblRec As BlockTableRecord
        acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                        OpenMode.ForWrite)

        '' Create a 2D polyline with two segments (3 points)
        Dim acPoly2d As Polyline2d = New Polyline2d()

        '' Add the new object to the block table record and the transaction
        acBlkTblRec.AppendEntity(acPoly2d)
        acTrans.AddNewlyCreatedDBObject(acPoly2d, True)

        '' Before adding vertexes, the polyline must be in the drawing
        Dim acPts2dPoly As Point3dCollection = New Point3dCollection()
        acPts2dPoly.Add(New Point3d(1, 1, 0))
        acPts2dPoly.Add(New Point3d(1, 2, 0))
        acPts2dPoly.Add(New Point3d(2, 2, 0))
        acPts2dPoly.Add(New Point3d(3, 2, 0))
        acPts2dPoly.Add(New Point3d(4, 4, 0))

        For Each acPt3d As Point3d In acPts2dPoly
            Dim acVer2d As Vertex2d = New Vertex2d(acPt3d, 0, 0, 0, 0)
            acPoly2d.AppendVertex(acVer2d)
            acTrans.AddNewlyCreatedDBObject(acVer2d, True)
        Next

        '' Set the normal of the 2D polyline
        acPoly2d.Normal = New Vector3d(0, 1, 2)

        '' Get the first coordinate of the 2D polyline
        Dim acPts3d As Point3dCollection = New Point3dCollection()
        Dim acFirstVer As Vertex2d = Nothing
        For Each acObjIdVert As ObjectId In acPoly2d
            acFirstVer = acTrans.GetObject(acObjIdVert, _
                                           OpenMode.ForRead)

            acPts3d.Add(acFirstVer.Position)

            Exit For
        Next

        '' Get the first point of the polyline and 
        '' use the eleveation for the Z value
        Dim pFirstVer As Point3d = New Point3d(acFirstVer.Position.X, _
                                               acFirstVer.Position.Y, _
                                               acPoly2d.Elevation)

        '' Translate the OCS to WCS
        Dim mWPlane As Matrix3d = Matrix3d.WorldToPlane(acPoly2d.Normal)
        Dim pWCSPt As Point3d = pFirstVer.TransformBy(mWPlane)

        Application.ShowAlertDialog("The first vertex has the following " & _
                                    "coordinates:" & _
                                    vbLf & "OCS: " + pFirstVer.ToString() & _
                                    vbLf & "WCS: " + pWCSPt.ToString())

        '' Save the new objects to the database
        acTrans.Commit()
    End Using
End Sub

C#

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

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table record for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                     OpenMode.ForRead) as BlockTable;

        // Open the Block table record Model space for write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                        OpenMode.ForWrite) as BlockTableRecord;

        // Create a 2D polyline with two segments (3 points)
        using (Polyline2d acPoly2d = new Polyline2d())
        {
            // Add the new object to the block table record and the transaction
            acBlkTblRec.AppendEntity(acPoly2d);
            acTrans.AddNewlyCreatedDBObject(acPoly2d, true);

            // Before adding vertexes, the polyline must be in the drawing
            Point3dCollection acPts2dPoly = new Point3dCollection();
            acPts2dPoly.Add(new Point3d(1, 1, 0));
            acPts2dPoly.Add(new Point3d(1, 2, 0));
            acPts2dPoly.Add(new Point3d(2, 2, 0));
            acPts2dPoly.Add(new Point3d(3, 2, 0));
            acPts2dPoly.Add(new Point3d(4, 4, 0));

            foreach (Point3d acPt3d in acPts2dPoly)
            {
                Vertex2d acVer2d = new Vertex2d(acPt3d, 0, 0, 0, 0);
                acPoly2d.AppendVertex(acVer2d);
                acTrans.AddNewlyCreatedDBObject(acVer2d, true);
            }

            // Set the normal of the 2D polyline
            acPoly2d.Normal = new Vector3d(0, 1, 2);

            // Get the first coordinate of the 2D polyline
            Point3dCollection acPts3d = new Point3dCollection();
            Vertex2d acFirstVer = null;

            foreach (ObjectId acObjIdVert in acPoly2d)
            {
                acFirstVer = acTrans.GetObject(acObjIdVert,
                                               OpenMode.ForRead) as Vertex2d;

                acPts3d.Add(acFirstVer.Position);

                break;
            }

            // Get the first point of the polyline and 
            // use the eleveation for the Z value
            Point3d pFirstVer = new Point3d(acFirstVer.Position.X,
                                            acFirstVer.Position.Y,
                                            acPoly2d.Elevation);

            // Translate the OCS to WCS
            Matrix3d mWPlane = Matrix3d.WorldToPlane(acPoly2d.Normal);
            Point3d pWCSPt = pFirstVer.TransformBy(mWPlane);

            Application.ShowAlertDialog("The first vertex has the following " +
                                        "coordinates:" +
                                        "\nOCS: " + pFirstVer.ToString() +
                                        "\nWCS: " + pWCSPt.ToString());
        }

        // Save the new objects to the database
        acTrans.Commit();
    }
}

VBA/ActiveX 代码参考

Sub TranslateCoordinates()
    ' Create a polyline in model space.
    Dim plineObj As AcadPolyline
    Dim points(0 To 14) As Double
 
    ' Define the 2D polyline points
    points(0) = 1: points(1) = 1: points(2) = 0
    points(3) = 1: points(4) = 2: points(5) = 0
    points(6) = 2: points(7) = 2: points(8) = 0
    points(9) = 3: points(10) = 2: points(11) = 0
    points(12) = 4: points(13) = 4: points(14) = 0
 
    ' Create a light weight Polyline object in model space
    Set plineObj = ThisDrawing.ModelSpace.AddPolyline(points)
 
    ' Find the X and Y coordinates of the
    ' first vertex of the polyline
    Dim firstVertex As Variant
    firstVertex = plineObj.Coordinate(0)
 
    ' Find the Z coordinate for the polyline
    ' using the elevation property
    firstVertex(2) = plineObj.Elevation
 
    ' Change the normal for the pline so that the
    ' difference between the coordinate systems
    ' is obvious.
    Dim plineNormal(0 To 2) As Double
    plineNormal(0) = 0#
    plineNormal(1) = 1#
    plineNormal(2) = 2#
    plineObj.Normal = plineNormal
 
    ' Translate the OCS coordinate into WCS
    Dim coordinateWCS As Variant
    coordinateWCS = ThisDrawing.Utility.TranslateCoordinates _
                        (firstVertex, acOCS, acWorld, False, plineNormal)
 
    ' Display the coordinates of the point
    MsgBox "The first vertex has the following coordinates:" _
           & vbCrLf & "OCS: (" & firstVertex(0) & "," & _
           firstVertex(1) & "," & firstVertex(2) & ")" & vbCrLf & _
           "WCS: (" & coordinateWCS(0) & "," & _
           coordinateWCS(1) & "," & coordinateWCS(2) & ")"
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2025-1-8 19:09

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部