In this post we will see how to load NextFEM API in CAD environments, such as AutoCAD®, by using built-in VBA interface.

 

We only need some simple steps, described in the following, to automatically load NextFEM API inside your preferred CAD environment, with a few lines of code.

Let’s see how to do that in AutoCAD, whatever is its version. First of all, if not already present, the VBA must be installed by downloading the proper module from here.

After installation, download this file and, from inside AutoCAD, go to Tools / Macro / Load project, and select the file Project.dvb you downloaded.

Write VBARUN in the command window and press enter. A new window called “Macro” will appear. Select “Modulo1.Show_Userform” and press Run. The following window will appear.

This is a demonstration of what NextFEM API can do in your CAD environment. By pressing “Open model and draw lines”, the VBA script will ask for a NextFEM model to open. Pick a model made by beams and watch it while it is drawn on screen!

Use the command Orbit to rotate the model, you’ll see the same way as in Designer in wireframe view.

How to customize the script

With the same auto-loading technique used for Excel in this article, there is a function called loadLib that automatically finds and loads NextFEM API. This can be called anytime.

Let’s have a look of the function called to draw the wireframe model. In function wireframeModel we used the object returned by NF.getNodePosition to store the vert3 object containing nodal coordinates.

Sub wireframeModel()

    ' draw the loaded wireframe model

    Call openFile

    Dim eList() As String

    eList = NF.elemsList()

   

    For i = 0 To NF.elemsNumber - 1

        Dim tp As String

        tp = NF.getElementProperty(eList(i), "type")

        ' beam

        If tp = 1 Then

            Dim conn() As String

            conn = NF.getElementConnectivity(eList(i))

            ' node 1

            Dim n1 As Object

            Set n1 = NF.getNodePosition(conn(0))

            ' node 2

            Dim n2 As Object

            Set n2 = NF.getNodePosition(conn(1))

            ' draw

            Call drawLine(n1.X, n1.y, n1.z, n2.X, n2.y, n2.z)

        End If

        ' TODO - other types

    Next i

   

End Sub

 

Sub drawLine(x1 As Double, y1 As Double, z1 As Double, x2 As Double, y2 As Double, z2 As Double)

    Dim pA(0 To 2) As Double

    Dim pB(0 To 2) As Double

    pA(0) = x1: pA(1) = y1: pA(2) = z1

    pB(0) = x2: pB(1) = y2: pB(2) = z2

    With ThisDrawing.ModelSpace

        .AddLine pA, pB

        .Item(.Count - 1).Update

    End With

End Sub

 

Only Line elements are managed by this sample code. Function drawLine finally draws all the lines with the gathered data.

Other programs may require additional step or changes in code to use VBA scripts. For instance, in BricsCAD® the user must install the VBA runtime installer contained in the installation directory, subfolder “VBA”.

 

Support and sharing in NextFEM developers’ forum

For all support enquiries about NextFEM APIs, that are free to use, please use the Developers’ forum.