Working with ByRef arrays in CATIA

This is not actually a script, but a solution I’ve found to a huge roadblock I encountered while trying to use Python to automate CATIA V5 using win32com AND use all funcionalities exposed to automation.

We all know it’s very easy to get started by just using late-binding (dynamic dispatch) and the more advanced users will already know that it will be not possible to use Subs that need Byref arrays. Developers of win32com related stuff are aware of the problem but not to the fact that in some applications these kind of Sub are very intensively used i.e. all subs to get triplets such as GetCoordinates of any class derived from the Point class, or GetFirstAxis of any class derived from the Plane class.
Developers state that it’s sufficient to switch to early bindig using makepy to solve the problem but in my case for CATIA V5 R19 it didn’t solve the problem at all, it even added more problems. Continue reading “Working with ByRef arrays in CATIA”

Python library for Dassault Systemes CATIA

There are already some examples on this website, which explain, how to automate Dassault Systemes CATIA V5 with Python and Microsoft COM. Yesterday Paul sent me a mail and mentioned, that he has created a Python Library, which packs a few of the COM magics into a Python library with some basic functions. It covers things like, traversing assembly structures, context manager, convert files, find elements in a part, etc. Continue reading “Python library for Dassault Systemes CATIA”

SendKey via AutoIT Interface

As AutoIT has been specifically created to deeply interact with Windows, I would recommend considering the interface to AutoIT whenever sphisticated control of Windows internals is required. Example: Sending keystrokes.

AutoIT can do that in an exhaustive way, and the interface between Python and AutoIT is VERY simple and elegant.
I cannot think of any keystroke, that cannot be sent with AutoIT.

See example below.

In addition, AutoIT is free and of relatively small size (18 MB).

from win32com.client import Dispatch
au = Dispatch("AutoItX3.Control")   # Access to AutoIT

...

# this sends "right, right, shift+right"
au.Send("{RIGHT}{RIGHT}+{RIGHT}")
txta = str.format("+{0}{1}{2}","{DOWN ",number,"}")
# this sends "shift+down" a 'number' of times
au.Send(txta)

Events in Autodesk Inventor

How to listen and respond to Inventor events in Python using win32com (from http://wikihelp.autodesk.com/Inventor/enu/Community/Third_Party_Tools/the_python_page/Listening_to_Events). This example assumes you have succefully been able to connect to Inventor’s COM object If you have not, please do that first.

# used to listen to the application events and also the events for a custom button
import ctypes
import win32com.client
from win32com.client.gencache import EnsureDispatch
import pythoncom

class Events():
    def __init__(self, oApp):
        global Application
        Application = oApp
   
    #Application Events
    class IVEvent():      
        def OnQuit(self, BeforeOrAfter, Context, HandlingCode):
            print("Quiting...")
            ctypes.windll.user32.PostQuitMessage(0)
            Application.Quit()
     
    #Custom Button Events      
    class BEvent():
        def OnExecute(self, Context):
            print "Dxf Button Clicked"

class DoylePlugin():    
    oApp=EnsureDispatch("Inventor.Application")
    oApp.Visible=True
      
    DxfButton=oApp.CommandManager.ControlDefinitions.AddButtonDefinition("Dxf Update", "dxf", 4)
    DxfButton.AutoAddToGUI()
    
    Events = Events(oApp)        
    AppEvents=win32com.client.DispatchWithEvents(oApp.ApplicationEvents, Events.IVEvent)
    DxfEvents=win32com.client.DispatchWithEvents(DxfButton, Events.BEvent)
    
    pythoncom.PumpMessages()

Simple VBA like MessageBox in Python

Some days ago I found a very easy pythonic way to show a VBA-like MessageBox. It was created with only one line of cpython code.

The code is very very easy. Simply change the last integer parameter to change the style of the message box.

import ctypes

msgbox = ctypes.windll.user32.MessageBoxA
ret = msgbox(None, 'Press OK to end the demo.', 'Deviare Python Demo', 0)
print ret

Find selected files in Windows Explorer

I searched a long time for this and also Google could not give me an answer to the following question. How can I detect the selected files in a Windows Explorer window? I found following by trial and error…

import win32com.client
# look in the makepy output for IE for the 'CLSIDToClassMap'
# dictionary, and find the entry for 'ShellWindows'
clsid='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
ShellWindows=win32com.client.Dispatch(clsid)

# a busy state can be detected:
# while ShellWindows[0].Busy == False:
# go in for-loop here

for i in range(ShellWindows.Count):
    print ShellWindows[i].LocationURL
    for j in range(ShellWindows[i].Document.SelectedItems().Count):
        print '  ', ShellWindows[i].Document.SelectedItems().Item(j).Path

# Be careful: Internet Explorer uses also the same CLSID. You should implement a detection!