Help with Hashed Expressions from Imported Parts
Help with Hashed Expressions from Imported Parts
(OP)
In our master part, we routinely import between 2 and 10 parts in various locations, depending on the application. Each of these imported parts are reused often and each contains two expressions that I would like to use to auto-fill multiple values in a table in the master part drawing.
The problem I am having is that after all the parts are imported, the expressions I am interested in end with a hash and a number ('#). The hash isn't a problem, but the number seems to correlate to the number of solids that exist, or have existed in the file. This is problematic because it means I can't be sure of what number will follow the hash and therefore can't have the appropriate code in the table to auto-fill based on the expression value.
In a past thread (thread561-239964: Program to remove '1 from expressions) John Baker uploaded a grip file to take off the hash and number combinations of non-repeating expressions. I am wondering if there is some way to reset the expressions so that the numbering after the hash will start at 1 and continue for as many expressions as I need for the part (so if I am interested in the expression "Cylinder_Height", the list will have "Cylinder_Height'1", "Cylinder_Height'2", etc.. for as many parts as I have imported). It would also work just as well if I could consistently anticipate the numbers that will follow the hash, no matter what they are.
I would really appreciate any help you guys could give. Thanks in advance.
The problem I am having is that after all the parts are imported, the expressions I am interested in end with a hash and a number ('#). The hash isn't a problem, but the number seems to correlate to the number of solids that exist, or have existed in the file. This is problematic because it means I can't be sure of what number will follow the hash and therefore can't have the appropriate code in the table to auto-fill based on the expression value.
In a past thread (thread561-239964: Program to remove '1 from expressions) John Baker uploaded a grip file to take off the hash and number combinations of non-repeating expressions. I am wondering if there is some way to reset the expressions so that the numbering after the hash will start at 1 and continue for as many expressions as I need for the part (so if I am interested in the expression "Cylinder_Height", the list will have "Cylinder_Height'1", "Cylinder_Height'2", etc.. for as many parts as I have imported). It would also work just as well if I could consistently anticipate the numbers that will follow the hash, no matter what they are.
I would really appreciate any help you guys could give. Thanks in advance.





RE: Help with Hashed Expressions from Imported Parts
John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Industry Sector
Cypress, CA
http://www.siemens.com/plm
UG/NX Museum: http://www.plmworld.org/p/cm/ld/fid=209
To an Engineer, the glass is twice as big as it needs to be.
RE: Help with Hashed Expressions from Imported Parts
RE: Help with Hashed Expressions from Imported Parts
We are currently running NX5, but will be shifting over to NX8 to take advantage of some automating features in the near future.
RE: Help with Hashed Expressions from Imported Parts
CODE
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 myExpressions as Expression()
myExpressions = workPart.Expressions.ToArray()
lw.Open
For Each myExp as Expression in myExpressions
if re.IsMatch(myExp.Name) then
lw.WriteLine(myExp.Name & " = " & myExp.RightHandSide & " evaluates to: " & myExp.Value)
i += 1
end if
Next
if i = 0 then
lw.WriteLine("No expression names matched the specified regular expression")
end if
lw.Close
End Sub
End Module
RE: Help with Hashed Expressions from Imported Parts
With a little bit of tweaking, I am able to use that code to find all the expressions with a (') and create a new expression with a name I can fully control that links back to the original expression I am interested in, regardless of the value after the (').
The one thing I am not sure about is how to use this if the expression in question is a string? The following line:
CODE
returns an error if the expression in question contains a string for a value. What needs to be modified to use strings?
RE: Help with Hashed Expressions from Imported Parts
CODE
RE: Help with Hashed Expressions from Imported Parts
CODE
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 myExpressions as Expression()
myExpressions = workPart.Expressions.ToArray()
lw.Open
For Each myExp as Expression in myExpressions
if re.IsMatch(myExp.Name) then
'lw.WriteLine("Type: " & myExp.Type)
if myExp.Type = "Number" then
lw.WriteLine(myExp.Name & " = " & myExp.RightHandSide & " evaluates to: " & myExp.Value)
else
'myExp.Type = "String", "Boolean", "Integer", "Point", "Vector"
lw.WriteLine(myExp.Name & " = " & myExp.RightHandSide)
end if
i += 1
end if
Next
if i = 0 then
lw.WriteLine("No expression names matched the specified regular expression")
end if
lw.Close
End Sub
End Module
RE: Help with Hashed Expressions from Imported Parts
Thanks again cowski!
RE: Help with Hashed Expressions from Imported Parts
Can't you rename the expressions in that specific part into names which are clearly unique ( such as "cyl_x_height" )and then run a journal that strips the "'n" ?
Regards,
Tomas
RE: Help with Hashed Expressions from Imported Parts
Thanks again to cowski for the code to get me on my way.