FREE Thai Alphabet Game F for Android

FREE Thai Alphabet Game F for Android
Free app for learning Thai Alphabet on Play Store

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


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

1) Create new file in Microsoft Excel

2) Press Alt+F11 to enter Microsoft Visual Basic windows as shown below



3) Right click in Project Explorer at new file name (in this example, new excel file name = Book2) > Insert > Module



4) To make Solid Edge type libraries available to the Visual Basic Object Browser, click References on the Project menu. On the References dialog box, select the Solid Edge libraries you want to access.



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.

5) Write the following code into VBA code area as shown below,


You can copy & paste the following code to your excel VBA.

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

6) Open Solid Edge file from previous example and delete formula in Variables Table as shown below, because in this example we will control the variable (dimension) using VBA code directly.



7) We are now ready for simulation. Resize VBA Windows as shown below then click run to see the result.



Watch the following video for the actual result!




Read more info about Mechanical Engineering Design Handbook

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


To link solid edge variables to Microsoft excel, we can do by enter formula in the variables table or use VBA code.

In this post, I would like to show how easy to make it using variables table.You can use the Variable Table to define and edit functional relationships between the dimensions of a design in a familiar spreadsheet format. The Variable Table is accessed with the Variables command on the Tools menu. There are two types of variables: dimensions displayed in the design and variables you create directly in the Variable Table. Dimension variables directly control elements of a design. Variables that you create cannot directly control the design. A user variable must be set equal to a value or mathematical expression; for example, PI = 3.14159. Variables can control dimensional relationships by setting the dimensional relationship equal to the variable.Let's begin...

1) Open Solid Edge and make a sketch as shown below.

Mechanism in this example consists of cam, lever, pull rod and four bar linkage. What we have to do is to make sure that when the dimension of cam angle is changed, the entire mechanism is changed accordingly. You can simply check this by clicking at the dimension of cam angle and rotate your mouse wheel to see if entire mechanism is moving or not.

2) To set the name of variables
When you place the dimensions for a design, variables for these dimensions are placed into the variable table automatically. If the Variable Table is open, any dimension that is placed by you or the software will display in the Variable Table after the dimension is placed. Working with the variable table open allows you to change the dimension's name that is generated by the software to a more logical name as you work. When you rename variables, the variable names should contain only letters, numbers, and the underscore character. You should not use punctuation characters.In Solid Edge: select Tools > Variables > Variables
and click at the dimension you want to change the name, for this case select the dimension of cam angle and change "Name" to cam_angle and select "Expose" to show this variable name to other application as shown below.



3) To Link variable in Solid Edge to Microsoft Excel
You can use Microsoft Excel or other spreadsheet software to link Solid Edge variables to a spreadsheet. Before you can link variables to a spreadsheet, you must first create the variables you want in the Solid Edge document. When you link Solid Edge variables to a spreadsheet, the document names and folder path for the spreadsheet and the Solid Edge document should contain only letters, numbers, and the underscore character. You should not use punctuation characters. To successfully edit the linked Solid Edge variables from the spreadsheet later, you must open the Solid Edge and spreadsheet documents in a specific order:
  • You can open the spreadsheet document first, then open the linked Solid Edge document.

  • You can open the Solid Edge document first, then click the Edit Links command on the shortcut menu when a linked formula is selected within the variable table. You can then use the Open Source option on the Links dialog box to open the spreadsheet document.

Create new Microsoft Excel workbook and save it.
In this example, the excel file is saved at E:\Online Biz\Web\Blog\Mechanical-Design-Handbook\

File name: ex-solid-edge1.xls
and this is the content of the excel file...


In Solid edge: select Tools > Variables > Variables and select the variable named "cam_angle" as you created in previous step. And enter the following formula into "Formula" column,

@'E:\Online Biz\Web\Blog\Mechanical-Design-Handbook\ex-solid-edge1.xls'!'Sheet1!R2C2'

You can see that to link to another excel workbook, just put "@" at the first position then follows by the path and file name of excel file, sheet name and cell.

R2C2 means row 2 and column 2 which is cell B2

Now solid edge variable is linked and control by cell value in excel.
If you change value in cell B2, you will see the movement of cam mechanism.

Watch the result in the following video clip.



Read more info about Mechanical Engineering Design Handbook