The TransformBy method can translate a point or a displacement from one coordinate system to another. You use the AlignCoordinateSystem method to specify which coordinate system you are translating from and which coordinate system you are going to. The AlignCoordinateSystem method requires the following:
TransformBy 方法可以将点或位移从一个坐标系转换到另一个坐标系。可以使用 AlignCoordinateSystem 指定你想从哪一个坐标系转换到别外的一个坐标系。AlignCoordinateSystem 需要下列参数:
World coordinate system: The reference coordinate system. All other coordinate systems are defined relative to the WCS, which never changes. Values measured relative to the WCS are stable across changes to other coordinate systems. All points passed in and out of the methods and properties in the .NET API are expressed in the WCS unless otherwise specified.
世界坐标系:参考坐标系。所有其他坐标系都相对于 WCS 定义,该坐标系恒定不变。相对于 WCS 测量的值可以稳定地转换到其他坐标系中。除非另行指定,.NET API 方法和属性传入和传出的所有点都以 WCS 表示。
User coordinate system (UCS): The working coordinate system. The user specifies a UCS to make drawing tasks easier. All points passed to AutoCAD commands, including those returned from AutoLISP routines and external functions, are points in the current UCS (unless the user precedes them with an * at the Command prompt). If you want your application to send coordinates in the WCS, OCS, or DCS to AutoCAD commands, you must first convert them to the UCS by calling the translating them and then transforming the Point3d or Point 2d object with the TransformBy method that represents the coordinate value.
用户坐标系 (UCS):正在使用的坐标系。用户可以指定 UCS 以方便执行绘图任务。所有传递到 AutoCAD 命令的点,包括从 AutoLISP 例程以及外部函数返回的点,都是当前 UCS 中的点(除非用户于命令提示中在点之前加上了 *)。如果希望应用程序以 WCS、OCS 或 DCS 向 AutoCAD 命令发送坐标,必须先通过调用代表坐标值的 Point3d 或 Point2d 对象的 TransformBy 方法将这些坐标转换为 UCS。
Object coordinate system (also known as Entity coordinate system or ECS): Point values specified by certain methods and properties for the Polyline2d and Polyline objects are expressed in this coordinate system, relative to the object. These points are usually converted into the WCS, current UCS, or current DCS, according to the intended use of the object. Conversely, points in WCS, UCS, or DCS must be translated into an OCS before they are written to the database by means of the same properties. See the AutoCAD .NET Managed Class Guide for the methods and properties that use this coordinate system.
When converting coordinates to or from the OCS you must consider the normal of the OCS.
对象坐标系(也叫作实体坐标或 ECS):由 Polyline2d 和 Polyline 对象的特定方法和属性指定的点值以此坐标系表示,它相对于对象。根据对象的用途,这些点通常会转换为 WCS、当前 UCS 或当前 DCS。相对地,WCS、UCS 或 DCS 中的点在使用相同的属性写入数据库之前,必须将其转换为 OCS。关于使用此坐标系的方法和属性,请参见 《AutoCAD .NET 托管类手册》。
将坐标转换到 OCS 或从 OCS 转换坐标时,必须在考虑 OCS 的法线。
Display coordinate system: The coordinate system where objects are transformed before they are displayed. The origin of the DCS is the point stored in the AutoCAD system variable TARGET, and its Z axis is the viewing direction. In other words, a viewport is always a plan view of its DCS. These coordinates can be used to determine where something will be displayed to the user.
显示坐标系:即对象在显示前要进行转换的坐标系。DCS 的原点是存储在 AutoCAD 系统变量 TARGET 中的点,并且其 Z 轴为观察方向。也就是说,某个视口始终是其 DCS 的一个平面视图。这些坐标可以用来确定对用户显示画面的位置。
Paper space DCS: This coordinate system can be transformed only to or from the DCS of a Model space viewport. This is essentially a 2D transformation, where the X and Y coordinates are always scaled. Therefore, it can be used to find the scale factor between the two coordinate systems. The PSDCS can be transformed only into a Model space viewport.
图纸空间 DCS:此坐标系只能与当前活动模型空间视口的 DCS 相互转换。这种转换实质上是一种二维转换,其中的 X 和 Y 坐标始终要进行缩放。因此,它可用于确定两个坐标系之间的比例因子。PSDCS 只能转换到当前的模型空间视口中。
This example creates a polyline in Model space. The first vertex for the polyline is then displayed in both the OCS and WCS coordinates.
本样例在模型空间中创建一条多段线。然后,以 OCS 和 WCS 坐标显示多段线的第一个顶点。
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)
''创建有两个片段的二维多段线(3个点) Create a 2D polyline with two segments (3 points)
Dim acPoly2d As Polyline2d = New Polyline2d()
acPoly2d.SetDatabaseDefaults()
'' 添加新对象到块表记录和事务中 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
''使用 eleveation 获得多段线的第一个顶点 Z 值 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)
'' 转换 OCS 坐标到 WCS 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
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)
Polyline2d acPoly2d = new Polyline2d();
acPoly2d.SetDatabaseDefaults();
// 添加新对象到块表记录和事务中 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();
}
}