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!

NX11 - Attribute VB Code Help 1

Status
Not open for further replies.

Kenja824

Automotive
Nov 5, 2014
949
I have the below code someone had given me in the past to add attributes to a file.

Could someone please explain how to adjust this so that all of the attributes are added to a category? Say give the Category the name "CONTROLS" for whatever it is worth.

So that when someone opens the file properties, there will be a new category named "CONTROLS" and all of these attributes would be inside of that category.








Dim TheSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim WorkPart As Part = TheSession.Parts.Work

Sub Main(ByVal args() As String)

' Public Sub Initialize()
For Each KP As KeyValuePair(Of String, String) In Attributes
If Not TheSession.Parts.Work.HasUserAttribute(KP.Key, NXObject.AttributeType.Any, -1) Then
TheSession.Parts.Work.SetUserAttribute(KP.Key, -1, KP.Value, Update.Option.Now)
TheSession.ListingWindow.Open()
TheSession.ListingWindow.WriteFullline(String.Format("Creating / Writing Attribute: {0} = {1}", KP.Key, KP.Value))
End If

Next
End Sub

Public ReadOnly Property Attributes As Dictionary(Of String, String)
Get
Dim Dict As New Dictionary(Of String, String)
Dict.Add("Build Source", "")
Dict.Add("Checked By", "")
Dict.Add("Checked Date", "")
Dict.Add("Consumable", "")
Dict.Add("Customer", "")
Dict.Add("Designed By", "")
Dict.Add("Detail Number", "")
Dict.Add("FCA Detail Number", "")
Dict.Add("FCA NPM Code", "")
Dict.Add("FCA Plant Name", "")
Dict.Add("FCA Tool Number (Opposite)", "")
Dict.Add("FCA Tool Number (Shown)", "")
Dict.Add("Job Number", "")
Dict.Add("Line Assy Name", "")
Dict.Add("Line Assy Number", "")
Dict.Add("Manufacturer Name", "")
Dict.Add("Manufacturer Part Number", "")
Dict.Add("Model Year", "")
Dict.Add("NX_ObjectMaterial", "")
Dict.Add("OPP QTY", "")
Dict.Add("Paint Code", "")
Dict.Add("Program", "")

Return Dict
End Get
End Property

End Module
 
Replies continue below

Recommended for you

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

Module kenja_attribute

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim WorkPart As Part = theSession.Parts.Work

    Sub Main(ByVal args() As String)

        ' Public Sub Initialize()
        For Each KP As KeyValuePair(Of String, String) In Attributes
            If Not theSession.Parts.Work.HasUserAttribute(KP.Key, NXObject.AttributeType.Any, -1) Then
                'theSession.Parts.Work.SetUserAttribute(KP.Key, -1, KP.Value, Update.Option.Now)
                AddPartStringAttribute(theSession.Parts.Work, KP.Key, KP.Value,, "CONTROLS",)
                theSession.ListingWindow.Open()
                theSession.ListingWindow.WriteFullline(String.Format("Creating / Writing Attribute: {0} = {1}", KP.Key, KP.Value))
            End If

        Next
    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

    Public ReadOnly Property Attributes As Dictionary(Of String, String)
        Get
            Dim Dict As New Dictionary(Of String, String)
            Dict.Add("Build Source", "")
            Dict.Add("Checked By", "")
            Dict.Add("Checked Date", "")
            Dict.Add("Consumable", "")
            Dict.Add("Customer", "")
            Dict.Add("Designed By", "")
            Dict.Add("Detail Number", "")
            Dict.Add("FCA Detail Number", "")
            Dict.Add("FCA NPM Code", "")
            Dict.Add("FCA Plant Name", "")
            Dict.Add("FCA Tool Number (Opposite)", "")
            Dict.Add("FCA Tool Number (Shown)", "")
            Dict.Add("Job Number", "")
            Dict.Add("Line Assy Name", "")
            Dict.Add("Line Assy Number", "")
            Dict.Add("Manufacturer Name", "")
            Dict.Add("Manufacturer Part Number", "")
            Dict.Add("Model Year", "")
            Dict.Add("NX_ObjectMaterial", "")
            Dict.Add("OPP QTY", "")
            Dict.Add("Paint Code", "")
            Dict.Add("Program", "")

            Return Dict
        End Get
    End Property

End Module

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor