Use FEMAP API to automate repetitive tasks and become a more efficient analyst

A Comprehensive Introduction to Femap Application Programming Interface (API)

What is the Femap Application Programming Interface (API)?

The Femap API is a means to programmatically call Femap functions from within Femap or another programming environment.
The code is an object-oriented code written in Visual Basic for Applications (VBA) language. Femap comes with an integrated programming environment but can be driven by other programs through the COM/OLE interface.

What does Structures.Aero typically use API for?

  1. Checking models against rules and requirements which is a model building best practice.
    1. Example: Ensuring all model nodes are defined in a particular coordinate system.
  2. Extending functionality
    1. Example: Printing out all dependent nodes of a group of RBE2 elements.
  3. Speeding up a modeling process
    1. Example: Updating a plate property thickness by a pshell property vs. thickness value list.
Writing an API might require some more time upfront, but the automation will pay off in the long term.

geeks and repetitive tasks

Programming within the Femap Interface

The best way to show the power of the Femap API and learn how to use it is via demonstration. This first demonstration covers a scenario where we have to check a simple model, with some non-rectangular features.

The auto-mesher has produced some triangular elements:

      mesh with hole

Our goal is to use the API to create a group with all the existing triangle elements.

You might ask: “What’s the point?” Triangle elements are generally less accurate than a quad element and artificially stiffer. An analyst could use this tool to pinpoint all the triangular elements and see where mesh modifications might be necessary.

Let’s get started.

First, open the Femap API Programming Window by clicking the button in the tool pane:

      femap menu bar

You’ll notice in the window, there is already some prewritten code.

femap API programming window

The first two lines define the Femap application object and connects the code to the current Femap session.

Dim App as femap.model

Set App = feFemap()
Next, we’ll declare an initialized set to hold the list of triangle element ID’s:

Dim elemSet As femap.Set

Set elemSet = App.feSet

Think of a set as a list or bucket of numbers.

We use various methods to programmatically isolate ID’s and then put them into the sets for later processing. In this case, it will be added to a group that we’re trying to create. Next, we’ll declare and initialize a group.

Dim newGroup As femap.Group

Set newGroup = App.feGroup

Note that this doesn’t yet create a Femap group, this is just a group object.

elemSet.AddRule(FTO_TRIA3,FGD_ELEM_BYSHAPE)

We’ll use the add rule method of the elem set/set object and narrow down selected model elements to only those of a certain shape. That’s why we’re using the FGD by shape argument for the add rule method.

The FTO_TRIA3 argument of the add rule method is used since we’re looking for three-noded triangles.

Using these two particular inputs in this way adds all triangle element IDs to the elem set/set object.

Next, we’ll create a variable to store an ID for the group we’ve created.

Dim new GroupID As Long

newGroupID = newGroup.NextEmptyID

The next empty ID method, of the group object, returns the next available Group ID in the model. We don’t have any existing groups, so right now it will just be Group 1, but if we already had a Group 1, it would automatically be placed into Group 2. This will be important when we save the new group to the model.

Add the set IDs to the group that we found before:

newGroup.SetAdd(FT_ELEM,elem.Set.ID)

Note that the IDs are associated with elements because it was a list that we had before in our set.

Lastly, we will create a title for the new group and then place the group in the database.

newGroup.title = "Triangle elements"

newGroup.Put(newGroupID)

Put will save the new group to the database using the ID found as the next empty ID.

Done.

Here’s what the final code looks like:

Sub Main
Dim App As femap.model
Set App = feFemap()

Dim elemSet As femap.Set
Set elemSet = App.feSet

Dim newGroup As femap.Group
Set newGroup = App.feGroup

elemSet.AddRule(FTO_TRIA3,FGD_ELEM_BYSHAPE)

Dim newGroupID As Long
newGroupID = newGroup.NextEmptyID

newGroup.SetAdd(FT_ELEM,elemSet.ID)

newGroup.title = "Triangle elements"
newGroup.Put(newGroupID)

End Sub

Click the play button:

      femap API programming window

Expand Groups and you will see a new group becomes available with the title that we defined. Highlighting the group shows that it has picked out all the triangle elements in the model.
      model info tree, highlight elements

RECAP: In a short amount of time, we were able to create a useful model check using a handful of methods belonging to three distinct objects: the Femap application object, the element set, and the group.

Programming in Excel (3rd Party Interfaces)

