3273x
001441
2017-05-23

COM Interface in VBA | 3. Creating a Tool

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

Basics

We want to create a tool that creates nonlinear nodal supports on the currently selected nodes in the program. To do this, the following key elements are required:

  • Get currently selected nodes
  • Creating Nodal Supports
  • Create and assign nonlinearity
  • Running a Tool in the Program

These points are to be explained now. This requires knowledge of the previous parts of this article series, which will not be described again. However, the available source code includes comments documenting the previously mentioned elements, such as getting the structure.

Get Currently Selected Node

You can edit the selected elements via the COM interface using the already known functions, such as IModelData.GetNodes for nodes. However, in order to return only the selected elements, it is necessary to switch to editing the current selection first by entering IModelData.EnableSelections. If EnableSelections is set to True, IModelData.GetNodes only returns the selected elements. Here is a specific example:

' Get selected nodes.
iModDat.EnableSelections True ' work with the selected object only
Dim nodes() As RFEM5.Node
nodes = iModDat.GetNodes
iModDat.EnableSelections False ' work with all objects again

After you get the selected nodes, EnableSelections is reset to false again, as this may often cause errors. Since the nodal support requires a list of nodes as a string for the definition, this is provided at the same time:

' Create a 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, or the diagram in the specific case, is a separate element. It can be assigned to a nodal support only if this is already available in the system. Therefore, it is necessary to create the nodal support first, following the principles explained in the previous articles:

' 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 you can see in the code, WorkingDiagramType is specified as a nonlinearity in the global z‑direction.

Creating and Assigning Nonlinear Support

After preparing the nodal support for the WorkingDiagramType nonlinearity, this should now 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 array. The first dimension corresponds to the data set, the second dimension to the elements of the data set. In this case, the first element in the data set is the displacement in m, and the second element is the force in N. Therefore, the example above created four data sets where the third point of the diagram at the location is 30 mm and 0.2 kN, for example. This nonlinearity is now to be assigned to the existing support. To do this, it is necessary to get the interface for the nodal support and then for the nonlinearity:

' Assign 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

In this case, the nonlinearity is along the Z‑axis, so AlongAxisZ must be selected when getting the nonlinearity. The nonlinearity is assigned in the known Modification block.

Running a Tool in the Program

External programs can be integrated in RFEM. To do this, it is necessary to modify the RFEM.ini in the program path. In contrast to programming in C# and VB where an executable file is created, VBA requires Excel to implement the tool. Excel can be assigned by an argument when opening a file to be opened. This appears in the command line as follows (the Excel file is 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

Thus, Excel opens with the desired file. In order to ensure that RFEM only opens the external program without the additional arguments (such as the file name behind EXCEL.EXE in this case), it is necessary to create a Windows batch file first. This simple text file including the file extension ".bat" is handled by Windows as an executable EXE file. Therefore, you can use the editor to create a TXT file, which includes the new file extension ".bat". The option to show the file extension must be set in Windows Explorer first. This batch file (hereinafter, EXCEL-Tool.bat) must then include 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 this executable file, it can be entered in RFEM.ini as an external tool. RFEM.ini is included in the program path of RFEM (default path for RFEM 5.07 64‑bit: C:\Program Files\Dlubal\RFEM 5.07). There, you should add the following entries:

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

Of course, the batch file EXCEL-Tool.bat and the macro table1.xlsm must also be stored under C:/temp/.

In order to run the macro automatically, you should start the subroutine in the subroutine executed automatically by Excel when opening the file. This subroutine is called Workbook_Open and must be stored in Workbook.

Now we need to close Excel after being executed successfully. If an error occurs or RFEM does not open, you can edit the macro. For this, a case distinction is integrated. The complete draft is as follows:

Private SubWorkbook_Open()

' Error handler run RFEM.
On Error GoTo e

' Interface with RFEM is retrieved.
Dim iApp AsRFEM5.Application
Set iApp = GetObject(, "RFEM5.Application")

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

' Error handler program.
On Error GoTo f

' COM license and program access is locked.
iApp.LockLicense

' Get interface for active model.
Dim iModel As RFEM5.model
If iApp.GetModelCount > 0 Then
   Set iModel = iApp.GetActiveModel
Else
' Display error when there are not enough models available.
Err.Raise vbObjectError, "LoadModel()", "iApp.GetModelCount < 1"
End If

' Place for your own source code.

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

' COM licence is unlocked, programme access possible again
iModel.GetApplication.UnlockLicense
' Tool closes Excel when successfully applied.
Application.Quit

End Sub

At this moment, only the reference to the previously created subroutine is missing, which is located under "Place for your own source code". The complete source code is available under the following link.

Summary

This article about creating a nonlinear nodal support describes the application of a new external module and thus complements the previous sections of this article series. Integration is more straightforward when using executable files, which are typically created using VisualStudio and VB or C#. Therefore, the complex process with Excel has been described.


Author

Mr. Günthel provides technical support for our customers.

Links
Downloads


;