Writing the FEMAP API in Python

Siemens Experimenter Siemens Experimenter
Siemens Experimenter

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"]


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

    existObj = pythoncom.connect(Pyfemap.model.CLSID) #Grabs active model
    app = Pyfemap.model(existObj)#Initializes object to active mode
    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”.


I referenced this post in setting up Python to access the FEMAP API and it was extremely helpful. I have all the control I could ever wish for but I am constrianed by speed. I am accessing each solid element in a model and creating a unique material and property pair for it. Accessing GetCentroid,  computing new properties, making a new material, referencing it to a new property, and updating each element to reference the new property is taking 11ms per element - which runs to 3 hours for a million elements - which, sadly, just isn't that many these days. I understand that Python is interpreted so I can't expect blazing speed from it but 10 of each 11 ms is spent in FEMAP according to task manager - so is there anything I can do to improve this? Is it because I'm coming at it through Python?  Thank you,  Paul Kirchman

Solution Partner Experimenter Solution Partner Experimenter
Solution Partner Experimenter



Unfortunately the commands available in the API to get element centroids are extremely slow (as they are the standard UI commands, which aren't optimized to be called a million times in a row).  to speed this up, you will want to start with a set of all the elements you want to use, then use the GetArray function.  


This will return the data for the elements (including nodal data) in a single fast API call.  Then use that data to build a list of nodes, and use GetArray on them as well.  With that data (which will include XYZ locations) you can calculate the centroid of each element in Python.  


Unfortunately convoluted, but will run MUCH faster.


Let me know if you have any further questions!


Aswin John

Structural Design and Analysis