CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

指定 3D 坐标 (.NET)

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

输入 3D 世界坐标系 (WCS) 坐标与输入 2D WCS 坐标类似。除了指定XY值外,还可以指定Z值。2D 坐标由对象表示,而使用对象表示 3D 坐标。AutoCAD .NET API 中的大多数属性和方法都使用 3D 坐标。Point2dPoint3d

定义和查询 2D 和 3D 折线的坐标

本示例创建两条折线,每条折线有三个坐标。第一条折线是 2D 多段线,第二条折线是 3D 折线。请注意,在创建 3D 折线时,包含顶点的数组的长度将扩展为包括Z坐标。该示例最后查询折线的坐标并在消息框中显示坐标。

VB.NET

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)
        Using acPoly As Polyline = New Polyline()
            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)
            Using acPoly3d As Polyline3d = New Polyline3d()
                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
                    Using acPolVer3d As PolylineVertex3d = New PolylineVertex3d(acPt3d)
                        acPoly3d.AppendVertex(acPolVer3d)
                        acTrans.AddNewlyCreatedDBObject(acPolVer3d, True)
                    End Using
                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())
            End Using
        End Using

        '' 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("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)
        using (Polyline acPoly = new Polyline())
        {
            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)
            using (Polyline3d acPoly3d = new Polyline3d())
            {
                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)
                {
                    using (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();
    }
}

VBA/ActiveX Code Reference

Sub Polyline_2D_3D()
    Dim pline2DObj As AcadLWPolyline
    Dim pline3DObj As AcadPolyline
 
    Dim points2D(0 To 5) As Double
    Dim points3D(0 To 8) As Double
 
    ' Define three 2D polyline points
    points2D(0) = 1: points2D(1) = 1
    points2D(2) = 1: points2D(3) = 2
    points2D(4) = 2: points2D(5) = 2
 
    ' Define three 3D polyline points
    points3D(0) = 1: points3D(1) = 1: points3D(2) = 0
    points3D(3) = 2: points3D(4) = 1: points3D(5) = 0
    points3D(6) = 2: points3D(7) = 2: points3D(8) = 0
 
    ' Create the 2D light weight Polyline
    Set pline2DObj = ThisDrawing.ModelSpace. _
                                      AddLightWeightPolyline(points2D)
    pline2DObj.Color = acRed
    pline2DObj.Update
 
    ' Create the 3D polyline
    Set pline3DObj = ThisDrawing.ModelSpace. _
                                      AddPolyline(points3D)
    pline3DObj.Color = acBlue
    pline3DObj.Update
 
    ' Query the coordinates of the polylines
    Dim get2Dpts As Variant
    Dim get3Dpts As Variant
 
    get2Dpts = pline2DObj.Coordinates
    get3Dpts = pline3DObj.Coordinates
 
    ' Display the coordinates
 
    MsgBox ("2D polyline (red): " & vbCrLf & "(" & _
            get2Dpts(0) & ", " & get2Dpts(1) & ")" & vbCrLf & "(" & _
            get2Dpts(2) & ", " & get2Dpts(3) & ")"& vbCrLf & "(" & _
            get2Dpts(4) & ", " & get2Dpts(5) & ")")
 
    MsgBox ("3D polyline (blue): " & vbCrLf & "(" & _
            get3Dpts(0) & ", " & get3Dpts(1) & ", " & _
            get3Dpts(2) & ")" & vbCrLf & "(" & _
            get3Dpts(3) & ", " & get3Dpts(4) & ", " & _
            get3Dpts(5) & ")" & vbCrLf & "(" & _
            get3Dpts(6) & ", " & get3Dpts(7) & ", " & _
            get3Dpts(8) & ")")
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 15:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部