Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

NX - Renumber Expressions - Revisited 2 1

Status
Not open for further replies.

jmarkus

Mechanical
Jul 11, 2001
377
Any NX VB.NET/Journal guru's can do the following?

1. Collect all the expressions in a part file in an array containing the name of each one as the contents of the array
2. Rename each "name" based on the position in the array (ie. p23464 is exp_array(0) and so becomes exp0 - using 'exp' instead of 'p' to avoid conflicts with existing expressions as it is going down the list.

Seems simple in concept, but I don't know about the implementation.

Ultimately I am trying to cleanup files which have had data imported into them resulting in expression names like p333'20'21'22'23'25'26'27.

(John Baker shared a Grip file which renumbers expressions in thread "NX - Renumber Expressions - Revisited", but can't handle the conflicts with duplicate names)

Thanks,
Jeff
 
Replies continue below

Recommended for you

That should be doable, but I have to ask one question. I would assume that you only want this to apply to so-called 'p' expressions, correct? I mean, you would NOT want to change the names of any user defined expressions, correct? By that I mean that you would have to assume that all expressions which DID NOT start with a single lower-case 'p' would left unchanged.

John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Industry Sector
Cypress, CA
Siemens PLM:
UG/NX Museum:

To an Engineer, the glass is twice as big as it needs to be.
 
Thanks Cowski! That was the boost I needed.

There may be some bloat here, since I didn't change your code much, but here is what worked for me:

Code:
Option Strict Off
Imports System
Imports System.Text.RegularExpressions
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display

Dim lw As ListingWindow = theSession.ListingWindow
Dim i as Integer = 0

'the pattern is set to look for 0 or more characters (\.*),
'  followed by a single apostrophe ('),
'  followed by 1 or more digits (\d+),
'  followed by 0 or more characters (\.*)
'Dim re as New RegEx("\.*'\d+\.*")

Dim re as New RegEx("\.*\d+\.*")
Dim newname() as string
Dim num_exps as integer = 0
Dim myExpressions as Expression()
Dim UserExp() as string

myExpressions = workPart.Expressions.ToArray()

lw.Open
' rename to a placeholder to avoid duplicates
For Each myExp as Expression in myExpressions
    if re.IsMatch(myExp.Name) then
		num_exps = num_exps + 1
        'lw.WriteLine("Type: " & myExp.Type)
        if myExp.Type = "Number" then
            lw.WriteLine("Renaming " & myExp.Name & " to " & "exp_" & num_exps.tostring)
			workpart.Expressions.Rename(myExp,"exp_" & num_exps.tostring)
        else
            'myExp.Type = "String", "Boolean", "Integer", "Point", "Vector"
            lw.WriteLine(myExp.Name & " = " & myExp.RightHandSide)
        end if
        i += 1
    end if
Next

' rename to standard pXX format
num_exps = 0
For Each myExp as Expression in myExpressions
    if re.IsMatch(myExp.Name) then
		num_exps = num_exps + 1
        'lw.WriteLine("Type: " & myExp.Type)
        if myExp.Type = "Number" then
            lw.WriteLine("Renaming " & myExp.Name & " to " & "p" & num_exps.tostring)
			workpart.Expressions.Rename(myExp,"p" & num_exps.tostring)
        else
            'myExp.Type = "String", "Boolean", "Integer", "Point", "Vector"
            lw.WriteLine(myExp.Name & " = " & myExp.RightHandSide)
        end if
        i += 1
    end if
Next
lw.writeline ("There are " & num_exps & " expressions which have been renumbered")

if i = 0 then
    lw.WriteLine("No expression names matched the specified regular expression")
end if

lw.Close

End Sub
End Module

Jeff
 
Test carefully!
The change you made to the regex may grab more than just the "p" expressions. The following is untested, but may be more focused on what you are after (only expressions that start with p, followed by one or more numbers, followed by an apostrophe and one or more numbers, one or more times).

Code:
Dim re as New RegEx("p\d+('\d+)+")


www.nxjournaling.com
 
I'm a little confused. When I list out all the expression using this subset of the code:

Code:
' List Expressions
' Original code from eng-tips, cowski
' Revised Oct 2013 Jeff Markus
Option Strict Off
Imports System
Imports System.Text.RegularExpressions
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim lw As ListingWindow = theSession.ListingWindow
Dim i as Integer = 0
Dim num_exps as integer = 0
Dim myExpressions as Expression()

myExpressions = workPart.Expressions.ToArray()

lw.Open
For Each myExp as Expression in myExpressions
    lw.WriteLine(myExp.Name & " = " & myExp.RightHandSide)
    i += 1
Next

lw.writeline ("There are " & num_exps & " expressions.")
lw.Close

End Sub
End Module

It only lists "p" expressions. What would be an example where there are non "p"- expressions?

Thanks,
Jeff
 
You can create a new expression (or rename a system expression) with pretty much anything you want (certain characters are not allowed and there is a length limit, but you get the idea).

Let's say someone created an expression named "diameter"; now if/when you import that file, there will be an expression named "diameter'n" (where n = some integer). If you do NOT want to rename this expression (as indicated in your answer to John's question), you will need to modify the regular expression used to test the expression names.

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor