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

I've updated the code. The rev is now optional and the rest of the pattern matching should be a bit more robust. I'm testing on NX 11 since that is what is in your signature line (I don't have NX 1847 up and running yet), so it should work for you in NX 11.

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()

        'JOB1234_010-01__00
        Dim regex As Regex = New Regex("(?<PL_PROJ_NO>[a-z\d]+)_(?<PL_PART_NO>[a-z\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)

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

                    If revNum = 0 Then
                        AddPartStringAttribute(theSession.Parts.Work, "DR_0_REV", "-")
                    End If

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

                End If

            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
 
This one works well in version 11 & 1847.
Very nice!

I did some thorough testing to see how it does with more complex names.

PL_PART_NO
Never fails with any combination of alphanumeric characters and dashes ("-")
I missed mentioning that this one always needs a "-" prefix (Unless PL_PROJ_NO doesn't exist in the scheme. See questions below)
This way when you marry the Proj & Part number in the drawing & part list it has a delimiter.
ex: "PL_PROJ_NO"-"PL_PART_NO"

PL_PROJ_NO
Same as above but won't parse a "-" character.
That I was able to change.

Noticed that if the "PL_PROJ_NO" portion of the scheme is missing it throws the error msg and doesn't process.
This brings up an issue for processing whole assemblies.
Often there are mixed naming schemes in assemblies when we run update for external change to set the set attributes.
This is sually due to releasing work progressively to the shop during the design process.
Some stuff is still being put together and organized while other assemblies are completed.

Questions:
1. Should the Journal be able to skip the parts that don't have the basic naming scheme yet?
2. Or, should the journal be tolerant and able to process parts inside and outside of the full naming scheme?
Ex: if the PL_PROJ_NO data block doesn't exist in the file name, process anyway and place the data found in the PL_PART_NO attribute by default.
Stuff like the standard model1.prt or Hex Bolt,AI,0.25-20x0.5.prt (Typical Machinery lib part)

Not sure what the right answer is, but number 2 seems like a better solution for the user.
This way the attributes in the parts lists get populated and can be seen in there real time as they are.

Thoughts?








Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
3dr said:
Noticed that if the "PL_PROJ_NO" portion of the scheme is missing it throws the error msg and doesn't process.
Yes, both portions (project and part) must be present for it to be considered a match. If you have other standard naming conventions {part number}__{rev} or the like, we can write more tests to compare against those if the first one fails.

3dr said:
Questions:
1. Should the Journal be able to skip the parts that don't have the basic naming scheme yet?
2. Or, should the journal be tolerant and able to process parts inside and outside of the full naming scheme?
The journal can be made to do either; it's really up to you. How do you want to handle it? If the name doesn't match the test(s), we can assign the entire name to the part number attribute.

www.nxjournaling.com
 
Ok, so I would say 2 would be the most effective method.

From a logical parsing standpoint I would think this is the order...
1. Run the "PL_PROJ_NO_PL_PART_NO__REV" method as designed.

If the scheme doesn't match

2. You run it as "PL_PART_NO__REV" (So it's possible to get just a part number and rev with no project number.
if no __XX is present... you simply handle it the same way as #1 above.

I would think that covers every possible scenario.
Just need to have...
"-" prefix for PL_PART_NO for #1 above.
No "-" prefix for PL_PART_NO for #2 above.

That should do it.


Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Give this one a whirl:

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()


        'JOB1234_010-01__00
        '010-01__00
        Dim regex1 As Regex = New Regex("(?<PL_PROJ_NO>[a-z\d-]+)_(?<PL_PART_NO>[a-z\d-]+)(?:__(?<REV>\d+))?", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result1 As MatchCollection = regex1.Matches(theSession.Parts.Work.Leaf)
        Dim regex2 As Regex = New Regex("(?<PL_PART_NO>[a-z\d-]+)(?:__(?<REV>\d+))?", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result2 As MatchCollection = regex2.Matches(theSession.Parts.Work.Leaf)

        If result1.Count = 1 Then
            AddPartStringAttribute(theSession.Parts.Work, "PL_PROJ_NO", result1.Item(0).Groups("PL_PROJ_NO").Value)
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", "-" & result1.Item(0).Groups("PL_PART_NO").Value)

            If Not String.IsNullOrEmpty(result1.Item(0).Groups("REV").Value) Then
                ParseRevAttribute(result1.Item(0).Groups("REV").Value)
            End If

        ElseIf result2.Count = 1 Then
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", result2.Item(0).Groups("PL_PART_NO").Value)

            If Not String.IsNullOrEmpty(result2.Item(0).Groups("REV").Value) Then
                ParseRevAttribute(result2.Item(0).Groups("REV").Value)
            End If

        Else
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", theSession.Parts.Work.Leaf)
        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

    Sub ParseRevAttribute(theValue As String)

        Dim revAttributeTitle As String = ""
        Dim revNum As Integer

        If Integer.TryParse(theValue, revNum) Then
            revAttributeTitle = "DR_" & revNum.ToString & "_REV"

            If revNum = 0 Then
                AddPartStringAttribute(theSession.Parts.Work, "DR_0_REV", "-")
            End If

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

        End If


    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
 
I think you nailed it.
Performs perfectly on the displayed part.

Rather than upgrading this to perform across a whole assembly... I wonder if it's just better to marry it too, or call it, from the journal I use to rename parts?
That way it just happens automatically at the point of save.

This is the code for the streamlined "save as"
Which I'm sure was yours originally.
Integrating it or calling it form this code would be masterful.
If not, doing it the original way I described would be fine.

What ya think?


Option Strict Off

Imports NXOpen
Imports NXOpen.UF
Imports System
Imports System.IO
Imports System.Environment
Imports System.Windows.Forms

Module Renomear

Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()

Sub Main()

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

If workPart Is Nothing Then Return

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Renomear")

Dim filename As String = ""
Dim wpPath As String = workPart.FullPath
filename = wpPath

If (select_file(filename) <> DialogResult.OK) Then
Return
Else
Dim partSaveStatus1 As PartSaveStatus
Try
partSaveStatus1 = workPart.SaveAs(filename)
Catch ex As NXException
lw.WriteLine("SaveAs of part " & filename & _
" failed with error code " & ex.ErrorCode & vbCrLf)
lw.WriteLine(ex.ToString)
End Try
End If

End Sub

Public Function select_file(ByRef filename) As DialogResult

Dim sfd As SaveFileDialog = New SaveFileDialog()
Dim result As DialogResult

sfd.Title = "Choose new part file name"
sfd.AddExtension = True
sfd.DefaultExt = "prt"
sfd.FileName = filename

result = sfd.ShowDialog()
filename = sfd.FileName
sfd.Dispose()
Return result

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function

End Module




Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
I'm not sure that the original "save-as" code was from me, but here is a combined journal that I think will work for you.

Code:
Option Strict Off

Imports NXOpen
Imports NXOpen.UF
Imports System
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Environment
Imports System.Windows.Forms

Module Module122

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

    Sub Main()

        Dim workPart As Part = theSession.Parts.Work

        If workPart Is Nothing Then Return

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Renomear")

        Dim filename As String = ""
        Dim wpPath As String = workPart.FullPath
        filename = wpPath

        If (select_file(filename) <> DialogResult.OK) Then
            Return
        Else
            Dim partSaveStatus1 As PartSaveStatus
            Try
                partSaveStatus1 = workPart.SaveAs(filename)
            Catch ex As NXException
                lw.WriteLine("SaveAs of part " & filename &
                " failed with error code " & ex.ErrorCode & vbCrLf)
                lw.WriteLine(ex.ToString)
            End Try
        End If


        Dim regex1 As Regex = New Regex("(?<PL_PROJ_NO>[a-z\d-]+)_(?<PL_PART_NO>[a-z\d-]+)(?:__(?<REV>\d+))?", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result1 As MatchCollection = regex1.Matches(theSession.Parts.Work.Leaf)
        Dim regex2 As Regex = New Regex("(?<PL_PART_NO>[a-z\d-]+)(?:__(?<REV>\d+))?", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result2 As MatchCollection = regex2.Matches(theSession.Parts.Work.Leaf)

        If result1.Count = 1 Then
            AddPartStringAttribute(theSession.Parts.Work, "PL_PROJ_NO", result1.Item(0).Groups("PL_PROJ_NO").Value)
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", "-" & result1.Item(0).Groups("PL_PART_NO").Value)

            If Not String.IsNullOrEmpty(result1.Item(0).Groups("REV").Value) Then
                ParseRevAttribute(result1.Item(0).Groups("REV").Value)
            End If

        ElseIf result2.Count = 1 Then
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", result2.Item(0).Groups("PL_PART_NO").Value)

            If Not String.IsNullOrEmpty(result2.Item(0).Groups("REV").Value) Then
                ParseRevAttribute(result2.Item(0).Groups("REV").Value)
            End If

        Else
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", theSession.Parts.Work.Leaf)
        End If

        theSession.Parts.Work.Save(BasePart.SaveComponents.False, BasePart.CloseAfterSave.False)

    End Sub

    Public Function select_file(ByRef filename) As DialogResult

        Dim sfd As SaveFileDialog = New SaveFileDialog()
        Dim result As DialogResult

        sfd.Title = "Choose new part file name"
        sfd.AddExtension = True
        sfd.DefaultExt = "prt"
        sfd.FileName = filename

        result = sfd.ShowDialog()
        filename = sfd.FileName
        sfd.Dispose()
        Return result

    End Function

    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

    Sub ParseRevAttribute(theValue As String)

        Dim revAttributeTitle As String = ""
        Dim revNum As Integer

        If Integer.TryParse(theValue, revNum) Then
            revAttributeTitle = "DR_" & revNum.ToString & "_REV"

            If revNum = 0 Then
                AddPartStringAttribute(theSession.Parts.Work, "DR_0_REV", "-")
            End If

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

        End If

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function

End Module

www.nxjournaling.com
 
So far so good Cowski, it's sweet.
What a leap forward in the work flow intellegence.
Thank you!

I did notice that as I progressed thru revs in the test saves that it wrote always wrote an "A" rather than A,B,C etc...
Not sure why I didn't see that before.

I fixed by adding the lines below:

If revNum > 2 Then
AddPartStringAttribute(theSession.Parts.Work, revAttributeTitle, "B")
End If

If revNum > 3 Then
AddPartStringAttribute(theSession.Parts.Work, revAttributeTitle, "C")
End If

And so on...

I'm looking at adjusting this to write to an attribute array instead.
DR_REV_[0] = "-"
DR_REV_[1] = "A"
Recorded some test journals over the weekend and starting to take a poke at it.








Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
If you are sure that you are going to have a small number of revisions, the following should work. If you go above 8 revisions it will fail and the code will need to be modified to be more robust.

Code:
Option Strict Off

Imports NXOpen
Imports NXOpen.UF
Imports System
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Environment
Imports System.Windows.Forms

Module Module122

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

    Sub Main()

        Dim workPart As Part = theSession.Parts.Work

        If workPart Is Nothing Then Return

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Renomear")

        Dim filename As String = ""
        Dim wpPath As String = workPart.FullPath
        filename = wpPath

        If (select_file(filename) <> DialogResult.OK) Then
            Return
        Else
            Dim partSaveStatus1 As PartSaveStatus
            Try
                partSaveStatus1 = workPart.SaveAs(filename)
            Catch ex As NXException
                lw.WriteLine("SaveAs of part " & filename &
                " failed with error code " & ex.ErrorCode & vbCrLf)
                lw.WriteLine(ex.ToString)
            End Try
        End If


        Dim regex1 As Regex = New Regex("(?<PL_PROJ_NO>[a-z\d-]+)_(?<PL_PART_NO>[a-z\d-]+)(?:__(?<REV>\d+))?", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result1 As MatchCollection = regex1.Matches(theSession.Parts.Work.Leaf)
        Dim regex2 As Regex = New Regex("(?<PL_PART_NO>[a-z\d-]+)(?:__(?<REV>\d+))?", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result2 As MatchCollection = regex2.Matches(theSession.Parts.Work.Leaf)

        If result1.Count = 1 Then
            AddPartStringAttribute(theSession.Parts.Work, "PL_PROJ_NO", result1.Item(0).Groups("PL_PROJ_NO").Value)
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", "-" & result1.Item(0).Groups("PL_PART_NO").Value)

            If Not String.IsNullOrEmpty(result1.Item(0).Groups("REV").Value) Then
                ParseRevAttribute(result1.Item(0).Groups("REV").Value)
            End If

        ElseIf result2.Count = 1 Then
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", result2.Item(0).Groups("PL_PART_NO").Value)

            If Not String.IsNullOrEmpty(result2.Item(0).Groups("REV").Value) Then
                ParseRevAttribute(result2.Item(0).Groups("REV").Value)
            End If

        Else
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", theSession.Parts.Work.Leaf)
        End If

        theSession.Parts.Work.Save(BasePart.SaveComponents.False, BasePart.CloseAfterSave.False)

    End Sub

    Public Function select_file(ByRef filename) As DialogResult

        Dim sfd As SaveFileDialog = New SaveFileDialog()
        Dim result As DialogResult

        sfd.Title = "Choose new part file name"
        sfd.AddExtension = True
        sfd.DefaultExt = "prt"
        sfd.FileName = filename

        result = sfd.ShowDialog()
        filename = sfd.FileName
        sfd.Dispose()
        Return result

    End Function

    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

    Sub ParseRevAttribute(theValue As String)

        Dim revAttributeTitle As String = ""
        Dim revNum As Integer
        Dim revList As New List(Of String)(New String() {"-", "A", "B", "C", "D", "E", "F", "G", "H"})

        If Integer.TryParse(theValue, revNum) Then
            revAttributeTitle = "DR_" & revNum.ToString & "_REV"

            If revNum < 9 Then
                AddPartStringAttribute(theSession.Parts.Work, revAttributeTitle, revList.Item(revNum))
            Else
                'warn user, add more rev letters?
            End If

        End If

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function

End Module

www.nxjournaling.com
 
Thank you.
This ones tighter but a little trickier to mod than the last one.

But I enjoy hacking around. lol

Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Replace the ParseRevAttribute sub with the one below and you will only need to worry about adding new letters to the revList. An algorithm with more smarts would generate the rev letter as required (A through Z then AA - AZ, BA - BZ, etc).

Code:
    Sub ParseRevAttribute(theValue As String)

        Dim revAttributeTitle As String = ""
        Dim revNum As Integer
        Dim revList As New List(Of String)(New String() {"-", "A", "B", "C", "D", "E", "F", "G", "H"})

        If Integer.TryParse(theValue, revNum) Then
            revAttributeTitle = "DR_" & revNum.ToString & "_REV"

            If revNum < revList.Count Then
                AddPartStringAttribute(theSession.Parts.Work, revAttributeTitle, revList.Item(revNum))
            Else
                'warn user, add more rev letters?
            End If

        End If

    End Sub

Edit: There was a bug in my last post that would lead to only the "DR_0_REV" attribute being added. Please update with this version.

www.nxjournaling.com
 
Thank you, this works great!
It's now writing to the separate attributes too.

I've piddled around with attribute array edits for most of the afternoon.
Your calls are a little different than what I'm recording in the journals.

Looks like a little more than a tweak, yes?









Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Sorry 3dr, I've not forgotten about this; but things got super busy around here. It'll probably be a week or so before thing settle back down. Do you want all the new attributes to be added to a custom group (name)? Also, the revisions should be an attribute array?

www.nxjournaling.com
 
No need to apologize, what your doing is a much appreciated gift.

These attribute do belong to a group and I've been wanting to start stuffing some of my attribute clutter into arrays to clear up the bulk editor.

Revision info is my first target.

I'll frame it out better over the weekend and whenever you get around to it is fine.

Thanks a lot for staying tuned!


Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
Ok so here it is...

The Rev attributes should write to an array:
DR_REV [0] = "-"
DR_REV [1] = "A"
and so on...
Group Name = "60 PROP 2"

Been using this tool heavily since we got it.

We some times get orphaned parts that wind up getting "made/named" outside of the Fast Rename Journal we're doing this in.
The Make Unique command is a culprit and used here a lot.
I forgot about that and guys just forget they need to do the final naming in this journal.

Can there be a simplified version of the same thing to use for those situations?
One that runs on the active work part only, so you don't need to make it the displayed part first?

If it's easier to do a single journal with a prompt on which action to perform... that would work too.
Right now I'm using the last iteration of what you did before marrying it to the Fast Rename journal.

TIA for whatever you can do.
And if you don't have the time or energy for it I'm still way ahead of where I was before we started.

Thanks Cowski!




















Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
The code below may need a bit more polish, but I think it does basically what you want. It will parse the rev from the drawing number and add it as an array part attribute (category: "60 PROP 2", title: "DR_REV").

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()

    'constants for the revision attribute title and group, change these as needed
    Const revCategoryName As String = "60 PROP 2"
    Const revAttTitle As String = "DR_REV"

    Sub Main()

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

        lw.Open()

        Dim curRevIndex As Integer = RevAttributeIndex(revAttTitle)
        'lw.WriteLine("current rev index: " & curRevIndex.ToString)

        'JOB1234_010-01__00
        '010-01__00
        Dim regex1 As Regex = New Regex("(?<PL_PROJ_NO>[a-z\d-]+)_(?<PL_PART_NO>[a-z\d-]+)(?:__(?<REV>\d+))?", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result1 As MatchCollection = regex1.Matches(theSession.Parts.Work.Leaf)
        Dim regex2 As Regex = New Regex("(?<PL_PART_NO>[a-z\d-]+)(?:__(?<REV>\d+))?", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
        Dim result2 As MatchCollection = regex2.Matches(theSession.Parts.Work.Leaf)

        If result1.Count = 1 Then
            AddPartStringAttribute(theSession.Parts.Work, "PL_PROJ_NO", result1.Item(0).Groups("PL_PROJ_NO").Value)
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", "-" & result1.Item(0).Groups("PL_PART_NO").Value)

            If Not String.IsNullOrEmpty(result1.Item(0).Groups("REV").Value) Then
                ParseRevAttribute(result1.Item(0).Groups("REV").Value, curRevIndex)
            End If

        ElseIf result2.Count = 1 Then
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", result2.Item(0).Groups("PL_PART_NO").Value)

            If Not String.IsNullOrEmpty(result2.Item(0).Groups("REV").Value) Then
                ParseRevAttribute(result2.Item(0).Groups("REV").Value, curRevIndex)
            End If

        Else
            AddPartStringAttribute(theSession.Parts.Work, "PL_PART_NO", theSession.Parts.Work.Leaf)
        End If

    End Sub

    Sub AddPartStringAttribute(ByVal thePart As Part,
                               ByVal attributeTitle As String,
                               ByVal attributeValue As String,
                               Optional index As Integer = -1,
                               Optional category 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 Not String.IsNullOrEmpty(category) Then
            attributePropertiesBuilder1.Category = category
        End If

        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

        If index = -1 Then
            attributePropertiesBuilder1.IsArray = False
        Else
            attributePropertiesBuilder1.IsArray = True
            attributePropertiesBuilder1.ArrayIndex = index
        End If

        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

    Sub ParseRevAttribute(theValue As String, curIndex As Integer)

        Dim revNum As Integer
        Dim revList As New List(Of String)(New String() {"-", "A", "B", "C", "D", "E", "F", "G", "H"})

        If Integer.TryParse(theValue, revNum) Then

            If revNum < 9 Then
                For i As Integer = curIndex To revNum - 1
                    AddPartStringAttribute(theSession.Parts.Work, revAttTitle, revList.Item(i), i, revCategoryName)
                Next
                AddPartStringAttribute(theSession.Parts.Work, revAttTitle, revList.Item(revNum), revNum, revCategoryName, "")
            Else
                'warn user, add more rev letters?
            End If

        End If

    End Sub

    Function RevAttributeIndex(theAttributeTitle As String) As Integer

        Dim allAttributeInfo() As NXObject.AttributeInformation
        Dim tempIndex As Integer

        If theSession.Parts.Work.HasUserAttribute(theAttributeTitle, NXObject.AttributeType.String, -1) Then
            allAttributeInfo = theSession.Parts.Work.GetUserAttributes(False)
        Else
            Return Nothing
        End If

        For Each tempAtt As NXObject.AttributeInformation In allAttributeInfo
            If tempAtt.Title.ToUpper = theAttributeTitle.ToUpper Then
                'lw.WriteLine("index: " & tempAtt.ArrayElementIndex.ToString)
                tempIndex = tempAtt.ArrayElementIndex
            End If
        Next

        Return tempIndex

    End Function

    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
 
Thank you for the support Cowski!

I'll put it to work this week.

Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

NX11, Win 10 Pro
 
That was solid cowski, works brilliantly.

Thanks so much for sticking with it.
I really appreciate it!

Dave
Automotive Tooling / Aircraft Tooling / Ground Support Structures

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

Part and Inventory Search

Sponsor