You can move all drawing objects and attribute reference objects along a specified vector.
用户可以沿指定的矢量移动所有的图形对象和属性参考对象。
To move an object, use the Displacement function of a transformation matrix. This function requires a Vector3d object as input. If you do not know the vector that you need, you can create a Point3d object and then use the GetVectorTo method to return the vector between two points. The displacement vector indicates how far the given object is to be moved and in what direction.
要移动对象,请使用一个转换矩阵的 Displacement 函数。这个函数需要一个 Vector3d 对象作为输入。如果用户不知道需要的矢量,可以创建一个 Point3d 对象然后用它的 GetVectorTo 方法返回两个点之间的矢量。位移矢量指示给定对象移动的距离和方向。
For more information about moving objects, see “Move Objects” in the AutoCAD User's Guide.
更多关于移动对象的详细信息,请参见请参见《AutoCAD 用户手册》中的“移动对象”。
This example creates a circle and then moves that circle two units along the X axis.
本例创建一个圆然后将圆沿X轴移动两个单位。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("MoveObject")> _
Public Sub MoveObject()
'' 获得当前文档和数据库 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)
'' 在点 2,2 创建一个半径为0.5的圆 Create a circle that is at 2,2 with a radius of 0.5
Dim acCirc As Circle = New Circle()
acCirc.SetDatabaseDefaults()
acCirc.Center = New Point3d(2, 2, 0)
acCirc.Radius = 0.5
'' 创建一个矩阵并利用一个矢量将圆从点(0,0,0)移动到点(2,0,0) Create a matrix and move the circle using a vector from (0,0,0) to (2,0,0)
Dim acPt3d As Point3d = New Point3d(0, 0, 0)
Dim acVec3d As Vector3d = acPt3d.GetVectorTo(New Point3d(2, 0, 0))
acCirc.TransformBy(Matrix3d.Displacement(acVec3d))
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc)
acTrans.AddNewlyCreatedDBObject(acCirc, 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("MoveObject")]
public static void MoveObject()
{
// 获得当前文档和数据库 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;
// 在点 2,2 创建一个半径为0.5的圆 Create a circle that is at 2,2 with a radius of 0.5
Circle acCirc = new Circle();
acCirc.SetDatabaseDefaults();
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 0.5;
// 创建一个矩阵并利用一个矢量将圆从点(0,0,0)移动到点(2,0,0) Create a matrix and move the circle using a vector from (0,0,0) to (2,0,0)
Point3d acPt3d = new Point3d(0, 0, 0);
Vector3d acVec3d = acPt3d.GetVectorTo(new Point3d(2, 0, 0));
acCirc.TransformBy(Matrix3d.Displacement(acVec3d));
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
// 保存新对象到数据库中 Save the new objects to the database
acTrans.Commit();
}
}