定义用户坐标系统
 
 

You define a user coordinate system ( UCS ) object to change the location of the (0, 0, 0) origin point and the orientation of the XY plane and Z axis. You can locate and orient a UCS anywhere in 3D space, and you can define, save, and recall as many user coordinate systems as you require. Coordinate input and display are relative to the current UCS.

定义用户坐标系 (UCS) 对象以更改 (0, 0, 0) 原点的位置以及 XY 平面和 Z轴的方向。可以在三维空间中的任意位置放置 UCS 并定义其方向,并且可以定义、保存和调用所需任意数目的用户坐标系。坐标的输入和显示均相对于当前 UCS。

To indicate the origin and orientation of the UCS, you can display the UCS icon at the UCS origin point using the IconAtOrigin property of a Viewport object or the UCSICON system variable. If the UCS icon is turned on (IconVisible property) and is not displayed at the origin, it is displayed at the WCS coordinate defined by the UCSORG system variable.

要标明 UCS 的原点和方向,请使用一个视口对象的 IconAtOrigin 属性或 UCSICON 系统变量,可以在 UCS 原点处显示 UCS 图标。如果已打开 UCS 图标(IconVisible 属性)但它没有显示在原点,则该图标将显示在由 UCSORG 系统变量定义的 WCS 坐标处。

You can create a new user coordinate system using the Add method of the UCSTable object. This method requires four values as input: the coordinate of the origin, a coordinate on the X and Y axes, and the name of the UCS.

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

All coordinates in the AutoCAD® ActiveX Automation are entered in the world coordinate system. Use the GetUCSMatrix method to return the transformation matrix of a given UCS. Use this transformation matrix to find the equivalent WCS coordinates.

AutoCAD® ActiveX Automation 中的所有坐标都是在世界坐标系中输入的。使用 GetUCSMatrix 方法可以返回给定 UCS 的转换矩阵。由此转换矩阵可以找出等价的 WCS 坐标。

To make a UCS active, use the ActiveUCS property on the Document object. If changes are made to the active UCS, the new UCS object must be reset as the active UCS for the changes to appear. To reset the active UCS, simply call the ActiveUCS property again with the updated UCS object.

要激活 UCS 坐标系,请使用 Document 对象的 ActiveUCS 属性。如果对活动 UCS 进行更改,则必须将新的 UCS 对象重置为活动的 UCS 才能显示所做的更改。要重置活动的 UCS,只需再次调用更新的 UCS 对象的 ActiveUCS 属性即可。

For more information about defining a UCS, see “Control the User Coordinate System in 3D” in the User's Guide.

有关定义 UCS 的详细信息,请参见《用户手册》中的“控制三维中的用户坐标系”。

创建新的 UCS,将其激活,并将一个点的坐标转换为 UCS 坐标

The following subroutine creates a new UCS and sets it as the active UCS for the drawing. It then asks the user to pick a point in the drawing, and returns both WCS and UCS coordinates for the point.

下面的子例程将创建新的 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()
      '' 以读的方式打开 UCS 表   Open the UCS table for read
      Dim acUCSTbl As UcsTable
      acUCSTbl = acTrans.GetObject(acCurDb.UcsTableId, _
                                   OpenMode.ForRead)
 
      Dim acUCSTblRec As UcsTableRecord
 
      ''检查看“New_UCS”用户坐标系表记录是否存在  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"
 
          ''以写的方式打开 UCSTable    Open the UCSTable for write
          acUCSTbl.UpgradeOpen()
 
          ''添加新的 UCS 表记录   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)
 
      ''在当前视口的原点位置显示 UCS 的图标   Display the UCS Icon at the origin of the current viewport
      acVportTblRec.IconAtOrigin = True
      acVportTblRec.IconEnabled = True
 
      ''设置 UCS 为当前坐标系  Set the UCS current
      acVportTblRec.SetUcs(acUCSTblRec.ObjectId)
      acDoc.Editor.UpdateTiledViewportsFromDatabase()
 
      ''显示当前 UCS 的名字   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
 
      ''如果输入了一个,然后转换它到当前 UCS  If a point was entered, then translate it to the current UCS
      If pPtRes.Status = PromptStatus.OK Then
          pPt3dWCS = pPtRes.Value
          pPt3dUCS = pPtRes.Value
 
          ''将点从当前 UCS 转换到 WCS   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
          acUCSTbl.UpgradeOpen();
 
          // 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 代码参考