In this post we will see how to load NextFEM API in Excel in an automated way, with the aim to integrate seamlessly calculated models with existing Excel® spreadsheets.
Excel® spreadsheets are the most common way
to implement engineering calculations. In the majority of cases, users need to
have results coming from an FE analysis.
To ease this workflow, we released a simple VBA macro, described in the
following, to automatically load NextFEM API
inside a workbook.
First of all, we must allow macro in Excel options: go to File / Options / Trust Center / Macro Settings and activate “Enable all macros” and “Trust access to the VBA project object model”.
Then, we want to allow Excel to access .NET Framework 4.0 libraries, such as NextFEM APIs. To do so, create a file called excel.exe.config with this content:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
</startup>
</configuration>
and place it in the excel.exe folder (typically C:\Program Files\Microsoft Office\Office15 or similar, depending on version installed).
We’re done. Now simply start this Excel file, which contains all the code needed to start automatically the loading of NextFEMapi.dll.
If you don’t want or need to get in deep of how the API works, you can skip the following paragraph.
Auto-loading of NextFEM API
By pressing ALT+F11, we ca see the loading occurs by the following function, contained in present workbook.
Private Sub Workbook_Open()
If NF Is Nothing Then
Call loadLib
End If
End Sub
Using this function at start-up is not compulsory – loadLib can be called anytime just to be sure the object NF (containing APIs) is loaded and working.
Such function automatically retry the installation path and load NextFEMapi.dll consequently. This is a suitable mechanism to don’t have the need for absolute path.
Since APIs are free, even non-licensed users can use them, and API-based Excel worksheets as well.
Usage of API in Excel
All the API functions can be called from VBA code, and they can become available to worksheets by simply encapsulating each call in a UDF function, like the following one:
Function NFgetBeamForce(num As Integer, loadcase As String, tp As Integer, station As Integer) As Double
' STATION: a beam has 5 stations (1, 2, 3, 4 or 5)
' TP: 1=N, 2=Vy, 3=Vz, 4=Mt, 5=My, 6=Mz
NFgetBeamForce = NF.getBeamForce(num, loadcase, "1", tp, station)
End Function
This function can be called by any cell and any sheet in the workbook.
Happy coding with NextFEM API !