A Simple Femap API Program Tutorial: to generate nodes from an Excel listing.
1) Follow steps 1 to 4 in tutorial 1 to add a command button to the worksheet
2) Add Node ID,X,Y,Z data to the worksheet as illustrated below:-
The program will read each line of data on the worksheet, until it reached a blank in column 1, and create a node at the specified location.
3) The code:-
Option Explicit
Private Sub CommandButton1_Click()
Dim gfemap As Object
Dim oNode As Object
Dim iNID As Integer ‘Will be used to count the node ID
Dim dx As Double
Dim dy As Double
Dim dz As Double
Dim WS As Worksheet
Dim iRow As Integer
‘The following line sets the ws variable to the worksheet we have our data and button on.
‘This allows us to reference the cells with out specifying ThisWorkbook.Worksheets(“Sheet1”).cells(R,C)
‘instead WS.Cells(R,C) is equivalent. You could also just write .Cells(R,C) but this would be assuming
‘your using the active worksheet which is not always the case.
Set WS = ThisWorkbook.Worksheets(“Sheet1”)
‘The following line will connect the gfemap variable to the open femap
‘session. Using gfemap we can access all the functionality of
‘femap from our VBA code.
Set gfemap = GetObject(, “femap.model”)
‘The next line connects oNode to the femap feNode object
Set oNode = gfemap.fenode
iRow = 2 ‘ The start row of our data in Excel
Do While WS.Cells(iRow, 1) <> “” ‘ loop until no data is in cells(iRow,1)
iNID = WS.Cells(iRow, 1) ‘ The node id we specified on the worksheet
dx = WS.Cells(iRow, 2) ‘ the X,Y & Z ordinates specified on the worksheet
dy = WS.Cells(iRow, 3)
dz = WS.Cells(iRow, 4)
‘Set the femap node to our read values
oNode.ID = iNID
oNode.x = dx
oNode.y = dy
oNode.z = dz
‘Put it into femap
oNode.put (iNID)
iRow = iRow + 1 ‘increment the Excel row number
Loop ‘Repeat until done
End Sub
3) Exit design mode, press the command button and do a Femap regen. The nodes should have been created as illustrated below:-
The next tutorial will generate some elements.