You can rotate all drawing objects and attribute reference objects.
用户可以旋转所有的图形对象和属性参考对象。
To rotate an object, use the Rotation function of a transformation matrix. This function requires a rotation angle represented in radians, an axis of rotation, and a base point. The axis of rotation must be expressed as a Vector3d object and the base point as a Point3d object. This angle determines how far an object rotates around the base point relative to its current location.
要旋转对象,请使用一个转换矩阵的 Rotation 函数。这个函数需要一个用弧度表示的旋转角度、一个旋转轴和一个基点。旋转轴必须以 Vector3d 对象的形式表示而基点要作为 Point3d 对象提供。此角度确定对象绕基点、相对于当前位置旋转的距离。
For more information about rotating objects, see “Rotate Objects” in the User's Guide.
更多关于旋转对象的详细信息,请参见《AutoCAD 用户手册》中的“旋转对象”部分。
This example creates a closed lightweight polyline, and then rotates the polyline 45 degrees about the base point (4, 4.25, 0).
本例创建一个封闭的轻量多段线,然后放多段线绕点(4,4.25,0)旋转45度。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("RotateObject")> _
Public Sub RotateObject()
'' 获得当前文档和数据库 Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
''启动一个事务 Start a transaction
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 lightweight polyline
Dim acPoly As Polyline = New Polyline()
acPoly.SetDatabaseDefaults()
acPoly.AddVertexAt(0, New Point2d(1, 2), 0, 0, 0)
acPoly.AddVertexAt(1, New Point2d(1, 3), 0, 0, 0)
acPoly.AddVertexAt(2, New Point2d(2, 3), 0, 0, 0)
acPoly.AddVertexAt(3, New Point2d(3, 3), 0, 0, 0)
acPoly.AddVertexAt(4, New Point2d(4, 4), 0, 0, 0)
acPoly.AddVertexAt(5, New Point2d(4, 2), 0, 0, 0)
'' 闭合多段线 Close the polyline
acPoly.Closed = True
Dim curUCSMatrix As Matrix3d = acDoc.Editor.CurrentUserCoordinateSystem
Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d
'' 利用基点(4,4.25,0)绕当前 UCS 的Z轴旋转多段线45度 Rotate the polyline 45 degrees, around the Z-axis of the current UCS
'' using a base point of (4,4.25,0)
acPoly.TransformBy(Matrix3d.Rotation(0.7854, _
curUCS.Zaxis, _
New Point3d(4, 4.25, 0)))
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly)
acTrans.AddNewlyCreatedDBObject(acPoly, True)
'' 保存新对象到数据库中 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("RotateObject")]
public static void RotateObject()
{
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只读方式打开块表 Open the Block table 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 lightweight polyline
Polyline acPoly = new Polyline();
acPoly.SetDatabaseDefaults();
acPoly.AddVertexAt(0, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 3), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 3), 0, 0, 0);
acPoly.AddVertexAt(3, new Point2d(3, 3), 0, 0, 0);
acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
acPoly.AddVertexAt(5, new Point2d(4, 2), 0, 0, 0);
// 闭合多段线 Close the polyline
acPoly.Closed = true;
Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
// 利用基点(4,4.25,0)绕当前 UCS 的Z轴旋转多段线45度 Rotate the polyline 45 degrees, around the Z-axis of the current UCS
// using a base point of (4,4.25,0)
acPoly.TransformBy(Matrix3d.Rotation(0.7854,
curUCS.Zaxis,
new Point3d(4, 4.25, 0)));
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
// 保存新对象到数据库中 Save the new objects to the database
acTrans.Commit();
}
}