You can create a polar or rectangular array of an object. Arrays of objects are not created using a dedicated set of functions, but are created through a combination of copying objects, and then using a transformation matrix to rotate and move the copied object. The following outlines the basic logic for each type of array:
我們可以創建對象的環形陣列或矩形陣列。對象陣列不是使用一組專門的函數創建的,而是通過複製對象然後使用變換矩陣旋轉和移動對象複本等組合動作創建的。下面簡述一下每種陣列類型的基本邏輯:
· Polar array. Copy the object to be arrayed and move it based on an angle around a the base point. The distance from the object to the base point of the array is used to calculate the placement of each copy that is created. Once the copied object is moved, you can then rotate the object based on its angle from the base point. Once each copy is created, it needs to be appended to the block table record.
· 環形陣列 複製要陣列的對象並圍繞基點按角度移動對象。對象到陣列基點的距離用來計算所創建的每個複本的位置。移動複本對象之後,就可以基於基點的角度旋轉對象。每創建一個複本,就需將其追加到塊表記錄去。
· Rectangular array. Copy the object to array based on the number of desired rows and columns. The distance that the copied objects are copied is based on a specified distance between the rows and columns. You first want to create the number of copies of the original to complete the first row or column. Once the first row or column is created, you can then create the number of objects for the remaining rows or columns based on the first row or column you created. Once each copy is created, it needs to be appended to the block table record.
· 矩形陣列 複製對象到基於所需行數和列數的陣列。複本對象間的距離基於給定的行列間距。首先複製一定數量的原件創建第一行或列,然後基於第一行或列創建其他的行或列。每創建一個複本,就需將其追加到塊表記錄去。
For more information about arrays, see “Create an Array of Objects” in theAutoCAD User's Guide.
關於陣列的更多信息,請參見AutoCAD用戶手冊中的 “創建對象的陣列”。
6.1、Create Polar Arrays創建環形陣列
This example creates a circle, and then performs a polar array of the circle. This creates four circles filling 180 degrees around a base point of (4, 4, 0).
本例創建一個圓,然後實現該圓的環形陣列,創建4個圓繞點(4, 4, 0) 圍成180度。
VB.NET
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Public Shared Function PolarPoints(ByVal pPt As Point2d, _
ByVal dAng As Double, _
ByVal dDist As Double)
Return New Point2d(pPt.X + dDist * Math.Cos(dAng), _
pPt.Y + dDist * Math.Sin(dAng))
End Function
<CommandMethod("PolarArrayObject")> _
Public Sub PolarArrayObject()
'' 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 record 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 1
Dim acCirc As Circle = New Circle()
acCirc.Center = New Point3d(2, 2, 0)
acCirc.Radius = 1
'' Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc)
acTrans.AddNewlyCreatedDBObject(acCirc, True)
'' Create a 4 object polar array that goes a 180
Dim nCount As Integer = 1
'' Set a value in radians for 60 degrees
Dim dAng As Double = 1.0472
'' Use (4,4,0) as the base point for the array
Dim acPt2dArrayBase As Point2d = New Point2d(4, 4)
While (nCount < 4)
Dim acEntClone As Entity = acCirc.Clone()
Dim acExts As Extents3d
Dim acPtObjBase As Point2d
'' Typically the upper-left corner of an object's extents is used
'' for the point on the object to be arrayed unless it is
'' an object like a circle.
Dim acCircArrObj As Circle = acEntClone
If IsDBNull(acCircArrObj) = False Then
acPtObjBase = New Point2d(acCircArrObj.Center.X, _
acCircArrObj.Center.Y)
Else
acExts = acEntClone.Bounds.GetValueOrDefault()
acPtObjBase = New Point2d(acExts.MinPoint.X, _
acExts.MaxPoint.Y)
End If
Dim dDist As Double = acPt2dArrayBase.GetDistanceTo(acPtObjBase)
Dim dAngFromX As Double = acPt2dArrayBase.GetVectorTo(acPtObjBase).Angle
Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase, _
(nCount * dAng) + dAngFromX, _
dDist)
Dim acVec2d As Vector2d = acPtObjBase.GetVectorTo(acPt2dTo)
Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0)
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d))
'' The following code demonstrates how to rotate each object like
'' the ARRAY command does.
'acExts = acEntClone.Bounds.GetValueOrDefault()
'acPtObjBase = New Point2d(acExts.MinPoint.X, _
' acExts.MaxPoint.Y)
'
'' Rotate the cloned entity and around its upper-left extents point
'Dim curUCSMatrix As Matrix3d = acDoc.Editor.CurrentUserCoordinateSystem
'Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d
'acEntClone.TransformBy(Matrix3d.Rotation(nCount * dAng, _
' curUCS.Zaxis, _
' New Point3d(acPtObjBase.X, _
' acPtObjBase.Y, 0)))
acBlkTblRec.AppendEntity(acEntClone)
acTrans.AddNewlyCreatedDBObject(acEntClone, True)
nCount = nCount + 1
End While
'' 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;
static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)
{
return new Point2d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng));
}
[CommandMethod("PolarArrayObject")]
public static void PolarArrayObject()
{
// 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 record for read以讀打開Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
// 以寫打開塊表記錄ModelSpace
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create a circle that is at 2,2 with a radius of 1
// 創建圓,圓心2,2半徑1
Circle acCirc = new Circle();
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 1;
// Add the new object to the block table record and the transaction
// 添加新對象到塊表記錄和事務
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
// Create a 4 object polar array that goes a 180
//創建4個對象的180度環形陣列
int nCount = 1;
// Set a value in radians for 60 degrees定義60度弧度值
double dAng = 1.0472;
// Use (4,4,0) as the base point for the array定義陣列基點
Point2d acPt2dArrayBase = new Point2d(4, 4);
while (nCount < 4)
{
Entity acEntClone = acCirc.Clone() as Entity;
Extents3d acExts;
Point2d acPtObjBase;
// Typically the upper-left corner of an object's extents is used
// for the point on the object to be arrayed unless it is
// an object like a circle.
//典型情況,使用對象範圍的左上角作爲要陣列對象上的點,除非
//要陣列對象是類似圓這樣的對象(使用圓心);
Circle acCircArrObj = acEntClone as Circle;
if (acCircArrObj != null)//是圓
{
acPtObjBase = new Point2d(acCircArrObj.Center.X,
acCircArrObj.Center.Y);
}
Else//不是圓
{
acExts = acEntClone.Bounds.GetValueOrDefault();
acPtObjBase = new Point2d(acExts.MinPoint.X,
acExts.MaxPoint.Y);
}
double dDist = acPt2dArrayBase.GetDistanceTo(acPtObjBase);
double dAngFromX = acPt2dArrayBase.GetVectorTo(acPtObjBase).Angle;
Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
(nCount * dAng) + dAngFromX,
dDist);
Vector2d acVec2d = acPtObjBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
/*
// The following code demonstrates how to rotate each object
// like the ARRAY command does.
// 下列代碼演示怎樣旋轉每個對象,就像Array命令所做的那樣;
acExts = acEntClone.Bounds.GetValueOrDefault();
acPtObjBase = new Point2d(acExts.MinPoint.X,
acExts.MaxPoint.Y);
// Rotate the cloned entity around its upper-left extents point
// 圍繞左上範圍點旋轉對象
Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
acEntClone.TransformBy(Matrix3d.Rotation(nCount * dAng,
curUCS.Zaxis,
new Point3d(acPtObjBase.X,
acPtObjBase.Y, 0)));
*/
// 添加新對象到塊表記錄和事務
acBlkTblRec.AppendEntity(acEntClone);
acTrans.AddNewlyCreatedDBObject(acEntClone, true);
nCount = nCount + 1;
}
// Save the new objects to the database保存新對象到數據庫
acTrans.Commit();
}
}
VBA/ActiveX Code Reference
Sub PolarArrayObject()
' 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 = 1
Set circleObj = ThisDrawing.ModelSpace. _
AddCircle(center, radius)
ZoomAll
' Define the polar array
Dim noOfObjects As Integer
Dim angleToFill As Double
Dim basePnt(0 To 2) As Double
noOfObjects = 4
angleToFill = 3.14 ' 180 degrees
basePnt(0) = 4#: basePnt(1) = 4#: basePnt(2) = 0#
' The following example will create 4 copies
' of an object by rotating and copying it about
' the point (4,4,0).
Dim retObj As Variant
retObj = circleObj.ArrayPolar _
(noOfObjects, angleToFill, basePnt)
ZoomAll
End Sub
6.2、Create Rectangular Arrays創建矩形陣列
This example creates a circle and then performs a rectangular array of the circle, creating five rows and five columns of circles.
本例創建一個圓並實現該圓的五行五列矩形陣列。
VB.NET
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Public Shared Function PolarPoints(ByVal pPt As Point2d, _
ByVal dAng As Double, _
ByVal dDist As Double)
Return New Point2d(pPt.X + dDist * Math.Cos(dAng), _
pPt.Y + dDist * Math.Sin(dAng))
End Function
<CommandMethod("RectangularArrayObject")> _
Public Sub RectangularArrayObject()
'' 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 record 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
'' Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc)
acTrans.AddNewlyCreatedDBObject(acCirc, True)
'' Create a rectangular array with 5 rows and 5 columns
Dim nRows As Integer = 5
Dim nColumns As Integer = 5
'' Set the row and column offsets along with the base array angle
Dim dRowOffset As Double = 1
Dim dColumnOffset As Double = 1
Dim dArrayAng As Double = 0
'' Get the angle from X for the current UCS
Dim curUCSMatrix As Matrix3d = acDoc.Editor.CurrentUserCoordinateSystem
Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d
Dim acVec2dAng As Vector2d = New Vector2d(curUCS.Xaxis.X, _
curUCS.Xaxis.Y)
'' If the UCS is rotated, adjust the array angle accordingly
dArrayAng = dArrayAng + acVec2dAng.Angle
'' Use the upper-left corner of the objects extents for the array base point
Dim acExts As Extents3d = acCirc.Bounds.GetValueOrDefault()
Dim acPt2dArrayBase As Point2d = New Point2d(acExts.MinPoint.X, _
acExts.MaxPoint.Y)
'' Track the objects created for each column
Dim acDBObjCollCols As DBObjectCollection = New DBObjectCollection()
acDBObjCollCols.Add(acCirc)
'' Create the number of objects for the first column
Dim nColumnsCount As Integer = 1
While (nColumns > nColumnsCount)
Dim acEntClone As Entity = acCirc.Clone()
acDBObjCollCols.Add(acEntClone)
'' Caclucate the new point for the copied object (move)
Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase, _
dArrayAng, _
dColumnOffset * nColumnsCount)
Dim acVec2d As Vector2d = acPt2dArrayBase.GetVectorTo(acPt2dTo)
Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0)
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d))
acBlkTblRec.AppendEntity(acEntClone)
acTrans.AddNewlyCreatedDBObject(acEntClone, True)
nColumnsCount = nColumnsCount + 1
End While
'' Set a value in radians for 90 degrees
Dim dAng As Double = Math.PI / 2
'' Track the objects created for each row and column
Dim acDBObjCollLvls As DBObjectCollection = New DBObjectCollection()
For Each acObj As DBObject In acDBObjCollCols
acDBObjCollLvls.Add(acObj)
Next
'' Create the number of objects for each row
For Each acEnt As Entity In acDBObjCollCols
Dim nRowsCount As Integer = 1
While (nRows > nRowsCount)
Dim acEntClone As Entity = acEnt.Clone()
acDBObjCollLvls.Add(acEntClone)
'' Caclucate the new point for the copied object (move)
Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase, _
dArrayAng + dAng, _
dRowOffset * nRowsCount)
Dim acVec2d As Vector2d = acPt2dArrayBase.GetVectorTo(acPt2dTo)
Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0)
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d))
acBlkTblRec.AppendEntity(acEntClone)
acTrans.AddNewlyCreatedDBObject(acEntClone, True)
nRowsCount = nRowsCount + 1
End While
Next
'' 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;
static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)
{
return new Point2d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng));
}
[CommandMethod("RectangularArrayObject")]
public static void RectangularArrayObject()
{
// 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
// 以寫打開塊表記錄ModelSpace
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create a circle that is at 200,200 with a radius of 60
Circle acCirc = new Circle();
acCirc.Center = new Point3d(200, 200, 0);
acCirc.Radius = 60;
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
// Create a rectangular array with 5 rows and 5 columns
// 創建5行8列的矩形陣列
int nRows = 5;
int nColumns = 5;
// Set the row and column offsets along with the base array angle
// 設置行列間距及陣列基角
double dRowOffset = 200;
double dColumnOffset = 300;
double dArrayAng = 0;
// Get the angle from X for the current UCS
// 獲取到當前UCS座標系X軸的角度
Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
Vector2d acVec2dAng = new Vector2d(curUCS.Xaxis.X,
curUCS.Xaxis.Y);
// If the UCS is rotated, adjust the array angle accordingly
// 如果UCS座標系旋轉了,相應地調整陣列的角度
dArrayAng = dArrayAng + acVec2dAng.Angle;
// Use the upper-left corner of the objects extents for the array base point
// 使用對象範圍的左上角作爲陣列的基點
Extents3d acExts = acCirc.Bounds.GetValueOrDefault();
Point2d acPt2dArrayBase = new Point2d(acExts.MinPoint.X,
acExts.MaxPoint.Y);
// Track the objects created for each column
// 跟蹤爲每列創建的對象
DBObjectCollection acDBObjCollCols = new DBObjectCollection();
acDBObjCollCols.Add(acCirc);
// Create the number of objects for the first column
// 創建第一行的對象(個數等於列數)
int nColumnsCount = 1;
while (nColumns > nColumnsCount)
{
Entity acEntClone = acCirc.Clone() as Entity;
acDBObjCollCols.Add(acEntClone);
// Caclucate the new point for the copied object (move)
// 計算新位置
Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
dArrayAng,
dColumnOffset * nColumnsCount);
Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
acBlkTblRec.AppendEntity(acEntClone);
acTrans.AddNewlyCreatedDBObject(acEntClone, true);
nColumnsCount = nColumnsCount + 1;
}
// Set a value in radians for 90 degrees
double dAng = Math.PI / 2;
// Track the objects created for each row and column
DBObjectCollection acDBObjCollLvls = new DBObjectCollection();
foreach (DBObject acObj in acDBObjCollCols)
{
acDBObjCollLvls.Add(acObj);
}
// Create the number of objects for each row創建其餘各行
foreach (Entity acEnt in acDBObjCollCols)
{
int nRowsCount = 1;
while (nRows > nRowsCount)
{
Entity acEntClone = acEnt.Clone() as Entity;
acDBObjCollLvls.Add(acEntClone);
// Caclucate the new point for the copied object (move)
Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,
dArrayAng + dAng,
dRowOffset * nRowsCount);
Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
acBlkTblRec.AppendEntity(acEntClone);
acTrans.AddNewlyCreatedDBObject(acEntClone, true);
nRowsCount = nRowsCount + 1;
}
}
// Save the new objects to the database保存到數據庫
acTrans.Commit();
}
}
VBA/ActiveX Code Reference
Sub RectangularArrayObject()
' 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 rectangular array
Dim numberOfRows As Long
Dim numberOfColumns As Long
Dim numberOfLevels As Long
Dim distanceBwtnRows As Double
Dim distanceBwtnColumns As Double
Dim distanceBwtnLevels As Double
numberOfRows = 5
numberOfColumns = 5
numberOfLevels = 0
distanceBwtnRows = 1
distanceBwtnColumns = 1
distanceBwtnLevels = 0
' Create the array of objects
Dim retObj As Variant
retObj = circleObj.ArrayRectangular _
(numberOfRows, numberOfColumns, numberOfLevels, _
distanceBwtnRows, distanceBwtnColumns, distanceBwtnLevels)
ZoomAll
End Sub