CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2018 开发者帮助

定义用户坐标系 (.NET)

2024-5-18 16:57| 发布者: admin| 查看: 73| 评论: 0|原作者: admin|来自: AutoCAD

定义用户坐标系 (.NET)

定义用户坐标系 (UCS) 对象以更改 (0, 0, 0) 原点的位置以及 XY 平面和 Z 轴的方向。您可以在 3D 空间中的任意位置定位和定位 UCS,并且可以根据需要定义、保存和调用任意数量的用户坐标系。坐标输入和显示相对于当前UCS。

要指示UCS的原点和方向,可以使用Viewport对象的属性或UCSICON系统变量在UCS原点显示UCS图标。如果 UCS 图标已打开(属性)且未显示在原点,则该图标将显示在 UCSORG 系统变量定义的 WCS 坐标处。IconAtOriginIconVisible

您可以使用对象的方法创建新的用户坐标系。此方法需要四个值作为输入:原点的坐标、X 轴和 Y 轴上的坐标以及 UCS 的名称。AddUCSTable

AutoCAD ActiveX Automation 中的所有坐标都输入到世界坐标系中。使用该方法返回给定 UCS 的转换矩阵。使用此变换矩阵查找等效的 WCS 坐标。 ® GetUCSMatrix

若要使 UCS 处于活动状态,请使用对象上的属性。如果对活动 UCS 进行了更改,则必须将新 UCS 对象重置为活动 UCS,才能显示更改。若要重置活动的 UCS,只需使用更新的 UCS 对象再次调用该属性即可。ActiveUCSDocumentActiveUCS

创建新的 UCS,使其处于活动状态,然后将点的坐标转换为 UCS 坐标

以下子例程创建一个新的 UCS,并将其设置为图形的活动 UCS。然后,它要求用户在绘图中选取一个点,并返回该点的 WCS 和 UCS 坐标。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("NewUCS")> _
Public Sub NewUCS()
    '' 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 UCS table for read
        Dim acUCSTbl As UcsTable
        acUCSTbl = acTrans.GetObject(acCurDb.UcsTableId, _
                                     OpenMode.ForRead)

        Dim acUCSTblRec As UcsTableRecord

        '' Check to see if the "New_UCS" UCS table record exists
        If acUCSTbl.Has("New_UCS") = False Then
            acUCSTblRec = New UcsTableRecord()
            acUCSTblRec.Name = "New_UCS"

            '' Open the UCSTable for write
            acTrans.GetObject(acCurDb.UcsTableId, OpenMode.ForWrite)

            '' Add the new UCS table record
            acUCSTbl.Add(acUCSTblRec)
            acTrans.AddNewlyCreatedDBObject(acUCSTblRec, True)
        Else
            acUCSTblRec = acTrans.GetObject(acUCSTbl("New_UCS"), _
                                            OpenMode.ForWrite)
        End If

        acUCSTblRec.Origin = New Point3d(4, 5, 3)
        acUCSTblRec.XAxis = New Vector3d(1, 0, 0)
        acUCSTblRec.YAxis = New Vector3d(0, 1, 0)

        '' Open the active viewport
        Dim acVportTblRec As ViewportTableRecord
        acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, _
                                          OpenMode.ForWrite)

        '' Display the UCS Icon at the origin of the current viewport
        acVportTblRec.IconAtOrigin = True
        acVportTblRec.IconEnabled = True

        '' Set the UCS current
        acVportTblRec.SetUcs(acUCSTblRec.ObjectId)
        acDoc.Editor.UpdateTiledViewportsFromDatabase()

        '' Display the name of the current UCS
        Dim acUCSTblRecActive As UcsTableRecord
        acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName, _
                                              OpenMode.ForRead)

        Application.ShowAlertDialog("The current UCS is: " & _
                                    acUCSTblRecActive.Name)

        Dim pPtRes As PromptPointResult
        Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")

        '' Prompt for a point
        pPtOpts.Message = vbLf & "Enter a point: "
        pPtRes = acDoc.Editor.GetPoint(pPtOpts)

        Dim pPt3dWCS As Point3d
        Dim pPt3dUCS As Point3d

        '' If a point was entered, then translate it to the current UCS
        If pPtRes.Status = PromptStatus.OK Then
            pPt3dWCS = pPtRes.Value
            pPt3dUCS = pPtRes.Value

            '' Translate the point from the current UCS to the WCS
            Dim newMatrix As Matrix3d = New Matrix3d()
            newMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin, _
                                                       Vector3d.XAxis, _
                                                       Vector3d.YAxis, _
                                                       Vector3d.ZAxis, _
                                                       acVportTblRec.Ucs.Origin, _
                                                       acVportTblRec.Ucs.Xaxis, _
                                                       acVportTblRec.Ucs.Yaxis, _
                                                       acVportTblRec.Ucs.Zaxis)

            pPt3dWCS = pPt3dWCS.TransformBy(newMatrix)

            Application.ShowAlertDialog("The WCS coordinates are: " & vbLf & _
                                        pPt3dWCS.ToString() & vbLf & _
                                        "The UCS coordinates are: " & vbLf & _
                                        pPt3dUCS.ToString())
        End If

        '' 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.EditorInput;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("NewUCS")]
