While many users begin experimenting with the FEMAP API within the API Programming window available in FEMAP, the API can also be written in most programming languages, as long as you can transfer over the type library. Before you begin trying to use the FEMAP API with Python, first install the win32com object which can be found at the following link:
To transfer the FEMAP type library over to Python, you need to run this script:
import sys from win32com.client import makepy sys.argv = ["makepy", "-o Pyfemap.py", r"C:\FEMAPv1132\femap.tlb"] makepy.main()
This script will create a Pyfemap.py file within your current directory. To use Pyfemap.py, copy it into the Lib folder of your Python installation.
Once you have the Pyfemap.py file copied over, you need to import the Pyfemap object at the beginning of your code to directly access the FEMAP API. The following code shows how to import the FEMAP type library and then how to initialize a femap model.
import pythoncom import Pyfemap import sys try: existObj = pythoncom.connect(Pyfemap.model.CLSID) #Grabs active model app = Pyfemap.model(existObj)#Initializes object to active mode except: sys.exit("femap not open") #Exits program if there is no active femap model
Attached is a code example which, through Python, counts the number of elements by type and sends that information to an Excel spreadsheet. This code is a good example of how to use FEMAP object, methods and how to communicate between two different programs using the API in a python environment. Another advantage of python is that it can be turned into an executable using the py2exe add on. I have turned the ElemByTypetoExcel into an executable using this method, but using this you must include the entire “dist” folder in order for the exe to run properly. For information watch SDA’s webinar “API Programming; Integrating Python”.