原点(0, 0, 0)の位置、XY 平面と Z 軸の方向を変更するには、ユーザ座標系(UCS)オブジェクトを定義します。3D 空間の任意の場所および方向に UCS を配置することができます。必要な数のユーザ座標系の定義、保存、呼び出しが可能です。座標の指定および表示は、現在の UCS を基準とした相対的な値で行います。
UCS の原点と方向を示すために、Viewport オブジェクトの IconAtOrigin プロパティまたはシステム変数 UCSICON を使用して、UCS 原点に UCS アイコンを表示できます。UCS アイコンが有効になっていて(IconVisible プロパティ)、原点に表示されない場合は、システム変数 UCSORG により定義された WCS 座標に表示されます。
UCSTable オブジェクトの Add メソッドを使用して新しいユーザ座標系を作成することができます。このメソッドでは、原点の座標、X 軸および Y 軸上の座標、UCS の名前の 4 つの値を入力する必要があります。
AutoCAD ® ActiveX オートメーションの座標は、すべてワールド座標系(WCS)で入力されます。GetUCSMatrix メソッドを使用して、特定の UCS の変換マトリックスを返します。この UCS に相当する WCS を検索するには、この変換マトリックスを使用します。
UCS をアクティブにするには、Document オブジェクトで ActiveUCS プロパティを使用します。アクティブな UCS に変更を加えた場合、変更結果を表示するには、その新しい UCS オブジェクトをアクティブな UCS に再設定してください。アクティブな UCS をリセットするには、更新した UCS オブジェクトの ActiveUCS プロパティをもう一度呼び出します。
以下のサブルーチンでは、新しい UCS を作成し、図面に対しアクティブな UCS として設定します。次にユーザに図面内の点をクリックするようプロンプトを表示し、その点の WCS および UCS の両座標を返します。
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
acUCSTbl.UpgradeOpen()
'' Add the new UCS table record
acUCSTbl.Add(acUCSTblRec)
acTrans.AddNewlyCreatedDBObject(acUCSTblRec, True)
acUCSTblRec.Dispose()
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
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);
acUCSTblRec.Dispose();
}
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();
}
}
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