How to automate CATIA V6 with Python on Windows

CATIA V6 can be automated in the same way like CATIA V5. You need either the comtypes module or the Pywin32 module. The next two examples show two simple programming examples by using Pywin32, which is included in the ActivePython Distribution. If you want to use another Python Distribution, you have to install this module manually.

When you run the following two scripts, CATIA V6 should be installed and already be running.

The first example prints the names (PLMExternalID) of all selected entries in a query list of the advanced search (Silver Layer):

import win32com.client

catiaObj=win32com.client.GetActiveObject('CATIA.Application')
for i in range(catiaObj.ActiveEditor.Selection.Count):
    print 'Selected Item %s: %s'%(i, catiaObj.ActiveEditor.Selection.Item(i + 1).Value.Name)

The next example throws a query (Weld0815*) to the VPM database, gets all relevant products and places all of them on a turntable view of the Silver Layer. This is very useful, if you want to compare different Products an one turntable view.

import win32com.client

# Search for every Product with name Weld0815*
catiaObj=win32com.client.GetActiveObject('CATIA.Application')
oSearchService = catiaObj.GetSessionService("PLMSearch")
oPLMSearches = oSearchService.Searches
oPLMSearch = oPLMSearches.Add()
oPLMSearch.Type = "PLMProductDS"
oPLMSearch.AddAttributeCriteria("PLM_ExternalID",  "Weld0815*")
oPLMSearch.search()

# Iterate over all found objects and put them into the turntable view
o3DShapeAsPLMEntities = oPLMSearch.EditedContent
print 'Found %s entries.'%o3DShapeAsPLMEntities.Count
for foundobj in o3DShapeAsPLMEntities:
    print 'Putting %s onto the table'%foundobj.Name
    oSelection = catiaObj.ActiveEditor.Selection
    oSelection.Add(foundobj)
    catiaObj.StartCommand("Turntable")