# COM Interface in VBA | 3. Creating Tool

### Technical Article

Part 2.2 of the article series about the COM interface described creating and modifying nodal supports, loads, load cases, load combinations, and result combinations on an example of a member. This part explains creating individual tools.

#### Basics

We want to create a tool that creates nonlinear nodal supports on the node currently selected in the program. The following core elements are necessary for this:

• Getting currently selected nodes
• Creating Nodal Supports
• Creating and Assigning Nonlinearities
• Creating tools in the program

In the following, these points will be processed. For this, the knowledge from the previous articles is required and will not be discussed again. However, the already available elements in the available source code, such as getting the structure in comments, are almost documented.

#### Obtain currently selected nodes

Editing selected elements via the COM interface is carried out using the already familiar functions such as IModelData.GetNodes for nodes. In order to only return the selected elements, you have to switch to the editing of the current selection via IModelData.EnableSelections. If EnableSelections is set to True, IModelData.GetNodes only returns the selected elements.Here is the concrete example:

'Get Selected Nodes'
iModDat.EnableSelections True 'Only work with selected objects
Dim nodes () as RFEM5.Node
nodes = iModDat.GetNodes
iModDat.EnableSelections False 'work with all objects again

Once the selected nodes have been fetched, EnableSelections is set to Incorrect again because it can become a frequent source of errors. Since the nodal support requires a list of nodes as a string, it is created as follows:

'Create List of Nodes
Dim nodeList as String
Dim i asLong
For i = 0 ToUBound (nodes, 1)
nodeList = nodeList & nodes (i) .No & ","
Next i

#### Creating Nodal Supports

The nonlinearity, in this case the diagram, is a separate element.It can only be assigned to a nodal support if it is already available in the structure. Therefore, it is necessary to create a nodal support according to the known principles of the previous contributions:

'Create Nodal Supports
Dim nodSup as RFEM5.NodalSupport
nodSup.No = 100
nodSup.IsColumn = False
nodSup.IsValid = True
nodSup.nodeList = nodeList
nodSup.ReferenceSystem = GlobalSystemType

nodSup.RestraintConstantX = 0.01
nodSup.RestraintConstantY = 0.01
nodSup.RestraintConstantZ = 0.01

nodSup.SupportConstantX = -1
nodSup.SupportConstantY = -1
nodSup.SupportConstantZ = -1

'Define Nonlinearity'
nodSup.SupportNonlinearityZ = WorkingDiagramType

iModDat.PrepareModification
iModDat.SetNodalSupport nodSup
iModDat.FinishModification

As can be seen in the code, the type WorkingDiagramType is indicated as nonlinearity in global z-direction.

#### Creating and Assigning Nonlinear Supports

Now that the nodal support has been prepared for the nonlinearity WorkingDiagramType, it has to be created:

'Create Nonlinearity'
Dim nldgrm as RFEM5.NonlinearityDiagram

nldgrm.ForceType = StiffnessDiagramForceType.NoneStiffnessForce
nldgrm.PositiveZoneType = DiagramAfterLastStepType.StopDiagramType
nldgrm.Symmetric = True

'Create Diagram
ReDim nldgrm.PositiveZone (3, 1)
nldgrm.PositiveZone (0, 0) = 0 #
nldgrm.PositiveZone (0, 1) = 0 #

nldgrm.PositiveZone (1, 0) = 0.01
nldgrm.PositiveZone (1, 1) = 100

nldgrm.PositiveZone (2, 0) = 0.03
nldgrm.PositiveZone (2, 1) = 200

nldgrm.PositiveZone (3, 0) = 0.05
nldgrm.PositiveZone (3, 1) = 300

The diagram consists of a two-dimensional field. The first dimension corresponds to the data record and the second dimension to the elements of the data set. Here, the first element in the data set is the displacement in m and the second the force in N. The above example thus creates four data sets, for example, where the third point of the diagram is 30 mm and 0.2 kN. This nonlinearity must now be assigned to the available support. To do this, we bring the interface to the nodal support and then to the nonlinearity:

