找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1061|回复: 0

AcadDocument events does not work the same way in VB as in VBA

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-11 20:30:05 | 显示全部楼层 |阅读模式

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

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

×

问题:
How can I initialize AcadDocument events in standalone VB as it is in VBA by default?

解答:

To be able to use AcadDocument events, you need to specify the 'WithEvents'
while declaring the AcadDocument object, then the events will be available for
that reference.


Public WithEvents AcadDoc As AcadDocument

Note that there is a significant difference between the 'ThisDrawing' object
in VBA and an 'AcadDocument' reference in VB, even if the latter is declared
with the 'WithEvents' type definition.

In VBA, you usually connect to the 'ThisDocument' (i.e. ThisDrawing) object,
which is always connected to the active document. This special object does not
exist anywhere else than in the VBA environment, and this object is managed
by the AutoCAD application. If you want to trap events in VB code, you need
to connect to a given document by referencing to its corresponding document object.
But doing so will get you the notifications for the connected document object
only. In VBA if the user switches from one document to another, the 'Thisdrawing'
reference is changed automatically to the active document and then your notification
system still continues to be notified in VBA. However, the same thing does not
occur in VB since you still have a connection to the previous document instead
of the new active document.

To solve this problem, you can choose between writing an AutoCAD in-process
ActiveX server, which exposes the 'ThisDrawing' object, or you can add a reference
to all AcadDocument objects available in AutoCAD.

The second method is much easier to implement in VB, but requires having a reference
on each AcadDocument object all the time. The attached VB sample demonstrates
how to simulate the 'ThisDrawing' behavior in VB.

You need to manage the reference to the real ActiveDocument in order to get
notified. In the sample, this is achieved by having a class that has a global
AcadDocument object declared using 'WithEvents'. All the document level events
can be specified in this class module. Then the sample simply creates a dynamic
array of class objects in run time where the dimension is equal to the number
of documents. Each class instance's AcadDocument object is connected to a corresponding
document. Hence events will fire for all the documents. And the
AcadApplication::NewDrawing event helps to add new reference to newly created
or opened document(s) at runtime. Unlike the VBA 'ThisDrawing', you need to
keep track of those.

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

本版积分规则

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

GMT+8, 2024-12-14 06:54 , Processed in 0.390884 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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