03-04 創建和編輯AutoCAD實體(四) 編輯二維命名對象 (3)

 5、Transform Objects變換對象

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函數在XYZ軸向上將對象縮放相同的倍數。對象的尺寸乘以縮放係數,縮放係數大於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

 

發佈了4 篇原創文章 · 獲贊 20 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章