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.
For more information on rotating in 3D, see “Rotate Objects” in the AutoCAD User's Guide.
Create a 3D box and rotate it about an axis
This example creates a 3D box. It then defines the axis for rotation and finally rotates the box 30 degrees about the axis.
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.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))
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
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.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();
}
}