Showing results for 
Search instead for 
Do you mean 

Writing the FEMAP API in Python

by Siemens Experimenter Siemens Experimenter ‎11-30-2016 12:25 PM - edited ‎12-08-2016 01:26 PM

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:

https://sourceforge.net/projects/pywin32/files/pywin32/Build%20220/

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”.

Comments
by Dreamer
2 weeks ago

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

Contributors