AutoCAD has provided the Overrule .NET API in recent versions. It is the most powerful .NET API so far and can provide almost the custom entity feature that is still only available in the most powerful AutoCAD API, ObjectARX. On the other hand, however, the Overrule API is also the most complex part in the AutoCAD .NET.
Centerline Circle is a common drawing element in the engineering design world but is not provided by AutoCAD natively. Though AutoCAD has some means to control how to show the circle center point, it is far from enough. We presented the centerline circle overrule implementations in some earlier posts.
Previously we added the extension dictionary fiter to the center line circles. Here are the two help methods to get or set the Xrecord in the extension dictionary to indicate that the circle will behave as the center line one and to remove the Xrecord from the extension dictionary of the circle.
/// <summary>
/// Retrieve or create an Entry with the given name into the Extension Dictionary of the passed-in object.
/// </summary>
/// <param name="id">The object hosting the Extension Dictionary</param>
/// <param name="entryName">The name of the dictionary entry to get or set</param>
/// <returns>The ObjectId of the diction entry, old or new</returns>
public static ObjectId GetSetExtensionDictionaryEntry(ObjectId id, string entryName)
{
ObjectId ret = ObjectId.Null;
using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead);
if (obj.ExtensionDictionary == ObjectId.Null)
{
obj.UpgradeOpen();
obj.CreateExtensionDictionary();
obj.DowngradeOpen();
}
DBDictionary dict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForRead);
if (!dict.Contains(entryName))
{
Xrecord xRecord = new Xrecord();
dict.UpgradeOpen();
dict.SetAt(entryName, xRecord);
tr.AddNewlyCreatedDBObject(xRecord, true);
ret = xRecord.ObjectId;
}
else
ret = dict.GetAt(entryName);
tr.Commit();
}
return ret;
}
/// <summary>
/// Remove the named Dictionary Entry from the passed-in object if applicable.
/// </summary>
/// <param name="id">The object hosting the Extension Dictionary</param>
/// <param name="entryName">The name of the Dictionary Entry to remove</param>
/// <returns>True if really removed, false if not there at all</returns>
public static bool RemoveExtensionDictionaryEntry(ObjectId id, string entryName)
{
bool ret = false;
using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead);
if (obj.ExtensionDictionary != ObjectId.Null)
{
DBDictionary dict = (DBDictionary)tr.GetObject(obj.ExtensionDictionary, OpenMode.ForRead);
if (dict.Contains(entryName))
{
dict.UpgradeOpen();
dict.Remove(entryName);
ret = true;
}
}
tr.Commit();
}
return ret;
}
Some highlights may look good to help understand the code.
• The ExtensionDictionary property of the DBObject indicates whether it has an extension dictionary or not (with null value in C# and Nothing in VB.NET)
• The CreateExtensionDictionary method of the DBObject can create the extension dictionary.
• The UpgradeOpen and the DowngradeOpen of the DBObject can upgrade it from READ to WRITE and downgrade from WRITE to READ.
• The DBDictionary (representing any dictionary objects including the extension dictionary) can have many Xrecord entries.
• The DBDictionary.SetAt method can create such an Xrecord entry.
• The DBDictionary.GetAt method can retrieve such an Xrecord entry.
• The DBDictionary.Remove method can remove such an Xrecord entry.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders including a Ribbon Creator, and widgets to help program AutoCAD .NET addins.
Posted by: |