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.
In this post, let’s see if we can call WorldDraw of AutoCAD in-memory entities in the WorldDraw of the Overrule. Here it is.
#region Namespaces
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcWindowsNS = Autodesk.AutoCAD.Windows;
#endregion
using Autodesk.AutoCAD.GraphicsInterface;
namespace AcadNetAddinWizard_Namespace
{
/// <summary>
/// Features:
/// A derivative from the DrawableOverrule;
/// Being made a singleton to avoid duplicate registrations;
/// UCS being hornored;
/// Adding center lines to all cirlces;
/// Line.WorldDraw() being used to draw the lines;
/// Drawing the center lines with a particular color (Cyan with index 4);
/// </summary>
public class CenterlineCircle_DrawableOverrule3 : DrawableOverrule
{
protected CenterlineCircle_DrawableOverrule3()
{
}
protected static CenterlineCircle_DrawableOverrule3 mInstance;
public static CenterlineCircle_DrawableOverrule3 Instance
{
get
{
if (mInstance == null)
mInstance = new CenterlineCircle_DrawableOverrule3();
return mInstance;
}
set
{
mInstance = value;
}
}
public override bool WorldDraw(Drawable drawable, WorldDraw wd)
{
if (drawable is Circle)
{
Matrix3d ucs = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
Circle cir = drawable as Circle;
Point3d cen = cir.Center.TransformBy(ucs.Inverse()); ;
double rad = cir.Radius;
Point3d line1Pt1 = new Point3d(cen.X - rad * 1.2, cen.Y, cen.Z).TransformBy(ucs);
Point3d line1Pt2 = new Point3d(cen.X + rad * 1.2, cen.Y, cen.Z).TransformBy(ucs);
Point3d line2Pt1 = new Point3d(cen.X, cen.Y - rad * 1.2, cen.Z).TransformBy(ucs);
Point3d line2Pt2 = new Point3d(cen.X, cen.Y + rad * 1.2, cen.Z).TransformBy(ucs);
short backupColor = wd.SubEntityTraits.Color;
wd.SubEntityTraits.Color = 4;
using (Line line = new Line(line1Pt1, line1Pt2))
{
line.WorldDraw(wd);
}
using (Line line = new Line(line2Pt1, line2Pt2))
{
line.WorldDraw(wd);
}
wd.SubEntityTraits.Color = backupColor;
}
return base.WorldDraw(drawable, wd);
}
}
}
Here are the commands to show or hide centerlines for circles.
[CommandMethod("ShowCircleCenterline3")]
public static void ShowCircleCenterline3_Method()
{
CenterlineCircle_Gadgets.RegisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule3.Instance);
}
[CommandMethod("HideCircleCenterline3")]
public static void HideCircleCenterline3_Method()
{
CenterlineCircle_Gadgets.UnregisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule3.Instance);
}
After the centerline circle overrule assembly is loaded into AutoCAD and the ShowCircleCenterline3 command is run, all regular circles will become centerline circles automatically and properly.
Some highlights may look good to help understand the centerline circle overrule.
• The centerline circle overrule is derived from the DrawableOverrule API class.
• The centerline circle overrule is made a singleton to avoid duplicate registrations or similar issues.
• The centerline circle overrule works with both UCS and WCS.
• The WorldDraw of the centerline circle overrule has been overridden and the Line.WorldDraw methods are called directly to draw the two lines rather than forward the graphic generation to the Geometry.WorldLine of the WorldDraw of the Overrule itself.
• The centerline circle overrule draws the centerlines with a hard coded color (Cyan with index 4) through setting the WorldDraw.SubEntityTraits.Color property.
• The WorldDraw.SubEntityTraits.Color call applies to the Line.WorldDraw calls as well.
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: |