EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 580|回复: 0
打印 上一主题 下一主题

[资料] 创建和编辑AutoCAD实体(四) 编辑二维命名对象 (4)

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12406

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-8 15:36:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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.
·         环形阵列 复制要阵列的对象并围绕基点按角度移动对象。对象到阵列基点的距离用来计算所创建的每个复本的位置。移动复本对象之后,就可以基于基点的角度旋转对象。每创建一个复本,就需将其追加到块表记录去。

                               
登录/注册后可看大图
·         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
  1. <div align="left"><strong><u></u></strong></div>
  2. <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>
  3. <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>
  4. <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>
  5. <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>
  6. <div align="left">  '' Start a transaction</div><div align="left">  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div>
  7. <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>
  8. <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>
  9. <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>
  10. <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>
  11. <div align="left">      '' Create a 4 object polar array that goes a 180</div><div align="left">      Dim nCount As Integer = 1</div>
  12. <div align="left">      '' Set a value in radians for 60 degrees</div><div align="left">      Dim dAng As Double = 1.0472</div>
  13. <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>
  14. <div align="left">      While (nCount < 4)</div><div align="left">          Dim acEntClone As Entity = acCirc.Clone()</div>
  15. <div align="left">          Dim acExts As Extents3d</div><div align="left">          Dim acPtObjBase As Point2d</div>
  16. <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>
  17. <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>
  18. <div align="left">          Dim dDist As Double = acPt2dArrayBase.GetDistanceTo(acPtObjBase)</div><div align="left">          Dim dAngFromX As Double = acPt2dArrayBase.GetVectorTo(acPtObjBase).Angle</div>
  19. <div align="left">          Dim acPt2dTo As Point2d = PolarPoints(acPt2dArrayBase, _</div><div align="left">                                                (nCount * dAng) + dAngFromX, _</div><div align="left">                                                dDist)</div>
  20. <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>
  21. <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>
  22. <div align="left">          acBlkTblRec.AppendEntity(acEntClone)</div><div align="left">          acTrans.AddNewlyCreatedDBObject(acEntClone, True)</div>
  23. <div align="left">          nCount = nCount + 1</div><div align="left">      End While</div>
  24. <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#

  1. <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>
  2. <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>
  3. <div align="left">[CommandMethod("PolarArrayObject")]</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>
  4. <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>
  5. <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[BlockTableRecord.ModelSpace],</div><div align="left">                                      OpenMode.ForWrite) as BlockTableRecord;</div>
  6. <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>
  7. <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>
  8. <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>
  9. <div align="left">      // Set a value in radians for 60 degrees定义60度弧度值</div><div align="left">      double dAng = 1.0472;</div>
  10. <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>
  11. <div align="left">      while (nCount < 4)</div><div align="left">      {</div><div align="left">          Entity acEntClone = acCirc.Clone() as Entity;</div>
  12. <div align="left">          Extents3d acExts;</div><div align="left">          Point2d acPtObjBase;</div>
  13. <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>
  14. <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>
  15. <div align="left">          double dDist = acPt2dArrayBase.GetDistanceTo(acPtObjBase);</div><div align="left">          double dAngFromX = acPt2dArrayBase.GetVectorTo(acPtObjBase).Angle;</div>
  16. <div align="left">          Point2d acPt2dTo = PolarPoints(acPt2dArrayBase,</div><div align="left">                                         (nCount * dAng) + dAngFromX,</div><div align="left">                                         dDist);</div>
  17. <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>
  18. <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>
  19. <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>
  20. <div align="left">// 添加新对象到块表记录和事务</div><div align="left">          acBlkTblRec.AppendEntity(acEntClone);</div><div align="left">          acTrans.AddNewlyCreatedDBObject(acEntClone, true);</div>
  21. <div align="left">          nCount = nCount + 1;</div><div align="left">      }</div>
  22. <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>
