- UID
- 14
- 积分
- 8264
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-4
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
AutoCAD Overrule .NET: Centerline Circle TransformOverruleAutoCAD 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.
In this post, let’s implement the TransformOverrule for our Centerline Circle to make it support the EXPLODE command. 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>
- ///
- /// </summary>
- public class CenterlineCircle_TransformOverrule : TransformOverrule
- {
- private CenterlineCircle_TransformOverrule()
- {
- //The overrule only works with certain circles marked with the Extension Dictionary Entry.
- SetExtensionDictionaryEntryFilter(CenterlineCircle_Gadgets.CLCircle_ID);
- SetCustomFilter();
- }
-
- private static CenterlineCircle_TransformOverrule mInstance;
- public static CenterlineCircle_TransformOverrule Instance
- {
- get
- {
- if (mInstance == null)
- mInstance = new CenterlineCircle_TransformOverrule();
- return mInstance;
- }
- }
-
- public override bool IsApplicable(RXObject overruledSubject)
- {
- return Overrule.HasOverrule(overruledSubject, RXClass.GetClass(typeof(CenterlineCircle_DrawableOverrule5)));
- }
-
- public override void Explode(Entity entity, DBObjectCollection entitySet)
- {
- //base.Explode(entity, entitySet); //eNotApplicable
-
- Matrix3d ucs = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
-
- Circle cir = entity as Circle;
- Point3d cen = cir.Center.TransformBy(ucs.Inverse()); ;
- double rad = cir.Radius;
-
- 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);
-
- ObjectId linetypeId = CenterlineCircle_DrawableOverrule5.GetCenterLinetype();
-
- Point3d line1Pt1 = new Point3d(cen.X - rad * ratio, cen.Y, cen.Z).TransformBy(ucs);
- Point3d line1Pt2 = new Point3d(cen.X + rad * ratio, cen.Y, cen.Z).TransformBy(ucs);
- Point3d line2Pt1 = new Point3d(cen.X, cen.Y - rad * ratio, cen.Z).TransformBy(ucs);
- Point3d line2Pt2 = new Point3d(cen.X, cen.Y + rad * ratio, cen.Z).TransformBy(ucs);
-
- Line line1 = new Line(line1Pt1, line1Pt2);
- line1.SetDatabaseDefaults();
- line1.ColorIndex = 4;
- line1.LinetypeId = linetypeId;
- entitySet.Add(line1);
-
- Line line2 = new Line(line2Pt1, line2Pt2);
- line2.SetDatabaseDefaults();
- line2.ColorIndex = 4;
- line2.LinetypeId = linetypeId;
- entitySet.Add(line2);
-
- entitySet.Add(entity.Clone() as DBObject);
- }
- }
Here is the test command.
- [CommandMethod("AddCLCircleTransformOverrule")]
- public static void AddCLCircleTransformOverrule_Method()
- {
- CenterlineCircle_Gadgets.RegisterOverrule(typeof(Circle), CenterlineCircle_TransformOverrule.Instance);
- }
-
- [CommandMethod("RemoveCLCircleTransformOverrule")]
- public static void RemoveCLCircleTransformOverrule_Method()
- {
- CenterlineCircle_Gadgets.UnregisterOverrule(typeof(Circle), CenterlineCircle_TransformOverrule.Instance);
- }
With the Centerline Circle TransformOverrule handy, the EXPLODE command will work fine with our Centerline Circle, the cenerline circle could be exploded and the circle centerlines will be kept as separate line segments.
Here is an image to show the effect.
Some highlights may look good to help understand the code.
• The centerline circle object overrule is derived from the TransformOverrule API class;
• The centerline circle TransformOverrule is made a singleton to avoid duplicate registrations or similar issues;
• The Explode override is implemented to create the centerlines when a Centerline Circle is exploded.
|
|