What if we wanted to modify the layer of a range of elements using Excel?

In our Excel sheet, we have a couple of cells defined:
      excel sheet

In the last demonstration, we got Femap to interact with itself, being that we were programming within its own application.

But now we’re going to drive Femap from Excel (using the COM / OLE interface in Windows).

All you need to know is that very little is different between programming in the two programs since they’re based on a similar coding language.

A couple of tricks to get started in Excel:

  1. Create Marco Enabled Worksheet
  2. Add the Developer Tab
  3. Add reference to the Femap type library file which can be found in the installation directory of Femap.
Adding a Reference to Femap Type Library:
 To add the reference, go into the Visual Basic for Applications Window.

Navigate to Tools > References.
      modifying excel type references


In the window that appears, scroll down and select Femap v1X.X.X. Press OK.


adding femap type library to excel type references


The type library includes a list of the objects, properties, and methods from the Femap API, and it makes it easier to program in Excel as you get pop-up tooltips in real time as you program.

The tooltips give you all available properties and methods related to a particular object or the parameter list for a method you may be defining. This makes it much easier to program in Excel.

Create a new Marco:

      adding an excel macro

Name the Macro “modifylayer” and press Create.

      excel macro manager

That will bring up the coding window and we can start coding for the operation.

We’re going to connect to Femap by using:

Dim app As femap.Model

Set app = GetObject (, "femap.model")

This line connects our worksheet to the current Femap session.

Next we’re going to set a variable for a return code:

Dim rc As Integer

A return code is used by the Femap API as a status, delivered after calling methods, that indicates whether an operation failed or succeeded. Methods can’t be called without capturing the return code in Excel.

(It can in Femap which is why we didn’t use that line in the first example)

Create a set to add the list of elements that are going to be moved to a different layer.

Dim elemSet As femap.Set

Set elemSet = app.feSet

Next call the add range method:

rc = elemSet.AddRange (ActiveSheet.Cells(6, 1).Value, ActiveSheet.Cells(6, 2).Value, 1)

The add range method of the element set object is the unique aspect of Excel where you specify the lower bound, upper bound, and increment using the cells in the Excel file.

Notice that we link cell (6,1) which is cell A6 and the range end in cell B6.

This will move elements with IDs between 400-500 onto a new layer.

Next, call the modify layer method of the Femap application object to enact that change:

rc = app.feModifyLayer (FT_ELEM, elemSet.ID, 2)

This takes in the type of entity being moved, which here is set as FT_ELEM.

FT_ELEM is a Femap constant that can be found in the API reference manual (we’ll discuss the reference manual later). It specifies the entity type and then we specify the set ID of the element being moved, which we previously referenced to the range of 400-500. It also takes in, as one of the arguments, the destination layer ID.

Our finalized code:

Sub modifylayer()

Dim app As femap.Model
Set app = GetObject(, "femap.model")

Dim rc As Integer

Dim elemSet As femap.Set
Set elemSet = app.feSet

rc = elemSet.AddRange(ActiveSheet.Cells(6, 1). Value, ActiveSheet.Cells(6,
2).Value, 1)

rc = app.feModifyLayer(FT_ELEM,
elemSet.ID, 2)

End Sub

If we go back to Femap and create a new layer.

      femap, creating a new layer

      femap, titling a new layer

and then press the run button in Excel.
  excel vba editor, connecting to FEMAP

you will see that all the elements between ID’s 400-500 have been moved to the new layer.
    femap, model info tree, layers

RECAP: We made a change to Femap using an external application; in this case Microsoft Excel.

Using the API to Automate Repetitive Tasks

Now that we have an idea of how to use the Femap API, let’s look at an example of how it can save you time.

In this demonstration, we’ll create a script that generates and assigns new properties to elements for all the selected groups in an orthogrid drop test model.
      
orthogrid mesh

The goal of this script is to turn each of the orthogrid webs into unique property zones.

Why?

One use of breaking up models in this manner is to allow optimization software to equally size each property to get as much mass out of the model as possible.

First we will create groups by going to Group > Operations > Generate.

      femap, group, generate

Select Methods > Property.
  femap, element selection dialog  

      femap, pick by property

Press Select All, and then check the Remove button and select the following node to unselect the outer ring.
      femap entity selection, property

     femap, remove entity from selection

Hit OK.

