Document Activated Event

Document Activated Event

Anonymous
Not applicable
535 Views
16 Replies
Message 1 of 17

Document Activated Event

Anonymous
Not applicable
A few weeks ago, a request was made for a component that can
provide an event when a document is activated and when
AutoCAD is closed, and a couple of other things. Since the
changes have been made to the newsgroups, I can't find the
original post nor the newsgroup search.

TrackA2K.DLL in the attached Zip file may provide a solution
for at least some of the functionality requested.

The other DLL in the Zip is XTimers.DLL, which comes with
the Coffee VB6 sample application from Microsoft.

After registering both DLLs with Regsvr32, the code you need
looks like this

Private WithEvents myDocTracks As TrackA2K.DocTracks
Private myAcad As AcadApplication

On Error Resume Next
Set myAcad = CreateObject("AutoCAD.Application.15")
If Err Then
MsgBox "Could not get an instance of AutoCAD"
Else
Set myDocTracks = New DocTracks
If Err Then
MsgBox "Could not get an instance of
TrackA2K.DocTracks"
End If

Set myDocTracks.Acad = myAcad
myAcad.Visible = True
End If

The DocTracks object exposes 3 events:

OnQuit()
Fired when the AutoCAD session exits. Set myAcad=Nothing
and myDocTracks=Nothing in this event, along with anything
else you need to do.

WillQuit()
Fired before message boxes for saving or not saving are
presented. OnQuit will not fire if cancel is chosen on any
Save prompt.

OnActivate(ByVal aDoc as AcadDocument)
Fired when a new document is opened or created.

Any comments are welcome

Mark Holder
0 Likes
536 Views
16 Replies
Replies (16)
Message 2 of 17

Anonymous
Not applicable
Frank-

The only reason I can think of to get that error would be
opening a second (read-only) copy of a drawing. Have you
tried it with code exactly like I posted?

I'll test it on an already open session to see if there is a
problem when >1 drawing is already open when you set the
Acad property.

Thanks,
Mark Holder

Frank Oquendo wrote:
>
> Hi Mark. I keep getting a run-time error 457 "This key is already associated
> with an element of this collection" whenever I try to set the Acad property
> of a DocTacks object. Am I doing something wrong?
>
0 Likes
Message 3 of 17

Anonymous
Not applicable
Frank-

I tested it using GetObject to get an existing session with
2 drawings open, and got no error. Do you need to have 2
copies of the same drawing open? If so, I can easily fix the
DLL so it does not matter.

Thenks,
Mark Holder

>
> Frank Oquendo wrote:
> >
> > Hi Mark. I keep getting a run-time error 457 "This key is already associated
> > with an element of this collection" whenever I try to set the Acad property
> > of a DocTacks object. Am I doing something wrong?
> >
0 Likes
Message 4 of 17

Anonymous
Not applicable
Frank-

I get the events on the drawing that was active when the
DocTracks' Acad property is set, and only get the runtime
error if the same dwg is opened more than once.

BTW 1: The events only fire correctly if the DocTracks
object is instantiated with New, not with CreateObject or
GetInterfaceObject from an out of process client.

BTW 2: I have not yet tried it from an in-process client.
I'll do so now...

Thanks, Mark Holder
0 Likes
Message 5 of 17

Anonymous
Not applicable
Frank-

Yes, I am using FullName to index the docs. For some reason,
I don't get an error when >1 unnamed dwg is open, though.
Hmmm...

There is no real reason to do so in this app, it just
carried over from the original app I developed the tool for.
I'll redo that part.

Thanks,
Mark Holder

Frank Oquendo wrote:
>
> Here's an update: you're using the FullName property to index your
> documents, aren't you? The problem only occurs if there's more than one
> unnamed drawing open when the object is created. You may want to consider
> using a test to determine whether to index on the FullName or just the Name.
>
0 Likes
Message 6 of 17

Anonymous
Not applicable
Frank-

Oops - I forgot to watch for new drawings. Here is a new
version that does, and does not index on FullName, so the
same drawing can be opened more than once.

Thanks,

Mark Holder