public static void NewUCS()
{
    // 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 UCS table for read
        UcsTable acUCSTbl;
        acUCSTbl = acTrans.GetObject(acCurDb.UcsTableId,
                                        OpenMode.ForRead) as UcsTable;

        UcsTableRecord acUCSTblRec;

        // Check to see if the "New_UCS" UCS table record exists
        if (acUCSTbl.Has("New_UCS") == false)
        {
            acUCSTblRec = new UcsTableRecord();
            acUCSTblRec.Name = "New_UCS";

            // Open the UCSTable for write
            acTrans.GetObject(acCurDb.UcsTableId, OpenMode.ForWrite);

            // Add the new UCS table record
            acUCSTbl.Add(acUCSTblRec);
            acTrans.AddNewlyCreatedDBObject(acUCSTblRec, true);
        }
        else
        {
            acUCSTblRec = acTrans.GetObject(acUCSTbl["New_UCS"],
                                            OpenMode.ForWrite) as UcsTableRecord;
        }

        acUCSTblRec.Origin = new Point3d(4, 5, 3);
        acUCSTblRec.XAxis = new Vector3d(1, 0, 0);
        acUCSTblRec.YAxis = new Vector3d(0, 1, 0);

        // Open the active viewport
        ViewportTableRecord acVportTblRec;
        acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                                            OpenMode.ForWrite) as ViewportTableRecord;

        // Display the UCS Icon at the origin of the current viewport
        acVportTblRec.IconAtOrigin = true;
        acVportTblRec.IconEnabled = true;

        // Set the UCS current
        acVportTblRec.SetUcs(acUCSTblRec.ObjectId);
        acDoc.Editor.UpdateTiledViewportsFromDatabase();

        // Display the name of the current UCS
        UcsTableRecord acUCSTblRecActive;
        acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName,
                                                OpenMode.ForRead) as UcsTableRecord;

        Application.ShowAlertDialog("The current UCS is: " +
                                    acUCSTblRecActive.Name);

        PromptPointResult pPtRes;
        PromptPointOptions pPtOpts = new PromptPointOptions("");

        // Prompt for a point
        pPtOpts.Message = "\nEnter a point: ";
        pPtRes = acDoc.Editor.GetPoint(pPtOpts);

        Point3d pPt3dWCS;
        Point3d pPt3dUCS;

        // If a point was entered, then translate it to the current UCS
        if (pPtRes.Status == PromptStatus.OK)
        {
            pPt3dWCS = pPtRes.Value;
            pPt3dUCS = pPtRes.Value;

            // Translate the point from the current UCS to the WCS
            Matrix3d newMatrix = new Matrix3d();
            newMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin,
                                                        Vector3d.XAxis,
                                                        Vector3d.YAxis,
                                                        Vector3d.ZAxis,
                                                        acVportTblRec.Ucs.Origin,
                                                        acVportTblRec.Ucs.Xaxis,
                                                        acVportTblRec.Ucs.Yaxis,
                                                        acVportTblRec.Ucs.Zaxis);

            pPt3dWCS = pPt3dWCS.TransformBy(newMatrix);

            Application.ShowAlertDialog("The WCS coordinates are: \n" +
                                        pPt3dWCS.ToString() + "\n" +
                                        "The UCS coordinates are: \n" +
                                        pPt3dUCS.ToString());
        }

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

VBA/ActiveX 代码参考

Sub NewUCS()
    ' Define the variables we will need
    Dim ucsObj As AcadUCS
    Dim origin(0 To 2) As Double
    Dim xAxisPnt(0 To 2) As Double
    Dim yAxisPnt(0 To 2) As Double
 
    ' Define the UCS points
    origin(0) = 4: origin(1) = 5: origin(2) = 3
    xAxisPnt(0) = 5: xAxisPnt(1) = 5: xAxisPnt(2) = 3
    yAxisPnt(0) = 4: yAxisPnt(1) = 6: yAxisPnt(2) = 3
 
    ' Add the UCS to the
    ' UserCoordinatesSystems collection
    Set ucsObj = ThisDrawing.UserCoordinateSystems. _
                     Add(origin, xAxisPnt, yAxisPnt, "New_UCS")
 
    ' Display the UCS icon
    ThisDrawing.ActiveViewport.UCSIconAtOrigin = True
    ThisDrawing.ActiveViewport.UCSIconOn = True
 
    ' Make the new UCS the active UCS
    ThisDrawing.ActiveUCS = ucsObj
    MsgBox "The current UCS is : " & ThisDrawing.ActiveUCS.Name _
           & vbCrLf & " Pick a point in the drawing."
 
    ' Find the WCS and UCS coordinate of a point
    Dim WCSPnt As Variant
    Dim UCSPnt As Variant
 
    WCSPnt = ThisDrawing.Utility.GetPoint(, "Enter a point: ")
    UCSPnt = ThisDrawing.Utility.TranslateCoordinates _
                 (WCSPnt, acWorld, acUCS, False)
 
    MsgBox "The WCS coordinates are: " & WCSPnt(0) & ", " _
           & WCSPnt(1) & ", " & WCSPnt(2) & vbCrLf & _
           "The UCS coordinates are: " & UCSPnt(0) & ", " _
           & UCSPnt(1) & ", " & UCSPnt(2)
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-12-15 11:57

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部