Select None under Attribute Breaks and NonManifold Edges under Geometric Breaks. This option creates a break so that each web segment is put in its own group.

      femap, group generate, model segments dialog

Press OK and now we have 189 groups that all correspond to new segments.
      femap, generated groups

Now we will use the API to generate and assign new properties to the elements in all 189 groups in one click!

We have this API already built and saved to our custom tools. On the toolbar, under Custom Tools, we’ll select the AutoAssignProperties API.
     femap, custom tools

A prompt shows up asking us for which groups to select. Select All to select all the groups we’ve created.
      
femap dialog select

A second prompt will ask for a starting ID, which we will set at 100. Press OK.

      femap, input window

Done.

Just like that, the script has generated and assigned new properties to all the individual webs.

To better visualize the effects, right click Properties, go to Color, and Press Random > OK.
      femap, split by property

Without using any Property Creation menus, we have now created 189 new properties.

RECAP: We used an API script to save a meaningful amount of time that would have otherwise been lost to repetitive mouse clicks in achieving the same effect of reassigning the individual web elements to new properties.


Case Study: Marine Engineering Contractor Reduces Modeling and Analysis Time by 80%

Cardinal Engineering is a small engineering contractor that primarily works with clients in the Marine sector. Three engineers were tasked with analyzing, accessing, and documenting over 1000 unique shipboard pipe hanger foundation designs within six months. The Cardinal team created programs for all the components of the piping run and hanger foundation, including linear pipe segments, bends and elbows, tees, reducers, pipe components, square tubes, U-bolts and clamps.

      

The programs for each component type included specific steps that automatically created appropriate finite element meshes, midsurfaces and nodal masses based on the geometry. The programs also accessed component technical data from Excel when needed. For postprocessing tasks, Cardinal engineers developed additional programs that determine maximum stresses from analysis output sets; perform plate pull-through and tear-out stress calculations; and calculate bolt stresses. For those calculations, the programs also output the resulting data to Excel spreadsheets. In the video below, John Hering from Cardinal Engineering details his company’s use of API’s in the analysis of ship piping for a customer.




Getting Started with Femap API: Additional Resources

Now that you have seen the power of the Femap API and how easy it is to use, we have provided some resources where you can learn more:

Femap API Reference Manual

The API Reference Manual by the Femap Team can be found in the \PDF folder in the Femap install directory. This is a comprehensive 2000+ page document that goes into great detail on all aspects of Femap API programming and is likely to have answers to any questions you might have.

FEMAP API Webinars

Introduction to Femap API Programming



Advanced API Program Development

This webinar will dive more deeply into to abilities and usefulness of API’s in FEMAP. Experience programming in Microsoft Excel, MATLAB, or other engineering programming languages is easily extensible to programming with FEMAP. The usefulness of FEMAP can be greatly extended with API programming. Custom user-written programs can also reduce modeling time and effort permitting faster analysis and decision making.



Femap API Programming: Integrating Python

The scope of Femap’s Application Programming Interface (API) can be extended beyond the reach of VBA programming. This demonstration will show how Python can be utilized to interface with Femap’s API, allowing access to Python’s large range of packages to extend the power of Femap.


    • Related Articles

    • Femap Advanced API Programming Development

      What is in this webinar? This Webinar will dive more deeply into to abilities and usefulness of API’s in FEMAP. Experience programming in Microsoft Excel, MATLAB, or other engineering programming languages is easily extensible to programming with ...
    • Intro to Femap API Programming

      What is in this Webinar? In this webinar, Eric Gustafson walks through getting up and running with Femap API programming. Experience programming in Microsoft Excel, MATLAB, or other engineering programming languages is easily extensible to ...
    • Integrating the Femap API with Python

      What is in this webinar? This demonstration will show how Python can be utilized to interface with Femap’s API, allowing access to Python’s large range of packages to extend the power of Femap. In this webinar, Blake Jeans covers programming in the ...
    • Part 1 - Femap User Interface

      Introduction Before we begin our analysis, let's get familiar with Femap’s user interface. User Interface We’ll look at the main components of the user interface, including the maps command interface, graphics windows, and tabbed and dockable panes. ...
    • FEMAP Licensing Info

      The attached presentation describes the three methods of licensing FEMAP, common problems and resolutions, and how to retrieve a license file from the Siemens Support Center. Femap Details and Licensing Advanced Support Services