找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1193|回复: 0

[分享] Customizing the display of standard AutoCAD objects using .NET

[复制链接]

已领礼包: 6个

财富等级: 恭喜发财

发表于 2013-5-27 01:43:50 | 显示全部楼层 |阅读模式

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

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

×

The code in this post is a direct port of the F# code in this previous post, which was entered by Qun Lu in the recent F# programming contest. Someone – very validly - commented on the fact the post involved both a new language and a new API, which was probably pushing things a little from a learning perspective. :-)

Without repeating my various comments in the previous post, I will reiterate the fact that this API is extremely interesting for developers who wish to customize the appearance and behaviour of standard AutoCAD objects without going through the pain of implementing full custom objects.

Here’s the equivalent C# code that works with AutoCAD 2010:
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.GraphicsInterface;
  6. using Autodesk.AutoCAD.Colors;

  7. namespace DrawOverrule
  8. {
  9.   public class DrawOverrule : DrawableOverrule
  10.   {
  11.     static public DrawOverrule theOverrule =
  12.       new DrawOverrule();

  13.     static private double Radius = 0.5;

  14.     private SweepOptions sweepOpts = new SweepOptions();

  15.     public override bool WorldDraw (Drawable d, WorldDraw wd)
  16.     {
  17.       if (d is Line)
  18.       {
  19.         Line line = (Line)d;

  20.         // Draw the line as is, with overruled attributes

  21.         base.WorldDraw(line, wd);
  22.         if (!line.Id.IsNull && line.Length > 0.0)
  23.         {
  24.           // Draw a pipe around the line

  25.           EntityColor c =
  26.             wd.SubEntityTraits.TrueColor;
  27.           wd.SubEntityTraits.TrueColor =
  28.             new EntityColor(0x00AfAfff);
  29.           wd.SubEntityTraits.LineWeight =
  30.             LineWeight.LineWeight000;
  31.           Circle clr =
  32.             new Circle(
  33.               line.StartPoint,
  34.               line.EndPoint - line.StartPoint,
  35.               DrawOverrule.Radius
  36.             );
  37.           ExtrudedSurface pipe = new ExtrudedSurface();
  38.           try
  39.           {
  40.             pipe.CreateExtrudedSurface(
  41.               clr, line.EndPoint-line.StartPoint, sweepOpts
  42.             );
  43.           }
  44.           catch
  45.           {
  46.             Document doc =
  47.               Application.DocumentManager.MdiActiveDocument;
  48.             doc.Editor.WriteMessage(
  49.               "\nFailed with CreateExtrudedSurface."
  50.             );
  51.           }
  52.           clr.Dispose();
  53.           pipe.WorldDraw(wd);
  54.           pipe.Dispose();
  55.           wd.SubEntityTraits.TrueColor = c;
  56.         }
  57.         return true;
  58.       }
  59.       else if (d is Circle)
  60.       {
  61.         Circle circle = (Circle)d;

  62.         // Draw the circle as is, with overruled attributes

  63.         base.WorldDraw(circle, wd);

  64.         // Needed to avoid ill-formed swept surface

  65.         if (circle.Radius > DrawOverrule.Radius)
  66.         {
  67.           // Draw a pipe around the cirle

  68.           EntityColor c = wd.SubEntityTraits.TrueColor;
  69.           wd.SubEntityTraits.TrueColor =
  70.             new EntityColor(0x3fffe0e0);
  71.           wd.SubEntityTraits.LineWeight =
  72.             LineWeight.LineWeight000;
  73.           Vector3d normal =
  74.             (circle.Center-circle.StartPoint).
  75.               CrossProduct(circle.Normal);
  76.           Circle clr =
  77.             new Circle(
  78.               circle.StartPoint, normal, DrawOverrule.Radius
  79.             );
  80.           SweptSurface pipe = new SweptSurface();
  81.           pipe.CreateSweptSurface(clr, circle, sweepOpts);
  82.           clr.Dispose();
  83.           pipe.WorldDraw(wd);
  84.           pipe.Dispose();
  85.           wd.SubEntityTraits.TrueColor = c;
  86.         }
  87.         return true;
  88.       }
  89.       return base.WorldDraw(d, wd);
  90.     }

  91.     public override int SetAttributes (Drawable d, DrawableTraits t)
  92.     {
  93.       int b = base.SetAttributes(d, t);
  94.       if (d is Line)
  95.       {
  96.         // If d is LINE, set color to index 6

  97.         t.Color = 6;

  98.         // and lineweight to .40 mm

  99.         t.LineWeight = LineWeight.LineWeight040;
  100.       }
  101.       else if (d is Circle)
  102.       {
  103.         // If d is CIRCLE, set color to index 2

  104.         t.Color = 2;

  105.         // and lineweight to .60 mm

  106.         t.LineWeight = LineWeight.LineWeight060;
  107.       }
  108.       return b;
  109.     }
  110.   }

  111.   public class Commands
  112.   {
  113.     public void Overrule(bool enable)
  114.     {
  115.       // Regen to see the effect
  116.       // (turn on/off Overruling and LWDISPLAY)

  117.       DrawableOverrule.Overruling = enable;
  118.       if (enable)
  119.         Application.SetSystemVariable("LWDISPLAY", 1);
  120.       else
  121.         Application.SetSystemVariable("LWDISPLAY", 0);

  122.       Document doc =
  123.         Application.DocumentManager.MdiActiveDocument;
  124.       doc.SendStringToExecute("REGEN3\n", true, false, false);
  125.       doc.Editor.Regen();
  126.     }

  127.     [CommandMethod("OVERRULE1")]
  128.     public void OverruleStart()
  129.     {
  130.       ObjectOverrule.AddOverrule
  131.         (RXClass.GetClass(typeof(Drawable)),
  132.         DrawOverrule.theOverrule, true);
  133.       Overrule(true);
  134.     }

  135.     [CommandMethod("OVERRULE0")]
  136.     public void OverruleEnd()
  137.     {
  138.       Overrule(false);
  139.     }
  140.   }
  141. }

[Repeating the results from the previous post…]

Here’s what happens when we load the application, turn the overrule on using the OVERRULE1 command (OVERRULE0 is the command to turn the overrule off) and draw some lines and circles:


0.jpg

Even in a 3D view – this time with the realistic visual style applied – you get the piping effect when you draw simple geometry:

1.jpg

These are standard AutoCAD lines and circles. When you use the OVERRULE0 command to disable the overrule, they revert to their original form:

2.jpg


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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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