天天看点

autoCAD 编辑命名和二维对象

要修改现有的对象,可以使用与该对象关联的方法和特性。如果修改图形对象的可见特性,请使用 Regen 方法来重画屏幕上的对象。Regen 方法是 Editor 对象的成员。

使用命名对象

删除对象

复制对象

偏移对象

转换对象

陈列对象

延伸和修剪对象

分解对象

编辑多段线

编辑样条曲线

编辑图案填充

使用命名对象

除了 AutoCAD 所使用的图形对象之外,图形数据库中还保存了几种非图形对象。这些对象都具有描述性的名称。例如所有的块、图层、编组和标注样式都指定了名字,并且大部分情况下都可以重命名。符号表记录的名字是被用来显示在 AutoCAD 的用户界面上的,而对象的 ObjectID 在 的大多数情况下用于在整个.NET API 中引用对象。

例如,LayerTableRecord 对象的 ObjectID 被指定给一个图形对象的 Layer 属性而非指定给 LayerTableRecord 的真实名字。可是,利用想访问的层的命令可以 从层表中获得 LayerTableRecord 的 ObjectID。

清理未引用的命名对象

未引用的命名对象可以随时从数据库中清除掉。不能清理被其他对象引用的命名对象。例如,字体文件可能被文字样式引用,图层可能被图层上的对象引用。 清理可以缩小保存到磁盘的图形文件的大小。

未引用对象使用 Purge 方法从图形数据库中被清除掉。Purge 方法需要一个想要清除的以 ObjectIdCollection 或 ObjectIdGraph 对象为形式对象列表。ObjectIdCollection 或 ObjectIdGraph 对象被传递给 Purge 方法,然后更新为可以从数据库中删除的对象。调用 Purge 后,还需要清除返回的每一个单独对象。

下列示例演示如何从数据库中清除所有未引用的图层。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("PurgeUnreferencedLayers")> _
Public Sub PurgeUnreferencedLayers()
  '' 获得当前文档和数据库   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 Layer table for read
      Dim acLyrTbl As LayerTable
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                   OpenMode.ForRead)
 
      '' 创建一个 ObjectIdCollection 用于保存每一个表记录的 ObjectID    Create an ObjectIdCollection to hold the object ids for each table record
      Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
 
      '' 遍历每个图层并添加到 ObjectIdCollection 中去    Step through each layer and add it to the ObjectIdCollection
      For Each acObjId As ObjectId In acLyrTbl
          acObjIdColl.Add(acObjId)
      Next
 
      '' 移除使用中的图层并返回一个可以被删除的图层集合   Remove the layers that are in use and return the ones that can be erased
      acCurDb.Purge(acObjIdColl)
 
      '' 遍历返回的 ObjectIdCollection     Step through the returned ObjectIdCollection
      For Each acObjId As ObjectId In acObjIdColl
          Dim acSymTblRec As SymbolTableRecord
          acSymTblRec = acTrans.GetObject(acObjId, _
                                          OpenMode.ForWrite)
 
          Try
              '' 删除未引用的图层     Erase the unreferenced layer
              acSymTblRec.Erase(True)
          Catch Ex As Autodesk.AutoCAD.Runtime.Exception
              '' 图层不能被删除    Layer could not be deleted
              Application.ShowAlertDialog("Error:" & vbLf & Ex.Message)
          End Try
      Next
 
      '' 提交修改并销毁事务  Commit the changes and dispose of the transaction
      acTrans.Commit()
  End Using
End Sub
           

重命名对象

图形变得比较复杂时,用户可以重命名对象,使名称有针对性或者避免与用户插入或附加到主图形的其他图形中的名称相冲突。Name 属性用于得到命名对象的当前名字或修改名字。

用户可以重任何命名对象,除了那些被 AutoCAD 保留的外,例如,图层 0 或 CONTINUOUS 线型。

