找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2717|回复: 0

[分享] AutoCAD Overrule .NET: Centerline Circle TransformOverrule

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-6-12 06:44:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
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.

  1. #region Namespaces

  2. using System;
  3. using System.Text;
  4. using System.Linq;
  5. using System.Xml;
  6. using System.Reflection;
  7. using System.ComponentModel;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Windows;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Forms;
  13. using System.Diagnostics;
  14. using System.Drawing;
  15. using System.IO;

  16. using Autodesk.AutoCAD.ApplicationServices;
  17. using Autodesk.AutoCAD.DatabaseServices;
  18. using Autodesk.AutoCAD.Runtime;
  19. using Autodesk.AutoCAD.EditorInput;
  20. using Autodesk.AutoCAD.Geometry;
  21. using Autodesk.AutoCAD.Windows;

  22. using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
  23. using MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document;
  24. using AcWindowsNS = Autodesk.AutoCAD.Windows;

  25. #endregion

  26. using Autodesk.AutoCAD.GraphicsInterface;
  27. namespace AcadNetAddinWizard_Namespace
  28. {
  29. /// <summary>
  30. ///
  31. /// </summary>
  32.     public class CenterlineCircle_TransformOverrule : TransformOverrule
  33.     {
  34.         private CenterlineCircle_TransformOverrule()
  35.         {
  36.             //The overrule only works with certain circles marked with the Extension Dictionary Entry.
  37.             SetExtensionDictionaryEntryFilter(CenterlineCircle_Gadgets.CLCircle_ID);
  38.             SetCustomFilter();
  39.         }

  40.         private static CenterlineCircle_TransformOverrule mInstance;
  41.         public static CenterlineCircle_TransformOverrule Instance
  42.         {
  43.             get
  44.             {
  45.                 if (mInstance == null)
  46.                     mInstance = new CenterlineCircle_TransformOverrule();
  47.                 return mInstance;
  48.             }
  49.         }

  50.         public override bool IsApplicable(RXObject overruledSubject)
  51.         {
  52.             return Overrule.HasOverrule(overruledSubject, RXClass.GetClass(typeof(CenterlineCircle_DrawableOverrule5)));
  53.         }

  54.         public override void Explode(Entity entity, DBObjectCollection entitySet)
  55.         {
  56.             //base.Explode(entity, entitySet);  //eNotApplicable

  57.             Matrix3d ucs = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;

  58.             Circle cir = entity as Circle;
  59.             Point3d cen = cir.Center.TransformBy(ucs.Inverse()); ;
  60.             double rad = cir.Radius;

  61.             double ratio;
  62.             object value = CenterlineCircle_Gadgets.GetDictionaryEntryTypedValue(
  63.                             CenterlineCircle_Gadgets.GetExtensionDictionaryEntry(cir.ObjectId, CenterlineCircle_Gadgets.CLCircle_ID),
  64.                             (int)DxfCode.ExtendedDataScale);
  65.             if (value == null)
  66.                 ratio = CenterlineCircle_Gadgets.DefaultRatioLengthToRadius;
  67.             else
  68.                 ratio = Convert.ToDouble(value);

  69.             ObjectId linetypeId = CenterlineCircle_DrawableOverrule5.GetCenterLinetype();

  70.             Point3d line1Pt1 = new Point3d(cen.X - rad * ratio, cen.Y, cen.Z).TransformBy(ucs);
  71.             Point3d line1Pt2 = new Point3d(cen.X + rad * ratio, cen.Y, cen.Z).TransformBy(ucs);
  72.             Point3d line2Pt1 = new Point3d(cen.X, cen.Y - rad * ratio, cen.Z).TransformBy(ucs);
  73.             Point3d line2Pt2 = new Point3d(cen.X, cen.Y + rad * ratio, cen.Z).TransformBy(ucs);

  74.             Line line1 = new Line(line1Pt1, line1Pt2);
  75.             line1.SetDatabaseDefaults();
  76.             line1.ColorIndex = 4;
  77.             line1.LinetypeId = linetypeId;
  78.             entitySet.Add(line1);

  79.             Line line2 = new Line(line2Pt1, line2Pt2);
  80.             line2.SetDatabaseDefaults();
  81.             line2.ColorIndex = 4;
  82.             line2.LinetypeId = linetypeId;
  83.             entitySet.Add(line2);

  84.             entitySet.Add(entity.Clone() as DBObject);
  85.         }
  86.     }

Here is the test command.
  1. [CommandMethod("AddCLCircleTransformOverrule")]
  2. public static void AddCLCircleTransformOverrule_Method()
  3. {
  4.     CenterlineCircle_Gadgets.RegisterOverrule(typeof(Circle), CenterlineCircle_TransformOverrule.Instance);
  5. }

  6. [CommandMethod("RemoveCLCircleTransformOverrule")]
  7. public static void RemoveCLCircleTransformOverrule_Method()
  8. {
  9.     CenterlineCircle_Gadgets.UnregisterOverrule(typeof(Circle), CenterlineCircle_TransformOverrule.Instance);
  10. }

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.
0.jpg
Some highlights may look good to help understand the code.
&#8226; The centerline circle object overrule is derived from the TransformOverrule API class;
&#8226; The centerline circle TransformOverrule is made a singleton to avoid duplicate registrations or similar issues;
&#8226; The Explode override is implemented to create the centerlines when a Centerline Circle is exploded.



论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:39 , Processed in 0.178635 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表