Entering 3D world coordinate system (WCS) coordinates is similar to entering 2D WCS coordinates. In addition to specifying X and Y values, you specify a Z value. 2D coordinates are represented by a Point2d object, while you use a Point3d object to represent 3D coordinates. Most properties and methods in the .NET API utilize 3D coordinates.
输入三维世界坐标系 (WCS) 坐标与输入二维 WCS 坐标类似。只是除了指定 X 和 Y 值以外,还要指定 Z 值。二维坐标是通过 Points2d 对象表示的,而用 Point3d 对象表示三维坐标。在 .NET API 中的大多数属性和方法都利用三维坐标。
For more information about specifying 3D coordinates, see “Enter 3D Coordinates” in the AutoCAD User's Guide.
有关指定三维坐标的详细信息,请参见《AutoCAD 用户手册》中的“输入三维坐标”。
This example creates two polylines, each with three coordinates. The first polyline is a 2D polyline, the second polyline is 3D. Notice that the length of the array containing the vertices is expanded to include the Z coordinates in the creation of the 3D polyline. The example concludes by querying the coordinates of the polylines and displaying the coordinates in a message box.
本例创建两条多段线,每条都有三个坐标。第一条多段线是二维多段线,第二条多段线是三维的。请注意,在创建三维多段线时,包含顶点的数组的长度会拉伸以包含 Z 坐标。样例查询多段线的坐标,并将坐标显示在消息框中。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("Polyline_2D_3D")> _
Public Sub Polyline_2D_3D()
''获得当前文档和数据库,并启动一个事务 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 polyline with two segments (3 points)
Dim acPoly As Polyline = New Polyline()
acPoly.SetDatabaseDefaults()
acPoly.AddVertexAt(0, New Point2d(1, 1), 0, 0, 0)
acPoly.AddVertexAt(1, New Point2d(1, 2), 0, 0, 0)
acPoly.AddVertexAt(2, New Point2d(2, 2), 0, 0, 0)
acPoly.ColorIndex = 1
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly)
acTrans.AddNewlyCreatedDBObject(acPoly, True)
''创建一个有两个片段的三维多段线(3点) Create a 3D polyline with two segments (3 points)
Dim acPoly3d As Polyline3d = New Polyline3d()
acPoly3d.SetDatabaseDefaults()
acPoly3d.ColorIndex = 5
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly3d)
acTrans.AddNewlyCreatedDBObject(acPoly3d, True)
''添加顶点前,多段线必须先要添加到图形中 Before adding vertexes, the polyline must be in the drawing
Dim acPts3dPoly As Point3dCollection = New Point3dCollection()
acPts3dPoly.Add(New Point3d(1, 1, 0))
acPts3dPoly.Add(New Point3d(2, 1, 0))
acPts3dPoly.Add(New Point3d(2, 2, 0))
For Each acPt3d As Point3d In acPts3dPoly
Dim acPolVer3d As PolylineVertex3d = New PolylineVertex3d(acPt3d)
acPoly3d.AppendVertex(acPolVer3d)
acTrans.AddNewlyCreatedDBObject(acPolVer3d, True)
Next
''获得轻量多段线的坐标 Get the coordinates of the lightweight polyline
Dim acPts2d As Point2dCollection = New Point2dCollection()
For nCnt As Integer = 0 To acPoly.NumberOfVertices - 1
acPts2d.Add(acPoly.GetPoint2dAt(nCnt))
Next
''获得三维多段线的坐标 Get the coordinates of the 3D polyline
Dim acPts3d As Point3dCollection = New Point3dCollection()
For Each acObjIdVert As ObjectId In acPoly3d
Dim acPolVer3d As PolylineVertex3d
acPolVer3d = acTrans.GetObject(acObjIdVert, _
OpenMode.ForRead)
acPts3d.Add(acPolVer3d.Position)
Next
''显示坐标 Display the Coordinates
Application.ShowAlertDialog("2D polyline (red): " & vbLf & _
acPts2d(0).ToString() & vbLf & _
acPts2d(1).ToString() & vbLf & _
acPts2d(2).ToString())
Application.ShowAlertDialog("3D polyline (blue): " & vbLf & _
acPts3d(0).ToString() & vbLf & _
acPts3d(1).ToString() & vbLf & _
acPts3d(2).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("Polyline_2D_3D")]
public static void Polyline_2D_3D()
{
// 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 polyline with two segments (3 points)
Polyline acPoly = new Polyline();
acPoly.SetDatabaseDefaults();
acPoly.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
acPoly.ColorIndex = 1;
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
// Create a 3D polyline with two segments (3 points)
Polyline3d acPoly3d = new Polyline3d();
acPoly3d.SetDatabaseDefaults();
acPoly3d.ColorIndex = 5;
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly3d);
acTrans.AddNewlyCreatedDBObject(acPoly3d, true);
// 添加顶点前,多段线必须先要添加到图形中 Before adding vertexes, the polyline must be in the drawing
Point3dCollection acPts3dPoly = new Point3dCollection();
acPts3dPoly.Add(new Point3d(1, 1, 0));
acPts3dPoly.Add(new Point3d(2, 1, 0));
acPts3dPoly.Add(new Point3d(2, 2, 0));
foreach (Point3d acPt3d in acPts3dPoly)
{
PolylineVertex3d acPolVer3d = new PolylineVertex3d(acPt3d);
acPoly3d.AppendVertex(acPolVer3d);
acTrans.AddNewlyCreatedDBObject(acPolVer3d, true);
}
// Get the coordinates of the lightweight polyline
Point2dCollection acPts2d = new Point2dCollection();
for (int nCnt = 0; nCnt < acPoly.NumberOfVertices; nCnt++)
{
acPts2d.Add(acPoly.GetPoint2dAt(nCnt));
}
// Get the coordinates of the 3D polyline
Point3dCollection acPts3d = new Point3dCollection();
foreach (ObjectId acObjIdVert in acPoly3d)
{
PolylineVertex3d acPolVer3d;
acPolVer3d = acTrans.GetObject(acObjIdVert,
OpenMode.ForRead) as PolylineVertex3d;
acPts3d.Add(acPolVer3d.Position);
}
// Display the Coordinates
Application.ShowAlertDialog("2D polyline (red): \n" +
acPts2d[0].ToString() + "\n" +
acPts2d[1].ToString() + "\n" +
acPts2d[2].ToString());
Application.ShowAlertDialog("3D polyline (blue): \n" +
acPts3d[0].ToString() + "\n" +
acPts3d[1].ToString() + "\n" +
acPts3d[2].ToString());
// 保存新对象到数据库中 Save the new object to the database
acTrans.Commit();
}
}