Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

"ug_askcurrentworkpart()" Journal to write processed results to attributes? 1

Status
Not open for further replies.

3dr

Automotive
Jul 10, 2004
451
Is this a thing?

I'm doing this now in the expression system and need to use Update For External Change to force updates.
This is fine on small assemblies but get exponentially more painful as they get larger.




Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Replies continue below

Recommended for you

Journals can access the current work part and process attributes without forcing a full update. The part of interest will need to be fully loaded to write attribute info and saved when you are done with it. Does this answer your question?

www.nxjournaling.com
 
Thanks Cowski
I know journals can write to attributes.... just wanted to be sure if Ug_Askcurrentworkpart could be used that way.
The askcurrentworkpart function requires a forced update. But if running the journal would force it I wouldn't need to use the update for external change.

If so, I can go out and try to get that developed by a third party. I just don't have that skill but this ones important.
It would put away some serious pain and suffering.

Any journal would need to be able to process all loaded parts if they have the two attributes required to write to are present.




Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
A journal can read the work part info and write what is needed to one or more attributes. It would not force, or require, a full update. However, if/when the workpart info changes, the journal would need to be run again to update the info. The results of the journal would not be associative in any way to the work part name.

The advantage of ug_AskCurrentWorkPart is that it is somewhat associative. It won't automatically update as soon as the part name changes, but an "update for external change" would cause it to update. Pros and cons both ways, I suppose.

If the journal method sounds like it might work for you, post more of what you are looking for. I'm currently working on a journal that reads/writes attribute info; I might be able to reuse some of that code to get you something useful.

www.nxjournaling.com
 
Thanks Cowski if you can help I'm very grateful.

I have no problem re-running the journal to force the updates, I'm doing that now anyway.

Lets see what you think.
It maybe a lot simpler to just force a refresh of the ug_askcurrentworkpart() source expression I'm using.
If I could do that everything would update on it's own. Just not sure how that could be done.
1. make new expression
2. replace the old one
3. delete the old one
4. rename the new one
Sounds messy....

The purpose:
I'm using a file naming scheme to drive part numbers and revision levels.
ug_askcurrentworkpart() pulls the file name and the result is processed through a series of down stream expressions.
The end result of those is communicated to the respective attributes by either ug_setPartAttrValue or a link.

Naming Scheme
ProjectorJobNumber_Part-Number__Rev

Example: JOB1234_010-01___00
ATTR, "PL_PROJ_NO" = JOB1234
ATTR, "PL_PART_NO" = -010-01

ATTR, "DR_0_REV" = -
or
ATTR, "DR_1_REV" = A (if suffix is "__01")
ATTR, "DR_2_REV" = A (if suffix is "__02")
and so on.

Attached are the expressions that do the work.
If refreshing the the source expression "WRK_PRT_NAME" in the attached isn't an option, that may require the logic to be done in the journal...
I defer to your opinion on all this regardless of wether you could help.
There may be an all together better method than this anyway.

TIA











Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
 https://files.engineering.com/getfile.aspx?folder=42748edd-a6de-4219-8b5c-d1805f9ac7f8&file=Workpart_Expressions.exp
A quick test of the method above worked fine.
1. make new expression
2. replace the old one
3. delete the old one
4. rename the new one

The raw journal output is below.
If this could be captured and applied to all loaded parts in the assembly with an expression named "WRK_PRT_NAME"
That would do it.



' NX 1847
' Journal created by dyoutz on Fri Jan 25 12:06:59 2019 Eastern Standard Time
'
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

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

Dim displayPart As NXOpen.Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Tools->Expressions...
' ----------------------------------------------
theSession.Preferences.Modeling.UpdatePending = False

Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

theSession.SetUndoMarkName(markId1, "Expressions Dialog")

' can not output code to find <Model4.prt E>: <UGS::ExpKf::ExpGroup> &00007FF4E6BA3E08 [1636186]
Dim nullNXOpen_ExpressionGroup As NXOpen.ExpressionGroup = Nothing

workPart.ExpressionGroups.Active = nullNXOpen_ExpressionGroup

Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Rename Exp")

Dim expression1 As NXOpen.Expression = CType(workPart.Expressions.FindObject("WRK_PRT_NAME"), NXOpen.Expression)

workPart.Expressions.Rename(expression1, "WRK_PRT_NAME_OLD")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As NXOpen.Session.UndoMarkId = Nothing
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Create Expression")

