Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Module Module1
Sub Main()
Dim theSession As Session = Session.GetSession()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "clean expressions"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim enUS As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")
Dim expsToDelete As New List(Of Expression)
'expressions that have names starting with this prefix will be deleted
Const expDeletePrefix As String = "A00"
For Each tempExp As Expression In workPart.Expressions
'skip measurment expressions
If tempExp.IsMeasurementExpression Then
Continue For
End If
'skip geometric expressions
If tempExp.IsGeometricExpression Then
Continue For
End If
'only process "Number" expressions
If tempExp.Type = "Number" Then
If tempExp.IsNoEdit Then
lw.WriteFullline(tempExp.Name & " is locked for editing and cannot be changed until it is unlocked.")
lw.WriteFullline(tempExp.Equation)
lw.WriteFullline("")
Continue For
End If
lw.WriteFullline(tempExp.Equation)
'replace formula of expression with just the expression value
Try
'workPart.Expressions.Edit(tempExp, tempExp.GetValueUsingUnits(Expression.UnitsOption.Expression).ToString(enUS))
tempExp.RightHandSide = tempExp.GetValueUsingUnits(Expression.UnitsOption.Expression).ToString(enUS)
lw.WriteFullline(tempExp.Equation)
Catch ex As NXException
lw.WriteFullline("!! Error: " & ex.Message)
Finally
lw.WriteFullline("")
End Try
'if the expression name matches the pattern, add it to the delete list
If tempExp.Name.Length >= expDeletePrefix.Length AndAlso tempExp.Name.Substring(0, expDeletePrefix.Length).ToUpper = expDeletePrefix.ToUpper Then
expsToDelete.Add(tempExp)
End If
End If
Next
'Attempt to delete expressions
For i As Integer = expsToDelete.Count - 1 To 0 Step -1
Dim usingFeatures() As Features.Feature = expsToDelete.Item(i).GetUsingFeatures
If usingFeatures.Length > 0 Then
'expression in use, do not delete
lw.WriteFullline(expsToDelete.Item(i).Name & " is currently used by the following features and cannot be deleted:")
For Each tempFeat As Features.Feature In usingFeatures
lw.WriteFullline(" " & tempFeat.GetFeatureName)
Next
'skip to next expression
Continue For
End If
Dim usingExps() As Expression = expsToDelete.Item(i).GetReferencingExpressions
If usingExps.Length > 0 Then
lw.WriteFullline(expsToDelete.Item(i).Name & " is currently used by the following expressions and cannot be deleted:")
For Each tempExp As Expression In usingExps
lw.WriteFullline(" " & tempExp.Name)
Next
'skip to next expression
Continue For
End If
Try
lw.WriteFullline(expsToDelete.Item(i).Name & ": attempting to delete")
workPart.Expressions.Delete(expsToDelete.Item(i))
lw.WriteFullline(" deleted!")
Catch ex As NXException
lw.WriteFullline("!! Error: " & ex.Message)
lw.WriteFullline(" " & expsToDelete.Item(i).Name & " could not be deleted")
Finally
lw.WriteFullline("")
End Try
Next
lw.Close()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module