Skip to main content

Featured Post

Why I Wrote The Sheet Mechanic (And Why Calculations Aren’t Enough)

For engineers who already know the math—but still lose projects. For the last few years, I’ve been sharing technical guides here on Mechanical Design Handbook —how to size a motor, how to calculate fits, and (as you recently read) how to choose between timing belts and ball screws. But after 25 years in industrial automation, I realized something uncomfortable: Projects rarely fail because the math was wrong. They fail because: The client changed the scope three times in one week. A critical vendor lied about a shipping date (and no one verified it). The installation technician couldn’t fit a wrench into the gap we designed. University taught us the physics. It didn’t teach us the reality. That gap is why I wrote my new book, The Sheet Mechanic . This is not a textbook. It is a field manual for the messy, political, and chaotic space between the CAD model and the factory floor. It captures the systems I’ve used to survive industrial projec...
NEW RELEASE: Stop trying to be a Hero. Start being a Mechanic. Get "The Sheet Mechanic" on Amazon »
Disclosure: As an Amazon Associate, I earn from qualifying purchases.

Solid Edge Motion Simulation with Excel VBA - Part 3

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 via the API.

Advertisement

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 Editor Window
Figure 1: The VBA Editor environment.

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

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 selecting Solid Edge libraries
Figure 2: Linking the Solid Edge API libraries to Excel.

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:

Advertisement

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)
        DoEvents ' Allow the system to update the screen
    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.

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.

Watch the following video for the actual result!

Comments

Popular posts from this blog

Ball Detent Torque Limiter – Working Principle & Selection

Figure 1: The ball detent mechanism provides precise overload protection by disengaging instantly when the torque limit is exceeded. 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 Advertisement Why Choose ...

Polynomial Cams: Analysis & Design Pitfalls (Part 3)

Figure 1: Mathematical coefficients determine the physical shape. Poor math leads to physical defects like the "dip" shown on the right. In [ Polynomial Cam Function (Derivation of Fifth-degree function) - Part 2 ], we derived the equations for the Fifth-Degree (3-4-5) Polynomial . Advertisement Now, we apply this math to the real world of Mechanical Cam Design . The shape of the physical cam is determined by plotting these functions. Unlike a standard Cycloid curve, the polynomial allows us to manipulate the Start Velocity (v 0 ) and End Velocity (v 1 ) of the follower. However, this flexibility requires careful design. If the coefficients are not balanced, the physical cam profile can develop "dips" or negative slopes, causing the mechanical linkage to behave unpredictably. Case 1: Standard Dwell-to-Dwell (Zero Velocity) Figure 2: The standard profile (v 0 =0, v 1 =0). Safe, smooth, and ide...

Conveyor Belt Tension Calculation: T1, T2 & Take-Up Design

In any friction-driven conveyor system, the most fundamental concept is the relationship between the Tight Side Tension (T 1 ) and the Slack Side Tension (T 2 ) . If you get this ratio wrong, your drive pulley will slip, your belt will wear out prematurely, or your take-up counterweight will be too light to maintain traction. In this guide, we will use CEMA standard calculations to determine the correct tensions and take-up weight. Table of Contents 1. The Basics: T1 vs T2 2. Euler’s Equation (The Grip Formula) 3. Worked Example: Calculating Tensions 4. Take-Up Units: Gravity vs Screw 5. Common Failure Modes Advertisement 1. The Basics: T1 vs T2 Imagine a conveyor belt running over a drive pulley. The motor pulls the belt, creating a tension differential: T 1 (Tight Side): The tension pulling the loaded belt toward the drive pulley. This is the highest tension point in the system. ...