try: import win32com.client except: print "Ooops Pywin32 needed !" app=win32com.client.Dispatch('CATIA.Application') doc=app.ActiveDocument.Product a=doc.Analyze.Volume print "The Volume is: ", a
Generating BOM (bill of material) from CATIA V5
To test this sample you should have installed Pywin32 and start CATIA V5 before running this script.
import win32com.client catapp = win32com.client.Dispatch("CATIA.Application") doc=catapp.ActiveDocument.Product product_count = doc.Products.Count print "This example prints all Parts and subproducts of \ a CATProduct up to the 2nd level." for products in range(product_count): products = products + 1 print doc.Products.Item(products).Name, ":" part_count = doc.Products.Item(products).Products.Count for parts in range(part_count): parts = parts +1 print " ", doc.Products.Item(products).Products.Item(parts).Name For a HTML-generated Bill of materialyou should better use the BOM-generation from CATIA V5: import win32com.client catapp = win32com.client.Dispatch("CATIA.Application") doc=catapp.ActiveDocument.Product doc.Product.ExtractBOM(2, "c:\BOM.html")
CATIA V4 and CATPart to VRML Converter
A CATPart to VRML Batchconverter:
The next example uses the \"CATIA V5 Interfaces Object Library\" to
connect to CATIA V5, open a CATPart and export it to VRML. You can do
that with other formats like IGES, STEP, … too (if you have the
licence).
You need Python and Pywin for this example and have also running CATIA V5:
# Demo for converting a CATPart to VRML # Please start CATIA V5 before running this script from win32com.client.dynamic import Dispatch catapp = Dispatch("CATIA.Application") catapp.Documents.Open("D:\work\Testmodell.CATPart") catapp.ActiveDocument.ExportData('D:\work\Testmodell.wrl', 'wrl')
A CATIA V4 model to VRML Batchconverter:
You can`t do this interactive in CATIA V5 but with Python. Simply
open a CATIA V4 model and export it to VRML (I did this with R14):
# Demo for converting a CATIA V4 model to VRML
# Please start CATIA V5 before running this script
# Demo for converting a CATIA V4 model to VRML # Please start CATIA V5 before running this script from win32com.client.dynamic import Dispatch catapp = Dispatch("CATIA.Application") catapp.Documents.Open("D:\work\Testmodell.model") catapp.ActiveDocument.ExportData('D:\work\Testmodell.wrl', 'wrl')
CATIA V5/DMU Navigator-Programming with Python
Ifyou want to automate CATIA V5 or DMU Navigator with Python you need Pywin32 and Python for Windows or Activestates ActivePython for Windows. Python is free software and you can develop professional applications with all advantages of Python for CATIA V5 or DMU Navigator.
To develop a small application which interacts to CATIA V5 you only have to download your prefered Python Distribution for Windows and can start developing applications for CATIA V5 or DMU Navigator (this should be running when testing the application).
If you have done that you can test it with this source code:
import win32com.client a = win32com.client.Dispatch('catia.application') doc=a.ActiveDocument.Product for i in range(doc.Products.Count): print 'Part Number:', doc.Products.Item(i+1).PartNumber
This small example prints out the part names of a loaded CATProduct.
DMU Navigator
If you want to automate DMU Navigator, you can use this dispatch string:
import win32com.client # Following doesn`t work with CATIA V5 R16 and later a = win32com.client.Dispatch('DMU.application')
Other examples can be found in the script collection.
Important notes
If you want to use early binding for the CATIA V5 object libraries you can use the Makepy utility from Pythonwin. After that every object and its methods will be usable from the pulldown menu while typing and you can have a performance advantage. You should use makepy for every object library which you want to use e.g. “ProductStructure Object Library” for working with CATProducts). But not for the “CATIA V5 Interfaces Objekt Library”!
If you do that every application which uses early binding for the “CATIA V5 Interfaces Objekt Library” will not work ! A workaround is not to use makepy for the “CATIA V5 Interfaces Objekt Library” or use explicit late binding “(import win32com.client.dynamic as the first line).
For starting developing applications with Python for CATIA V5 you should use the manual V5Automation.chm which you can find in the bin-directory of CATIA V5. You will see that you can develop Python applications in the same way like VB with the builtin VB editor.
If you have problems to access the COM Interface from CATIA V5 (eg. no object libraries from CATIA V5 in makepy) please check whether CATIA V5 was registrated as a COM server. If not you can do that with cnext.exe /regserver in the CATIA V5 bin-directory. Then everything should work well…
More on Microsofts Component Object Model (COM) and Python
More information for developing with Python for COM applications with Pywin32 you can find in this excellent book.
Python Programming on Win32
by Mark Hammond, Andy Robinson
ISBN: 1-56592-621-8
652 pages. January 2000.
How to automate CATIA V5 with Python on Windows
If you want to automate CATIA V5 with Python you need Pywin32 and
Python for Windows. Python is free software and you can develop
professional applications with all advantages of Python for CATIA V5.
And you can deploy your applications as a standard Windows executable
what you cannot do with the builtin Macro language VB.
For developing a small application which interacts to CATIA V5 you
only have to download Python for Windows and Pywin32 from Marc Hammond
(please see the Download section). CATIA V5 must be also installed and should be running when testing the application.
If you have done that you can test it with this source code:
import win32com.client a = win32com.client.Dispatch('catia.application') doc=a.ActiveDocument.Product for i in range(doc.Products.Count): print 'Part Number:', doc.Products.Item(i+1).PartNumber
It prints out the part names of a loaded CATProduct. Other examples can be found in the script collection.
Hint: You can automate DMU Navigator as well as CATIA V5 with this Dispatch string:
a = win32com.client.Dispatch(‘DMU.application’)
Important notes:
If you want to use early binding for the CATIA V5 object libraries
you can use the Makepy utility from Pythonwin. After that every object
and its methods will be usable from the pulldown menu while typing and
you can have a performance advantage. You should use makepy for every
object library which you want to use e.g. “ProductStructure Object
Library” for working with CATProducts). Not for the “CATIA V5 Interfaces Objekt Library”!
But if you have done that stupidly, every application which uses early binding for the
“CATIA V5 Interfaces Objekt Library” will not work ! A workaround
is not to use makepy for the “CATIA V5 Interfaces Objekt Library”
or use explicit late binding “(import win32com.client.dynamic as the
first line).
For starting developing applications with Python for CATIA V5 you should use the manual V5Automation.chm
which you can find in the bin-directory of CATIA V5. You will see that
you can develop Python applications in the same way like VB with the
builtin VB editor.
If you have problems to access the COM Interface from CATIA V5 (eg.
no object libraries from CATIA V5 in makepy) please check whether CATIA
V5 was registrated as a COM server. If not you can do that with cnext.exe /regserver in the CATIA V5 bin-directory. Then everything should work well…
More information for developing with Python for COM applications
with Pywin32 you can find in this excellent book. A whole example
Chapter about COM Programming with Python/Pywin32 can be found here.
Python Programming on Win32
by Mark Hammond, Andy Robinson
ISBN: 1-56592-621-8
652 pages. January 2000.
Userdefined Commands in CATIA V5
With “startCommand” one can start userdefined commands in the same
way as you type the command in the command prompt in CATIA V5.
try: import win32com.client except: print "Oops PYwin32 from Marc Hammond needed" catapp = win32com.client.Dispatch("CATIA.application") #Attention: runs only if you have set TOOLS/CUSTOMIZE/OPTIONS to Englisch. catapp.StartCommand("Open") #Starts the "File open"-Menu #other examples: #catapp.StartCommand("What's This?") # Runs the Online-Help (Strg+F1) #catapp.StartCommand("CompassDisplayOff") # Compass visible #catapp.StartCommand("CompassDisplayOn") # Compass invisible