Sharpen your Edge with


SeGuruCool
The Largest Independant Solid Edge Resource Outside UGS


Solid Edge - XL:VBA Parametric Part

Tushar Suradkar
www.oocities.org/SeGuruCool

  segurucool @ indiatimes.com


SeGuruCool's Newsletter

Stay updated when new tutorials and articles are posted.

(Note: Do not change first text box)
List Name:
Your Email:


SE Customization eBook
 
  • 79 Seventy-nine chapters
  •  
  • Cust. using VB and VB.NET
  •  
  • Insight Customization
  •  
  • Excel Cust w.r.t. Solid Edge
  •  
  • Coding for Custom Sensors
  •  
  • Coding for Multiple SE versions
  •  
  • XML & BOM-Database connectivity


  •   Download FREE 6 chapters & source code



    In this this tutorial, you learn :

  • How to prepare a part for parametrization.
  • How to open a part file from Excel.
  • How to access the variable table via Excel.
  • How to update a part from Excel.
  • How to save and close the part file using Excel.


  • 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     the midpoints of the vertical lines to the circle.

    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,

  • You save cooking an interface in VB.
  • Input data can be used in billing calculations alongside in the same sheet.
  • More importantly, VB is available in Excel for free just as this tutorial.


  • 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.PartDocument
     
    The 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 String
     
    To 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 = False
     
    First 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", KeyLength
     
    Change 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     segurucool @ indiatimes.com

    Also Visit :

    CadGuruCool   |   SeGuruCool   |   ProeGuruCool