Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Expressions NX 1

Status
Not open for further replies.

wOOzi

Automotive
May 4, 2015
10
thread561-322813

Hello together,

I am an engineer from Germany and I have a question. I would like to refer to the above mentioned thread.
sooooo I need a vb script that will do the following:
Tools --> Expressions --> now the script should replace the formula with the value. So formula = value. --> then I want to delete the user expressions named "A00..."

would be nice if someone could help me with a vb script.
in the above mentioned script there was a similar solution but the script didn't work in my NX Version.

I am using NX 9

thank you in advance.
 
Replies continue below

Recommended for you

What is it about the referenced journal that does not work for you? Do you get error messages or it just doesn't do quite what you want?

www.nxjournaling.com
 
hey. thank you for your Response.

I get error Messages when I try to run the Journal.
Obviously it is because there a measures in the part. But I need them... is it possible to skip measures?
And I would like to have added a command to delete all user defined expressions afterwards.

:)


Journal_Editor_-_New_File_sht43u.jpg
 
The fix for the above error is in the thread you reference, post by petulf dated 30 May 12 18:17:

Code:
If exp.Type.Contains("Number") And [highlight #FCE94F]Not exp.IsMeasurementExpression[/highlight] Then
    'process expression


Deleting all the user expressions will be a bit trickier. Differentiating between user expressions and system expressions may be difficult; a quick look through the API documentation didn't reveal a method to tell the difference. Also, if any feature directly references a user expression, the expression will not be able to be deleted until the dependency is broken.

www.nxjournaling.com
 
yeah you are right. Thank you! didn't see that...

Could you please help me and say where do I have to put that fix? I am not so familiar with VB programming. This is the VB script by petulf I need:
Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module simplify_expressions

    Dim s As Session = Session.GetSession()
    Dim enUS As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")

    Sub Main()

        Dim dp As Part = s.Parts.Display
        Dim exps() As Expression = dp.Expressions.ToArray()

        For Each thisExp As Expression In exps

            If thisExp.Type.ToString().Contains("Number") Then

                dp.Expressions.Edit(thisExp, thisExp.Value().ToString(enUS))

            End If

        Next

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        Return Session.LibraryUnloadOption.Immediately

    End Function

End Module

next question. My user expressions always start with "A00.......". Do you think it is possible to say please delete all expressions with the name beginning of "A00"?

I hope you understand what I mean :D

thank you!!
 
Not extensively tested, but this should work for most cases...

Code:
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

www.nxjournaling.com
 
that works for me perfectly. thank you very much!!

[thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor