PurgeAll command:
using System;
using System.Text;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
[assembly: ExtensionApplication(typeof(cgabriel.PurgeTools))]
[assembly: CommandClass(typeof(cgabriel.PurgeTools))]
namespace cgabriel
{
public class PurgeTools : IExtensionApplication
{
public void Initialize() { }
public void Terminate() { }
public static bool purgeSymbolTables(Database db, ObjectIdCollection tableIds, bool silent)
{
bool itemsPurged = false;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectIdCollection purgeableIds = new ObjectIdCollection();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId tableId in tableIds)
{
SymbolTable table = (SymbolTable)tr.GetObject(tableId, OpenMode.ForRead, false);
foreach (ObjectId recordId in table)
purgeableIds.Add(recordId);
}
db.Purge(purgeableIds);
if (purgeableIds.Count == 0) return false;
itemsPurged = true;
foreach (ObjectId id in purgeableIds)
{
try
{
SymbolTableRecord record = (SymbolTableRecord)tr.GetObject(id, OpenMode.ForWrite);
string recordName = record.Name;
record.Erase();
if (!silent)
{
if (!recordName.Contains("|"))
{
ed.WriteMessage("\nPurging " + record.GetType().Name + " " + recordName);
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
if ((e.ErrorStatus == ErrorStatus.CannotBeErasedByCaller) || (e.ErrorStatus == (ErrorStatus)20072))
itemsPurged = false;
else
throw e;
}
}
tr.Commit();
}
return itemsPurged;
}
public static bool purgeDictionaries(Database db, ObjectIdCollection dictIds, bool silent)
{
bool itemsPurged = false;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectIdCollection purgeableIds = new ObjectIdCollection();
using ( Transaction tr = db.TransactionManager.StartTransaction() )
{
foreach (ObjectId dictId in dictIds)
{
DBDictionary dict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForRead, false);
foreach (DBDictionaryEntry entry in dict)
{
purgeableIds.Add(entry.m_value);
}
}
db.Purge(purgeableIds);
if (purgeableIds.Count == 0) return false;
itemsPurged = true;
DBDictionary nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
foreach (ObjectId id in purgeableIds)
{
try
{
DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForWrite);
obj.Erase();
if (!silent)
{
foreach (ObjectId dictId in dictIds)
{
DBDictionary dict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForRead, false);
string dictName = nod.NameAt(dictId);
if (dict.Contains(id))
{
ed.WriteMessage("\nPurging " + dict.NameAt(id) + " from " + dictName);
break;
}
}
}
}
catch(Autodesk.AutoCAD.Runtime.Exception e)
{
if ((e.ErrorStatus == ErrorStatus.CannotBeErasedByCaller) || (e.ErrorStatus == (ErrorStatus)20072))
itemsPurged = false;
else
throw e;
}
}
tr.Commit();
}
return itemsPurged;
}
public static void purgeAll(Database db, bool silent)
{
ObjectIdCollection tableIds = new ObjectIdCollection();
tableIds.Add(db.BlockTableId);
tableIds.Add(db.DimStyleTableId);
tableIds.Add(db.LayerTableId);
tableIds.Add(db.LinetypeTableId);
tableIds.Add(db.RegAppTableId);