- UID
- 14
- 积分
- 8264
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-4
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
AutoCAD Overrule .NET: Centerline Circle DrawableOverrule Supporting Block References (INSERTs)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 have presented the centerline circle overrule implementations in some earlier posts.
However, the previous version of the Centerline Circle DrawableOverrule does not support circles nested in something else such as block references (INSERTs). In this post, let’s enhance our Centerline Circle DrawableOverrule to make it support block references (INSERTs). 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;
- /// </summary>
- public class CenterlineCircle_DrawableOverrule6 : DrawableOverrule
- {
- protected CenterlineCircle_DrawableOverrule6()
- {
- //The overrule only works with certain circles marked with the Extension Dictionary Entry.
- SetExtensionDictionaryEntryFilter(CenterlineCircle_Gadgets.CLCircle_ID);
- //SetCustomFilter();
- }
-
- protected static CenterlineCircle_DrawableOverrule6 mInstance;
- public static CenterlineCircle_DrawableOverrule6 Instance
- {
- get
- {
- if (mInstance == null)
- mInstance = new CenterlineCircle_DrawableOverrule6();
- 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.Geometry.WorldLine(line1Pt1, line1Pt2);
- wd.Geometry.WorldLine(line2Pt1, line2Pt2);
- wd.SubEntityTraits.LineType = backupLinetypeId;
-
- wd.SubEntityTraits.Color = backupColor;
- }
-
- 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("ShowCircleCenterline6")]
- public static void ShowCircleCenterline6_Method()
- {
- CenterlineCircle_Gadgets.RegisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule6.Instance);
- }
-
- [CommandMethod("HideCircleCenterline6")]
- public static void HideCircleCenterline6_Method()
- {
- CenterlineCircle_Gadgets.UnregisterOverrule(typeof(Circle), CenterlineCircle_DrawableOverrule6.Instance);
- }
Here is an image to show the effect.
As can be seen, the centerlines of both block references (on the left side) and circles themselves (on the right side) all appear well. 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 support Block References (INSERTs) some extra transformation work needs to be done as demonstrated.
|
评分
-
查看全部评分
|