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 implementation in some earlier posts. Let’s contine to enhance the centerline circle in this post, making centerlines support viewport dependant graphics.
Here is the enhanced version of the Centerline Circle.
#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;
/// ViewportDraw being overridden to draw view dependant graphics;
/// </summary>
public class CenterlineCircle_DrawableOverrule2 : DrawableOverrule
{
protected CenterlineCircle_DrawableOverrule2()
{
}
protected static CenterlineCircle_DrawableOverrule2 mInstance;
public static CenterlineCircle_DrawableOverrule2 Instance
{
get
{
if (mInstance == null)
mInstance = new CenterlineCircle_DrawableOverrule2();
return mInstance;
}
set
{
mInstance = value;
}
}
public override bool WorldDraw(Drawable drawable, WorldDraw wd)
{
base.WorldDraw(drawable, wd);
return false;
}
public override void ViewportDraw(Drawable drawable, ViewportDraw vd)
{
if (drawable is Circle)
{
Matrix3d ucs = vd.Viewport.EyeToWorldTransform;
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);
vd.Geometry.WorldLine(line1Pt1, line1Pt2);
vd.Geometry.WorldLine(line2Pt1, line2Pt2);
}
base.ViewportDraw(drawable, vd);
}
}
}
Here are the commands to show or hide centerlines for circles.
[CommandMethod("ShowCircleCenterline2")]
public static void ShowCircleCenterline2_Method()
{
CenterlineCircle_Gadgets.RegisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule2.Instance);
}
[CommandMethod("HideCircleCenterline2")]
public static void HideCircleCenterline2_Method()
{
CenterlineCircle_Gadgets.UnregisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule2.Instance);
}
After the centerline circle overrule assembly is loaded into AutoCAD and the ShowCircleCenterline1 command is run, all regular circles will become centerline circles automatically with the centerlines looking real CENTER.
If the WorldDraw is implemented but the ViewportDraw not in the Overrule, as was done previously, the centerlines of circles will only honor the view direction of the UCS/WCS of the model/world view. In each viewport of layouts, the centerlines may not be parallel to the layout coordinate system.
If the ViewportDraw is implemented instead in the Overrule, as was done here, the centerlines of circles will not only honor the view direction of the UCS/WCS of the model/world view but also each viewport direction of each layout. In each viewport of layouts, the centerlines will be parallel to the layout coordinate system.
However, the centerlines may not look natural along with the circle graphics in each viewport, as shown by the above screenshot. We will address this matter in later posts.
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 its Geometry.WorldLine is used to draw the centerlines;
• The centerline circle overrule draws the centerlines with a hard coded color (Cyan with index 4) through setting the WorldDraw.SubEntityTraits.Color property.
• The ViewportDraw has been implemented to support viewport dependant graphics.
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.
Thank you Spider would you be able to elaborate on what this code here is doing?
Matrix3d ucs = vd.Viewport.EyeToWorldTransform;
Circle cir = drawable as Circle;
Point3d cen = cir.Center.TransformBy(ucs.Inverse());
much appreciated. chrs
Posted by: BKSpurgeon | 07/31/2018 at 07:01 AM
chrs, you are welcome. It transforms the circle center coordinate from the World system to the Eye system. The Viewport.EyeToWorldTransform returns a matrix from the Eye CS (on the computer screen) to the WCS in which all AutoCAD entity geometries are expressed, thus the Center.TransformBy methods accepts its Inverse copy.
Posted by: Spiderinnet1 | 08/01/2018 at 04:48 PM