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.
A basic Centerline Circle DrawableOverrule has been presented previously. It is also demonstrated there registering and unregistering the DrawableOverrule singleton.
…
CenterlineCircle_Gadgets.RegisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule.Instance);
…
…
CenterlineCircle_Gadgets.UnregisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule.Instance);
…
Here are the RegisterOverrule and UnregisterOverrule help methods.
public class CenterlineCircle_Gadgets
{
public const string CLCircle_ID = "CenterlineCircle";
public const double DefaultRatioLengthToRadius = 1.2;
public static void RegisterOverrule(Type type, Overrule overrule)
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
Overrule.AddOverrule(RXClass.GetClass(type), overrule, true);
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.Regen();
}
catch (Autodesk.AutoCAD.Runtime.Exception acadEx)
{
if (acadEx.ErrorStatus == ErrorStatus.DuplicateKey)
ed.WriteMessage("\nThe Overrule has already been registered.");
else
ed.WriteMessage("\n" + acadEx.Message);
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
public static void UnregisterOverrule(Type type, Overrule overrule)
{
Overrule.RemoveOverrule(RXClass.GetClass(type), overrule);
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.Regen();
}
}
Here are some hightlights about the help methods and the Overrule APIs.
• The first argument of the Overrule.AddOverrule API is Type of the RXClass indicating that the Overrule going to be registered will apply to the whole type of the AutoCAD entity by default.
• If we want the Overrule only applies to some entities instead of all of the same Entity kind such as Circle in the case, we need to use some overrule filters. We will demonstrate these in the future.
• The second argument of the Overrule.AddOverrule API is an instance of the Overrule that is going to be registered. It indicates that many instances of the same Overrule can apply to the same AutoCAD entity.
• Registering the same Overrule multiple times may not be as expected most of times. That explains why we make the Centerline Circle Overrule a singleton.
• The Overrule.AddOverrule API accepts a Boolean as the third argument indicating whether to add the Overrule to the end of the Overrule collection.
• If only one Overrule sets the Boolean argument as True, it can be guaranteed to be at the end of the Overrule queue; if every Overrule does so, however, it will be interesting to see which will be registered first and which last.
• Therefore, if a single application has many Overrule classes and the Overrule registration sequence really matters, it is obviously a good idea to register and unregister all the Overrule classes in a central place at the same time.
• The Overrule.RemoveOverrule API accepts the same RXClass type and the same Overrule instance as the arguments to unregister the same Overrule instance from the same Entity type.
• The graphics cache will not be updated automatically when an Overrule is registered or unregistered, so we have to regenerate all entities. Calling the Editor.Regen method makes it.
• If the same Overrule instance is registered onto the same AutoCAD RXClass type however, the Overrule.RemoveOverrule API will throw out an exception with the ErrorStatus saying DuplicateKey.
• We detect the error status and the exception to know whether the Overrule singleton is trying to be registered twice.
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: |