在三维空间旋转
 
 

With the TransformBy method of an object and the Rotation method of a Matrix, you can rotate objects in 2D about a specified point. The direction of rotation for 2D objects is around the Z axis. For 3D objects, the axis of rotation is not limited to the Z axis. When using the Rotation method instead of using the Z axis for the rotation axis, you specify a specific 3D vector.

使用对象的 TransformBy 方法和 Matrix 的 Rotation 可以在二维空间中绕指定点旋转对象。对于二维对象,旋转的方向是围绕 Z 轴旋转。对于三维对象,旋转轴就不仅仅局限于 Z 轴。当使用 Rotation 方法而利用 Z 轴作为旋转轴时,你必须为这个方法提供一个三维矢量(Vector3d 对象)。

For more information on rotating in 3D, see “Rotate Objects” in the AutoCAD User's Guide.

有关在三维中进行旋转的详细信息,请参见《AutoCAD 用户手册》中的“旋转对象”部分。

创建三维长方体,并将其绕轴旋转

This example creates a 3D box. It then defines the axis for rotation and finally rotates the box 30 degrees about the axis.

本样例创建一个三维长方体,然后定义旋转轴,最后将长方体绕该轴旋转 30 度。 

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("Rotate_3DBox")> _
Public Sub Rotate_3DBox()
  ''获得当前文档和数据库,并启动一个事务   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 3D solid box
      Dim acSol3D As Solid3d = New Solid3d()
      acSol3D.SetDatabaseDefaults()
      acSol3D.CreateBox(5, 7, 10)
 
      ''定位三维实体的中心点在(5,5,0) Position the center of the 3D solid at (5,5,0) 
      acSol3D.TransformBy(Matrix3d.Displacement(New Point3d(5, 5, 0) - _
                                                Point3d.Origin))
 
      Dim curUCSMatrix As Matrix3d = acDoc.Editor.CurrentUserCoordinateSystem
      Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d
 
      '' Rotate the 3D solid 30 degrees around the axis that is
      '' defined by the points (-3,4,0) and (-3,-4,0)
      Dim vRot As Vector3d = New Point3d(-3, 4, 0). _
                                         GetVectorTo(New Point3d(-3, -4, 0))
 
      acSol3D.TransformBy(Matrix3d.Rotation(0.5236, _
                                            vRot, _
                                            New Point3d(-3, 4, 0)))
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acSol3D)
      acTrans.AddNewlyCreatedDBObject(acSol3D, True)
 
      '' 保存新对象到数据库中   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("Rotate_3DBox")]
public static void Rotate_3DBox()
{
  // 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 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 3D solid box
      Solid3d acSol3D = new Solid3d();
      acSol3D.SetDatabaseDefaults();
      acSol3D.CreateBox(5, 7, 10);
 
      // Position the center of the 3D solid at (5,5,0)
      acSol3D.TransformBy(Matrix3d.Displacement(new Point3d(5, 5, 0) -
                                                Point3d.Origin));
 
      Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
      CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
 
      // Rotate the 3D solid 30 degrees around the axis that is
      // defined by the points (-3,4,0) and (-3,-4,0)
      Vector3d vRot = new Point3d(-3, 4, 0).
                                  GetVectorTo(new Point3d(-3, -4, 0));
 
      acSol3D.TransformBy(Matrix3d.Rotation(0.5236,
                                            vRot,
                                            new Point3d(-3, 4, 0)));
 
      // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acSol3D);
      acTrans.AddNewlyCreatedDBObject(acSol3D, true);
 
      // 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考