Author Topic: VB6, ThisDrawing, and Document Events  (Read 3849 times)

0 Members and 1 Guest are viewing this topic.

lanceg

  • Guest
VB6, ThisDrawing, and Document Events
« on: August 11, 2007, 01:54:56 PM »
Hi, everyone....I tried this over at the ADesk VB/VBA forum and nobody responded.... :-(

I have a VBA routine that works nicely with the AcadDocument_EndCommand event, but I'm having trouble trying to convert it over to VB6. I've read everything I can find, but still can't make it work. The Event routine just won't fire.

I created a class module, declared a WithEvents AcadDocument, initialized the class, and placed a sub in my program to connect my document with ThisDrawing. I created ThisDrawing like this:
Code: [Select]
**Class Module**
Public WithEvents Doc as AcadDocument

Private Sub Class_Initialize()
Set Doc=Application.ActiveDocument
End Sub

Private Sub Doc_EndCommand(ByVal CommandName as String)
Select Case CommandName
--------- do stuff -------
End Select
End Sub

**Main Sub**
Option Explicit
Dim X as New EventClassModule
Public Application as AcadApplication
Public ThisDrawing as AcadDocument
Sub Main()
Call InitializeEvents
End Sub

Sub InitializeEvents()
On Error Resume Next
Set Application=GetObject(, "AutoCAD.Application")
If Err.Number<>0 Then
  Err. Clear
  MsgBox "Start AutoCAD, dummy!"
  End
End If
Set ThisDrawing=Application.ActiveDocument
Set X.doc=ThisDrawing
End Sub
ThisDrawing has the appropriate properties, and Doc shows the events in the class module properly, but nothing happens when I end a command in AutoCAD (using 2007), though it does work properly with the VBA version. What's missing?

Murph

  • Bull Frog
  • Posts: 402
  • P.I.T.A.
Re: VB6, ThisDrawing, and Document Events
« Reply #1 on: August 12, 2007, 07:47:29 AM »
You set Doc = Application.ActiveDocument
I have not done any programming in VB, just VBA but, shouldn't Doc = AcadApplication.ActiveDocument ?
Murphy's Law applies to all.
Whatever can go wrong, will, and at the most inopportune time.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16873
  • Superior Stupidity at its best
Re: VB6, ThisDrawing, and Document Events
« Reply #2 on: August 12, 2007, 09:58:51 AM »
Where is your event programming? Perhaps something is wrong there.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

lanceg

  • Guest
Re: VB6, ThisDrawing, and Document Events
« Reply #3 on: August 12, 2007, 10:25:51 AM »
Thanks for the responses.

Murph said:
Quote
You set Doc = Application.ActiveDocument.....shouldn't Doc = AcadApplication.ActiveDocument ?

In my Main sub, I set Application=AcadApplication, so this should take care of it, I think.  I have wondered if there's a problem with using variable names like "Application", but at least my declaration should take care of Murph's concern.

Keith said:
Quote
Where is your event programming? Perhaps something is wrong there.

The event programming is in the Class Module, which is (I think) where it needs to be.  See the part
that says "Private Sub Doc_EndCommand(ByVal CommandName as String)".  That part has been working
great in VBA as a Document event (got the initial idea from Adesk code to use the Design Center in VBA), so I don't think there's anything wrong with it.  I can't even get the first line to execute, and Err.Number is 0, so I don't think this is the problem.

I have noticed that the class module (or rather my instance of it) terminates when my main sub ends.  I'm now thinking that this is because I am not setting up my instance of the class in ACAD.DVB.  This may be the whole problem, so I will check that out today.

Again, thanks for the responses.
Lance


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16873
  • Superior Stupidity at its best
Re: VB6, ThisDrawing, and Document Events
« Reply #4 on: August 12, 2007, 04:18:49 PM »
I have noticed that the class module (or rather my instance of it) terminates when my main sub ends.

Ah .. that may be the problem .. while your main sub instanciates the class (it is held in the variable X), When main sub terminates, the variable holding the instance of AutoCAD also terminates. In AutoCAD, so long as the AutoCAD environment is open and VBA is enabled, any global variables stay active, even after the program ends. The global variables do not get reset because they are being held in the VBA environment by AutoCAD.

Your VB program should stay active for the duration of AutoCAD being active. Try using a timer or loop in the main sub to check for AutoCAD being open.

Something like this:
Code: [Select]
Sub Main()
Call InitializeEvents
While Application Is Not Nothing
' --- meaningless code to keep program busy ---
Wend
'If Application is nothing (i.e. AutoCAD closed) then let the program end
End Sub

This will of course utilize a little bit of processor power by constantly evaluating Application. If you set a timer in your program to constantly check if Application is active, then stop the timer after Application is nothing, it should respond a little better.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

lanceg

  • Guest
Re: VB6, ThisDrawing, and Document Events
« Reply #5 on: August 12, 2007, 04:57:02 PM »
Thanks, Keith -- I think you've hit it exactly.

I went in and used the ACAD.DVB file to create my class and its instance, and it worked!  The problem is, of course, that then I'm back in VBA where I started.  Your idea is exactly what I need to do.  At least I'm not stuck anymore with an event that never fires.  This is the FUN part.  :lol:

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16873
  • Superior Stupidity at its best
Re: VB6, ThisDrawing, and Document Events
« Reply #6 on: August 12, 2007, 06:44:10 PM »
What you can do is create your own application events and compile them to an activex dll. If you want to make it simple, you can create your application instance in lisp by creating the object in lisp. You can also create the object in VBA as well, while keeping the application in a seperate dll, or just create an activex executable that you can run from windows and will stay active just so long as AutoCAD is running. There are many ways you can do it, each with its own pros and cons.
If you need help, there are many people here who can aide you in your endevour.

good luck
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

lanceg

  • Guest
Re: VB6, ThisDrawing, and Document Events
« Reply #7 on: August 13, 2007, 12:40:32 PM »
OK, so now I realize that there are a number of ways to proceed.

I think that using ACAD.DVB to set up my events, and then placing the actual code that runs after the event is detected into a DLL or EXE, might be the best choice.  On the other front (creating an app that initializes the events and then just sits there and waits for the event), I tried putting a sort of a wait loop into my main sub per Keith's suggestion, but ended up bringing my system to its knees!  Is there a good way to keep my program open but doing nothing while waiting for the events to fire?  I had just put a simple variable assignment in there:
Code: [Select]
Sub Main()
   Call InitializeEvents
   While Not Application Is Nothing
      Set ctr=1
   Wend
   MsgBox "AutoCAD has been shut down"
End Sub
I'm sure there's some way to do this without maxing out my CPU.

So far I think that starting a separate DLL or EXE when the event triggers is probably easier and safer.  Can anyone give me a little guidance here?

Thanks in advance --
Lance

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16873
  • Superior Stupidity at its best
Re: VB6, ThisDrawing, and Document Events
« Reply #8 on: August 13, 2007, 02:17:34 PM »
Is there a good way to keep my program open but doing nothing while waiting for the events to fire?

Sure, create a userform, put a timer on the form, initialize the form, but don't show it. Have the timer on the userform check for the value of "Application", if it does not find it, have it close the form.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie