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. In this post, let’s contine to enhance the centerline circle, making centerline look real center through being associated with the Center linetype.
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;
/// WorldDraw.Geometry being used to draw the lines;
/// Drawing the center lines with a particular color (Cyan with index 4);
/// Drawing the center lines with the CENTER linetype if loaded;
/// </summary>
public class CenterlineCircle_DrawableOverrule1 : DrawableOverrule
{
protected CenterlineCircle_DrawableOverrule1()
{
}
protected static CenterlineCircle_DrawableOverrule1 mInstance;
public static CenterlineCircle_DrawableOverrule1 Instance
{
get
{
if (mInstance == null)
mInstance = new CenterlineCircle_DrawableOverrule1();
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;
ObjectId backupLinetypeId = wd.SubEntityTraits.LineType;
if (GetCenterLinetype() != ObjectId.Null)
{
wd.SubEntityTraits.LineType = CenterLinetypeId;
}
wd.Geometry.WorldLine(line1Pt1, line1Pt2);
wd.Geometry.WorldLine(line2Pt1, line2Pt2);
wd.SubEntityTraits.LineType = backupLinetypeId;
wd.SubEntityTraits.Color = backupColor;
}
return base.WorldDraw(drawable, wd);
}
private ObjectId CenterLinetypeId = ObjectId.Null;
public ObjectId GetCenterLinetype()
{
Database db = HostApplicationServices.WorkingDatabase;
if (CenterLinetypeId != ObjectId.Null && CenterLinetypeId.IsValid && CenterLinetypeId.IsResident && CenterLinetypeId.Database == db)
return CenterLinetypeId;
else
CenterLinetypeId = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in db.LinetypeTableId.GetObject(OpenMode.ForRead) as LinetypeTable)
{
LinetypeTableRecord ltr = id.GetObject(OpenMode.ForRead) as LinetypeTableRecord;
if (ltr.Name == "CENTER")
{
CenterLinetypeId = ltr.ObjectId;
break;
}
}
tr.Commit();
}
return CenterLinetypeId;
}
}
}
Here are the commands to show or hide centerlines for circles.
[CommandMethod("ShowCircleCenterline1")]
public static void ShowCircleCenterline1_Method()
{
CenterlineCircle_Gadgets.RegisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule1.Instance);
}
[CommandMethod("HideCircleCenterline1")]
public static void HideCircleCenterline1_Method()
{
CenterlineCircle_Gadgets.UnregisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule1.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.
Command: C
CIRCLE Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: 0,0
Specify radius of circle or [Diameter] <10.0000>: 10
Command: -LINETYPE
Current line type: "Continuous"
Enter an option [?/Create/Load/Set]: load
Enter linetype(s) to load: center
Linetype "CENTER" loaded.
Enter an option [?/Create/Load/Set]:
Command: SHOWCIRCLECENTERLINE1
Regenerating model.
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 centerlines are drawn with the Center linetype through assigning its ObjectId to the WorldDraw.SubEntityTraits.LineType property before adding the centerlines.
• Please do not forget to back up the original linetype and assign it back after the centerline being drawn. Otherwise, the circle itself will be drawn with the Center linetype too.
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.
Recent Comments