名称最长可以包含 255 个字符。除了字母和数字之外,名称中还可以包含空格(虽然 AutoCAD 会删除名称前后的空格)以及 Microsoft Windows 或 AutoCAD 未用作其他用途的任何特殊字符。不能使用的特殊字符包括小于号和大于号 (< >)、正斜杠和反斜杠 (/ \)、引号 (")、冒号 (:)、分号 (;)、问号 (?)、逗号 (,) 星号 (*)、竖线 (|)、等号 (=) 和单引号 (')。也不能使用以 Unicode 字体创建的特殊字符。

重命令图层

本例创建图层 ‘0“ 的副本,然后重命令图层为 “MyLayer”。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("RenameLayer")> _
Public Sub RenameLayer()
  '' 获得当前文档和数据库   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()
 
      ''返回当前数据库的层表   Returns the layer table for the current database
      Dim acLyrTbl As LayerTable
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                   OpenMode.ForWrite)
 
      '' 复制 0 图层(复制它及它的属性)为一个新的图层     Clone layer 0 (copy it and its properties) as a new layer
      Dim acLyrTblRec As LayerTableRecord
      acLyrTblRec = acTrans.GetObject(acLyrTbl("0"), _
                                      OpenMode.ForRead).Clone()
 
      '' 修改复制图层的名字    Change the name of the cloned layer
      acLyrTblRec.Name = "MyLayer"
 
      '' 添加复制的图层到层表和事务中     Add the cloned layer to the Layer table and transaction
      acLyrTbl.Add(acLyrTblRec)
      acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
 
      '' 保存更改和事务的销毁     Save changes and dispose of the transaction
      acTrans.Commit()
  End Using
End Sub
           

删除对象

使用 Erase 方法可以删除非图形对象和图形对象。

警告虽然许多非图形对象,像层表和模型空间块表记录都有 Erase 方法,但它不应该被调用。如果这些对象的任何的一个的 Erase 被调用,将会产生错误。

创建和删除多段线

本例创建一个轻量多段线,然后删除它。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("EraseObject")> _
Public Sub EraseObject()
  '' 获得当前文档和数据库   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.SetDatabaseDefaults()
      acPoly.AddVertexAt(0, New Point2d(2, 4), 0, 0, 0)
      acPoly.AddVertexAt(1, New Point2d(4, 2), 0, 0, 0)
      acPoly.AddVertexAt(2, New Point2d(6, 4), 0, 0, 0)
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acPoly)
      acTrans.AddNewlyCreatedDBObject(acPoly, True)
 
      '' 更新显示并显示一条提醒消息    Update the display and display an alert message
      acDoc.Editor.Regen()
      Application.ShowAlertDialog("Erase the newly added polyline.")
 
      '' 从图形中删除多段线    Erase the polyline from the drawing
      acPoly.Erase(True)
 
      '' 保存新对象到数据库中   Save the new object to the database
      acTrans.Commit()
  End Using
End Sub
           

复制对象

用户可以创建数据中大部分的非图形和图形对象的副本。用户创建对象的副本使用 Clone 函数。一旦对象被复制,就可以在将返回的对象添加到数据库之前修改它。通过 Clone 和 TransformBy 方法的使用,用户可以模仿许多 AutoCAD 中的修改命令。

与直接创建一个的对象的副本一起,用户也可以使用 Clone 和 TransformBy 方法偏移、镜像和阵列对象。

复制一个对象

若要复制一个对象,使用为对象提供的 Clone 函数。这个方法创建一个新对象,它是原始对象的副本。一旦复制的对象被创建,然后就可以添加或追加它到数据库前修改它。如果用户不转换对象或修改它的位置,新对象将被放置在与原始对象相同的位置。

如果用户有很多对象想复制,可以添加每一个 ObjectID 到一个 ObjectIDCollection 对象中然后迭代每个对象。当迭代每一个对象时,可以为每个对象使用 Clone 函数,然后添加或追加新对象到数据库中。

复制一个对象

下面的示例创建一个新的圆,然后创建一个圆的直接副本作为创建的第二个圆。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("SingleCopy")> _
Public Sub SingleCopy()
  '' 获得当前文档和数据库   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)
 
      '' 创建一个中心点在 (2,3,0) ,半径为4.25 的圆  Create a circle that is at 2,3 with a radius of 4.25
      Dim acCirc As Circle = New Circle()
      acCirc.SetDatabaseDefaults()
      acCirc.Center = New Point3d(2, 3, 0)
      acCirc.Radius = 4.25
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acCirc)
      acTrans.AddNewlyCreatedDBObject(acCirc, True)
 
      '' 创建一个圆的副本并修改它的半径     Create a copy of the circle and change its radius
      Dim acCircClone As Circle = acCirc.Clone()
      acCircClone.Radius = 1
 
      '' 添加复制的圆     Add the cloned circle
      acBlkTblRec.AppendEntity(acCircClone)
      acTrans.AddNewlyCreatedDBObject(acCircClone, True)
 
      '' 保存新对象到数据库中   Save the new object to the database
      acTrans.Commit()
  End Using
End Sub
           

在数据库之间复制对象

用户可以在两个数据库之间复制对象。Clone 函数用于在同一个数据库内部复制对象,而 WblockCloneObjects 方法用于从一个数据库复制对象到另一个数据库。WblockCloneObjects 方法是 Database 对象的成员。WblockCloneObjects 方法需要下列参数:

  • ObjectIdCollection - 要复制对象的列表。
  • ObjectId - 被复制对象的新父对象的 ObjectID。
  • IdMapping - 当前的数据结构和被复制对象的新的 ObjectIds 。
  • DuplicateRecordCloning - 确定重复的对象应该如何处理。
  • Defer Translation - 控制是否 ObjectID 应该被转换。

从一个数据库复制对象到另一个数据库中

本例创建两个圆对象,然后使用 WblockCloneObjects 方法复制圆到一个新的图形中。示例在复制圆之前利用 acad.dwt 文件创建一个新的图形。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("CopyObjectsBetweenDatabases", CommandFlags.Session)> _
Public Sub CopyObjectsBetweenDatabases()
  Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
 
  '' 获得当前文档和数据库   Get the current document and database
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  '' 锁定当前图形   Lock the current document
  Using acLckDocCur As DocumentLock = acDoc.LockDocument()
 
  ''启动一个事务   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)
 
          '' 创建一个圆心为(0,0,0),半径为 5 的圆    Create a circle that is at (0,0,0) with a radius of 5
          Dim acCirc1 As Circle = New Circle()
          acCirc1.SetDatabaseDefaults()
          acCirc1.Center = New Point3d(0, 0, 0)
          acCirc1.Radius = 5
 
          '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
          acBlkTblRec.AppendEntity(acCirc1)
          acTrans.AddNewlyCreatedDBObject(acCirc1, True)
 
          '' 创建一个圆心为(0,0,0),半径为 7 的圆   Create a circle that is at (0,0,0) with a radius of 7
          Dim acCirc2 As Circle = New Circle()
          acCirc2.SetDatabaseDefaults()
          acCirc2.Center = New Point3d(0, 0, 0)
          acCirc2.Radius = 7
 
          '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
          acBlkTblRec.AppendEntity(acCirc2)
          acTrans.AddNewlyCreatedDBObject(acCirc2, True)
 
          '' 添加所有要复制的对象到新的文档中    Add all the objects to copy to the new document
          acObjIdColl = New ObjectIdCollection()
          acObjIdColl.Add(acCirc1.ObjectId)
          acObjIdColl.Add(acCirc2.ObjectId)
 
          '' 保存新对象到数据库中   Save the new objects to the database
          acTrans.Commit()
      End Using
 
      '' 解锁文档   Unlock the document
  End Using
 
  '' 修改文件和路径以匹配你工作站中的图形模板     Change the file and path to match a drawing template on your workstation
  Dim sLocalRoot As String = Application.GetSystemVariable("LOCALROOTPREFIX")
  Dim sTemplatePath As String = sLocalRoot + "Template\acad.dwt"
 
  '' 创建新的图形用于复制对象    Create a new drawing to copy the objects to
  Dim acDocMgr As DocumentCollection = Application.DocumentManager
  Dim acNewDoc As Document = acDocMgr.Add(sTemplatePath)
  Dim acDbNewDoc As Database = acNewDoc.Database
 
  '' 锁定新文档    Lock the new document
  Using acLckDoc As DocumentLock = acNewDoc.LockDocument()
 
      '' 在新数据库中启动事务   Start a transaction in the new database
      Using acTrans = acDbNewDoc.TransactionManager.StartTransaction()
 
          '' 以只读方式打开块表   Open the Block table for read
          Dim acBlkTblNewDoc As BlockTable
          acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId, _
                                             OpenMode.ForRead)
 
          '' 以只读方式打开模型空间的块表记录    Open the Block table record Model space for read
          Dim acBlkTblRecNewDoc As BlockTableRecord
          acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace), _
                                                OpenMode.ForRead)
 
          '' 复制对象到新的数据库中    Clone the objects to the new database
          Dim acIdMap As IdMapping = New IdMapping()
          acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId, acIdMap, _
                                     DuplicateRecordCloning.Ignore, False)
 
          '' 保存复制的对象到数据库中    Save the copied objects to the database
          acTrans.Commit()
      End Using
 
      '' 解锁文档   Unlock the document
  End Using
 
  '' 设置新文档为当前文档   Set the new document current
  acDocMgr.MdiActiveDocument = acNewDoc
