Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Reorder Standard Parameter in NX

Status
Not open for further replies.

MEichWey

Industrial
Apr 16, 2014
39
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
 
Replies continue below

Recommended for you

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:
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
 
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
 
Sure This should work

Code:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor