找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1651|回复: 0

[分享] AutoCAD Overrule .NET: Centerline Circle DrawableOverrule Adding Selection Ma...

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

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

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

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

×
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.
  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.     /// Features:
  31.     ///     A derivative from the DrawableOverrule;
  32.     ///     Being made a singleton to avoid duplicate registrations;
  33.     ///     UCS being hornored;
  34.     ///     Adding center lines to applicable cirlces;
  35.     ///     WorldDraw.Geometry being used to draw the lines;
  36.     ///     Drawing the center lines with a particular color (Cyan with index 4);
  37.     ///     Drawing the center lines with the CENTER linetype if loaded;
  38.     ///     SetExtensionDictionaryEntryFilter() being called to filter circles with Extension Dictionary Entry;
  39.     ///     IsApplicable being implemented to support circle filtering;
  40.     ///     The length of the center lines of each circle can be overridden individually;
  41.     ///     Circles in block references (INSERTs) are also supported;
  42.     ///     Add the selection marker support;
  43.     /// </summary>
  44.     public class CenterlineCircle_DrawableOverrule7 : DrawableOverrule
  45.     {
  46.         protected CenterlineCircle_DrawableOverrule7()
  47.         {
  48.             //The overrule only works with certain circles marked with the Extension Dictionary Entry.
  49.             SetExtensionDictionaryEntryFilter(CenterlineCircle_Gadgets.CLCircle_ID);
  50.             //SetCustomFilter();
  51.         }

  52.         protected static CenterlineCircle_DrawableOverrule7 mInstance;
  53.         public static CenterlineCircle_DrawableOverrule7 Instance
  54.         {
  55.             get
  56.             {
  57.                 if (mInstance == null)
  58.                     mInstance = new CenterlineCircle_DrawableOverrule7();
  59.                 return mInstance;
  60.             }
  61.             set
  62.             {
  63.                 mInstance = value;
  64.             }
  65.         }

  66.         public override bool WorldDraw(Drawable drawable, WorldDraw wd)
  67.         {
  68.             if (drawable is Circle)
  69.             {
  70.                 Circle cir = drawable as Circle;
  71.                 Point3d cen = cir.Center;
  72.                 double rad = cir.Radius;

  73.                 Matrix3d transform = wd.Geometry.ModelToWorldTransform;
  74.                 if (transform == Matrix3d.Identity)
  75.                 {
  76.                     transform = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
  77.                     cen = cen.TransformBy(transform.Inverse());
  78.                 }

  79.                 double ratio;
  80.                 object value = CenterlineCircle_Gadgets.GetDictionaryEntryTypedValue(
  81.                                 CenterlineCircle_Gadgets.GetExtensionDictionaryEntry(cir.ObjectId, CenterlineCircle_Gadgets.CLCircle_ID),
  82.                                 (int)DxfCode.ExtendedDataScale);
  83.                 if (value == null)
  84.                     ratio = CenterlineCircle_Gadgets.DefaultRatioLengthToRadius;
  85.                 else
  86.                     ratio = Convert.ToDouble(value);

  87.                 Point3d line1Pt1 = new Point3d(cen.X - rad * ratio, cen.Y, cen.Z).TransformBy(transform);
  88.                 Point3d line1Pt2 = new Point3d(cen.X + rad * ratio, cen.Y, cen.Z).TransformBy(transform);
  89.                 Point3d line2Pt1 = new Point3d(cen.X, cen.Y - rad * ratio, cen.Z).TransformBy(transform);
  90.                 Point3d line2Pt2 = new Point3d(cen.X, cen.Y + rad * ratio, cen.Z).TransformBy(transform);

  91.                 short backupColor = wd.SubEntityTraits.Color;
  92.                 wd.SubEntityTraits.Color = 4;

  93.                 ObjectId backupLinetypeId = wd.SubEntityTraits.LineType;
  94.                 if (GetCenterLinetype() != ObjectId.Null)
  95.                 {
  96.                     wd.SubEntityTraits.LineType = CenterLinetypeId;
  97.                 }

  98.                 wd.SubEntityTraits.SetSelectionMarker((IntPtr)1);   //Horizontal center line as marker #1
  99.                 wd.Geometry.WorldLine(line1Pt1, line1Pt2);

  100.                 wd.SubEntityTraits.SetSelectionMarker((IntPtr)2);   //Vertical center line as marker #2
  101.                 wd.Geometry.WorldLine(line2Pt1, line2Pt2);

  102.                 wd.SubEntityTraits.LineType = backupLinetypeId;
  103.                 wd.SubEntityTraits.Color = backupColor;
  104.                 wd.SubEntityTraits.SetSelectionMarker((IntPtr)0);   //Circle still as marker #0
  105.            }

  106.             return base.WorldDraw(drawable, wd);
  107.         }

  108.         //The Extension Dictionary Entry filtering can also be implemented here, but we choose to call the
  109.         // straightforward SetExtensionDictionaryEntryFilter() in the constructor to simply things. For custom
  110.         // filters, the IsApplicable() has to be implemented.
  111.         public override bool IsApplicable(RXObject overruledSubject)
  112.         {
  113.             return base.IsApplicable(overruledSubject);
  114.         }

  115.         private static ObjectId CenterLinetypeId = ObjectId.Null;
  116.         public static ObjectId GetCenterLinetype()
  117.         {
  118.             Database db = HostApplicationServices.WorkingDatabase;
  119.             if (CenterLinetypeId != ObjectId.Null && CenterLinetypeId.IsValid && CenterLinetypeId.IsResident && CenterLinetypeId.Database == db)
  120.                 return CenterLinetypeId;
  121.             else
  122.                 CenterLinetypeId = ObjectId.Null;

  123.             using (Transaction tr = db.TransactionManager.StartTransaction())
  124.             {
  125.                 foreach (ObjectId id in db.LinetypeTableId.GetObject(OpenMode.ForRead) as LinetypeTable)
  126.                 {
  127.                     LinetypeTableRecord ltr = id.GetObject(OpenMode.ForRead) as LinetypeTableRecord;
  128.                     if (ltr.Name == "CENTER")
  129.                     {
  130.                         CenterLinetypeId = ltr.ObjectId;
  131.                         break;
  132.                     }
  133.                 }
  134.                 tr.Commit();
  135.             }

  136.             return CenterLinetypeId;
  137.         }
  138.     }
  139. }

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

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

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.
&#8226; The centerline circle overrule is derived from the DrawableOverrule API class;
&#8226; The centerline circle overrule is made a singleton to avoid duplicate registrations or similar issues;
&#8226; The centerline circle overrule works with both UCS and WCS;
&#8226; The WorldDraw of the centerline circle overrule has been overridden and its Geometry.WorldLine is used to draw the centerlines;
&#8226; The centerline circle overrule draws the centerlines with a hard coded color (Cyan with index 4) through setting the WorldDraw.SubEntityTraits.Color property.
&#8226; The centerlines are drawn with the Center linetype through assigning its ObjectId to the WorldDraw.SubEntityTraits.LineType property before adding the centerlines.
&#8226; 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.
&#8226; A filter is added to make some circles behave with centerlines added and others behave still normal.
&#8226; The SetExtensionDictionaryEntryFilter() is called to filter circles with a particular Extension Dictionary Entry;
&#8226; The IsApplicable is implemented to support circle filtering.
&#8226; The Extension Dictionary Entry filtering can also be implemented in the IsApplicable method.
&#8226; We choose to call the straightforward SetExtensionDictionaryEntryFilter() in the constructor to simply things since it work fine here.
&#8226;  The length ratio parameter of the centerlines of each circle has been recorded into the extension dictionary as an XRecord.
&#8226; 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.



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

本版积分规则

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

GMT+8, 2024-11-17 22:18 , Processed in 0.169109 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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