Dim expression2 As NXOpen.Expression = Nothing
expression2 = workPart.Expressions.CreateExpression("String", "WRK_PRT_NAME=replaceString(ug_askcurrentworkpart(), "".prt"", """")")

Dim markId4 As NXOpen.Session.UndoMarkId = Nothing
markId4 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Check Circular")

Dim objects1(0) As NXOpen.NXObject
objects1(0) = expression2
theSession.UpdateManager.MakeUpToDate(objects1, markId4)

expression2.EditComment("")

Dim markId5 As NXOpen.Session.UndoMarkId = Nothing
markId5 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Update Expression")

Dim nErrs1 As Integer = Nothing
nErrs1 = theSession.UpdateManager.DoUpdate(markId5)

theSession.DeleteUndoMark(markId5, "Update Expression")

Dim markId6 As NXOpen.Session.UndoMarkId = Nothing
markId6 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Replace Expressions")

Dim markId7 As NXOpen.Session.UndoMarkId = Nothing
markId7 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Start")

Dim replaceExpressionsBuilder1 As NXOpen.ReplaceExpressionsBuilder = Nothing
replaceExpressionsBuilder1 = workPart.Expressions.CreateReplaceExpressionsBuilder()

theSession.SetUndoMarkName(markId7, "Replace Expressions Dialog")

' ----------------------------------------------
' Dialog Begin Replace Expressions
' ----------------------------------------------
replaceExpressionsBuilder1.CurrentString = "WRK_PRT_NAME_OLD"

replaceExpressionsBuilder1.ReplaceString = "WRK_PRT_NAME"

Dim markId8 As NXOpen.Session.UndoMarkId = Nothing
markId8 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Replace Expressions")

theSession.DeleteUndoMark(markId8, Nothing)

Dim markId9 As NXOpen.Session.UndoMarkId = Nothing
markId9 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Replace Expressions")

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = replaceExpressionsBuilder1.Commit()

theSession.DeleteUndoMark(markId9, Nothing)

theSession.SetUndoMarkName(markId7, "Replace Expressions")

replaceExpressionsBuilder1.Destroy()

theSession.DeleteUndoMark(markId7, Nothing)

Dim markId10 As NXOpen.Session.UndoMarkId = Nothing
markId10 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Expressions")

Dim markId11 As NXOpen.Session.UndoMarkId = Nothing
markId11 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Make Up to Date")

Dim markId12 As NXOpen.Session.UndoMarkId = Nothing
markId12 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Edit Expression")

Dim expression3 As NXOpen.Expression = CType(workPart.Expressions.FindObject("WRK_PRT_REV_03"), NXOpen.Expression)

workPart.Expressions.Edit(expression3, "replaceString(WRK_PRT_NAME, ""__"", ""/"")")

Dim markId13 As NXOpen.Session.UndoMarkId = Nothing
markId13 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Edit Expression")

Dim expression4 As NXOpen.Expression = CType(workPart.Expressions.FindObject("WRK_PRT_NO_02"), NXOpen.Expression)

workPart.Expressions.Edit(expression4, "if(Length(WRK_PRT_REV_02 )>1)(nth(1,WRK_PRT_REV_02))else(WRK_PRT_NAME)")

Dim objects2(1) As NXOpen.NXObject
objects2(0) = expression3
objects2(1) = expression4
theSession.UpdateManager.MakeUpToDate(objects2, markId11)

Dim markId14 As NXOpen.Session.UndoMarkId = Nothing
markId14 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "NX update")

Dim nErrs2 As Integer = Nothing
nErrs2 = theSession.UpdateManager.DoUpdate(markId14)

theSession.DeleteUndoMark(markId14, "NX update")

theSession.DeleteUndoMark(markId11, Nothing)

theSession.DeleteUndoMark(markId10, Nothing)

theSession.SetUndoMarkName(markId1, "Expressions")

theSession.DeleteUndoMark(markId6, Nothing)

Dim markId15 As NXOpen.Session.UndoMarkId = Nothing
markId15 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

theSession.SetUndoMarkName(markId15, "Expressions Dialog")

' ----------------------------------------------
' Dialog Begin Expressions
' ----------------------------------------------
Dim markId16 As NXOpen.Session.UndoMarkId = Nothing
markId16 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Delete Expression")

workPart.Expressions.Delete(expression1)

Dim markId17 As NXOpen.Session.UndoMarkId = Nothing
markId17 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "NX update")

Dim nErrs3 As Integer = Nothing
nErrs3 = theSession.UpdateManager.DoUpdate(markId17)

theSession.DeleteUndoMark(markId17, "NX update")

Dim markId18 As NXOpen.Session.UndoMarkId = Nothing
markId18 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Expressions")

theSession.DeleteUndoMark(markId18, Nothing)

Dim markId19 As NXOpen.Session.UndoMarkId = Nothing
markId19 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Expressions")

Dim markId20 As NXOpen.Session.UndoMarkId = Nothing
markId20 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Make Up to Date")

Dim markId21 As NXOpen.Session.UndoMarkId = Nothing
markId21 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "NX update")

Dim nErrs4 As Integer = Nothing
nErrs4 = theSession.UpdateManager.DoUpdate(markId21)

theSession.DeleteUndoMark(markId21, "NX update")

theSession.DeleteUndoMark(markId20, Nothing)

theSession.DeleteUndoMark(markId19, Nothing)

theSession.SetUndoMarkName(markId15, "Expressions")

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module






Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Hello,
the best way to read file name and capture attributes is to use:

Code:
Dim file_name as string
Dim textlength as Integer
Dim PL_PROJ_NO as String
Dim PL_PART_NO as String
Dim DR_0_REV as String

file_name = workpart.leaf()	
textlength = file_name.length

if TextLength >= 19 then
[indent]PL_PROJ_NO = file_name.Substring(0, 7)[/indent]
[indent]PL_PART_NO = file_name.Substring(7, 7)[/indent]
[indent]DR_0_REV = file_name.Substring(17, 2)[/indent]
[indent]workPart.SetUserAttribute("PL_PROJ_NO",-1, PL_PROJ_NO, update.option.now)[/indent]
[indent]workPart.SetUserAttribute("PL_PART_NO",-1, PL_PART_NO, update.option.now)[/indent]
[indent]workPart.SetUserAttribute("DR_0_REV",-1, DR_0_REV, update.option.now)[/indent]
end if

With best regards
Michael
 
3dr said:
A quick test of the method above worked fine.
1. make new expression
2. replace the old one
3. delete the old one
4. rename the new one

Seems like a bit more work than necessary; would it be OK to edit the existing expression formula rather than creating a new expression and replacing the old?

www.nxjournaling.com
 
Most definitely Cowski.

The reason I did that was to force a refresh of the ug_askcurrentworkpart() call

Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
I'm assuming there is another way to force the refresh in a Journal?

Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
If you don't mind re-running the journal to get fresh information, I'd suggest getting rid of the ug_askCurrentWorkPart call and letting the journal write the part name directly into the WRK_PRT_NAME expression formula. The other expressions can be left as-is to parse out the info; or the journal could parse the part name and feed the info directly to the expressions (or attributes).

www.nxjournaling.com
 
I don't mind re-running the journal.
Unless Siemens provides a live update mechanism for it it's the only way at this point.

I think if I had my pick of the two, I would let the journal parse it and write directly to the attributes.
It would be easier to manage the behavior down the road without touching every seed part in a big library.




Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
3dr said:
Naming Scheme
ProjectorJobNumber_Part-Number__Rev

Example: JOB1234_010-01___00
ATTR, "PL_PROJ_NO" = JOB1234
ATTR, "PL_PART_NO" = -010-01

ATTR, "DR_0_REV" = -
or
ATTR, "DR_1_REV" = A (if suffix is "__01")
ATTR, "DR_2_REV" = A (if suffix is "__02")
and so on.

I'm putting together some code to parse the file name and could use some clarification. It looks like everything up to the first underscore character is the project number, followed by part number, followed by a revision code. Something like: (project)_(part #)__(rev). Is this basically correct?

I'm a bit fuzzy on what "DR_#_REV" attribute is for. If the suffix is "__02" will the part have 3 attributes: "DR_0_REV", "DR_1_REV", and "DR_2_REV"?

www.nxjournaling.com
 
You have the file naming scheme correct.

DR_#_REV family of attributes report to the rev table in the drawing.

My seeds have DR_0_REV thru DR_5_REV attributes.
So I can support 0 as the "-" (initial release) and up to 5 revisions with dwg links.

After that I have to improvise but it never happens.





Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Below is some code that parses the file name and adds attributes. This version works only on the current work part; if it parses the name and adds attributes as desired, we can modify it to work on all parts in an assembly. Please test it out and let me know what needs to be changed.

Code:
Option Strict Off
Imports System
Imports System.Text.RegularExpressions
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module Module121

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow()

    Sub Main()

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "part name attributes")

        lw.Open()


        '(?<PL_PROJ_NO>[\w|\d]*)_(?<PL_PART_NO>\d*-\d*)__(?<REV>\d*)
        'JOB1234_010-01__00
        Dim regex As Regex = New Regex("(?<PL_PROJ_NO>[\w|\d]*)_(?<PL_PART_NO>\d*-\d*)__(?<REV>\d*)", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result As MatchCollection = regex.Matches(theSession.Parts.Work.Leaf)
        Dim revAttributeTitle As String = ""

        If result.Count = 1 Then
            'lw.WriteLine(result.Item(0).Groups("PL_PROJ_NO").Value)
            AddPartStringAttribute(theSession.Parts.Work, "PL_PROJ_NO", result.Item(0).Groups("PL_PROJ_NO").Value)
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", result.Item(0).Groups("PL_PART_NO").Value)
            AddPartStringAttribute(theSession.Parts.Work, "DR_0_REV", "-")

            Dim revNum As Integer = 0
            If Integer.TryParse(result.Item(0).Groups("REV").Value, revNum) Then
                revAttributeTitle = "DR_" & revNum.ToString & "_REV"
            End If

            If revNum > 0 Then
                AddPartStringAttribute(theSession.Parts.Work, revattributetitle, "A")
            End If

        Else
            lw.WriteLine("unable to parse part name")
        End If


    End Sub

    Sub AddPartStringAttribute(ByVal thePart As Part, ByVal attributeTitle As String, ByVal attributeValue As String, Optional refstring As String = "")

        Dim id1 As NXOpen.Session.UndoMarkId
        id1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "add attribute")

        Dim attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder
        attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(thePart, {thePart}, NXOpen.AttributePropertiesBuilder.OperationType.None)

        If refstring = "" Then
            attributePropertiesBuilder1.IsReferenceType = False
            attributePropertiesBuilder1.StringValue = attributeValue
        Else
            attributePropertiesBuilder1.IsReferenceType = True
            Dim expression1 As NXOpen.Expression
            expression1 = thePart.Expressions.CreateSystemExpressionFromReferenceString(refstring)
            attributePropertiesBuilder1.Expression = expression1
        End If

        attributePropertiesBuilder1.IsArray = False
        attributePropertiesBuilder1.DataType = NXOpen.AttributePropertiesBaseBuilder.DataTypeOptions.String
        attributePropertiesBuilder1.Title = attributeTitle
        Dim nXObject1 As NXOpen.NXObject
        nXObject1 = attributePropertiesBuilder1.Commit()
        Dim nErrs1 As Integer
        nErrs1 = theSession.UpdateManager.DoUpdate(id1)
        attributePropertiesBuilder1.Destroy()
        theSession.DeleteUndoMark(id1, Nothing)

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module

www.nxjournaling.com
 
Thanks Cowski.
I'm getting the "unable to parse part name" msg with nothing written to attributes.

Tried naming schemes of just alpha characters, numeric and alphanumeric just to make sure that wasn't part of the issue.
Also started a new blank part with just those three attributes to make sure it wasn't an issue with the seed.
Same error.



Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
When parsing the part name, it looks for anything before an underscore character and saves that as the project number. Any digits after the underscore and before the double underscore is the part number, and any digits after the double underscore is the rev ID. If the part name doesn't follow this pattern, it will give the "unable to parse" message.

Something about your part numbers doesn't match the pattern. Can you post a few examples of your part numbers?

3dr said:
Naming Scheme
ProjectorJobNumber_Part-Number__Rev

Example: JOB1234_010-01___00
ATTR, "PL_PROJ_NO" = JOB1234
ATTR, "PL_PART_NO" = -010-01

Looks like your first example has 3 underscore characters before the rev ID; is this the difference?

www.nxjournaling.com
 
The 3 underscore in front of the Rev block should have been 2.
Fat fingered that.

I've got a displayed part open named "PROJ1_PART1__00.prt"

When I run the journal it I get that error msg.
Not sure what's up.

Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
OK WAIT!
I was running it in version 11
It works in 1847.



Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Ran a few quick tests and performs well so far.

I'd like to ask for just one thing if I could....
That it will function with or with out the rev block piece( "__XX" ) in the file name.
This is something we use on an as needed basis and it's not mandatory.

Alot of the time it's only added when there is an actual revision
ex: XXXX_XX-XX.prt to --> XXXX_XX-XX__01.prt

So when there is no rev block in the part name format, nothing gets written to any rev attributes.

I should have declared that up front... sorry Cowski.


Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor