清理未引用的命名对象
 
 

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.

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

VBA/ActiveX 交叉引用

清除所有未引用的图层

The following example demonstrates how to purge all unreferenced layers from a database.

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

VB.NET

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

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("PurgeUnreferencedLayers")]
public static void PurgeUnreferencedLayers()
{
  // 获得当前文档和数据库   Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // 启动一个事务  Start a transaction
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // 以只读方式打开图层表   Open the Layer table for read
      LayerTable acLyrTbl;
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                   OpenMode.ForRead) as LayerTable;
 
      // 创建一个 ObjectIdCollection 用于保存每一个表记录的 ObjectID    Create an ObjectIdCollection to hold the object ids for each table record
      ObjectIdCollection acObjIdColl = new ObjectIdCollection();
 
      // Step through each layer and add iterator to the ObjectIdCollection
      foreach (ObjectId acObjId in acLyrTbl)
      {
          acObjIdColl.Add(acObjId);
      }
 
      // 移除使用中的图层并返回一个可以被删除的图层集合   Remove the layers that are in use and return the ones that can be erased
      acCurDb.Purge(acObjIdColl);
 
      // 遍历返回的 ObjectIdCollection     Step through the returned ObjectIdCollection
      // and erase each unreferenced layer
      foreach (ObjectId acObjId in acObjIdColl)
      {
          SymbolTableRecord acSymTblRec;
          acSymTblRec = acTrans.GetObject(acObjId,
                                          OpenMode.ForWrite) as SymbolTableRecord;
 
          try
          {
              // 删除未引用的图层     Erase the unreferenced layer
              acSymTblRec.Erase(true);
          }
          catch (Autodesk.AutoCAD.Runtime.Exception Ex)
          {
              // 图层不能被删除    Layer could not be deleted
              Application.ShowAlertDialog("Error:\n" + Ex.Message);
          }
      }
 
      // 提交修改并销毁事务  Commit the changes and dispose of the transaction
      acTrans.Commit();
  }
}