![]() SeGuruCool The Largest Independant Solid Edge Resource Outside UGS |
www.oocities.org/SeGuruCool ![]() |
|
|
In this this tutorial, you learn :
It is assumed that you are familiar with the basics of Solid Edge and Excel. | ![]() |
Define the Goal We want to control parameters of a shaft with a keyway as shown in figure. Start with creating the part. Make a circle in the x-z plane and protrude it. | ![]() |
The Constraints For the keyway cutout, sketch a rectangle as shown. Connect ![]() See figure (brown arrows). This ensures symmetry for the keyway slot both horizontally and vertically. Also, the keyway slot stays with the shaft all the time, i.e. even when the diameter of the shaft changes, we need not worry about the position of the keyway from the center of shaft. | ![]() |
The Cutout Complete the cutout and do not make it through. We want to control the length of the keyway as a parameter. Check the part consistency by modifying the width and height of the cutout (keyway) and the diameter of the shaft. Two Connect contraints have done the trick. | ![]() |
Setup the Variables Rename the shaft diameter variable to ShaftDia as shown in figure. Similarly, ShaftLength, KeyWidth, KeyDepth and KeyLength. Save the part and quit Solid Edge. | ![]() |
Prepare Excel Start Excel and fill the first few cells as shown in figure. Although, these values can be manually linked to the variable table, (see Excel and VLookUp) or, can be controlled from a VB program, (see VB:SE parametric Part) this tutorial shows you how to do it using Excel and VBA together. By using Excel as a front end, | ![]() |
Coding Time Still in Excel, press Alt +F11 This brings up the built-in VBA (Visual Basic for Applications) environment. Double click Sheet1 in the Project explorer to bring up the code window for Sheet1. | ![]() |
Involve Solid Edge In the Excel VBA window, Click Tools > References Select Solid Edge Framework and Part type libraries. This helps Excel communicate with Solid Edge. | ![]() |
Start Coding Start coding by opting to declare variables explicitly. Option Explicit Dim strFName As String Dim objVars As Variables Dim objApp As SolidEdgeFramework.Application Dim objDoc As SolidEdgePart.PartDocumentThe string strFName holds the filename of the Solid Edge part file containing the Shaft. Also, declare object variables for the Solid Edge application, the part document and the variables collection. Download (40 kb) Excel 2000 and SE14 Part files for this tutorial. To see the code, open the xls file and press Alt + F11 |
More Variables The datatype for the paramters to control is string and not numeric. Dim strFName As String Dim ShaftLength As String Dim ShaftDia As String Dim KeyWidth As String Dim KeyDepth As String Dim KeyLength As StringTo assign the cell values to the corresponding variables, use the following code : Sub UpDatePart() ShaftLength = CStr(Range("B1").Value) ShaftDia = CStr(Range("B2").Value) KeyWidth = CStr(Range("B3").Value) KeyDepth = CStr(Range("B4").Value) KeyLength = CStr(Range("B5").Value)In Excel, the Range refers to a single cell or a group of seleted cells. Programatically, Range is part of the the WorkSheet object. Here, Range is undestood, since we are already coding in Sheet1. Also, the cell value is converted to string using CStr before assiging it to the string variable. |
Go for It
Sub UpdatePart() strFName = ThisWorkbook.Path & "\Shaft.par" On Error Resume Next Set objApp = GetObject(, "SolidEdge.Application") If Err Then Err.Clear Set objApp = CreateObject("SolidEdge.Application") Set objDoc = objApp.Documents.Open(strFName) End If objApp.DisplayAlerts = False objApp.Visible = FalseFirst strFName is assigned the filename. In this case, the part file Shaft.par sits in the same folder as the workbook (Excel file), hence, ThisWorkBook.path. Also note the leading Slash ( \ ) in the filename Press Ctrl + G in Excel's VBA environment to bring up the Immediate Window. Type MsgBox (ThisWorkBook.Path) and press Enter - note that the trailing backslash ( \ ) is missing - hence we add it to the filename string as a leading slash. Next we try to connect to a running instance of Solid Edge using GetObject. If SE is not running, fire it up using CreateObject. Open the part file and assign it to ObjDoc and keep SE invisible throughout. If you keep objApp.Visible = True, you can see the things in action. But that would be an innocent way to stare at things. Watch it happening - like a pro Before you click the Update button on the form, right click the taskbar (Win 2000 only) and select Task manager.... In the task manager dialog, take the Processes tab and click on the Image Name column header twice ( do not double-click ) to list the processes alphabetically. Edge.exe should be one of them, but only until the objects are set to Nothing. You can also see the Mem Usage figure flare up at key time intervals, especially when the part document is opened and the program is updating the variables. |
The Meaty Part objVars is assigned the variables collection from the variable table of Solid Edge part file. Set objVars = objDoc.Variables objVars.Edit "ShaftDia", ShaftDia objVars.Edit "ShaftLength", ShaftLength objVars.Edit "KeyDepth", KeyDepth objVars.Edit "KeyWidth", KeyWidth objVars.Edit "KeyLength", KeyLengthChange the value of the variables using the Edit method of the objVars collection object. Here the first "ShaftDia" (in double quotes) is the name of the variable from the variable table of the part file. The second ShaftDia (without double quotes) is the string variable from the txtShaftDia textbox. |
Scavenging The Scavenging stuff in any Solid Edge - VB customization thing should be taken seriously. Call objDoc.Save Call objDoc.Close Call objApp.Quit Set objApp = Nothing Set objDoc = Nothing Set objVars = Nothing MsgBox "Done !", vbExclamation End Sub |
Who will call the Sub ? Switch to the Excel spreadsheet using Alt+Tab Select View > Toolbars > Forms Create a button as shown. The Click event of this button will call the UpdatePart Sub. | ![]() |
Assign the Sub As soon as you create the button, the Assign Macro dialog comes up. Select the only sub available and click OK In case you miss the dialog, right click the button and select Assign Macro. | ![]() |
Tushar Suradkar ![]() |