AcadDocument events does not work the same way in VB as in VBA
问题:
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.
页:
[1]