NX - Renumber Expressions - Revisited 2
NX - Renumber Expressions - Revisited 2
(OP)
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
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





RE: NX - Renumber Expressions - Revisited 2
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.
RE: NX - Renumber Expressions - Revisited 2
Jeff
RE: NX - Renumber Expressions - Revisited 2
thread561-314963: Help with Hashed Expressions from Imported Parts
www.nxjournaling.com
RE: NX - Renumber Expressions - Revisited 2
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 ModuleJeff
RE: NX - Renumber Expressions - Revisited 2
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
RE: NX - Renumber Expressions - Revisited 2
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 ModuleIt only lists "p" expressions. What would be an example where there are non "p"- expressions?
Thanks,
Jeff
RE: NX - Renumber Expressions - Revisited 2
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