Monday 6 October 2014

Making of Purge Variables for Solid Edge - Part 3

...continued from Part 2

For this, a For Each loop is used as below and the respective list is flooded:

For Each oVar As variable In oVarList
  If oVar.Type = SolidEdgeConstants.ObjectType.igVariable Then
    lName.Add(oVar.DisplayName)
    lFormula.Add(oVar.Formula)
  End If
Next

Next, we will check if a variable is unused. For this each variable name in lName list is stored in sName and the variable is assumed to be unused as on the second line below:

Dim sName As String = String.Empty, sFormula As String = String.Empty
Dim bVarIsUsed As Boolean = False

A nest For Each loop is used as below and the variable name is checked against each formula to see if it is used in any of the formulas in the list.

If the formula contains the name bVarUsed is set to True and the For loop exits.

For Each sName In lName
  For Each sFormula In lFormula
    If sFormula.Contains(sName) Then
      bVarIsUsed = True
      Exit For
    End If

  Next
  If
bVarIsUsed = False Then
    lstVars.Items.Add(sName)
  Else
    bVarIsUsed = False
  End If
Next

On the other hand, if the name of the variable is not found in any of the formulas, the bVarUsed is still False as set at the beginning and the variable name is thus unused. In such case the variable name is added to the listbox on the form for unused variables.

This completes the meaty part of the code.

All of this happens immediately on the Form Load, so the user is furnished with the list of unused variables in the listbox and ready to pick those which needs to be removed.

The list box's SelectionMode is set to multiple extended so you can without hesitation offer usage of Shift and ctrl keys for multiple selection from the unused variables.

Following this, the user picks one or more variables or just uses the Select All button and click the Purge Selected button. The Purge Selected button then uses a For loop to go through selected names in the list box and looks up the name in the variables in the Solid Edge document and deletes them:

For Each oSelectedVar As Object In lstVars.SelectedItems
  Dim oVar As variable = oApp.ActiveDocument.Variables.Item(oSelectedVar.ToString)
  oVar.Delete()
Next

Continued to Part 4...

Index of all Solid Edge Tutorials, Tips, Videos...

No comments:

Post a Comment