A Simple Femap API Program Tutorial: to change the element colours to blue  

The character ” ‘ ” in front of a line of VB code denotes it as a comment. It is also a good idea to use the keywords “option explicit” at the top of VB module to ensure all variables are declared explicitly. The VB editor can be configured to ensure “option explicit” always appears at the top of a module under tools-options menu check the “require declaration” box.

1) The Femap session should be open.

2) Open Excel

3) In Excel add a button to the worksheet

   Open the control toolbox (go to View-Toolbars-Control Toolbox) and click the command button icon then drag a button onto the page.

4) Double clicking the button takes you to the buttons event procedure where we add the functional code. Note by creating the button Excel has gone into design mode. This means we can resize and move the button without it executing it’s event procedure. The picture below shows the command buttons event procedure which will execute each time the button is pressed – when not in design mode.

5) Add the functional code

Option Explicit

Private Sub CommandButton1_Click()

Dim gfemap As Object

Dim oElem As Object

‘The following line will connect gfemap variable to the open femap

‘session. Using gfemap we can access all the functionallity of

‘femap from our VBA code.

Set gfemap = GetObject(, “femap.model”)

‘ the following line connects oElem to all the elements in the femap session

Set oElem = gfemap.feElem

‘ oElem is an object of type feElem and the femap user guide documments all

‘ the properties and methods the pertain to an element.

‘ the feElem object also has all the properties and methods of the entity object

‘ by inheritance.

Do While oElem.Next

  ‘ This loop will pass over ever element in the femap model

  ‘ The .Next is common to all derived from the entity object

  ‘ the first call made by calling oElem.Next set oElem to the first element in the

  ‘ model subsequent calls increment oElem to the next element.

  oElem.Color = 50

  ‘.Color is a property of the element object

  ‘ some other properties are layer,type,PropID,topology for all see femap documentaion

  ‘ oElem.PropID=1 would change the elements property ID to 1

  ‘ oElem.Color = 50 has set the elements colour to blue

  oElem.Put (oElem.ID)

  ‘The .Put will put this element back into femap with changes we have made at the

  ‘ element ID location

Loop

End Sub

6) To execute the code we must exit design mode, this is done by pressing the button illustrated below:-

Now close the VBA editor and press the command button, we added, on the worksheet. In Femap do a view regenerate or double click on the drawing area.

The elements should all have changed colour to blue.

This wasn’t a particularly useful example but might have been if you had wanted to increment all element property IDs by adding 1000. The next tutorial will generate some nodes.