×
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

Reorder Standard Parameter in NX

Reorder Standard Parameter in NX

Reorder Standard Parameter in NX

(OP)
Hi,

is there any way to reorder all standard parameters (p*) in NX (10/11)? I have a part which have currently up to parameter-number of 9000! This is a result of some pattern-features and some changes to them for different versions. So I have e.g. no parameters within 1000 to 2000 and more like that. So I want to renumber all parameters beginning with a "p".
Do any one of you have a tool, e.g. VB for that? Or do we have a standard function?

Best regards

Michael

RE: Reorder Standard Parameter in NX

The following program renumbers all expressions that have names that contain "p-Numbers".

The program is provided as is and I take no responsibility for any issues that could be encountered :)

CODE --> .net

Imports NXOpen
Imports System
Imports System.Collections.Generic

'' Renumber the all system expressions in workpart that match the specified regular expression
Module rename_system_expressions
    Dim TS As Session
    Dim LW As ListingWindow
    Dim WP As Part

    Sub Main(ByVal args() As String)
        TS = Session.GetSession()
        WP = TS.Parts.Work
        LW = TS.ListingWindow

        Dim systemRegularExpression = "p[0-9]+"
        Dim pRegExp As Text.RegularExpressions.Regex = New Text.RegularExpressions.Regex(systemRegularExpression)

        Dim temporaryRegularExpression As String = "_temp_[0-9]+"
        Dim tmpRegExp As Text.RegularExpressions.Regex = New Text.RegularExpressions.Regex(temporaryRegularExpression)

        Dim undoMark As Session.UndoMarkId = TS.SetUndoMark(Session.MarkVisibility.Visible, "Renumber System Expressions")

        LW.Open()
        LW.WriteLine("Processing Expressions")

        Dim expDictionary As New SortedDictionary(Of String, Expression)
        Dim expArray As Expression() = WP.Expressions.ToArray


        '' find and give all matching expressions a temporary name
        For ii As Integer = 0 To expArray.Length - 1
            Dim exp As Expression = expArray(ii)

            If pRegExp.IsMatch(exp.Name) Then

                Dim matchExp As String = pRegExp.Match(exp.Name).ToString()
                Dim pNumber As Integer = Text.RegularExpressions.Regex.Match(matchExp, "\d+").ToString()
                Dim uniqueKey As String = pNumber.ToString.PadLeft(100, "0") + "-" + ii.ToString

                Try

                    WP.Expressions.SystemRename(exp, exp.Name + "_temp_" + ii.ToString)
                    expDictionary.Add(uniqueKey, exp)

                Catch ex As Exception

                    LW.WriteLine("Failed to rename " + exp.Name + " due to " + ex.Message)

                End Try

            End If

        Next

        '' Renumber the found expressions
        Dim newNameCounter As Integer = 0

        For Each pair As KeyValuePair(Of String, Expression) In expDictionary
            Dim newName As String = "p" + newNameCounter.ToString
            Dim oldName As String = tmpRegExp.Replace(pair.Value.Name, "")

            Try

                WP.Expressions.SystemRename(pair.Value, newName)
                LW.WriteLine("Renamed " + oldName + " to " + newName)
                newNameCounter += 1

            Catch ex As Exception

                LW.WriteLine("Failed to rename " + oldName + " with " + newName + " due to :" + ex.Message)

            End Try

        Next

        TS.UpdateManager.DoUpdate(undoMark)

    End Sub


End Module 

RE: Reorder Standard Parameter in NX

(OP)
Hi,

Thank you! That works fine! Is it possible to modify it in that way, that if there are some parameters like "pattern_p*", that these parameters will get also the prefix "pattern_" after renaming?

Best regards

Michael

RE: Reorder Standard Parameter in NX

Sure This should work

CODE --> VB.net

Imports NXOpen
Imports System
Imports System.Collections.Generic

'' Renumber all system expressions in workpart that match the specified regular expression
Module rename_system_expressions
    Dim TS As Session
    Dim LW As ListingWindow
    Dim WP As Part

    Sub Main(ByVal args() As String)
        TS = Session.GetSession()
        WP = TS.Parts.Work
        LW = TS.ListingWindow

        Dim systemRegularExpression = "p[0-9]+"
        Dim pRegExp As Text.RegularExpressions.Regex = New Text.RegularExpressions.Regex(systemRegularExpression)

        Dim temporaryRegularExpression As String = "_temp_[0-9]+"
        Dim tmpRegExp As Text.RegularExpressions.Regex = New Text.RegularExpressions.Regex(temporaryRegularExpression)
		
	Dim keepTextRegularExpression As String = "pattern_"
	Dim keepRegExp As Text.RegularExpressions.Regex = New Text.RegularExpressions.Regex(keepTextRegularExpression)

        Dim undoMark As Session.UndoMarkId = TS.SetUndoMark(Session.MarkVisibility.Visible, "Renumber System Expressions")

        LW.Open()
        LW.WriteLine("Processing Expressions")

        Dim expDictionary As New SortedDictionary(Of String, Expression)
        Dim expArray As Expression() = WP.Expressions.ToArray


        '' find and give all matching expressions a temporary name
        For ii As Integer = 0 To expArray.Length - 1
            Dim exp As Expression = expArray(ii)

            If pRegExp.IsMatch(exp.Name) Then

                Dim matchExp As String = pRegExp.Match(exp.Name).ToString()
                Dim pNumber As Integer = Text.RegularExpressions.Regex.Match(matchExp, "\d+").ToString()
                Dim uniqueKey As String = pNumber.ToString.PadLeft(100, "0") + "-" + ii.ToString

                Try

                    WP.Expressions.SystemRename(exp, exp.Name + "_temp_" + ii.ToString)
                    expDictionary.Add(uniqueKey, exp)

                Catch ex As Exception

                    LW.WriteLine("Failed to rename " + exp.Name + " due to " + ex.Message)

                End Try

            End If

        Next

        '' Renumber the found expressions
        Dim newNameCounter As Integer = 0

        For Each pair As KeyValuePair(Of String, Expression) In expDictionary
            Dim newName As String = "p" + newNameCounter.ToString
	    Dim oldName As String = tmpRegExp.Replace(pair.Value.Name, "")
		
	    If keepRegExp.IsMatch(oldName) then
	        newName = keepTextRegularExpression + newName
	    End If             

            Try

                WP.Expressions.SystemRename(pair.Value, newName)
                LW.WriteLine("Renamed " + oldName + " to " + newName)
                newNameCounter += 1

            Catch ex As Exception

                LW.WriteLine("Failed to rename " + oldName + " with " + newName + " due to :" + ex.Message)

            End Try

        Next

        TS.UpdateManager.DoUpdate(undoMark)

    End Sub


End Module 

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