End Sub
           

偏移对象

偏移对象可以在距原始对象的指定偏移距离处创建新的对象。可以偏移圆弧、圆、椭圆、直线、优化多段线、多段线、样条曲线和构造线。

要偏移对象,请使用该对象的 GetOffsetCurves 方法。这个函数需要一个正的或负的数值作为偏移对象的距离。如果此距离为负,则 AutoCAD 会将其解释为一段用于生成“更小”曲线的偏移(即对于圆弧来说,将偏移到小于起始曲线的半径给定距离的半径)。如果“更小”没有意义,则 AutoCAD 将在更小的 X,Y,Z WCS 坐标方向上偏移。

对于许多对象,此操作的结果是一条新的曲线(可能与原始曲线的类型不同)。例如,偏移椭圆会形成样条曲线,因为结果的确符合椭圆的表达式。在某些情况下,偏移结果可能会形成若干条曲线。因此,该函数会返回 DBObjectCollection 对象,它包含通过偏移曲线所创建的所有对象。返回的 DBObjectCollection 对象需要循环每一个被创建的对象然后追加到图形数据库中。

偏移多段线

本例创建一条优化多段线,然后偏移该多段线。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("OffsetObject")> _
  Public Sub OffsetObject()
  '' 获得当前文档和数据库   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.SetDatabaseDefaults()
      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)
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acPoly)
      acTrans.AddNewlyCreatedDBObject(acPoly, True)
 
      '' 将多段线偏移一定距离   Offset the polyline a given distance
      Dim acDbObjColl As DBObjectCollection = acPoly.GetOffsetCurves(0.25)
 
      '' 遍历新创建的对象    Step through the new objects created
      For Each acEnt As Entity In acDbObjColl
          '' 添加每个偏移的对象    Add each offset object
          acBlkTblRec.AppendEntity(acEnt)
          acTrans.AddNewlyCreatedDBObject(acEnt, True)
      Next
 
      '' 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit()
  End Using
End Sub
           

转换对象

利用 4 x 4 转换矩阵表示的一个 Matrix3d 对象和 TransformBy 方法,可以移动、缩放、旋转和镜像对象。也可以使用 GetTransformedCopy 方法创建一个图元的副本然后应用变换到副本中。Matrix3d 对象是 Geometry 命名空间的一部分。

矩阵的前面三列确定缩放和旋转。矩阵的第四列是一个平移矢量。下面的表格演示转换矩阵配置,R = 旋转而 T = 平移:

转换矩阵配置
R00 R01 R02 T0
R10 R11 R12 T1
R20 R21 R22 T2
1

 要转换一个对象,首先要初始化一个 Matrix3d 对象。用户可以使用一个双精度整数数组或以一个表示世界坐标系或用户坐标系的矩阵来初始化转换矩阵。一旦初始化完成,就可以使用 Matrix3d 对象的函数用于修改缩放、旋转或平移的转换矩阵。

变换矩阵完成后,利用 TransformBy 方法应用这个矩阵到对象中。下面的代码行演示应用一个矩阵(dMatrix) 到一个对象(acObj):

acObj.TransformBy(dMatrix)      

一个旋转矩阵的例子

下面显示一个用于定义一个转换矩阵的单个数据数组,并赋值给变量 dMatrix,它将图元绕点 (0,0,0)旋转90度。

旋转矩阵:绕点(0,0,0)旋转90度
0.0 -1.0 0.0 0.0
1.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
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)
           

初始化一个没有数据组的转换矩阵,并使用 Rotation 函数返回一个将一个对象旋转90度的转换矩阵。