Frank Oquendo wrote:
>
> The problems I'm having are centered on unnamed drawings:
> 1) Multiple unnamed drawings prior to instantiation crashes the app
> 2) Unnamed drawings created after instantiation do not trigger the
> OnActivate event
>
> With named (saved) documents, everything works beautifully. I believe it's
> the unconditional use of the FullName property that is causing these
> problems.
0 Likes
Message 7 of 17

Anonymous
Not applicable
Frank-

Let me think about distribution. I'll look at Autodesk's web
store first, if it still exists, and I can locate it.

BTW: What about your "switch state" thing? I don't remember
exactly what it was supposed to do.

Thanks,
Mark Holder

Frank Oquendo wrote:
>
> Excellent! That's got it. Now for the thorny part: what's your
> redistribution policy? Is this freeware? Will you be donating the code to
> the public domain? If you're going to retain ownership, will I need to buy
> license to distribute your application with my own? If you'd rather not
> discuss these matters here, please feel free to email me directly. Just
> reply to this message and remove the "nospam." Thanks for all your effort,
> Mark. I appreciate it.
>
0 Likes
Message 8 of 17

Anonymous
Not applicable
VB6 sp3

Mark

Frank Oquendo wrote:
BTW, what is this dll written in?
0 Likes
Message 9 of 17