复制代码
  1. <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>
  2. <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>
  3. <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>
  4. <div align="left">    ZoomAll</div><div align="left">End Sub</div>
  5. 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>
  6. <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>
  7. <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>
  8. <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>


  9. <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>
  10. <div align="left">  '' Start a transaction</div><div align="left">  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div>
  11. <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>
  12. <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>
  13. <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>
  14. <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>
  15. <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>
  16. <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>
  17. <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>
  18. <div align="left">      '' If the UCS is rotated, adjust the array angle accordingly</div><div align="left">      dArrayAng = dArrayAng + acVec2dAng.Angle</div>
  19. <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>
  20. <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>
  21. <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>
  22. <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>
  23. <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>
  24. <div align="left">          acBlkTblRec.AppendEntity(acEntClone)</div><div align="left">          acTrans.AddNewlyCreatedDBObject(acEntClone, True)</div>
  25. <div align="left">          nColumnsCount = nColumnsCount + 1</div><div align="left">      End While</div>
  26. <div align="left">      '' Set a value in radians for 90 degrees</div><div align="left">      Dim dAng As Double = Math.PI / 2</div>
  27. <div align="left">      '' Track the objects created for each row and column</div><div align="left">      Dim acDBObjCollLvls As DBObjectCollection = New DBObjectCollection()</div>
  28. <div align="left">      For Each acObj As DBObject In acDBObjCollCols</div><div align="left">          acDBObjCollLvls.Add(acObj)</div><div align="left">      Next</div>
  29. <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>
  30. <div align="left">          While (nRows > nRowsCount)</div><div align="left">              Dim acEntClone As Entity = acEnt.Clone()</div><div align="left">              acDBObjCollLvls.Add(acEntClone)</div>
  31. <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>
  32. <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>
  33. <div align="left">              acBlkTblRec.AppendEntity(acEntClone)</div><div align="left">              acTrans.AddNewlyCreatedDBObject(acEntClone, True)</div>
  34. <div align="left">              nRowsCount = nRowsCount + 1</div><div align="left">          End While</div><div align="left">      Next</div>
  35. <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>
复制代码
  1. <div align="left">
  2. </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>
  3. <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>
  4. <div align="left">[CommandMethod("RectangularArrayObject")]</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>
  5. <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>
  6. <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[BlockTableRecord.ModelSpace],</div><div align="left">                                      OpenMode.ForWrite) as BlockTableRecord;</div>
  7. <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>
  8. <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>
  9. <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>
  10. <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>
  11. <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>
  12. <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>
  13. <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>
  14. <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>
  15. <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>
  16. <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>
  17. <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>
  18. <div align="left">          acBlkTblRec.AppendEntity(acEntClone);</div><div align="left">          acTrans.AddNewlyCreatedDBObject(acEntClone, true);</div>
  19. <div align="left">          nColumnsCount = nColumnsCount + 1;</div><div align="left">      }</div>
  20. <div align="left">      // Set a value in radians for 90 degrees</div><div align="left">      double dAng = Math.PI / 2;</div>
  21. <div align="left">      // Track the objects created for each row and column</div><div align="left">      DBObjectCollection acDBObjCollLvls = new DBObjectCollection();</div>
  22. <div align="left">      foreach (DBObject acObj in acDBObjCollCols)</div><div align="left">      {</div><div align="left">          acDBObjCollLvls.Add(acObj);</div><div align="left">      }</div>
  23. <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>
  24. <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>
  25. <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>
  26. <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>
  27. <div align="left">              acBlkTblRec.AppendEntity(acEntClone);</div><div align="left">              acTrans.AddNewlyCreatedDBObject(acEntClone, true);</div>
  28. <div align="left">              nRowsCount = nRowsCount + 1;</div><div align="left">          }</div><div align="left">      }</div>
  29. <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>
  30. <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>
  31. <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>
  32. <div align="left">    ZoomAll</div><div align="left">End Sub</div>
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对

相关帖子

工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-27 04:26

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表