Dim acMat3d As Matrix3d = New Matrix3d()
 
Matrix3d.Rotation(Math.PI / 2, _
                  curUCS.Zaxis, _
                  New Point3d(0, 0, 0))
           

另外的转换矩阵的例子

下面有很多转换矩阵的例子:

旋转矩阵:绕点(5,5,0)旋转45度
0.707107 -0.707107 0.0 5.0
0.707107 0.707107 0.0 -2.071068
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
转换矩阵:将图元移到 (10, 10, 0)
1.0 0.0 0.0 10.0
0.0 1.0 0.0 10.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
缩放矩阵:在点 (0, 0, 0) 上按 10,10 进行缩放
10.0 0.0 0.0 0.0
0.0 10.0 0.0 0.0
0.0 0.0 10.0 0.0
0.0 0.0 0.0 1.0
缩放矩阵:在点 (2, 2, 0) 上按 10,10 进行缩放
10.0 0.0 0.0 -18.0
0.0 10.0 0.0 -18.0
0.0 0.0 10.0 0.0
0.0 0.0 0.0 1.0

移动对象

用户可以沿指定的矢量移动所有的图形对象和属性参考对象。

要移动对象,请使用一个转换矩阵的 Displacement 函数。这个函数需要一个 Vector3d 对象作为输入。如果用户不知道需要的矢量,可以创建一个 Point3d 对象然后用它的 GetVectorTo 方法返回两个点之间的矢量。位移矢量指示给定对象移动的距离和方向。

沿一个矢量移动圆

本例创建一个圆然后将圆沿X轴移动两个单位。

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)
 
      '' 在点 2,2 创建一个半径为0.5的圆    Create a circle that is at 2,2 with a radius of 0.5
      Dim acCirc As Circle = New Circle()
      acCirc.SetDatabaseDefaults()
      acCirc.Center = New Point3d(2, 2, 0)
      acCirc.Radius = 0.5
 
      '' 创建一个矩阵并利用一个矢量将圆从点(0,0,0)移动到点(2,0,0)     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
           

旋转对象

用户可以旋转所有的图形对象和属性参考对象。

要旋转对象,请使用一个转换矩阵的 Rotation 函数。这个函数需要一个用弧度表示的旋转角度、一个旋转轴和一个基点。旋转轴必须以 Vector3d 对象的形式表示而基点要作为 Point3d 对象提供。此角度确定对象绕基点、相对于当前位置旋转的距离。

绕基点旋转多段线

本例创建一个封闭的轻量多段线,然后放多段线绕点(4,4.25,0)旋转45度。

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.SetDatabaseDefaults()
      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
 
      '' 利用基点(4,4.25,0)绕当前 UCS 的Z轴旋转多段线45度    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
           

镜像对象

镜像可以绕着轴或镜像线翻转一个对象。可以镜像所有图形对象。

要镜像对象,请使用一个转换矩阵的 Mirroring 函数。这个函数需要一个 Point3d、Plane 或 Line3d 对象用来定义镜像线。由于镜像是通过转换矩阵来完成的,所以不会创建新对象。如果想操作原始对象,就需要首先创建一个对象的副本然后再镜像它。

要管理 Text 对象的反射特性,请使用 MIRRTEXT 系统变量。MIRRTEXT 的默认设置为开 (1),这将导致 Text 对象像其他对象一样也被镜像。当 MIRRTEXT 设置为关 (0) 时,不会镜像文字。使用 GetSystemVariable 和 SetSystemVariable 方法可以查询和设置 MIRRTEXT 设置。

用户可以在图纸空间中镜像 Viewport 对象,虽然这样做并不会影响其模型空间视图或模型空间对象。

绕轴镜像多段线

本例创建一条优化多段线,然后绕一个轴镜像该多段线。新创建的多段线会着上蓝色。

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.SetDatabaseDefaults()
      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)
 
      '' 在顶点1处创建一个-2的凸度   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)
 
      '' 通过 X 轴镜像多段线   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
           

缩放对象

用户可以通过指定基点和基于当前图形单位的缩放比例来缩放对象。可以缩放所有的图形对象以及属性参考对象。