Anonymous
Not applicable
Mark - Your server's OnActivateDocument event does not
fire when the Active document is changed via API calls
(e.g., calling an AcadDocument's Activate() method).

You can see this by using my QuickDoc demo program for
my AcadX ActiveX server. If you click on the name of a
document in the list, it is activated, but your event
never fires (or at least, not in an out-of-process VB5
test client).

Nice try, though.

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 10 of 17

Anonymous
Not applicable
Tony-

Thanks for looking at my little widget.

When I first saw your post, I wondered whether you just stumbled onto that
problem or if you had reason to suspect it would be there. It seems that
AutoCAD does not fire the Activate event when the active document is changed
by an automation api call. I'm guessing you knew that already. 🙂

To my surprise, I managed to come up with a workaround using the events that
do fire. I'm going to do a bit more testing before I post an update, though.

BTW: Frank indicates that performance problems are to be expected from a VB
implementation of this capability. Do you agree, and if so, would you mind
reposting that Lisp code you posted earlier for benchmarking? I can't find
anything since Autodesk "improved" their web presence.

Thanks again for the input. I hope this message gets through...

Mark Holder

Tony Tanzillo wrote in message
news:38DD30BD.C3ADD1F1@worldnet.att.net...
> Mark - Your server's OnActivateDocument event does not
> fire when the Active document is changed via API calls
> (e.g., calling an AcadDocument's Activate() method).
>
> You can see this by using my QuickDoc demo program for
> my AcadX ActiveX server. If you click on the name of a
> document in the list, it is activated, but your event
> never fires (or at least, not in an out-of-process VB5
> test client).
>
> Nice try, though.
>
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* tony.tanzillo@worldnet.att.net */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes
Message 11 of 17

Anonymous
Not applicable
Mark - Yes, I know about the bug in the Activated event.

However, I'm quite puzzled by your suggestion that
you have a workaround, since there are no other events
available to VB that would serve as a means of working
around the problem.

The performance problem has nothing to do with VB, or
the language in use. The same problem exists with any
controller regardless of what language it is written
in. The reason is very simple: There are three events
on the Document events interface that fire at a very
high frequency:

ObjectAdded
ObjectModified
ObjectErased

These events fire every time an object is added,
erased, or modified. Because of the way ActiveX
events work, an event-sinking controller must handle
ALL of the events on an outgoing event interface,
regardless of whether the programmer is interested
in the event or not.

Mark Holder wrote:
>
> Tony-
>
> Thanks for looking at my little widget.
>
> When I first saw your post, I wondered whether you just stumbled onto that
> problem or if you had reason to suspect it would be there. It seems that
> AutoCAD does not fire the Activate event when the active document is changed
> by an automation api call. I'm guessing you knew that already. 🙂
>
> To my surprise, I managed to come up with a workaround using the events that
> do fire. I'm going to do a bit more testing before I post an update, though.
>
> BTW: Frank indicates that performance problems are to be expected from a VB
> implementation of this capability. Do you agree, and if so, would you mind
> reposting that Lisp code you posted earlier for benchmarking? I can't find
> anything since Autodesk "improved" their web presence.
>
> Thanks again for the input. I hope this message gets through...
>

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 12 of 17

Anonymous
Not applicable
Any idea why Frank has deleted all of his posts in this
thread?

Mark Holder wrote:
>
> A few weeks ago, a request was made for a component that can
> provide an event when a document is activated and when
> AutoCAD is closed, and a couple of other things. Since the
> changes have been made to the newsgroups, I can't find the
> original post nor the newsgroup search.......

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 13 of 17

Anonymous
Not applicable
Tony-

Surprisingly, the Deactivate event fires in the outgoing
document even when an API call triggers the document switch.
Unfortunately, I have not been able to get 100% reliability
in handling document switches with it - almost, but not
quite. I'm not sure if it is my code that's buggy or just
quirks in the firing of the events. Since a fix has already
been made to MCAD, I doubt it is worth pursuing anyway.
Surely a fix for vanilla AutoCAD can't be too far away
(???).

Thanks again,

Mark Holder

Tony Tanzillo wrote:
>
> However, I'm quite puzzled by your suggestion that
> you have a workaround, since there are no other events
> available to VB that would serve as a means of working
> around the problem.
>
0 Likes
Message 14 of 17

Anonymous
Not applicable
Tony-

I didn't know posts could be deleted by the sender. Maybe
they just got lost in the reorganization . I notice his
are not the only ones missing. Maybe Frank will clue us in.

Mark Holder

Tony Tanzillo wrote:
>
> Any idea why Frank has deleted all of his posts in this
> thread?
0 Likes
Message 15 of 17

Anonymous
Not applicable
Why can't you do this in simple VB6?

--
Stuart Nathan @
http://www.office-manager.co.uk
0 Likes
Message 16 of 17

Anonymous
Not applicable
Mark - What product/release are you referring to by 'MCAD'?

Can you do a _VERNUM on it and tell me what it says?

Regardless of whether you find a workaround for the document
activated problem, you're still not going to circumvent the
performance problem using VB. Others have done exactly, or
nearly the same thing you're doing, and all of their efforts
have eventually hit the same wall (there is sample code that
is posted in the customer-files newsgroup that isn't very much
different from what you're doing).

I'm sorry that Frank's lack of programming experience, and
lack of understanding the underlying problem has mislead you
into believing that you have come up with a 'breakthrough'
of some sort, but unfortunately, that's not the case. There
is only one acceptable solution to the problem, and that is
to use ARX to source the events, as my AcadX server does.

Mark Holder wrote:
>
> Tony-
>
> Surprisingly, the Deactivate event fires in the outgoing
> document even when an API call triggers the document switch.
> Unfortunately, I have not been able to get 100% reliability
> in handling document switches with it - almost, but not
> quite. I'm not sure if it is my code that's buggy or just
> quirks in the firing of the events. Since a fix has already
> been made to MCAD, I doubt it is worth pursuing anyway.
> Surely a fix for vanilla AutoCAD can't be too far away
> (???).
>

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 17 of 17

Anonymous
Not applicable
Tony-

The info I got was that a fix has been made to Mechanical
Desktop 4.0 (T133 vs T098 for plain AutoCAD, T108 for my
AutoCAD with the service pack).

Also, I didn't really think this was supposed to be
something hard to do. The Autodesk docs make it sound like
something any automation client should be able to do easily.
It just gets hard when you try to make it work like it
should. I haven't looked in customer files for quite a while
now due to time limits.

As far a performance, I don't observe a perceptible
degradation when I have AcadDocument objects declared
WithEvents. I'd be interested in any additional info or
links you may have on this subject.

Do you know if it helps to delete the definitions of the
ObjectModified, ObjectAdded, and ObjectErased events
entirely?

Thanks,

Mark Holder

Tony Tanzillo wrote:
>
> Mark - What product/release are you referring to by 'MCAD'?
>
> Can you do a _VERNUM on it and tell me what it says?
>
0 Likes