'Apply Nonlinearity'
Dim iNodSup as RFEM5.INodalSupport
Set iNodSup = iModDat.GetNodalSupport (100, ItemAt.AtNo)
Dim iNldiag as RFEM5.INonlinearityDiagram
Set iNldiag = iNodSup.GetNonlinearity (AlongAxisZ)

iModDat.PrepareModification
iNldiag.SetData nldgrm
iModDat.FinishModification

The nonlinearity is along the Z-axis and therefore must be selected when LengthAxisZ is called. They are assigned in the well-known Modification Block.

#### Creating tools in the program

In RFEM, it is possible to integrate external programs. To do this, you have to modify RFEM.ini in the program path. In contrast to programming in C # and VB, where an executable file is created, Excel has to be used for the execution of the tool in VBA. Excel can be assigned an open file by using an argument at startup. This would look like this in the command line, where the Excel file is stored under C: \ temp and Excel is installed as a 32-bit version:
"C: \ Program Files (x86) \ Microsoft Office \ Office15 \ EXCEL.EXE" C: \ temp \ table1.xlsm

This opens Excel with the desired file. Since you can only open an external program of RFEM without additional arguments (such as the filename EXCEL.EXE in this case), you have to create a Windows batch file.This simple text file with the file extension ".bat" is like Windows covers an executable EXE file. To create a TXT file, you have to create a TXT file, for example, which receives the new file extension ".bat" after it has been created. This batch file (hereinafter EXCEL-Werzeug.bat) must have the following entry:
"C: \ Program Files (x86) \ Microsoft Office \ Office15 \ EXCEL.EXE" C: \ temp \ table1.xlsm
This corresponds to the command as it would be entered in the command line.

After creating the executable file, you can enter it in the RFEM.ini file as an external tool. RFEM.ini is located in the program path of RFEM (Standard for RFEM 5.07 64-Bit: C: \ Program Files \ Dlubal \ RFEM 5.07). The following entries have to be added:

ModuleName2 = EXCEL tool
ModuleExeName2 = "c: \ temp \ EXCEL-Werkzeug.bat"
ModuleDescription2 = Nonlinear member end release

Of course, you have to save the batch file EXCEL-Werkzeug.bat and the macro table1.xlsmauch under C:/temp/.

To ensure that the macro is also executed automatically, you have to start the subroutine in the subroutine automatically executed by Excel when you open it. This subroutine is called Workbook_Open and must be stored in the workbook.

Now it is missing that Excel will be closed after the successful execution. If an error occurs or RFEM is not opened, the macro should be able to be edited For this, a case distinction will be built in. The complete template looks like this:

Private SubWorkbook_Open ()

'Error handling routine program start RFEM
On Error GoTo e

Brings the interface to RFEM
Dim iApp asRFEM5.Application
Set iApp = GetObject (, "RFEM5.Application")

e: 'If an error occurs,' RFEM not open 'is displayed
If Err.Number <> 0 Then
MsgBox "RFEM not open", Err.Source
   Exit Sub
End If

'Error handling routine program
On Error GoTo F

COM license and program access are blocked
iApp.LockLicense

'Get Interface for Active Model
Dim iModel as RFEM5.model
If iApp.GetModelCount> 0 Then
Set iModel = iApp.GetActiveModel
Else
'Errors if there are not enough models available
Err.Raise vbObjectError, "LoadModel ()", "iApp.GetModelCount <1"
End If

'Space for own source code

f: 'Intercept errors from source code
If Err.Number <> 0 Then
MsgBox Err.Description,, Err.Source
'COM license is released, program access is possible again
iModel.GetApplication.UnlockLicense
'Tool should keep Excel open when an error occurs
   Exit Sub
End If

COM license is released, program access is possible again
iModel.GetApplication.UnlockLicense
'Tool should close Excel when the application is successful
Application.Quit

End Sub

Now, all that is missing is the reference to the previously created subroutine, which you can find under "Place for own source code". The complete source code is available in the following link.

#### Summary

The creation of the nonlinear nodal support is a supplement to the previous contributions, whereas the creation of an external module is new territory. Embedding is less problematic when using executable files, such as those typically created with the help of Visual Studio and VB or C #. For this reason, the more complex procedure with Excel was described here.