要缩放对象,使用一个转换矩阵的 Scaling 函数。这个函数需要数组值作为对象的比例因子以及一个 Point3d 对象作为缩放操作的基点。Scaling 函数是在 X、Y 和 Z 方向等比缩放对象。缩放时用对象的尺寸乘以缩放比例。缩放比例大于 1 时将放大对象,缩放比例大于 0 小于 1 时将缩小对象。

注意如果需要非等比缩放图形,就需要使用合适的数据组初始化一个转换矩阵,然后使用对象的 TransformBy 方法。

缩放多段线

本例创建一条闭合的优化多段线,然后以 0.5 的缩放比例及基点(4,4.25,0)调整该多段线。

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.SetDatabaseDefaults()
      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
 
      '' 利用基点(4,4.25,0)及0.5的缩放比例缩小对象    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
           

阵列对象

用户可以创建一个对象的环形或矩形阵列。对象的阵列没有一组专用的函数用于创建阵列,但是可以通过结合复制对象,然后利用转换矩阵旋转或移动复制的对象。下面列出了每种阵列类型的基本要素。

  • 复制用于阵列的对象然后围绕一个基点基于一定角度移动复制的对象。从对象到阵列基点的距离用于计算每一个被创建的对象副本的位置。对象的副本被移动后,就可以从基点根据一定的角度旋转对象。每一个副本创建后,必须把它们追加到块表记录中。
  • 基于指定的行数和列数复制用于阵列的对象。副本对象之间的距离是的基于指定的行和列之间的距离。用户首先应该完成首行或首列原始副本复制份数的创建。首行或首列创建后,再基于首行或首列创建的对象再创建剩余的对象。每一个副本被创建后,都必须把它们追加到块表记录中去。

创建环形阵列

本例创建一个圆,然后对圆执行环形阵列操作。这个过程将围绕基点 (4,4,0),在 180 度内创建四个圆。

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)
 
      ''在点(2,2,0)以半径为1创建一个圆   Create a circle that is at 2,2 with a radius of 1
      Dim acCirc As Circle = New Circle()
      acCirc.SetDatabaseDefaults()
      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)
 
      '' 在180度的圆内使用环形阵列创建4个对象  Create a 4 object polar array that goes a 180
      Dim nCount As Integer = 1
 
      '' 设置一个60度的以弧度表示的值   Set a value in radians for 60 degrees
      Dim dAng As Double = 1.0472
 
      '' 使用点(4,4,0)作为基点进行阵列  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
           

创建矩形阵列

本例创建一个圆,然后对该圆执行矩形阵列操作,创建 5 行 5 列的圆。

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)
 
      '' 在点 2,2 创建一个半径为0.5的圆    Create a circle that is at 2,2 with a radius of 0.5
      Dim acCirc As Circle = New Circle()
      acCirc.SetDatabaseDefaults()
      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)
 
      '' 创建一个5行5列的矩形阵列   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
 
      ''获得当前用户坐标系的X轴的角度   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
 
      '' 使用弧度设置一个90度的值   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
           

延伸和修剪对象

用户可以更改圆弧的角度,也可以改变直线、开放多段线、椭圆弧和开放样条曲线的长度。结果类似于延伸和修剪对象。

用户可以通过编辑对象的特性来延伸或修剪对象。例如,若要拉长直线,只需修改 StartPoint 或 EndPoint 属性的坐标即可。要改变圆弧的角度,请更改圆弧的 StartAngle 或 EndAngle 属性。改变对象的一个和多个特性后,就必须重生成以查看图形窗口中的变化。

延长直线

本例创建一条直线,然后修改其端点拉长该直线。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("ExtendObject")> _
Public Sub ExtendObject()
  '' 获得当前文档和数据库   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)
 
      '' 创建一条起点为(4,4,0)终点为(7,7,0)的直线   Create a line that starts at (4,4,0) and ends at (7,7,0)
      Dim acLine As Line = New Line(New Point3d(4, 4, 0), _
                                    New Point3d(7, 7, 0))
 
      acLine.SetDatabaseDefaults()
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acLine)
      acTrans.AddNewlyCreatedDBObject(acLine, True)
 
      '' 更新显示并显示一个消息框   Update the display and diaplay a message box
      acDoc.Editor.Regen()
      Application.ShowAlertDialog("Before extend")
 
      ''将直线的长度增加到原来的双倍长   Double the length of the line
      acLine.EndPoint = acLine.EndPoint + acLine.Delta
 
      '' 保存新对象到数据库中   Save the new object to the database
      acTrans.Commit()
  End Using
