|
Edit Named and 2D Objects 编辑二维命名对象Existing objects can be modified with the methods and properties associated with each object. If you modify a visible property of a graphic object, use the Regen method to redraw the object on screen. The Regen method is a member of the Editor object. 可以使用与对象关联的方法和属性对现有对象进行修改。如果修改了图形对象的可见性属性,记着用Regen方法重画屏幕上的对象。Regen方法是Editor对象的一个成员。
Topics in this section 本节内容 · Work with Named Objects 使用命名对象 · Erase Objects 删除对象 · Copy Objects 复制对象 · Offset Objects 偏移对象 · Transform Objects 变换对象 · Array Objects 阵列对象 · Extend and Trim Objects 延伸和修剪对象 · Explode Objects 分解对象 · Edit Polylines 编辑多段线 · Edit Splines 编辑样条曲线 · Edit Hatches 编辑图案填充
1、Work with Named Objects使用命名对象In addition to the graphical objects used by AutoCAD, there are several types of nongraphical objects stored in a drawing database. These objects have descriptive designations associated with them. For example, blocks, layers, groups, and dimension styles all have a name assigned to them and in most cases can be renamed. The names of symbol table records are displayed in the user interface of AutoCAD, while the object id of an object is used to reference the object in most cases throughout the .NET API. AutoCAD除了使用图形对象外,还有许多非图形对象类型存储在图形数据库中。这些对象都有与之相关联的描述性名称。例如,块、图层、编组以及标注样式等,所有这些对象都被赋予一个名称,并且多数情况下可以对其重命名。符号表记录的名称直接显示在AutoCAD用户界面上,而多数情况下通过.NET API编程引用对象时使用的是对象的标识Id。 For example, the object id of a LayerTableRecord object is assigned to a graphical object’s Layer property and not the actual name that is assigned to the LayerTableRecord. However, the object id of a LayerTableRecord object can be obtained from the Layer table using the name of the layer you want to access. 例如,某图形对象的图层属性获得的赋值是LayerTableRecord对象的标识id,而不是LayerTableRecord对象的实际名称。不过,LayerTableRecord对象的标识id可以用所访问图层的名称从Layer表获得。
Topics in this section 本小节内容 · Purge Unreferenced Named Objects 清理未引用的命名对象 · Rename Objects 重命名对象
1.1、Purge Unreferenced Named Objects清理未引用的命名对象Unreferenced named objects can be purged from a database at any time. You cannot purge named objects that are referenced by other objects. For example, a font file might be referenced by a text style or a layer might be referenced by the objects on that layer. Purging reduces the size of a drawing file when saved to disk. 可以随时将未引用的对象从数据库中清理出去。不可以清理被其他对象引用的对象。例如,某字体文件可能正由文字样式引用,或者,某图层正由该层上的对象引用。清理工作可以减小图形文件存盘时的大小。 Unreferenced objects are purged from a drawing database with the Purge method. The Purge method requires a list of objects you want to purge in the form of an ObjectIdCollection or ObjectIdGraph objects. The ObjectIdCollection or ObjectIdGraph objects passed into the Purge method are updated with the objects in which can be erased from the database. After the call to Purge, you must erase each individual object returned. 使用Puege方法从图形数据库中清除未引用对象。Purge方法需要一个包含要清除的对象的列表,列表为ObjectIdCollection对象或ObjectIdGraph对象。传入Purge方法的ObjectIdCollection对象或ObjectIdGraph对象会更新为只包含要从数据库中删除的对象。调用Purge方法后,必须显式地删除返回的每个对象。 VBA/ActiveX Cross Reference VBA/ActiveX交叉参考 In the ActiveX Automation library you would use the PurgeAll method to remove all unreferenced named objects and it would identify which objects can be removed. However, with the .NET API you need to supply which objects you would like to purge and then the Purge method returns to you which ones actually can be. So there is a bit more work involved when working with the .NET API to purge all unreferenced named objects from a database. 在ActiveX Automation库中,我们使用PurgeAll方法删除所有未被引用的命名对象,而且该方法能够识别哪个对象可以删除。可是,使用.NET API我们需提供要清除的对象给Purge方法,然后Purge方法会返回实际可以删除的对象。可见,使用.NET API从数据库中清除所有未引用的命名对象时,相对繁琐一些。 ThisDrawing.PurgeAll Purge all unreferenced layers 清除所有未引用的图层
The following example demonstrates how to purge all unreferenced layers from a database. 下面这个例子演示怎样从数据库清除所有未引用的图层。 VB.NETImports Autodesk.AutoCAD.Runtime - <div align="left"><strong></strong></div><div align="left">Imports Autodesk.AutoCAD.ApplicationServices</div><div align="left">Imports Autodesk.AutoCAD.DatabaseServices</div><div align="left">
- </div><div align="left"><CommandMethod("<img src="http://bimcad.org/static/image/smiley/default/titter.gif" smilieid="9" border="0" alt="">urgeUnreferencedLayers")> _</div><div align="left">Public Sub PurgeUnreferencedLayers()</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">
- </div><div align="left"> '' Start a transaction</div><div align="left"> Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div><div align="left">
- </div><div align="left"> '' Open the Layer table for read</div><div align="left"> Dim acLyrTbl As LayerTable</div><div align="left"> acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _</div><div align="left"> OpenMode.ForRead)</div><div align="left">
- </div><div align="left"> '' Create an ObjectIdCollection to hold the object ids for each table record</div><div align="left"> Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()</div><div align="left">
- </div><div align="left"> '' Step through each layer and add it to the ObjectIdCollection</div><div align="left"> For Each acObjId As ObjectId In acLyrTbl</div><div align="left"> acObjIdColl.Add(acObjId)</div><div align="left"> Next</div><div align="left">
- </div><div align="left"> '' Remove the layers that are in use and return the ones that can be erased</div><div align="left"> acCurDb.Purge(acObjIdColl)</div><div align="left">
- </div><div align="left"> '' Step through the returned ObjectIdCollection</div><div align="left"> For Each acObjId As ObjectId In acObjIdColl</div><div align="left"> Dim acSymTblRec As SymbolTableRecord</div><div align="left"> acSymTblRec = acTrans.GetObject(acObjId, _</div><div align="left"> OpenMode.ForWrite)</div><div align="left">
- </div><div align="left"> Try</div><div align="left"> '' Erase the unreferenced layer</div><div align="left"> acSymTblRec.Erase(True)</div><div align="left"> Catch Ex As Autodesk.AutoCAD.Runtime.Exception</div><div align="left"> '' Layer could not be deleted</div><div align="left"> Application.ShowAlertDialog("Error:" & vbLf & Ex.Message)</div><div align="left"> End Try</div><div align="left"> Next</div><div align="left">
- </div><div align="left"> '' Commit the changes and dispose of the transaction</div><div align="left"> acTrans.Commit()</div><div align="left"> End Using</div><div align="left">End Sub</div>
复制代码C#using Autodesk.AutoCAD.Runtime; - <div align="left"><strong></strong></div><div align="left">using Autodesk.AutoCAD.ApplicationServices;</div><div align="left">using Autodesk.AutoCAD.DatabaseServices;</div><div align="left">
- </div><div align="left">[CommandMethod("<img src="http://bimcad.org/static/image/smiley/default/titter.gif" smilieid="9" border="0" alt="">urgeUnreferencedLayers")]</div><div align="left">public static void PurgeUnreferencedLayers()</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">
- </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 Layer table for read以读打开Layer表</div><div align="left"> LayerTable acLyrTbl;</div><div align="left"> acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,</div><div align="left"> OpenMode.ForRead) as LayerTable;</div><div align="left">
- </div><div align="left"> // Create an ObjectIdCollection to hold the object ids for each table record</div><div align="left"> // 创建ObjectIdCollection用来存放每条表记录的对象id;</div><div align="left"> ObjectIdCollection acObjIdColl = new ObjectIdCollection();</div><div align="left">
- </div><div align="left"> // Step through each layer and add iterator to the ObjectIdCollection</div><div align="left"> // 遍历图层并将图层id添加到ObjectIdCollection</div><div align="left"> foreach (ObjectId acObjId in acLyrTbl)</div><div align="left"> {</div><div align="left"> acObjIdColl.Add(acObjId);</div><div align="left"> }</div><div align="left">
- </div><div align="left"> // Remove the layers that are in use and return the ones that can be erased</div><div align="left"> // 调用Purge方法:传入参数 - ObjectIdCollection对象;</div><div align="left"> // 返回 – 更新了的ObjectIdCollection对象,包含可以删除的图层;</div><div align="left"> acCurDb.Purge(acObjIdColl);</div><div align="left">
- </div><div align="left"> // Step through the returned ObjectIdCollection</div><div align="left"> // and erase each unreferenced layer</div><div align="left"> // 遍历返回的ObjectIdCollection对象,并删除未引用的图层;</div><div align="left"> foreach (ObjectId acObjId in acObjIdColl)</div><div align="left"> {</div><div align="left"> SymbolTableRecord acSymTblRec;</div><div align="left"> acSymTblRec = acTrans.GetObject(acObjId,</div><div align="left"> OpenMode.ForWrite) as SymbolTableRecord;</div><div align="left">
- </div><div align="left"> try</div><div align="left"> {</div><div align="left"> // Erase the unreferenced layer删除未引用的图层</div><div align="left"> acSymTblRec.Erase(true);</div><div align="left"> }</div><div align="left"> catch (Autodesk.AutoCAD.Runtime.Exception Ex)</div><div align="left"> {</div><div align="left"> // Layer could not be deleted图层不能删除</div><div align="left"> Application.ShowAlertDialog("Error:\n" + Ex.Message);</div><div align="left"> }</div><div align="left"> }</div><div align="left">
- </div><div align="left"> // Commit the changes and dispose of the transaction</div><div align="left"> // 提交修改,关闭事务</div><div align="left"> acTrans.Commit();</div><div align="left"> }</div><div align="left">}</div>
复制代码
1.2、Rename Objects重命名对象As your drawings become more complex, you can rename objects to keep the names meaningful or to avoid conflicts with names in other drawings you have inserted or attached. The Name property is used to get the current name or change the name of a named object. 随着图形越来越复杂,我们可以给对象重新命名,以使对象名称有意义,或避免与所插入或附加的其他图形中的对象名发生冲突。Name属性用来获取命名对象的当前名称,也可用来修改命名对象的名称。 You can rename any named object except those reserved by AutoCAD, for example, layer 0 or the CONTINUOUS linetype. 我们可以对任何命名对象重命名,AutoCAD保留的除外,如图层0或CONTINUOUS线型。 Names can be up to 255 characters long. In addition to letters and numbers, names can contain spaces (although AutoCAD removes spaces that appear directly before and after a name) and any special character not used by Microsoft® Windows® or AutoCAD for other purposes. Special characters that you cannot use include less-than and greater-than symbols (< >), forward slashes and backslashes (/ \), quotation marks ("), colons (undefined, semicolons (;), question marks (?), commas (,), asterisks (*), vertical bars (|), equal signs (=), and single quotes ('). You also cannot use special characters created with Unicode fonts. 名称最长可以到255个字符,除字母和数字外,还可以包含空格(尽管AutoCAD会删除直接出现在名称前后的空格)以及Microsoft® Windows® 或AutoCAD没有使用的其他特殊字符。不能用于对象名称的特殊字符包括大于号和小于号(< >)、前后斜杠(/ \)、引号(”)、冒号(:)、分号(;)、问号(?)、逗号(,)、星号(*)、竖杠(|)、等号(=)及单引号(’)。Unicode字体创建的特殊字符也不能用。 Rename a layer 重命名图层
This example creates a copy of layer "0" and renames the new layer to “MyLayer”. 本例创建图层0的一个副本并命名为MyLayer。 VB.NETImports Autodesk.AutoCAD.Runtime - <div align="left"><strong></strong></div><div align="left">Imports Autodesk.AutoCAD.ApplicationServices</div><div align="left">Imports Autodesk.AutoCAD.DatabaseServices</div><div align="left">
- </div><div align="left"><CommandMethod("RenameLayer")> _</div><div align="left">Public Sub RenameLayer()</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">
- </div><div align="left"> '' Start a transaction</div><div align="left"> Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div><div align="left">
- </div><div align="left"> '' Returns the layer table for the current database</div><div align="left"> Dim acLyrTbl As LayerTable</div><div align="left"> acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _</div><div align="left"> OpenMode.ForWrite)</div><div align="left">
- </div><div align="left"> '' Clone layer 0 (copy it and its properties) as a new layer</div><div align="left"> Dim acLyrTblRec As LayerTableRecord</div><div align="left"> acLyrTblRec = acTrans.GetObject(acLyrTbl("0"), _</div><div align="left"> OpenMode.ForRead).Clone()</div><div align="left">
- </div><div align="left"> '' Change the name of the cloned layer</div><div align="left"> acLyrTblRec.Name = "MyLayer"</div><div align="left">
- </div><div align="left"> '' Add the cloned layer to the Layer table and transaction</div><div align="left"> acLyrTbl.Add(acLyrTblRec)</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)</div><div align="left">
- </div><div align="left"> '' Save changes and dispose of the transaction</div><div align="left"> acTrans.Commit()</div><div align="left"> End Using</div><div align="left">End Sub</div>
复制代码C#using Autodesk.AutoCAD.Runtime; - <div align="left"><strong></strong></div><div align="left">using Autodesk.AutoCAD.ApplicationServices;</div><div align="left">using Autodesk.AutoCAD.DatabaseServices;</div><div align="left">
- </div><div align="left">[CommandMethod("RenameLayer")]</div><div align="left">public static void RenameLayer()</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">
- </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"> // Returns the layer table for the current database</div><div align="left"> // 返回当前数据库的图层表</div><div align="left"> LayerTable acLyrTbl;</div><div align="left"> acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,</div><div align="left"> OpenMode.ForWrite) as LayerTable;</div><div align="left">
- </div><div align="left"> // Clone layer 0 (copy it and its properties) as a new layer</div><div align="left"> // 克隆图层0(复制其及其属性)</div><div align="left"> LayerTableRecord acLyrTblRec;</div><div align="left"> acLyrTblRec = acTrans.GetObject(acLyrTbl["0"],</div><div align="left"> OpenMode.ForRead).Clone() as LayerTableRecord;</div><div align="left">
- </div><div align="left"> // Change the name of the cloned layer</div><div align="left"> // 修改克隆得到的图层的名称</div><div align="left"> acLyrTblRec.Name = "MyLayer";</div><div align="left">
- </div><div align="left"> // Add the cloned layer to the Layer table and transaction</div><div align="left"> // 使用图层MyLayer可用</div><div align="left"> acLyrTbl.Add(acLyrTblRec);</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);</div><div align="left">
- </div><div align="left"> // Save changes and dispose of the transaction</div><div align="left"> // 保存修改,关闭事务</div><div align="left"> acTrans.Commit();</div><div align="left"> }</div><div align="left">}</div>
复制代码
2、Erase Objects删除对象You can delete non-graphical and graphical objects with the Erase method. 我们可以使用Erase方法删除非图形对象及图形对象。 WarningWhile many non-graphical objects, such as the Layer table and Model space block table records have an Erase method, it should not be called. If Erase is called on one of these objects, an error will occur. 警告:尽管许多非图形对象,如Layer表及模型空间块表记录等,都拥有Erase方法,但不能对这些对象调用该方法,否则会发生错误。 Create and erase a polyline 创建一个多段线并删除它
This example creates a lightweight polyline, then erases it. 本例创建一个轻量级多段线,然后将它删除。 VB.NETImports Autodesk.AutoCAD.Runtime - <div align="left"><strong></strong></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">
- </div><div align="left"><CommandMethod("EraseObject")> _</div><div align="left">Public Sub EraseObject()</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">
- </div><div align="left"> '' Start a transaction</div><div align="left"> Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div><div align="left">
- </div><div align="left"> '' Open the Block table for read</div><div align="left"> Dim acBlkTbl As BlockTable</div><div align="left"> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)</div><div align="left">
- </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">
- </div><div align="left"> '' Create a lightweight polyline</div><div align="left"> Dim acPoly As Polyline = New Polyline()</div><div align="left"> acPoly.AddVertexAt(0, New Point2d(2, 4), 0, 0, 0)</div><div align="left"> acPoly.AddVertexAt(1, New Point2d(4, 2), 0, 0, 0)</div><div align="left"> acPoly.AddVertexAt(2, New Point2d(6, 4), 0, 0, 0)</div><div align="left">
- </div><div align="left"> '' Add the new object to the block table record and the transaction</div><div align="left"> acBlkTblRec.AppendEntity(acPoly)</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acPoly, True)</div><div align="left">
- </div><div align="left"> '' Update the display and display an alert message</div><div align="left"> acDoc.Editor.Regen()</div><div align="left"> Application.ShowAlertDialog("Erase the newly added polyline.")</div><div align="left">
- </div><div align="left"> '' Erase the polyline from the drawing</div><div align="left"> acPoly.Erase(True)</div><div align="left">
- </div><div align="left"> '' Save the new object to the database</div><div align="left"> acTrans.Commit()</div><div align="left"> End Using</div><div align="left">End Sub</div>
复制代码C#using Autodesk.AutoCAD.Runtime; - <div align="left"><strong></strong></div><div align="left">using Autodesk.AutoCAD.ApplicationServices;</div><div align="left">using Autodesk.AutoCAD.DatabaseServices;</div><div align="left">
- </div><div align="left">[CommandMethod("EraseObject")]</div><div align="left">public static void EraseObject()</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">
- </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">
- </div><div align="left"> // Open the Block table record Model space for write</div><div align="left"> // 以写打开块表记录模型空间</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><div align="left">
- </div><div align="left"> // Create a lightweight polyline创建轻量级多段线</div><div align="left"> Polyline acPoly = new Polyline();</div><div align="left"> acPoly.AddVertexAt(0, new Point2d(2, 4), 0, 0, 0);</div><div align="left"> acPoly.AddVertexAt(1, new Point2d(4, 2), 0, 0, 0);</div><div align="left"> acPoly.AddVertexAt(2, new Point2d(6, 4), 0, 0, 0);</div><div align="left">
- </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(acPoly);</div><div align="left"> acTrans.AddNewlyCreatedDBObject(acPoly, true);</div><div align="left">
- </div><div align="left"> // Update the display and display an alert message</div><div align="left"> // 更新显示并显示一条告警消息</div><div align="left"> acDoc.Editor.Regen();</div><div align="left"> Application.ShowAlertDialog("Erase the newly added polyline.");</div><div align="left">
- </div><div align="left"> // Erase the polyline from the drawing</div><div align="left"> // 从图形中删除多段线</div><div align="left"> acPoly.Erase(true);</div><div align="left">
- </div><div align="left"> // Save the new object to the database</div><div align="left"> // 提交修改</div><div align="left"> acTrans.Commit();</div><div align="left"> }</div><div align="left">}</div>
复制代码VBA/ActiveX Code ReferenceSub EraseObject() - <div align="left"><strong></strong></div><div align="left"> ' Create the polyline</div><div align="left"> Dim lwpolyObj As AcadLWPolyline</div><div align="left"> Dim vertices(0 To 5) As Double</div><div align="left"> vertices(0) = 2: vertices(1) = 4</div><div align="left"> vertices(2) = 4: vertices(3) = 2</div><div align="left"> vertices(4) = 6: vertices(5) = 4</div><div align="left"> Set lwpolyObj = ThisDrawing.ModelSpace. _</div><div align="left"> AddLightWeightPolyline(vertices)</div><div align="left"> ZoomAll</div><div align="left"> ' Erase the polyline</div><div align="left"> lwpolyObj.Delete</div><div align="left"> ThisDrawing.Regen acActiveViewport</div><div align="left">End Sub</div>
复制代码 |
|
|