找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 3003|回复: 1

[分享] ObjectARX Overrule API

[复制链接]

已领礼包: 6个

财富等级: 恭喜发财

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

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

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

×
本帖最后由 CSharpBoy 于 2013-5-27 01:23 编辑


定制一个entity有2种方法

  • 从AcdbEntity继承
    这是之前常用的方法,坏处是如果不把你的dbx载入进来,就只能显示proxy entity, 另外一个坏处custom entity一般会保存自己的数据(涉及到filer), 数据招到破坏的话,图形不能正确显示了
  • 使用overrule api
    使用overrule API, 你同样可以定制entity。Autocad在调用默认的function(如worlddraw)前,会先调用overrule class中相应的function(callback function, 你在这个函数中实现定制行为)


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

已领礼包: 6个

财富等级: 恭喜发财

 楼主| 发表于 2013-5-27 01:23:22 | 显示全部楼层
The new APIs in AutoCAD 2010 - Part 1
This is the first post in a series looking at the new APIs in AutoCAD 2010, and follows on from this post looking at the migration steps required. I've copied the information in this post from the recently-published Platform Technologies Customization Newsletter, a quarterly newsletter available to ADN members. A big thank you to Stephen Preston, Fenton Webb and Gopinath Taget for putting the material together.

AutoCAD 2010 New API Overview

AutoCAD 2010 has some really cool APIs. Please download the ObjectARX 2010 Beta SDK and review the Migration Guide for a complete list of changes and additions. [This is currently available to ADN members on the ADN extranet.]
Here are the highlights:

Overrule API

One of the most powerful ObjectARX APIs is the custom objects API. The custom object API allows you to create your own entities in a drawing that behave in the same way as standard AutoCAD entities. So, for example, where AutoCAD has a line, you might develop a custom entity that looks like a ‘pipe’. You can define how your pipe displays itself, the pipes grip- and snap- points, how the pipe behaves when moves or copied, etc.
However, with great power comes great responsibility. Custom objects are saved to a drawing. Without your Object Enabler, your custom object is loaded into AutoCAD as a dumb proxy object. So when you are considering creating a custom object, you need to consider whether you’re prepared to make a commitment to your application users that you will continue to support your custom object through multiple AutoCAD releases. If you’re not prepared to make that commitment, then you really shouldn’t be creating custom objects.
And because your custom object is responsible for filing itself when a drawing is saved or opened, you also have an extremely powerful mechanism for corrupting all your customers drawings if you make a mistake in your implementation.
To provide you with an alternative to custom objects – an alternative that requires less long term support commitment from you – AutoCAD 2010 introduces the new Overrule API. Think of Overrule as customized objects, rather than custom objects. It’s essentially a mechanism for AutoCAD to call your implementation of certain object functions instead of immediately calling the functions for that object. Your implementation can then choose whether to refer the call back to the native object. Unlike custom objects, the overrule definitions are not filed to the DWG file, so it’s a lot harder to corrupt your drawing. Instead, the Overrule API will only customize an entity when your application is loaded. (Although, you can save data used by your Overrul 0.jpg e as Xdata or in Xrecords).
As a simple example, you can overrule an entity’s worldDraw function and draw your own graphical representation instead. (In the simple sample we demonstrated at Developer Days, we took a Line and turned it into a Thermometer (see image).

Image: Two Lines – Can you tell which one has been Overruled? ;-).


The Overrule API is available in ObjectARX (C++) and .NET. Here’s a simple VB.NET example of how you’d create an overrule…
First, create your custom Overrule class, inheriting from one of the available Overrules, and overriding the functions you want to overrule. In this case, we’re overruling an entity’s WorldDraw function. WorldDraw is part of the DrawableOverrule.

  1. Imports Autodesk.AutoCAD.GraphicsInterface
  2. Public Class MyDrawOverrule
  3.     Inherits DrawableOverrule
  4.     'This is the function that gets called to add/replace
  5.     'an entity's WorldDraw graphics
  6.     Public Overrides Function WorldDraw( _
  7.       ByVal drawable As Drawable, _
  8.       ByVal wd As WorldDraw) As Boolean
  9.         'Draw my own graphics here ...
  10.         'Call the object's own worldDraw function (if you want to)
  11.         Return MyBase.WorldDraw(drawable, wd)
  12.     End Function
  13. End Class

Next, instantiate your Overrule, add it to the entity you want to overrule, and turn Overruling on. (You can also specify how the overrule is applied – you can apply it to every object of that type, apply it depending on Xdata or Xrecords, maintain a list of ObjectIds of entities to be overruled, or define your own custom filter).
[Note that you will need to have Imported Autodesk,AutoCAD.Runtime and DatabaseServices for the below code to build.]
'mDrawOverrule is a class member variable
'we declared elsewhere
mDrawOverrule = New MyDrawOverrule
'Add the Overrule to the entity class - in this case Line
Overrule.AddOverrule( _
    RXObject.GetClass(GetType(Line)), _
    mDrawOverrule, False)