End Sub
           

分解对象

分解对象将对象从单一对象转换为构成该对象的组件。用户可以使用 例如 Explode 函数来分解对象,它需要一个 DBObjectCollection 类型的对象用于接收返回的结果对象。例如,分解一条多段线可能导致产生一个由包含多个直线和圆弧构成的对象集合。

如果一个块被分解了,一个对象集合将会被返回,它保存了定语块的图形对象。对象被分解后,原始对象将不会被改变。如果你想用返回的对象代替原始对象,原始对象必须被删除并且返回的对象必须添加到块表记录中去。

分解多段线

本例创建一个优化多段线对象,然后这条分解多段线成简单对象。多段线被分解后,原多段线将被销毁掉,而返回的对象将添加到模型空间中去。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("ExplodeObject")> _
Public Sub ExplodeObject()
  '' 获得当前文档和数据库   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
      Using acPoly As Polyline = New Polyline()
          acPoly.SetDatabaseDefaults()
          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)
 
          '' 设置索引为3的点的凸度  Sets the bulge at index 3
          acPoly.SetBulgeAt(3, -0.5)
 
          '' 分解多段线   Explodes the polyline
          Dim acDBObjColl As DBObjectCollection = New DBObjectCollection()
          acPoly.Explode(acDBObjColl)
 
          For Each acEnt As Entity In acDBObjColl
              '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
              acBlkTblRec.AppendEntity(acEnt)
              acTrans.AddNewlyCreatedDBObject(acEnt, True)
          Next
 
          '' 销毁内存中的多段线   Dispose of the in memory polyline
      End Using
 
      '' 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit()
  End Using
End Sub
           

编辑多段线

二维和三维多段线、矩形、多边形、圆环和三维多边形网格都是多段线的变化形式而且编辑方式都相同。

AutoCAD 可以识别拟合多段线和样条曲线拟合多段线。样条曲线拟合多段线使用曲线进行拟合,类似于 B 样条曲线。有两种样条曲线拟合多段线:二次和三次。这两种多段线均由 SPLINETYPE 系统变量控制。拟合多段线使用标准曲线来进行曲线拟合,并利用所有在给定顶点上设置的切线方向。

要编辑多段线,请使用 Polyline、 Polyline2d、或 Polyline3d 对象的属性和方法。请使用以下属性和方法开放或闭合多段线、修改多段线顶点的坐标或添加顶点:

  • Closed 属性
  • 开放或闭合多段线。
  • ConstantWidth 属性
  • 为轻量或二维多段线设置恒定的宽度。
  • AppendVertex 方法
  • 为二维或三维多段线添加顶点。
  • AddVertexAt 方法
  • 为轻量多段线添加顶点。
  • ReverseCurve
  • 反转多段线的方向。

使用下列方法更新多段线的凸点或宽度。

  • SetBulgeAt
  • 设置轻量多段线指定索引片段处的凸度。
  • SetStartWidthAt
  • 设置轻量多段线指定索引片段处的起始宽度。
  • Straighten
  • 非曲线化二维或三维多段线。

编辑多段线

本例创建一条优化多段线,然后向多段线的第三段添加凸度,向多段线附加顶点,修改最后一段的宽度,最后闭合多段线。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("EditPolyline")> _
Public Sub EditPolyline()
  '' 获得当前文档和数据库   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.SetDatabaseDefaults()
      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)
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acPoly)
      acTrans.AddNewlyCreatedDBObject(acPoly, True)
 
      '' 设置第三段的凸度  Sets the bulge at index 3
      acPoly.SetBulgeAt(3, -0.5)
 
      ''添加新的顶点  Add a new vertex
      acPoly.AddVertexAt(5, New Point2d(4, 1), 0, 0, 0)
 
      '' 设置第四段的起始和终止宽度  Sets the start and end width at index 4
      acPoly.SetStartWidthAt(4, 0.1)
      acPoly.SetEndWidthAt(4, 0.5)
 
      '' 闭合多段线   Close the polyline
      acPoly.Closed = True
 
      '' 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit()
  End Using
End Sub
           

继续阅读