Skip to main content

Disclosure: As an Amazon Associate, I earn from qualifying purchases.

Motion Simulation in Solid Edge using sketches together with Microsoft Excel VBA Programming Part III

In a previous example, I showed how to control variables in a Solid Edge sketch using a direct link to an Excel spreadsheet. In this example, I am going to show how to use VBA (Visual Basic for Applications) in Excel to control the variables in a Solid Edge sketch directly.

1. Setup Excel VBA

Create a new file in Microsoft Excel.

Press Alt+F11 to enter the Microsoft Visual Basic window as shown below.

Excel VBA Window

Right-click in the Project Explorer on the new file name (in this example, Book2) > Insert > Module.

Insert Module in VBA

2. Add References

To make Solid Edge type libraries available to the Visual Basic Object Browser, click References on the Tools menu (or Project menu depending on your version). On the References dialog box, select the Solid Edge libraries you want to access (specifically the Solid Edge Framework Type Library and Solid Edge Part Type Library).

VBA References Dialog

All variable automation is accessed through the Variables collection and Variable objects. The Variables collection serves two purposes: it allows you to create and access variable objects, and it allows you to work with dimensions as variables.

Note: When debugging programs that interact with the Variable Table, it helps to have the Variable Table displayed while stepping through the program. The Variable Table shows the results as they are executed.

3. The VBA Code

Write the following code into the VBA code area as shown below:

VBA Code Editor

You can copy & paste the following code to your Excel VBA module:

' =================================================================
' Example of using VBA code to control variables in Solid Edge document
' By AkeBlogger
' http://mechanical-design-handbook.blogspot.com/
' =================================================================

Sub Simulate()
On Error Resume Next

    'Declare the program variables.
    Dim iAngle As Single
    Dim objApp As Object
    Dim objVariables As Object
    Dim objVariable As Object
   
    '------------------------------------------------------------------
    ' Checking for the active document
    Set objApp = GetObject(, "SolidEdge.Application")
    If Err Then
        MsgBox "You must open Solid Edge before executing this module!!", vbOKOnly + vbExclamation, "Error"
        Exit Sub
    Else
        'Connect to a running instance of Solid Edge.
        Set objApp = GetObject(, "SolidEdge.Application")
        'Access the Variables collection.
        Set objVariables = objApp.ActiveDocument.Variables
    End If
       
    'SIMULATION STARTS!
    For iAngle = 0 To 360 Step 2
        ' Changing Cam_angle from 0, 2, 4, ..., 360 deg.
        Call objVariables.Edit("Cam_angle", iAngle)
       
    Next iAngle
   
End Sub

4. Prepare Solid Edge

Open the Solid Edge file from the previous example and delete the formula in the Variables Table as shown below. In this example, we will control the variable (dimension) using VBA code directly, so the link to the external spreadsheet must be removed.

Solid Edge Variable Table Cleanup

5. Run Simulation

We are now ready for simulation. Resize the VBA Windows as shown below so you can see both applications, then click run to see the result.

Running the Simulation

Watch the following video for the actual result!

Comments

Popular posts from this blog

Hoekens Linkage: Kinematics, Optimization, and Walking Robot Applications

Introduction to the Hoekens Linkage The Hoekens linkage is a specialized four-bar mechanism designed to convert rotational motion into an approximate straight-line motion. While it serves a similar purpose to other straight-line generators, its unique coupler curve—a "tear-drop" shape—makes it exceptionally useful for intermittent motion and walking machines. One of the most fascinating aspects of kinematic theory is the concept of "Cognates." The Hoekens linkage is actually a cognate linkage of the Chebyshev Straight-line Mechanism . This means that while the physical structure and link lengths differ, they can generate the exact same coupler curve geometry. Search for "Design of Machinery" Books Kinematics and Optimization Unlike the Watt linkage, which has a central pivot, the Hoekens linkage relies on a rotating crank to drive a floating coupler arm. The "straight" portion of the curve occurs when t...

The Engineer’s Guide to Precision Alignment: Dowel Pins, Diamond Pins & Fits

The Unsung Heroes of Precision: Dowel Pins & Locators In the world of Precision Engineering , the difference between a high-quality product and a scrap part often comes down to microns. While bolts hold parts together, they are terrible at positioning them. This is where Dowel Pins and Locating Pins become essential components in industrial tooling . What is a Dowel Pin? Dowel pins are precision-ground fasteners used to secure the relative position of two parts. They are typically machined to extremely tight tolerances (often within 0.0001 inches) and are available in materials like: Hardened Steel: For high-wear applications in CNC fixtures . Stainless Steel: For corrosion resistance in medical or food processing machinery. Plastic (Thermoplastic): For lightweight, non-conductive, low-load alignment. The "Play" Problem: Bolts vs. Dowels When mechanical design engineers create components, they cannot rely on bolts for alignm...

Ball Detent Torque Limiter: The Ultimate Guide to Precision Overload Protection

The First Line of Defense: Overload Clutches In high-speed automation and heavy industrial machinery, a "jam" is not a matter of if , but when . Whether it is a cardboard box getting stuck in a packaging machine or a tool crashing in a CNC lathe, the resulting torque spike can destroy gearboxes, twist shafts, and burn out expensive servo motors in milliseconds. A torque limiter (or overload clutch) is the mechanical fuse of the drive system. While electronic monitoring (current limiting) is common, it is often too slow to prevent physical damage from the massive kinetic energy stored in the system inertia. A mechanical torque limiter provides a physical disconnect that operates in a fraction of a second. Search for Torque Limiters & Safety Couplings Why Choose a Ball Detent Limiter? Not all torque limiters are created equal. In precision applications, the Ball Detent type is superior to friction or shear types for several reason...