'Optional - specify filter
'(In this case we only apply overrule to Lines with entry
' named "RDS_MyData" in Extension Dictionary)
mDrawOverrule.SetExtensionDictionaryEntryFilter("RDS_MyData")
'Turn overruling on
Overrule.Overruling = True
And that’s all there is to it.
You can find a (simple) working Overrule sample with the Developer Days material posted on the ADN website. [I will post my own C# sample to this blog over the coming weeks, as I play around with the API myself - Kean] We’ll be extending that sample soon and using it as the basis of a webcast after AutoCAD 2010 has shipped. And look at the ‘Behavior Overrules’ section of the ObjectARX Developers Guide for information on the ObjectARX implementation of this API, and for details of methods affected by this API.
Freeform Modeling API
3D modeling in AutoCAD tends to be a bit ‘blocky’. It’s hard to create a shape that looks really organic. That’s where Freeform modeling comes in. It’s hard to describe succinctly the power of this feature, so I’d encourage you to review Heidi’s product demonstration [Once again, this link is to the ADN site - I will post more about the freeform modelling capabilities of AutoCAD 2010, in due course - Kean]. The basic idea is to take a solid or mesh, twist it around a bit by pushing and pulling at its edges, vertices and faces, and then smooth it and crease it. The smoothing is performed using Subdivision – we use the Catmull-Clark algorithm that is already being used by other Autodesk products.
The API centers on the Sub-division mesh object – AcDbSubDMesh in ObjectARX, DatabaseServices.SubDMesh in .NET, and AcadSubDMesh in ActiveX. The API allows you to do essentially everything a user can through the UI. Here’s a simple VB.NET sample showing how to generate a SubDMesh from a Solid3d and then apply level 1 smoothing to it.





  1. Imports Autodesk.AutoCAD.ApplicationServices
  2. Imports Autodesk.AutoCAD.DatabaseServices
  3. Imports Autodesk.AutoCAD.EditorInput
  4. Imports Autodesk.AutoCAD.Geometry
  5. Imports Autodesk.AutoCAD.Runtime

  6. Public Class FreeFormSample

  7.   <CommandMethod("CREATEMESH")> _
  8.   Public Sub MySub()

  9.     'Select a solid.
  10.     Dim ed As Editor = _
  11.       Application.DocumentManager.MdiActiveDocument.Editor
  12.     Dim opts As _
  13.       New PromptEntityOptions(vbCrLf + "Select Solid:")
  14.     opts.SetRejectMessage(vbCrLf & "That's not a solid!")
  15.     opts.AddAllowedClass(GetType(Solid3d), False)
  16.     Dim res As PromptEntityResult = ed.GetEntity(opts)

  17.     'Exit sub if user cancelled selection.
  18.     If res.Status <> PromptStatus.OK Then Exit Sub

  19.     'Usual transaction stuff
  20.     Dim db As Database = _
  21.       Application.DocumentManager.MdiActiveDocument.Database
  22.     Using tr As Transaction = _
  23.       db.TransactionManager.StartTransaction

  24.       Dim mySolid As Solid3d = _
  25.         tr.GetObject( _
  26.           res.ObjectId, _
  27.           OpenMode.ForRead, False)
  28.       Dim ext As Extents3d = mySolid.Bounds
  29.       Dim vec As Vector3d = (ext.MaxPoint - ext.MinPoint)

  30.       'Define params governing mesh generation algorithm
  31.       '(See ObjectARX helpfiles for explanation of params –
  32.       ' you may need to change them depending on the scale
  33.       ' of the solid)
  34.       Dim myFaceterData As _
  35.         New MeshFaceterData( _
  36.           0.01 * vec.Length, _
  37.           40 * Math.PI / 180, _
  38.           2, 2, 15, 5, 5, 0)

  39.       'Create new mesh from solid (smoothing level 1)
  40.       Dim meshData As MeshDataCollection = _
  41.         SubDMesh.GetObjectMesh(mySolid, myFaceterData)
  42.       Dim myMesh As New SubDMesh
  43.       myMesh.SetSubDMesh( _
  44.         meshData.VertexArray, meshData.FaceArray, 1)

  45.       'Add mesh to database. (Don't remove solid).
  46.       myMesh.SetDatabaseDefaults()
  47.       Dim btr As BlockTableRecord = _
  48.         tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
  49.       btr.AppendEntity(myMesh)
  50.       tr.AddNewlyCreatedDBObject(myMesh, True)

  51.       'Our work here is done
  52.       tr.Commit()
  53.     End Using
  54.   End Sub

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-17 22:46 , Processed in 0.165324 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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