找回密码
 立即注册
查看: 2293|回复: 0

[分享] Optimized overruling in AutoCAD 2010 using .NET

[复制链接]
发表于 2013-5-27 01:41:35 | 显示全部楼层 |阅读模式

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

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

×

In the last post we looked at some code that attaches additional data to individual entities, allowing them to be drawn independently with different visual properties via the new Overrule API in AutoCAD 2010.

A couple of comments – one from Qun, who provided the original F# sample, and one from Tony Tanzillo – have prompted me to optimize the code somewhat. Tony pointed out, very validly, that as the previous code registers its overrule against Drawable objects it will get called for every entity (and various objects besides) which could clearly impact performance. Qun pointed me to some interesting framework capabilities that allow you to let AutoCAD decide whether to call your overrule based on the existence of certain XData or some other criterion (such as the existence of a particular entry in the object’s extension dictionary).

So here’s what I’ve done…

I decided to define separate overrule classes for each object type we want to handle and have these classes derive from a common abstract base class. This allows us to encapsulate all the common XData-related stuff inside the base class, and leave the child classes to worry about the type-specific display. We will then register our overrules for the specific classes they care about, which will lead them to only being called for objects of those types.

A nice feature is the ability to let AutoCAD filter on the existence of specific XData prior to invoking our overrule: check out the constructor of the PipeOverrule class – it calls SetXDataFilter() on the base DrawableOverrule class, telling AutoCAD the registered application name for which to look. An interesting test: draw some lines and/or circles, and use the MP command in the below sample to attach radii to some of them. If you set a breakpoint in your WorldDraw() functions and run the code as it stands, you’ll see that they only get called for the circles/lines that have been modified by the MP command. If you comment that one line, you should see WorldDraw() being called for all circles and lines, irrespective of whether they’ve been given a radius or not.

Before we get into the code, here’s the protocol for the DrawableOverrule class, as provided by Visual Studio:
namespace Autodesk.AutoCAD.GraphicsInterface { [Wrapper("AcGiDrawableOverrule")] public abstract class DrawableOverrule : Overrule { protected internal DrawableOverrule(); public virtual int SetAttributes( Drawable drawable, DrawableTraits traits); public override sealed void SetCustomFilter(); public override sealed void SetExtensionDictionaryEntryFilter( string entryName); public override sealed void SetIdFilter(ObjectId[] ids); public override sealed void SetNoFilter(); public override sealed void SetXDataFilter( string registeredApplicationName); public virtual void ViewportDraw( Drawable drawable, ViewportDraw vd); public virtual int ViewportDrawLogicalFlags(
展开

Aside from filtering on XData, we can see that we can also filter on specific ObjectIds (good for overruling objects very specifically), extension dictionary entries (good if you’re working with lots of data per object and XData is no longer viable) or something more custom.

Here’s the protocol for the Overrule class, the base class for DrawableOverrule:
namespace Autodesk.AutoCAD.Runtime { [Wrapper("AcRxOverrule")] public abstract class Overrule : RXObject { public static bool Overruling { get; set; } public static void AddOverrule( RXClass targetClass, Overrule overrule, bool bAtLast); public static bool HasOverrule( RXObject overruledSubject, RXClass targetClass); public virtual bool IsApplicable(RXObject overruledSubject); public static void RemoveOverrule( RXClass targetClass, Overrule overrule); public abstract void SetCustomFilter(); public abstract void SetExtensionDictionaryEntryFilter( string entryName); public abstract void SetIdFilter(ObjectId[] ids); public abstract void SetNoFilter();
展开

The protocol in which we’re interested has been declared as abstract (== pure virtual) at this level, so we need to use DrawableOverrule as a base class unless we want to implement the protocol ourselves. One additional function to be overridden at this level is IsApplicable(), which is the fundamental way the overrule tells AutoCAD whether it can work with a specific object, or not. This is the function we would override to implement a custom filter, and is the only part of the filter protocol available from ObjectARX: more work has been done on the .NET side of things, which thankfully makes our lives much simpler. :-)

One open question in my mind: while asking AutoCAD to filter on XData is simpler and means our code is called less often, it does mean that both AutoCAD and our code will be checking for XData (when we retrieve the radius information). Presumably AutoCAD is going to check very efficiently, which means the code should be quicker in the cases where we have lots of objects that are not overruled, but in the cases where we are overruling a lot of objects there does appear to be some potential for this making the application run marginally slower. Ultimately – as mentioned earlier - there’s just a single line of code to comment out if you want to see the difference (and presumably profile the performance in some way). Just something to watch for, in case – I doubt this is something that’s going to have a significant – or even measurable – performance impact, but anyway.

Here’s the modified C# code:

using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.GraphicsInterface; using Autodesk.AutoCAD.Colors; namespace DrawOverrule { public abstract class PipeOverrule : DrawableOverrule { const string regAppName = "TTIF_PIPE"; public PipeOverrule() { // Tell AutoCAD to filter on our application name // (this means our overrule will only be called // on objects possessing XData with this name)
展开

In terms of what happens when we run the code, here are the results from the previous post (as it should function identically).

First some simple geometry for which we’re going to overrule the display:

0.jpg

And here’s some geometry for which we’ve attached radii using the MP command, once we’ve turned our overrules on via the OVERRULE1 command:

1.jpg

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

本版积分规则

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

GMT+8, 2026-12-1 21:10 , Processed in 0.106066 second(s), 33 queries , Gzip On.

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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