参照されていない名前の付いたオブジェクトは、いつでもデータベースから削除できます。他のオブジェクトから参照されている名前の付いたオブジェクトは削除できません。たとえば、フォント ファイルが文字スタイルによって参照されている場合や、画層がその画層上のオブジェクトによって参照されている場合があります。削除することによって、ディスクに保存したときの図面ファイルのサイズが小さくなります。
参照されていないオブジェクトは、Purge メソッドを使用して図面データベースから削除されます。Purge メソッドは、削除するオブジェクトの一覧を、ObjectIdCollection または ObjectIdGraph オブジェクトの形式で必要とします。Purge メソッドに渡される ObjectIdCollection または ObjectIdGraph オブジェクトは、データベースから消去できるオブジェクトによって更新されます。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)
'' Create an ObjectIdCollection to hold the object ids for each table record
Dim acObjIdColl As ObjectIdCollection = New 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)
'' 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
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;
// 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);
// 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();
}
}
ActiveX オートメーション ライブラリでは、PurgeAll メソッドを使用して参照されていない名前の付いたオブジェクトをすべて削除します。このメソッドは、削除できるオブジェクトを識別します。一方、AutoCAD .NET API では、削除するオブジェクトを指定する必要があり、Purge メソッドは実際に削除できるオブジェクトを返します。したがって、AutoCAD .NET API を使用して、参照されていない名前の付いたオブジェクトをすべてデータベースから削除する場合、必要な作業が多くなります。
ThisDrawing.PurgeAll