Sign in to follow this  
Followers 0
wccppp

COM - catch application event?

9 posts in this topic

Hello group,

I was experimenting with the latest beta and tried to automate AutoCAD. One question came into mind is can AutoIt catch the event fired by the AutoCAD application? For example, whenever a drawing opening is finished, AutoCAD fire the event. With VBA or lisp, I can do something that I want to. But I have no idea if this can be done with AutoIt. Can someone enlighten me? Thanks very much for your time.

Btw, AutoIt looks great, especially now with the COM support in beta. Big thanks to the developer team.

-wcc

Share this post


Link to post
Share on other sites



Sure -- Have a look at ObjEvent in the helpfile.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Thanks to both of you, Dale & w0uter for your reply. I did some experiment and didn't get what I want. I know AutoCAD application has an EndOpen event. Here is vba example from AutoCAD help file.

Public WithEvents ACADApp As AcadApplication ' Use with Application Event Examples

Sub Example_AcadApplication_Events()

' This example intializes the public variable (ACADApp) which will be used

' to intercept AcadApplication Events

'

' The VBA WithEvents statement makes it possible to intercept an generic object

' with the events associated with that object.

'

' Before you will be able to trigger any of the AcadApplication events,

' you will first need to run this procedure.

' We could get the application from the ThisDocument object, but that would

' require having a drawing open, so we grab it from the system.

Set ACADApp = GetObject(, "AutoCAD.Application.16")

End Sub

Private Sub ACADApp_EndOpen(ByVal FileName As String)

' This example intercepts an Application EndOpen event.

'

' This event is triggered when AutoCAD finishes opening a drawing.

'

' To trigger this example event:

' 1) Make sure to run the example that initializes

' the public variable (named ACADApp) linked to this event.

'

' 2) Use AutoCAD to open an existing drawing and wait until the

' operation finishes

' Use the "FileName" variable to determine which drawing file is being opened

MsgBox "A drawing was just loaded from: " & FileName

End Sub

The AutoIt doc says ObjEvent returns either an object or a function name. So how do I know which case it is? Is it an object or a function name? I suppose in my case "EndOpen" will be returned, if the ObjEven returns a function name. If an object is returned, what would it be?

Below is the code I tried. I kept getting error saying varible $event used without being declared. I don't know how to fix it. Obviously I'm doing something wrong. Any advice? Sorry for the long post. Thanks a lot.

$acad = ObjCreate("AutoCAD.Application")

$acad.Visible = True

$acad.WindowState = 3

$acad.documents.open ("C:\Drawings\Temp\Drawing1.dwg")

$event = ObjEvent($acad, "acadEvent_")

$i = 0

;;the drawing is very small so it can be opened in couple of seconds.

while not (IsString($event) and $event = "EndOpen" and $i < 15)

sleep(1000)

$i += 1

$event = ObjEvent($acad, "acadEvent_")

wend

$event = ""

Exit

Func acadEvent_EndOpen()

MsgBox (0, "AutoIt Message", "Drawing opened.")

EndFunc

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Why are you issuing the ObjEvent _after_ doing the documents.open?

Also, you use $event 4 times... which occurance is it telling you you are using it without being declared? Is it an Au3Check error or a run-time error?

Regarding the return value -- it will be an object unless the first parameter is "AutoIt.Error" and you don't pass the second parameter... then the return is the name of the current error handling function (see Remarks for ObjEvent).

Dale

Edit: Added info on return value

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Thanks Dale. It is the first one that was causing error. I actually tried adding these lines after

$event = ObjEvent($acad, "acadEvent_")

If Not IsDeclared ("event") then

MsgBox(0,"", "$event is NOT declared")

EndIf

and sure enough I got the prompt message.

My goal is to issue some AutoCAD commands once the drawing has been opened.

The error I got was runtime error. The code starts AutoCAD and opens the drawing with no problem. I haven't tried Au3Check yet. Time to explore it.

So, if the return value is an object. What is it? The AutoCAD application object? That doesn't make sense to me. Since the variable $acad is for that AutoCAD application.

Thanks again.

-wcc

Why are you issuing the ObjEvent _after_ doing the documents.open?

Also, you use $event 4 times... which occurance is it telling you you are using it without being declared? Is it an Au3Check error or a run-time error?

Regarding the return value -- it will be an object unless the first parameter is "AutoIt.Error" and you don't pass the second parameter... then the return is the name of the current error handling function (see Remarks for ObjEvent).

Dale

Edit: Added info on return value

Share this post


Link to post
Share on other sites

I think you might be missing the point a bit here. $event is really not very important other than as a switch to turn the event processing off. Make certain taht you are also reading the Com Event description in the helpfile in the main section heading for Obj/COM Reference and not just what is in ObjEvent:

The main line in this script is: $SinkObject=ObjEvent($RecoContext,"MYEvent_").

This function takes the object $RecoContext and reroutes it's events to AutoIt functions whose names start with MYEvent_.

The Object responsible for the continuous rerouting is $SinkObject. This variable does not require any further attention, unless you want to stop the events.

To stop rerouting Events, just assign this variable any non-object value, e.g. $SinkObject=""

What is important is that the event handling functions you are creating get called when the specified event fires in the application.

Regarding $event being used before it is declared... you say it is the _first_ occurance of $event that is returning that message. That doesn't make sense unless you are also using Opt("MustDeclareVars", 1) -- otherwise using the variable on the left side of an equation is an implicit declaration. So you are either not showing all of your code or you are misinterpreting the output.

So, it is helpful to reduce your code to the minimum required to demonstrate the error, but don't leave any out when you post your example. Also, please post the exact messages returned (including AutoIt version and the line numbers and exact warning or error messages) otherwise you will end up wasting a lot of time for those people who are trying to help you.

Two more tips 1) it doesn't appear that you are using the SciTe editor for your code -- I highly recommend it. Using Alt-F5 to execute your code in SciTe runs it in the beta, runs Au3Check for you and routes output to the SciTe console to make sharing debug information here easy 2) please put your code examples inside a CODE block for readability. Just prior to your code, place the word CODE enclosed in square brackets and just after, place /CODE enclosed in square brackets.

regards,

Dale

Thanks Dale. It is the first one that was causing error. I actually tried adding these lines after

$event = ObjEvent($acad, "acadEvent_")

If Not IsDeclared ("event") then

MsgBox(0,"", "$event is NOT declared")

EndIf

and sure enough I got the prompt message.

My goal is to issue some AutoCAD commands once the drawing has been opened.

The error I got was runtime error. The code starts AutoCAD and opens the drawing with no problem. I haven't tried Au3Check yet. Time to explore it.

So, if the return value is an object. What is it? The AutoCAD application object? That doesn't make sense to me. Since the variable $acad is for that AutoCAD application.

Thanks again.

-wcc


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Folllowing code works

You need to have a instance of autocad launched before executing this script (Because i ma using ObjGet)

But you can modify the code to use ObjCreate

$Obj = ObjGet("","AutoCAD.Application.17")
If @error Then
    MsgBox( 0 , "" , "Failed") 
    Exit
EndIf

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")    
$objevent = ObjEvent( $Obj, "MyEvent_","_DAcadApplicationEvents")
If @error Then
    MsgBox( 0 , "" , "Event registration Failed") 
    Exit
EndIf

$Obj.Documents.Open ("C:\Hello.dwg")

Func MyEvent_EndOpen($EventName)
 MsgBox( 0 , "" , $EventName) 
EndFunc


; This is my custom defined error handler
Func MyErrFunc()

  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"      & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & hex($oMyError.number,8)  & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )
            
    Local $err = $oMyError.number
    If $err = 0 Then $err = -1
    
    $g_eventerror = $err  ; to check for after this function returns
Endfunc
Edited by gauravmishra

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0