[python-win32] question about pywin32 and COM event again

Laurent Dufréchou Laurent.Dufrechou at free.fr
Fri Dec 9 20:49:38 CET 2005


wccppp a écrit :

> Hello,
>
> I'm learning to use python to automate AutoCAD and I was able to catch
> the AutoCAD application event using the code below.  But I don't know
> if I can catch the event from the active document (or drawing) or
> not.  The AutoCAD.Application has a property of "ActiveDocument" which
> is the active drawing opened.  The document has an event "BeginClose"
> which is what I'm after.  I'd like to do some clean up before the
> drawing is closed.  Can anyone give me guide?  Thank you very much!
>
> import win32com.client
> class ACADEvents:
>     def OnEndOpen(self, fName):
>         print "Drawing " + str(fName) + " opened..."
>     def OnEndSave(self, fName):
>         print "Drawing " + str(fName) + " saved..."
>     def OnBeginCommand(self, cmdName):
>         print "Beginning command " + str(cmdName) + "..."
>
> acad = win32com.client.DispatchWithEvents("AutoCAD.Application",
> ACADEvents)
> acad.WindowState = 3
> acad.Visible = 1
> dwgName = r"C:\AutoCAD\DWG\1.dwg"
> doc = acad.Documents.Open(dwgName)
> doc.Layers("0").LayerOn = False
> doc.Save()
> doc.Close()
>
> -- 
>
> Regards,
> - wcc
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Python-win32 mailing list
>Python-win32 at python.org
>http://mail.python.org/mailman/listinfo/python-win32
>  
>
Hello, had the same problem, but now it's solved :)
Take a look at the code below:

class VisualDSP_AppEvents:
    def OnAppClose(self):
        print '<VisualDSP_Manager> Visual DSP++ has been closed, program
will terminate now!'
       
class VisualDSP_ProcessorEvents:
    def OnHalted(self,ReasonCode,HaltReason):
        print '<VisualDSP_Manager> <event : processor halted>'
       
app = DispatchWithEvents('VisualDSP.ADspApplication',VisualDSP_AppEvents)
...
processor = session.ActiveProcessor
processor_events = WithEvents(processor, VisualDSP_ProcessorEvents)

Here I'm catching application event with DispatchWithEvents
for the processors event that depends on app I've used WithEvents function,
So for your problem try something like:

class session_event_class:
    def BeginClose(self):
        print 'catched'

acad = win32com.client.DispatchWithEvents("AutoCAD.Application", ACADEvents)
active_session = acad.ActiveSession

active_session_events = WithEvents(active_session,session_event_class)
or
WithEvents(active_session,session_event_class)

active_session_events is usefull if you've done a main class and you
want your event class methods access your main class methods.

Laurent


More information about the Python-win32 mailing list