Dim App as femap.model
Set App = feFemap()
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:
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:
In the window that appears, scroll down and select Femap v1X.X.X. Press OK.
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:
Name the Macro “modifylayer” and press Create.
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.
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.
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.
Hit OK.
Now we will use the API to generate and assign new properties to the elements in all 189 groups in one click!
Done.
Just like that, the script has generated and assigned new properties to all the individual webs.
Without using any Property Creation menus, we have now created 189 new properties.
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.
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:
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.