You move, scale, rotate and mirror an object using a 4 by 4 transformation matrix represented by a Matrix3d object and the TransformBy method. You can also use the GetTransformedCopy method to create a copy of an entity and then apply the transformation to the copy. The Matrix3d object is part of the Geometry namespace.
我们使用Matrix3d对象表示的4×4变换矩阵和TransformBy方法来对对象进行移动、缩放、旋转和镜像等变换操作。我们还可以使用GetTransformedCopy方法创建实体复本,然后对复本进行变换操作。Matrix3d对象是Geometry命名空间的组成部分。
The first three columns of the matrix specify scale and rotation. The fourth column of the matrix is a translation vector. The following table demonstrates the transformation matrix configuration, where R = Rotation and T = Translation:
矩阵的前三列确定缩放和旋转,矩阵的第四列是个平移向量。下表为变换矩阵的配置示范,其中R表示旋转,T表示平移:
Transformation matrix configuration变换矩阵配置 | |||
R00 | R01 | R02 | T0 |
R10 | R11 | R12 | T1 |
R20 | R21 | R22 | T2 |
0 | 0 | 0 | 1 |
To transform an object, first initialize a Matrix3d object. You can initialize the transformation matrix using an array of doubles or starting with a matrix in which represents the World coordinate system or a user coordinate system. Once initialized, you then use the functions of the Matrix3d object to modify the scaling, rotation, or displacement transformation of the matrix.
要变换一个对象,首先初始化Matrix3d对象。我们可以使用一个双精度实数数组或者一个代表世界坐标系或用户坐标系的矩阵来初始化变换矩阵。完成初始化后,我们就可以使用Matrix3d对象的功能修改矩阵的缩放、旋转或平移变换。
After the transformation matrix is complete, apply the matrix to the object using the TransformBy method. The following line of code demonstrates applying a matrix (dMatrix) to an object (acObj):
完成变换矩阵后,使用TransformBy方法将变换矩阵应用在对象上就OK了。下面这行代码演示在对象acObj上应用矩阵dMatrix:
VB.NET
acObj.TransformBy(dMatrix)
C#
acObj.TransformBy(dMatrix);
Example of a rotation matrix 旋转矩阵示例
The following shows a single data array to define a transformation matrix, assigned to the variable dMatrix, which will rotate an entity by 90 degrees about the point (0, 0, 0).
下面演示用一维数组定义变换矩阵,赋值给变量dMatrix,让实体绕点(0, 0, 0)旋转90度。
Rotation Matrix: 90 degrees about point (0, 0, 0) 旋转矩阵: 绕点(0, 0, 0)旋转90度 | |||
0 | -1 | 0 | 0 |
1 | 0 | 0 | 0 |
0 | 0 | 1 | 0 |
0 | 0 | 0 | 1 |
VB.NET
Initialize a transformation matrix with a data array in which contains the information to rotate an object 90 degrees.
使用数据数组初始化变换矩阵,数组包含旋转对象90度的数据。
Dim dMatrix(0 To 15) As Double
dMatrix(0) = 0.0
dMatrix(1) = -1.0
dMatrix(2) = 0.0
dMatrix(3) = 0.0
dMatrix(4) = 1.0
dMatrix(5) = 0.0
dMatrix(6) = 0.0
dMatrix(7) = 0.0
dMatrix(8) = 0.0
dMatrix(9) = 0.0
dMatrix(10) = 1.0
dMatrix(11) = 0.0
dMatrix(12) = 0.0
dMatrix(13) = 0.0
dMatrix(14) = 0.0
dMatrix(15) = 1.0
Dim acMat3d As Matrix3d = New Matrix3d(dMatrix)
Initialize a transformation matrix without a data array and use the Rotation function to return a transformation matrix that rotates an object 90 degrees.
不用数据数组初始化变换矩阵,而是用Rotation函数返回一个实现旋转对象90度的变换矩阵。
Dim acMat3d As Matrix3d = New Matrix3d()
Matrix3d.Rotation(Math.PI / 2, curUCS.Zaxis, New Point3d(0, 0, 0))
C#
Initialize a transformation matrix with a data array in which contains the information to rotate an object 90 degrees.
使用数据数组初始化变换矩阵,数组包含旋转对象90度的数据。
double[] dMatrix = new double[16];
dMatrix[0] = 0.0;
dMatrix[1] = -1.0;
dMatrix[2] = 0.0;
dMatrix[3] = 0.0;
dMatrix[4] = 1.0;
dMatrix[5] = 0.0;
dMatrix[6] = 0.0;
dMatrix[7] = 0.0;
dMatrix[8] = 0.0;
dMatrix[9] = 0.0;
dMatrix[10] = 1.0;
dMatrix[11] = 0.0;
dMatrix[12] = 0.0;
dMatrix[13] = 0.0;
dMatrix[14] = 0.0;
dMatrix[15] = 1.0;
Matrix3d acMat3d = new Matrix3d(dMatrix);
Initialize a transformation matrix without a data array and use the Rotation function to return a transformation matrix that rotates an object 90 degrees.
不用数据数组初始化变换矩阵,而是用Rotation函数返回一个实现旋转对象90度的变换矩阵。
Matrix3d acMat3d = new Matrix3d();
acMat3d = Matrix3d.Rotation(Math.PI / 2, curUCS.Zaxis, new Point3d(0, 0, 0));
Additional examples of transformation matrices 变换矩阵的其他示例
The following are more examples of transformation matrices:
下面为更多的变换矩阵示例
Rotation Matrix: 45 degrees about point (5, 5, 0) 旋转矩阵:围绕点(5, 5, 0)旋转45度 | |||
0.707107 | -0.707107 | 0 | 5 |
0.707107 | 0.707107 | 0 | -2.071068 |
0 | 0 | 1 | 0 |
0 | 0 | 0 | 1 |
Translation Matrix: move an entity by (10, 10, 0) 平移矩阵:沿向量(10, 10, 0)移动实体 | |||
1 | 0 | 0 | 10 |
0 | 1 | 0 | 10 |
0 | 0 | 1 | 0 |
0 | 0 | 0 | 1 |
Scaling Matrix: scale by 10,10 at point (0, 0, 0) 放大矩阵:以(0, 0, 0)为基点x、y、z方向均放大10倍 | |||
10 | 0 | 0 | 0 |
0 | 10 | 0 | 0 |
0 | 0 | 10 | 0 |
0 | 0 | 0 | 1 |
Scaling Matrix: scale by 10,10 at point (2, 2, 0) 放大矩阵:以(2, 2, 0)为基点x、y、z方向均放大10倍 | |||
10 | 0 | 0 | -18 |
0 | 10 | 0 | -18 |
0 | 0 | 10 | 0 |
0 | 0 | 0 | 1 |
Topics in this section
本小姐内容
· Move Objects 移动对象
· Rotate Objects 旋转对象
· Mirror Objects 镜像对象
· Scale Objects 缩放对象
5.1、Move Objects移动对象
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函数,该函数要求输入一个Vecto3d对象。如果不知道所需的向量,可以创建一个Point3d对象然后使用GetVectorTo方法返回两点间的向量。位移向量表示将给定对象移动多远及往哪个方向移动。
For more information about moving objects, see “Move Objects” in theAutoCAD User's Guide.
关于移动对象的更多内容见AutoCAD用户指南中“移动对象”一节。
Move a circle along a vector 沿向量移动一个圆
This example creates a circle and then moves that circle two units along theX axis.
本例创建一个圆,然后将这个圆沿X轴移动2个单位。
VB.NET
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)
'' Create a circle that is at 2,2 with a radius of 0.5
Dim acCirc As Circle = New Circle()
acCirc.Center = New Point3d(2, 2, 0)
acCirc.Radius = 0.5
'' 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
C#
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;
// Create a circle that is at 2,2 with a radius of 0.5
// 创建圆,圆心2,2半径0.5
Circle acCirc = new Circle();
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 0.5;
// Create a matrix and move the circle using a vector from (0,0,0) to (2,0,0)
// 创建一个矩阵,使用(0,0,0)到(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();
}
}
VBA/ActiveX Code Reference
Sub MoveObject()
' Create the circle
Dim circleObj As AcadCircle
Dim center(0 To 2) As Double
Dim radius As Double
center(0) = 2#: center(1) = 2#: center(2) = 0#
radius = 0.5
Set circleObj = ThisDrawing.ModelSpace. _
AddCircle(center, radius)
ZoomAll
' Define the points that make up the move vector.
' The move vector will move the circle 2 units
' along the x axis.
Dim point1(0 To 2) As Double
Dim point2(0 To 2) As Double
point1(0) = 0: point1(1) = 0: point1(2) = 0
point2(0) = 2: point2(1) = 0: point2(2) = 0
' Move the circle
circleObj.Move point1, point2
circleObj.Update
End Sub
5.2、Rotate Objects旋转对象
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 theUser's Guide.
关于旋转对象的更多内容见AutoCAD用户指南中“旋转对象”一节。
Rotate a polyline about a base point 绕基点旋转多段线
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度。
VB.NET
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.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
'' 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
C#
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.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;
// Rotate the polyline 45 degrees, around the Z-axis of the current UCS
// using a base point of (4,4.25,0)
//绕当前UCS的Z轴将多段线旋转45度,基点(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();
}
}
VBA/ActiveX Code Reference
Sub RotateObject()
' Create the polyline
Dim plineObj As AcadLWPolyline
Dim points(0 To 11) As Double
points(0) = 1: points(1) = 2
points(2) = 1: points(3) = 3
points(4) = 2: points(5) = 3
points(6) = 3: points(7) = 3
points(8) = 4: points(9) = 4
points(10) = 4: points(11) = 2
Set plineObj = ThisDrawing.ModelSpace. _
AddLightWeightPolyline(points)
plineObj.Closed = True
ZoomAll
' Define the rotation of 45 degrees about a
' base point of (4, 4.25, 0)
Dim basePoint(0 To 2) As Double
Dim rotationAngle As Double
basePoint(0) = 4: basePoint(1) = 4.25: basePoint(2) = 0
rotationAngle = 0.7853981 ' 45 degrees
' Rotate the polyline
plineObj.Rotate basePoint, rotationAngle
plineObj.Update
End Sub
5.3、Mirror Objects镜像对象
Mirroring flips an object along an axis or mirror line. You can mirror all drawing objects.
镜像就是沿着坐标轴或镜像线将对象翻过来。我们可以镜像所有图像对象。
To mirror an object, use the Mirroring function of a transformation matrix. This function requires a Point3d, Plane, or Line3d object to define the mirror line. Since mirroring is done with a transformation matrix, a new object is not created. If you want to maintain the original object, you will need to create a copy of the object first and then mirror it.
镜像对象使用变换矩阵的Mirroring函数,该函数要求输入Point3d对象、Plane对象或Line3d对象来确定镜像线。由于镜像是通过变换矩阵实现的,因此并没有创建新对象。如果需要保留原来对象,需要先创建对象的复本然后再镜像。
To manage the reflection properties of Text objects, use the MIRRTEXT system variable. The default setting of MIRRTEXT is On (1), which causes Text objects to be mirrored just as any other object. When MIRRTEXT is Off (0), text is not mirrored. Use the GetSystemVariable and SetSystemVariable methods to query and set the MIRRTEXT setting.
使用系统变量MIRRTEXT来管理文字对象的翻转属性。MIRRTEXT的默认设置为开(1),结果是文字对象和其他任何对象一样被翻转过来。MIRRTEXT设置为关(0)时,文字不被翻转。可以使用GetSystemVariable方法和SetSystemVariable方法查询和设置MIRRTEXT变量。
镜像前 镜像后(MIRRTEXT=1) 镜像后(MIRRTEXT=0)
You can mirror a Viewport object in paper space, although doing so has no effect on its model space view or on model space objects.
我们可以镜像图纸空间的视口对象,虽然这样做对模型空间的视图和对象没有作用。
For more information about mirroring objects, see “Copy, Offset, or Mirror Objects” in theAutoCAD User's Guide.
关于镜像对象的更多内容见AutoCAD用户指南中“复制、偏移或镜像对象”一节。
Mirror a polyline about an axis 绕轴线镜像一个多段线
This example creates a lightweight polyline and mirrors that polyline about an axis. The newly created polyline is colored blue.
本例创建一个轻量级多段线,然后绕轴线对其进行镜像。得到的新对象改为蓝色。
VB.NET
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("MirrorObject")> _
Public Sub MirrorObject()
'' 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.AddVertexAt(0, New Point2d(1, 1), 0, 0, 0)
acPoly.AddVertexAt(1, New Point2d(1, 2), 0, 0, 0)
acPoly.AddVertexAt(2, New Point2d(2, 2), 0, 0, 0)
acPoly.AddVertexAt(3, New Point2d(3, 2), 0, 0, 0)
acPoly.AddVertexAt(4, New Point2d(4, 4), 0, 0, 0)
acPoly.AddVertexAt(5, New Point2d(4, 1), 0, 0, 0)
'' Create a bulge of -2 at vertex 1
acPoly.SetBulgeAt(1, -2)
'' Close the polyline
acPoly.Closed = True
'' Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly)
acTrans.AddNewlyCreatedDBObject(acPoly, True)
'' Create a copy of the original polyline
Dim acPolyMirCopy As Polyline = acPoly.Clone()
acPolyMirCopy.ColorIndex = 5
'' Define the mirror line
Dim acPtFrom As Point3d = New Point3d(0, 4.25, 0)
Dim acPtTo As Point3d = New Point3d(4, 4.25, 0)
Dim acLine3d As Line3d = New Line3d(acPtFrom, acPtTo)
'' Mirror the polyline across the X axis
acPolyMirCopy.TransformBy(Matrix3d.Mirroring(acLine3d))
'' Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPolyMirCopy)
acTrans.AddNewlyCreatedDBObject(acPolyMirCopy, 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("MirrorObject")]
public static void MirrorObject()
{
// 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.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
acPoly.AddVertexAt(3, new Point2d(3, 2), 0, 0, 0);
acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
acPoly.AddVertexAt(5, new Point2d(4, 1), 0, 0, 0);
// Create a bulge of -2 at vertex 1
//设置顶点1到顶点2间的多段线段的凸度为-2
acPoly.SetBulgeAt(1, -2);
// Close the polyline闭合多段线
acPoly.Closed = true;
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
// Create a copy of the original polyline
// 创建原多段线的复本
Polyline acPolyMirCopy = acPoly.Clone() as Polyline;
acPolyMirCopy.ColorIndex = 5;
// Define the mirror line定义镜像线
Point3d acPtFrom = new Point3d(0, 4.25, 0);
Point3d acPtTo = new Point3d(4, 4.25, 0);
Line3d acLine3d = new Line3d(acPtFrom, acPtTo);
// Mirror the polyline across the X axis沿X轴方向翻转多段线
acPolyMirCopy.TransformBy(Matrix3d.Mirroring(acLine3d));
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPolyMirCopy);
acTrans.AddNewlyCreatedDBObject(acPolyMirCopy, true);
// Save the new objects to the database
acTrans.Commit();
}
}
VBA/ActiveX Code Reference
Sub MirrorObject()
' Create the polyline
Dim plineObj As AcadLWPolyline
Dim points(0 To 11) As Double
points(0) = 1: points(1) = 1
points(2) = 1: points(3) = 2
points(4) = 2: points(5) = 2
points(6) = 3: points(7) = 2
points(8) = 4: points(9) = 4
points(10) = 4: points(11) = 1
Set plineObj = ThisDrawing.ModelSpace. _
AddLightWeightPolyline(points)
plineObj.SetBulge 1, -2
plineObj.Closed = True
ZoomAll
' Define the mirror axis
Dim point1(0 To 2) As Double
Dim point2(0 To 2) As Double
point1(0) = 0: point1(1) = 4.25: point1(2) = 0
point2(0) = 4: point2(1) = 4.25: point2(2) = 0
' Mirror the polyline
Dim mirrorObj As AcadLWPolyline
Set mirrorObj = plineObj.Mirror(point1, point2)
mirrorObj.color = acBlue
ZoomAll
End Sub
5.4、Scale Objects缩放对象
You scale an object by specifying a base point and scale factor based on the current drawing units. You can scale all drawing objects, as well as attribute reference objects.
我们通过指定一个基准点和基于当前绘图单位的缩放系数来缩放对象。我们可以缩放任何对象,包括属性参照对象。
To scale an object, use the Scaling function of a transformation matrix. This function requires a numeric value for the scale factor of the object and a Point3d object for the base point of the scaling operation. The Scaling function scales the object equally in the X, Y, and Z directions. The dimensions of the object are multiplied by the scale factor. A scale factor greater than 1 enlarges the object. A scale factor between 0 and 1 reduces the object.
缩放对象使用变换矩阵的Scaling函数,该函数需要输入一个数值作为对象的缩放系数,还需要输入一个Point3d对象作为缩放操作的基点。Scaling函数在X、Y和Z轴向上将对象缩放相同的倍数。对象的尺寸乘以缩放系数,缩放系数大于1则放大对象,缩放系数小于1则缩小对象。
Note If you need to scale an object non-uniformly, you need to initialize a transformation matrix using the appropriate data array and then use the TransformBy method of the object. For more information on initializing a transformation matrix with a data array, see Transform Objects.
注意 如果要采用不一致的缩放系数缩放对象,需使用相应的数据数组初始化变换矩阵,然后调用对象的TransformBy方法。详见前面“变换对象”的内容
For more information about scaling, see “Resize or Reshape Objects” in theAutoCAD User's Guide.
关于缩放的更多内容,见AutoCAD用户指南中“更改对象的形状和大小”一节。
Scale a polyline 修改多段线的大小
This example creates a closed lightweight polyline and then scales the polyline by 0.5 from the base point (4, 4.25, 0).
本例创建一个闭合多段线,然后以(4, 4.25, 0)为基点将多段线缩小0.5倍。
VB.NET
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("ScaleObject")> _
Public Sub ScaleObject()
'' 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.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
'' Reduce the object by a factor of 0.5
'' using a base point of (4,4.25,0)
acPoly.TransformBy(Matrix3d.Scaling(0.5, 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
C#
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("ScaleObject")]
public static void ScaleObject()
{
// 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.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;
// Reduce the object by a factor of 0.5
// using a base point of (4,4.25,0)
acPoly.TransformBy(Matrix3d.Scaling(0.5, 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();
}
}
VBA/ActiveX Code Reference
Sub ScaleObject()
' Create the polyline
Dim plineObj As AcadLWPolyline
Dim points(0 To 11) As Double
points(0) = 1: points(1) = 2
points(2) = 1: points(3) = 3
points(4) = 2: points(5) = 3
points(6) = 3: points(7) = 3
points(8) = 4: points(9) = 4
points(10) = 4: points(11) = 2
Set plineObj = ThisDrawing.ModelSpace. _
AddLightWeightPolyline(points)
plineObj.Closed = True
ZoomAll
' Define the scale
Dim basePoint(0 To 2) As Double
Dim scalefactor As Double
basePoint(0) = 4: basePoint(1) = 4.25: basePoint(2) = 0
scalefactor = 0.5
' Scale the polyline
plineObj.ScaleEntity basePoint, scalefactor
plineObj.Update
End Sub