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