×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Deleting Expressions NX

Deleting Expressions NX

Deleting Expressions NX

(OP)
thread561-322813: deleting nx expressions

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.

RE: Deleting Expressions NX

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

RE: Deleting Expressions NX

(OP)
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.

:)


RE: Deleting Expressions NX

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 Not exp.IsMeasurementExpression 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

RE: Deleting Expressions NX

(OP)
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 --> vb

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!!

RE: Deleting Expressions NX

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

RE: Deleting Expressions NX

(OP)
that works for me perfectly. thank you very much!!

thumbsup2

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources