找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2829|回复: 2

[分享] AutoCAD Overrule .NET: Centerline Circle DrawableOverrule Supporting Block Re...

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

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

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

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

×
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.
  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.     /// </summary>
  43.     public class CenterlineCircle_DrawableOverrule6 : DrawableOverrule
  44.     {
  45.         protected CenterlineCircle_DrawableOverrule6()
  46.         {
  47.             //The overrule only works with certain circles marked with the Extension Dictionary Entry.
  48.             SetExtensionDictionaryEntryFilter(CenterlineCircle_Gadgets.CLCircle_ID);
  49.             //SetCustomFilter();
  50.         }

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

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

  72.                 Matrix3d transform = wd.Geometry.ModelToWorldTransform;
  73.                 if (transform == Matrix3d.Identity)
  74.                 {
  75.                     transform = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
  76.                     cen = cen.TransformBy(transform.Inverse());
  77.                 }
  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.Geometry.WorldLine(line1Pt1, line1Pt2);
  99.                 wd.Geometry.WorldLine(line2Pt1, line2Pt2);
  100.                 wd.SubEntityTraits.LineType = backupLinetypeId;

  101.                 wd.SubEntityTraits.Color = backupColor;
  102.             }

  103.             return base.WorldDraw(drawable, wd);
  104.         }

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

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

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

  133.             return CenterLinetypeId;
  134.         }

  135.     }
  136. }

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

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

Here is an image to show the effect.
0.jpg
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.
&#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 support Block References (INSERTs) some extra transformation work needs to be done as demonstrated.



评分

参与人数 1D豆 +2 收起 理由
ScmTools + 2 很给力!经验;技术要点;资料分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2013-6-12 11:42:33 | 显示全部楼层
怎么全是英文的?还以为上错网站了。

点评

中文资料确实少啊!  详情 回复 发表于 2013-6-12 12:05
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 593个

财富等级: 财运亨通

 楼主| 发表于 2013-6-12 12:05:34 | 显示全部楼层
Gdlprfcu 发表于 2013-6-12 11:42
怎么全是英文的?还以为上错网站了。

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

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-23 00:32 , Processed in 0.442118 second(s), 43 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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