using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Linq;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly
: CommandClass
(typeof(SetAllLayersUnpurgeable
.Commands))]
namespace SetAllLayersUnpurgeable
{
public class Commands
{
[CommandMethod("Cmd")]
public void SetAllLayersUnpurgeableCmd()
{
var db = HostApplicationServices.WorkingDatabase;
using (var tr = db.TransactionManager.StartTransaction())
{
var layerTable = db.LayerTableId.GetObject<LayerTable>();
foreach (ObjectId layerId in layerTable)
{
if (layerId != db.LayerZero)
{
layerId.GetObject<LayerTableRecord>().SetUnpurgeable();
}
}
tr.Commit();
}
}
}
public static class Extensions
{
// Opens the object for read.
public static T GetObject<T>(this ObjectId id) where T : DBObject
{
return (T)id.GetObject(OpenMode.ForRead, false, false);
}
// Opens the object in the specified mode
public static T GetObject<T>(this ObjectId id, OpenMode mode) where T : DBObject
{
return (T)id.GetObject(mode, false, false);
}
// Gets the object extension dictionary (returns null if not found)
public static DBDictionary GetExtensionDictionary(this DBObject obj)
{
ObjectId dictId = obj.ExtensionDictionary;
if (dictId == ObjectId.Null)
{
return null;
}
return dictId.GetObject<DBDictionary>();
}
// Gets the object extension dictionary (creates it if not already exists)
public static DBDictionary GetOrCreateExtensionDictionary(this DBObject obj)
{
if (obj.ExtensionDictionary == ObjectId.Null)
{
obj.UpgradeOpen();
obj.CreateExtensionDictionary();
}
return obj.ExtensionDictionary.GetObject<DBDictionary>();
}
// Gets the xrecord data
public static ResultBuffer GetXrecordData(this DBDictionary dict, string key)
{
if (!dict.Contains(key)) return null;
ObjectId id = (ObjectId)dict[key];
if (id
.ObjectClass != RXObject
.GetClass(typeof(Xrecord
))) return null; return id.GetObject<Xrecord>().Data;
}
// Sets the xrecord data
public static void SetXrecordData(this DBDictionary dict, string key, ResultBuffer data)
{
Xrecord xrec;
if (dict.Contains(key))
{
xrec = ((ObjectId)dict[key]).GetObject<Xrecord>(OpenMode.ForWrite);
}
else
{
dict.UpgradeOpen();
dict.SetAt(key, xrec);
dict.Database.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(xrec, true);
}
xrec.Data = data;
}
// Sets the symbol table record unpurgeable by referencing it in its symbol table extension dictionary
public static void SetUnpurgeable(this SymbolTableRecord str)
{
SymbolTable st = str.OwnerId.GetObject<SymbolTable>();
DBDictionary xdict = st.GetOrCreateExtensionDictionary();
ResultBuffer data
= xdict
.GetXrecordData("GILE_UNPURGE") ?? new ResultBuffer
(); data
.Add(new TypedValue
(340, str
.ObjectId)); xdict.SetXrecordData("GILE_UNPURGE", data);
}
// Resets the symbol table record purgeable by unreferencing it in its symbol table extension dictionary
public static void ResetPurgeable(this SymbolTableRecord str)
{
SymbolTable st = str.OwnerId.GetObject<SymbolTable>();
DBDictionary xdict = st.GetExtensionDictionary();
if (xdict == null) return;
ResultBuffer data = xdict.GetXrecordData("GILE_UNPURGE");
if (data == null) return;
var values = data.AsArray()
.Where(tv => tv.TypeCode != 340 || (ObjectId)tv.Value != st.ObjectId)
.ToArray();
xdict
.SetXrecordData("GILE_UNPURGE",
new ResultBuffer
(values
)); }
}
}