[资料] 创建和编辑AutoCAD实体(四) 编辑二维命名对象 (4)
6、Array Objects阵列对象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.· 环形阵列 复制要阵列的对象并围绕基点按角度移动对象。对象到阵列基点的距离用来计算所创建的每个复本的位置。移动复本对象之后,就可以基于基点的角度旋转对象。每创建一个复本,就需将其追加到块表记录去。http://hi.csdn.net/attachment/201108/11/0_13130503721Vzo.gif· 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.· 矩形阵列 复制对象到基于所需行数和列数的阵列。复本对象间的距离基于给定的行列间距。首先复制一定数量的原件创建第一行或列,然后基于第一行或列创建其他的行或列。每创建一个复本,就需将其追加到块表记录去。http://hi.csdn.net/attachment/201108/11/0_1313050392UXs8.gifFor 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<div align="left"><strong><u></u></strong></div>
<div align="left">Imports Autodesk.AutoCAD.Runtime</div><div align="left">Imports Autodesk.AutoCAD.ApplicationServices</div><div align="left">Imports Autodesk.AutoCAD.DatabaseServices</div><div align="left">Imports Autodesk.AutoCAD.Geometry</div>
<div align="left">Public Shared Function PolarPoints(ByVal pPt As Point2d, _</div><div align="left"> ByVal dAng As Double, _</div><div align="left"> ByVal dDist As Double)</div>
<div align="left">Return New Point2d(pPt.X + dDist * Math.Cos(dAng), _</div><div align="left"> pPt.Y + dDist * Math.Sin(dAng))</div><div align="left">End Function</div>
<div align="left"><CommandMethod("PolarArrayObject")> _</div><div align="left">Public Sub PolarArrayObject()</div><div align="left">'' Get the current document and database</div><div align="left">Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument</div><div align="left">Dim acCurDb As Database = acDoc.Database</div>
<div align="left">'' Start a transaction</div><div align="left">Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div>
<div align="left"> '' Open the Block table record for read</div><div align="left"> Dim acBlkTbl As BlockTable</div><div align="left"> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _</div><div align="left"> OpenMode.ForRead)</div>
<div align="left"> '' Open the Block table record Model space for write</div><div align="left"> Dim acBlkTblRec As BlockTableRecord</div><div align="left"> acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _</div><div align="left"> OpenMode.ForWrite)</div>
<div align="left"> '' Create a circle that is at 2,2 with a radius of 1</div><div align="left"> Dim acCirc As Circle = New Circle()</div><div align="left"> acCirc.Center = New Point3d(2, 2, 0)</div><div align="left"> acCirc.Radius = 1</div>
<div align="left"> '' Add the new object to the block table record and the transaction</div><div align="left"> acBlkTblRec.AppendEntity(acCirc)</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acCirc, True)</div>
<div align="left"> '' Create a 4 object polar array that goes a 180</div><div align="left"> Dim nCount As Integer = 1</div>
<div align="left"> '' Set a value in radians for 60 degrees</div><div align="left"> Dim dAng As Double = 1.0472</div>
<div align="left"> '' Use (4,4,0) as the base point for the array</div><div align="left"> Dim acPt2dArrayBase As Point2d = New Point2d(4, 4)</div>
<div align="left"> While (nCount < 4)</div><div align="left"> Dim acEntClone As Entity = acCirc.Clone()</div>
<div align="left"> Dim acExts As Extents3d</div><div align="left"> Dim acPtObjBase As Point2d</div>
<div align="left"> '' Typically the upper-left corner of an object's extents is used</div><div align="left"> '' for the point on the object to be arrayed unless it is</div><div align="left"> '' an object like a circle.</div><div align="left"> Dim acCircArrObj As Circle = acEntClone</div>
<div align="left"> If IsDBNull(acCircArrObj) = False Then</div><div align="left"> acPtObjBase = New Point2d(acCircArrObj.Center.X, _</div><div align="left"> acCircArrObj.Center.Y)</div><div align="left"> Else</div><div align="left"> acExts = acEntClone.Bounds.GetValueOrDefault()</div><div align="left"> acPtObjBase = New Point2d(acExts.MinPoint.X, _</div><div align="left"> acExts.MaxPoint.Y)</div><div align="left"> End If</div>
<div align="left"> Dim dDist As Double = acPt2dArrayBase.GetDistanceTo(acPtObjBase)</div><div align="left"> Dim dAngFromX As Double = acPt2dArrayBase.GetVectorTo(acPtObjBase).Angle</div>
<div align="left"> Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase, _</div><div align="left"> (nCount * dAng) + dAngFromX, _</div><div align="left"> dDist)</div>
<div align="left"> Dim acVec2d As Vector2d = acPtObjBase.GetVectorTo(acPt2dTo)</div><div align="left"> Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0)</div><div align="left"> acEntClone.TransformBy(Matrix3d.Displacement(acVec3d))</div>
<div align="left"> '' The following code demonstrates how to rotate each object like</div><div align="left"> '' the ARRAY command does.</div><div align="left"> 'acExts = acEntClone.Bounds.GetValueOrDefault()</div><div align="left"> 'acPtObjBase = New Point2d(acExts.MinPoint.X, _</div><div align="left"> ' acExts.MaxPoint.Y)</div><div align="left"> '</div><div align="left"> '' Rotate the cloned entity and around its upper-left extents point</div><div align="left"> 'Dim curUCSMatrix As Matrix3d = acDoc.Editor.CurrentUserCoordinateSystem</div><div align="left"> 'Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d</div><div align="left"> 'acEntClone.TransformBy(Matrix3d.Rotation(nCount * dAng, _</div><div align="left"> ' curUCS.Zaxis, _</div><div align="left"> ' New Point3d(acPtObjBase.X, _</div><div align="left"> ' acPtObjBase.Y, 0)))</div>
<div align="left"> acBlkTblRec.AppendEntity(acEntClone)</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acEntClone, True)</div>
<div align="left"> nCount = nCount + 1</div><div align="left"> End While</div>
<div align="left"> '' Save the new objects to the database</div><div align="left"> acTrans.Commit()</div><div align="left">End Using</div><div align="left">End Sub</div>C#
<div align="left">using Autodesk.AutoCAD.Runtime;</div><div align="left">using Autodesk.AutoCAD.ApplicationServices;</div><div align="left">using Autodesk.AutoCAD.DatabaseServices;</div><div align="left">using Autodesk.AutoCAD.Geometry;</div>
<div align="left">static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)</div><div align="left">{</div><div align="left">return new Point2d(pPt.X + dDist * Math.Cos(dAng),</div><div align="left"> pPt.Y + dDist * Math.Sin(dAng));</div><div align="left">}</div>
<div align="left"></div><div align="left">public static void PolarArrayObject()</div><div align="left">{</div><div align="left">// Get the current document and database获取当前文档和数据库</div><div align="left">Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left">Database acCurDb = acDoc.Database;</div>
<div align="left">// Start a transaction启动事务</div><div align="left">using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())</div><div align="left">{</div><div align="left"> // Open the Block table record for read以读打开Block表</div><div align="left"> BlockTable acBlkTbl;</div><div align="left"> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,</div><div align="left"> OpenMode.ForRead) as BlockTable;</div>
<div align="left"> // Open the Block table record Model space for write</div><div align="left"> // 以写打开块表记录ModelSpace</div><div align="left"> BlockTableRecord acBlkTblRec;</div><div align="left"> acBlkTblRec = acTrans.GetObject(acBlkTbl,</div><div align="left"> OpenMode.ForWrite) as BlockTableRecord;</div>
<div align="left"> // Create a circle that is at 2,2 with a radius of 1</div><div align="left"> // 创建圆,圆心2,2半径1</div><div align="left"> Circle acCirc = new Circle();</div><div align="left"> acCirc.Center = new Point3d(2, 2, 0);</div><div align="left"> acCirc.Radius = 1;</div>
<div align="left"> // Add the new object to the block table record and the transaction</div><div align="left"> // 添加新对象到块表记录和事务</div><div align="left"> acBlkTblRec.AppendEntity(acCirc);</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acCirc, true);</div>
<div align="left"> // Create a 4 object polar array that goes a 180</div><div align="left"> //创建4个对象的180度环形阵列</div><div align="left"> int nCount = 1;</div>
<div align="left"> // Set a value in radians for 60 degrees定义60度弧度值</div><div align="left"> double dAng = 1.0472;</div>
<div align="left"> // Use (4,4,0) as the base point for the array定义阵列基点</div><div align="left"> Point2d acPt2dArrayBase = new Point2d(4, 4);</div>
<div align="left"> while (nCount < 4)</div><div align="left"> {</div><div align="left"> Entity acEntClone = acCirc.Clone() as Entity;</div>
<div align="left"> Extents3d acExts;</div><div align="left"> Point2d acPtObjBase;</div>
<div align="left"> // Typically the upper-left corner of an object's extents is used</div><div align="left"> // for the point on the object to be arrayed unless it is</div><div align="left"> // an object like a circle.</div><div align="left"> //典型情况,使用对象范围的左上角作为要阵列对象上的点,除非</div><div align="left"> //要阵列对象是类似圆这样的对象(使用圆心);</div><div align="left"> Circle acCircArrObj = acEntClone as Circle;</div>
<div align="left"> if (acCircArrObj != null)//是圆</div><div align="left"> {</div><div align="left"> acPtObjBase = new Point2d(acCircArrObj.Center.X,</div><div align="left"> acCircArrObj.Center.Y);</div><div align="left"> }</div><div align="left"> Else//不是圆</div><div align="left"> {</div><div align="left"> acExts = acEntClone.Bounds.GetValueOrDefault();</div><div align="left"> acPtObjBase = new Point2d(acExts.MinPoint.X,</div><div align="left"> acExts.MaxPoint.Y);</div><div align="left"> }</div>
<div align="left"> double dDist = acPt2dArrayBase.GetDistanceTo(acPtObjBase);</div><div align="left"> double dAngFromX = acPt2dArrayBase.GetVectorTo(acPtObjBase).Angle;</div>
<div align="left"> Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,</div><div align="left"> (nCount * dAng) + dAngFromX,</div><div align="left"> dDist);</div>
<div align="left"> Vector2d acVec2d = acPtObjBase.GetVectorTo(acPt2dTo);</div><div align="left"> Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);</div><div align="left"> acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));</div>
<div align="left"> /*</div><div align="left"> // The following code demonstrates how to rotate each object</div><div align="left"> // like the ARRAY command does.</div><div align="left"> // 下列代码演示怎样旋转每个对象,就像Array命令所做的那样;</div><div align="left"> acExts = acEntClone.Bounds.GetValueOrDefault();</div><div align="left"> acPtObjBase = new Point2d(acExts.MinPoint.X,</div><div align="left"> acExts.MaxPoint.Y);</div>
<div align="left"> // Rotate the cloned entity around its upper-left extents point</div><div align="left"> // 围绕左上范围点旋转对象</div><div align="left"> Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;</div><div align="left"> CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;</div><div align="left"> acEntClone.TransformBy(Matrix3d.Rotation(nCount * dAng,</div><div align="left"> curUCS.Zaxis,</div><div align="left"> new Point3d(acPtObjBase.X,</div><div align="left"> acPtObjBase.Y, 0)));</div><div align="left"> */</div>
<div align="left">// 添加新对象到块表记录和事务</div><div align="left"> acBlkTblRec.AppendEntity(acEntClone);</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acEntClone, true);</div>
<div align="left"> nCount = nCount + 1;</div><div align="left"> }</div>
<div align="left"> // Save the new objects to the database保存新对象到数据库</div><div align="left"> acTrans.Commit();</div><div align="left">}</div><div align="left">}</div><div align="left"><u><strong>VBA/ActiveX Code Reference</strong></u></div><div align="left">Sub PolarArrayObject()</div><div align="left"> ' Create the circle</div><div align="left"> Dim circleObj As AcadCircle</div><div align="left"> Dim center(0 To 2) As Double</div><div align="left"> Dim radius As Double</div><div align="left"> center(0) = 2#: center(1) = 2#: center(2) = 0#</div><div align="left"> radius = 1</div><div align="left"> Set circleObj = ThisDrawing.ModelSpace. _</div><div align="left"> AddCircle(center, radius)</div><div align="left"> ZoomAll</div>
<div align="left"> ' Define the polar array</div><div align="left"> Dim noOfObjects As Integer</div><div align="left"> Dim angleToFill As Double</div><div align="left"> Dim basePnt(0 To 2) As Double</div><div align="left"> noOfObjects = 4</div><div align="left"> angleToFill = 3.14 ' 180 degrees</div><div align="left"> basePnt(0) = 4#: basePnt(1) = 4#: basePnt(2) = 0#</div>
<div align="left"> ' The following example will create 4 copies</div><div align="left"> ' of an object by rotating and copying it about</div><div align="left"> ' the point (4,4,0).</div><div align="left"> Dim retObj As Variant</div><div align="left"> retObj = circleObj.ArrayPolar _</div><div align="left"> (noOfObjects, angleToFill, basePnt)</div>
<div align="left"> ZoomAll</div><div align="left">End Sub</div>
6.2、Create Rectangular Arrays创建矩形阵列<div align="left">This example creates a circle and then performs a rectangular array of the circle, creating five rows and five columns of circles.</div><div align="left">本例创建一个圆并实现该圆的五行五列矩形阵列。</div>
<div align="left"><strong><u>VB.NET</u></strong></div><div align="left">Imports Autodesk.AutoCAD.Runtime</div><div align="left">Imports Autodesk.AutoCAD.ApplicationServices</div><div align="left">Imports Autodesk.AutoCAD.DatabaseServices</div><div align="left">Imports Autodesk.AutoCAD.Geometry</div>
<div align="left">Public Shared Function PolarPoints(ByVal pPt As Point2d, _</div><div align="left"> ByVal dAng As Double, _</div><div align="left"> ByVal dDist As Double)</div>
<div align="left">Return New Point2d(pPt.X + dDist * Math.Cos(dAng), _</div><div align="left"> pPt.Y + dDist * Math.Sin(dAng))</div><div align="left">End Function</div>
<div align="left"><CommandMethod("RectangularArrayObject")> _</div><div align="left">Public Sub RectangularArrayObject()</div><div align="left">'' Get the current document and database</div><div align="left">Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument</div><div align="left">Dim acCurDb As Database = acDoc.Database</div>
<div align="left">'' Start a transaction</div><div align="left">Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div>
<div align="left"> '' Open the Block table record for read</div><div align="left"> Dim acBlkTbl As BlockTable</div><div align="left"> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _</div><div align="left"> OpenMode.ForRead)</div>
<div align="left"> '' Open the Block table record Model space for write</div><div align="left"> Dim acBlkTblRec As BlockTableRecord</div><div align="left"> acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _</div><div align="left"> OpenMode.ForWrite)</div>
<div align="left"> '' Create a circle that is at 2,2 with a radius of 0.5</div><div align="left"> Dim acCirc As Circle = New Circle()</div><div align="left"> acCirc.Center = New Point3d(2, 2, 0)</div><div align="left"> acCirc.Radius = 0.5</div>
<div align="left"> '' Add the new object to the block table record and the transaction</div><div align="left"> acBlkTblRec.AppendEntity(acCirc)</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acCirc, True)</div>
<div align="left"> '' Create a rectangular array with 5 rows and 5 columns</div><div align="left"> Dim nRows As Integer = 5</div><div align="left"> Dim nColumns As Integer = 5</div>
<div align="left"> '' Set the row and column offsets along with the base array angle</div><div align="left"> Dim dRowOffset As Double = 1</div><div align="left"> Dim dColumnOffset As Double = 1</div><div align="left"> Dim dArrayAng As Double = 0</div>
<div align="left"> '' Get the angle from X for the current UCS</div><div align="left"> Dim curUCSMatrix As Matrix3d = acDoc.Editor.CurrentUserCoordinateSystem</div><div align="left"> Dim curUCS As CoordinateSystem3d = curUCSMatrix.CoordinateSystem3d</div><div align="left"> Dim acVec2dAng As Vector2d = New Vector2d(curUCS.Xaxis.X, _</div><div align="left"> curUCS.Xaxis.Y)</div>
<div align="left"> '' If the UCS is rotated, adjust the array angle accordingly</div><div align="left"> dArrayAng = dArrayAng + acVec2dAng.Angle</div>
<div align="left"> '' Use the upper-left corner of the objects extents for the array base point</div><div align="left"> Dim acExts As Extents3d = acCirc.Bounds.GetValueOrDefault()</div><div align="left"> Dim acPt2dArrayBase As Point2d = New Point2d(acExts.MinPoint.X, _</div><div align="left"> acExts.MaxPoint.Y)</div>
<div align="left"> '' Track the objects created for each column</div><div align="left"> Dim acDBObjCollCols As DBObjectCollection = New DBObjectCollection()</div><div align="left"> acDBObjCollCols.Add(acCirc)</div>
<div align="left"> '' Create the number of objects for the first column</div><div align="left"> Dim nColumnsCount As Integer = 1</div><div align="left"> While (nColumns > nColumnsCount)</div><div align="left"> Dim acEntClone As Entity = acCirc.Clone()</div><div align="left"> acDBObjCollCols.Add(acEntClone)</div>
<div align="left"> '' Caclucate the new point for the copied object (move)</div><div align="left"> Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase, _</div><div align="left"> dArrayAng, _</div><div align="left"> dColumnOffset * nColumnsCount)</div>
<div align="left"> Dim acVec2d As Vector2d = acPt2dArrayBase.GetVectorTo(acPt2dTo)</div><div align="left"> Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0)</div><div align="left"> acEntClone.TransformBy(Matrix3d.Displacement(acVec3d))</div>
<div align="left"> acBlkTblRec.AppendEntity(acEntClone)</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acEntClone, True)</div>
<div align="left"> nColumnsCount = nColumnsCount + 1</div><div align="left"> End While</div>
<div align="left"> '' Set a value in radians for 90 degrees</div><div align="left"> Dim dAng As Double = Math.PI / 2</div>
<div align="left"> '' Track the objects created for each row and column</div><div align="left"> Dim acDBObjCollLvls As DBObjectCollection = New DBObjectCollection()</div>
<div align="left"> For Each acObj As DBObject In acDBObjCollCols</div><div align="left"> acDBObjCollLvls.Add(acObj)</div><div align="left"> Next</div>
<div align="left"> '' Create the number of objects for each row</div><div align="left"> For Each acEnt As Entity In acDBObjCollCols</div><div align="left"> Dim nRowsCount As Integer = 1</div>
<div align="left"> While (nRows > nRowsCount)</div><div align="left"> Dim acEntClone As Entity = acEnt.Clone()</div><div align="left"> acDBObjCollLvls.Add(acEntClone)</div>
<div align="left"> '' Caclucate the new point for the copied object (move)</div><div align="left"> Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase, _</div><div align="left"> dArrayAng + dAng, _</div><div align="left"> dRowOffset * nRowsCount)</div>
<div align="left"> Dim acVec2d As Vector2d = acPt2dArrayBase.GetVectorTo(acPt2dTo)</div><div align="left"> Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0)</div><div align="left"> acEntClone.TransformBy(Matrix3d.Displacement(acVec3d))</div>
<div align="left"> acBlkTblRec.AppendEntity(acEntClone)</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acEntClone, True)</div>
<div align="left"> nRowsCount = nRowsCount + 1</div><div align="left"> End While</div><div align="left"> Next</div>
<div align="left"> '' Save the new objects to the database</div><div align="left"> acTrans.Commit()</div><div align="left">End Using</div><div align="left">End Sub</div><div align="left"><strong><u>C#</u></strong></div><div align="left">
</div><div align="left">using Autodesk.AutoCAD.Runtime;</div><div align="left">using Autodesk.AutoCAD.ApplicationServices;</div><div align="left">using Autodesk.AutoCAD.DatabaseServices;</div><div align="left">using Autodesk.AutoCAD.Geometry;</div>
<div align="left">static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)</div><div align="left">{</div><div align="left">return new Point2d(pPt.X + dDist * Math.Cos(dAng),</div><div align="left"> pPt.Y + dDist * Math.Sin(dAng));</div><div align="left">}</div>
<div align="left"></div><div align="left">public static void RectangularArrayObject()</div><div align="left">{</div><div align="left">// Get the current document and database获取当前文档和数据库</div><div align="left">Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left">Database acCurDb = acDoc.Database;</div>
<div align="left">// Start a transaction启动事务</div><div align="left">using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())</div><div align="left">{</div><div align="left"> // Open the Block table for read以读打开块表</div><div align="left"> BlockTable acBlkTbl;</div><div align="left"> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,</div><div align="left"> OpenMode.ForRead) as BlockTable;</div>
<div align="left"> // Open the Block table record Model space for write</div><div align="left"> // 以写打开块表记录ModelSpace</div><div align="left"> BlockTableRecord acBlkTblRec;</div><div align="left"> acBlkTblRec = acTrans.GetObject(acBlkTbl,</div><div align="left"> OpenMode.ForWrite) as BlockTableRecord;</div>
<div align="left"> // Create a circle that is at 200,200 with a radius of 60</div><div align="left"> Circle acCirc = new Circle();</div><div align="left"> acCirc.Center = new Point3d(200, 200, 0);</div><div align="left"> acCirc.Radius = 60;</div>
<div align="left"> // Add the new object to the block table record and the transaction</div><div align="left"> acBlkTblRec.AppendEntity(acCirc);</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acCirc, true);</div>
<div align="left"> // Create a rectangular array with 5 rows and 5 columns</div><div align="left"> // 创建5行8列的矩形阵列</div><div align="left"> int nRows = 5;</div><div align="left"> int nColumns = 5;</div>
<div align="left"> // Set the row and column offsets along with the base array angle</div><div align="left"> // 设置行列间距及阵列基角</div><div align="left"> double dRowOffset = 200;</div><div align="left"> double dColumnOffset = 300;</div><div align="left"> double dArrayAng = 0;</div>
<div align="left"> // Get the angle from X for the current UCS</div><div align="left"> // 获取到当前UCS坐标系X轴的角度</div><div align="left"> Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;</div><div align="left"> CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;</div><div align="left"> Vector2d acVec2dAng = new Vector2d(curUCS.Xaxis.X,</div><div align="left"> curUCS.Xaxis.Y);</div>
<div align="left"> // If the UCS is rotated, adjust the array angle accordingly</div><div align="left"> // 如果UCS坐标系旋转了,相应地调整阵列的角度</div><div align="left"> dArrayAng = dArrayAng + acVec2dAng.Angle;</div>
<div align="left"> // Use the upper-left corner of the objects extents for the array base point</div><div align="left"> // 使用对象范围的左上角作为阵列的基点</div><div align="left"> Extents3d acExts = acCirc.Bounds.GetValueOrDefault();</div><div align="left"> Point2d acPt2dArrayBase = new Point2d(acExts.MinPoint.X,</div><div align="left"> acExts.MaxPoint.Y);</div>
<div align="left"> // Track the objects created for each column</div><div align="left"> // 跟踪为每列创建的对象</div><div align="left"> DBObjectCollection acDBObjCollCols = new DBObjectCollection();</div><div align="left"> acDBObjCollCols.Add(acCirc);</div>
<div align="left"> // Create the number of objects for the first column</div><div align="left"> // 创建第一行的对象(个数等于列数)</div><div align="left"> int nColumnsCount = 1;</div><div align="left"> while (nColumns > nColumnsCount)</div><div align="left"> {</div><div align="left"> Entity acEntClone = acCirc.Clone() as Entity;</div><div align="left"> acDBObjCollCols.Add(acEntClone);</div>
<div align="left"> // Caclucate the new point for the copied object (move)</div><div align="left"> // 计算新位置</div><div align="left"> Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,</div><div align="left"> dArrayAng,</div><div align="left"> dColumnOffset * nColumnsCount);</div>
<div align="left"> Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);</div><div align="left"> Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);</div><div align="left"> acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));</div>
<div align="left"> acBlkTblRec.AppendEntity(acEntClone);</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acEntClone, true);</div>
<div align="left"> nColumnsCount = nColumnsCount + 1;</div><div align="left"> }</div>
<div align="left"> // Set a value in radians for 90 degrees</div><div align="left"> double dAng = Math.PI / 2;</div>
<div align="left"> // Track the objects created for each row and column</div><div align="left"> DBObjectCollection acDBObjCollLvls = new DBObjectCollection();</div>
<div align="left"> foreach (DBObject acObj in acDBObjCollCols)</div><div align="left"> {</div><div align="left"> acDBObjCollLvls.Add(acObj);</div><div align="left"> }</div>
<div align="left"> // Create the number of objects for each row创建其余各行</div><div align="left"> foreach (Entity acEnt in acDBObjCollCols)</div><div align="left"> {</div><div align="left"> int nRowsCount = 1;</div>
<div align="left"> while (nRows > nRowsCount)</div><div align="left"> {</div><div align="left"> Entity acEntClone = acEnt.Clone() as Entity;</div><div align="left"> acDBObjCollLvls.Add(acEntClone);</div>
<div align="left"> // Caclucate the new point for the copied object (move)</div><div align="left"> Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,</div><div align="left"> dArrayAng + dAng,</div><div align="left"> dRowOffset * nRowsCount);</div>
<div align="left"> Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);</div><div align="left"> Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);</div><div align="left"> acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));</div>
<div align="left"> acBlkTblRec.AppendEntity(acEntClone);</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acEntClone, true);</div>
<div align="left"> nRowsCount = nRowsCount + 1;</div><div align="left"> }</div><div align="left"> }</div>
<div align="left"> // Save the new objects to the database保存到数据库</div><div align="left"> acTrans.Commit();</div><div align="left">}</div><div align="left">}</div><div align="left"><strong><u>VBA/ActiveX Code Reference</u></strong></div><div align="left">Sub RectangularArrayObject()</div><div align="left"> ' Create the circle</div><div align="left"> Dim circleObj As AcadCircle</div><div align="left"> Dim center(0 To 2) As Double</div><div align="left"> Dim radius As Double</div><div align="left"> center(0) = 2#: center(1) = 2#: center(2) = 0#</div><div align="left"> radius = 0.5</div><div align="left"> Set circleObj = ThisDrawing.ModelSpace. _</div><div align="left"> AddCircle(center, radius)</div><div align="left"> ZoomAll</div>
<div align="left"> ' Define the rectangular array</div><div align="left"> Dim numberOfRows As Long</div><div align="left"> Dim numberOfColumns As Long</div><div align="left"> Dim numberOfLevels As Long</div><div align="left"> Dim distanceBwtnRows As Double</div><div align="left"> Dim distanceBwtnColumns As Double</div><div align="left"> Dim distanceBwtnLevels As Double</div><div align="left"> numberOfRows = 5</div><div align="left"> numberOfColumns = 5</div><div align="left"> numberOfLevels = 0</div><div align="left"> distanceBwtnRows = 1</div><div align="left"> distanceBwtnColumns = 1</div><div align="left"> distanceBwtnLevels = 0</div>
<div align="left"> ' Create the array of objects</div><div align="left"> Dim retObj As Variant</div><div align="left"> retObj = circleObj.ArrayRectangular _</div><div align="left"> (numberOfRows, numberOfColumns, numberOfLevels, _</div><div align="left"> distanceBwtnRows, distanceBwtnColumns, distanceBwtnLevels)</div>
<div align="left"> ZoomAll</div><div align="left">End Sub</div>
页:
[1]