- UID
- 14
- 积分
- 8264
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-4
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
AutoCAD Overrule .NET: Centerline Circle DrawableOverrule Adding Selection Markers for CenterlinesAutoCAD 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 have presented the centerline circle DrawableOverrule implementations in some earlier posts.
In this post, let’s enhance our Centerline Circle DrawableOverrule a bit further to make it support selection markers for the circle centerlines. Here is the code.
- #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 applicable 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;
- /// SetExtensionDictionaryEntryFilter() being called to filter circles with Extension Dictionary Entry;
- /// IsApplicable being implemented to support circle filtering;
- /// The length of the center lines of each circle can be overridden individually;
- /// Circles in block references (INSERTs) are also supported;
- /// Add the selection marker support;
- /// </summary>
- public class CenterlineCircle_DrawableOverrule7 : DrawableOverrule
- {
- protected CenterlineCircle_DrawableOverrule7()
- {
- //The overrule only works with certain circles marked with the Extension Dictionary Entry.
- SetExtensionDictionaryEntryFilter(CenterlineCircle_Gadgets.CLCircle_ID);
- //SetCustomFilter();
- }
-
- protected static CenterlineCircle_DrawableOverrule7 mInstance;
- public static CenterlineCircle_DrawableOverrule7 Instance
- {
- get
- {
- if (mInstance == null)
- mInstance = new CenterlineCircle_DrawableOverrule7();
- return mInstance;
- }
- set
- {
- mInstance = value;
- }
- }
-
- public override bool WorldDraw(Drawable drawable, WorldDraw wd)
- {
- if (drawable is Circle)
- {
- Circle cir = drawable as Circle;
- Point3d cen = cir.Center;
- double rad = cir.Radius;
-
- Matrix3d transform = wd.Geometry.ModelToWorldTransform;
- if (transform == Matrix3d.Identity)
- {
- transform = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
- cen = cen.TransformBy(transform.Inverse());
- }
-
- double ratio;
- object value = CenterlineCircle_Gadgets.GetDictionaryEntryTypedValue(
- CenterlineCircle_Gadgets.GetExtensionDictionaryEntry(cir.ObjectId, CenterlineCircle_Gadgets.CLCircle_ID),
- (int)DxfCode.ExtendedDataScale);
- if (value == null)
- ratio = CenterlineCircle_Gadgets.DefaultRatioLengthToRadius;
- else
- ratio = Convert.ToDouble(value);
-
- Point3d line1Pt1 = new Point3d(cen.X - rad * ratio, cen.Y, cen.Z).TransformBy(transform);
- Point3d line1Pt2 = new Point3d(cen.X + rad * ratio, cen.Y, cen.Z).TransformBy(transform);
- Point3d line2Pt1 = new Point3d(cen.X, cen.Y - rad * ratio, cen.Z).TransformBy(transform);
- Point3d line2Pt2 = new Point3d(cen.X, cen.Y + rad * ratio, cen.Z).TransformBy(transform);
-
- short backupColor = wd.SubEntityTraits.Color;
- wd.SubEntityTraits.Color = 4;
-
- ObjectId backupLinetypeId = wd.SubEntityTraits.LineType;
- if (GetCenterLinetype() != ObjectId.Null)
- {
- wd.SubEntityTraits.LineType = CenterLinetypeId;
- }
-
- wd.SubEntityTraits.SetSelectionMarker((IntPtr)1); //Horizontal center line as marker #1
- wd.Geometry.WorldLine(line1Pt1, line1Pt2);
-
- wd.SubEntityTraits.SetSelectionMarker((IntPtr)2); //Vertical center line as marker #2
- wd.Geometry.WorldLine(line2Pt1, line2Pt2);
-
- wd.SubEntityTraits.LineType = backupLinetypeId;
- wd.SubEntityTraits.Color = backupColor;
- wd.SubEntityTraits.SetSelectionMarker((IntPtr)0); //Circle still as marker #0
- }
-
- return base.WorldDraw(drawable, wd);
- }
-
- //The Extension Dictionary Entry filtering can also be implemented here, but we choose to call the
- // straightforward SetExtensionDictionaryEntryFilter() in the constructor to simply things. For custom
- // filters, the IsApplicable() has to be implemented.
- public override bool IsApplicable(RXObject overruledSubject)
- {
- return base.IsApplicable(overruledSubject);
- }
-
- private static ObjectId CenterLinetypeId = ObjectId.Null;
- public static 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 is the test command.
- [CommandMethod("ShowCircleCenterline7")]
- public static void ShowCircleCenterline7_Method()
- {
- CenterlineCircle_Gadgets.RegisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule7.Instance);
- }
-
- [CommandMethod("HideCircleCenterline7")]
- public static void HideCircleCenterline7_Method()
- {
- CenterlineCircle_Gadgets.UnregisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule7.Instance);
- }
The selection markers of the circle centerlines do not demonstrate themselves in the graphics of centerline circles, but they will play some important roles when we implement the OsnapOverrule that we are going to introduce soon. Some highlights may look good to help understand the code.
• 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.
• A filter is added to make some circles behave with centerlines added and others behave still normal.
• The SetExtensionDictionaryEntryFilter() is called to filter circles with a particular Extension Dictionary Entry;
• The IsApplicable is implemented to support circle filtering.
• The Extension Dictionary Entry filtering can also be implemented in the IsApplicable method.
• We choose to call the straightforward SetExtensionDictionaryEntryFilter() in the constructor to simply things since it work fine here.
• The length ratio parameter of the centerlines of each circle has been recorded into the extension dictionary as an XRecord.
• To add selection markers for the circle centerlines, a few WorldDraw.SubEntityTraits.SetSelectionMarker() call are made to give the circle itself seletction marker #0, the horizontal circle centerline selection marker #1, and the vertical centerline selection marker #2.
|
|