Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Apply Attributes to Component Instance Using Journal File

Status
Not open for further replies.

jiloebel

Mechanical
Aug 7, 2012
14
0
0
US
All,

I currently have a .vb journal program (not written by me) that takes one attribute value (DB_Sequence_No) and sets it to another attribute value (Callout). This journal worked great in NX 6, but after upgrading to NX 8.0.3, the attribute is set using "Apply to Occurrence in Assembly". For some reason, attributes applied to occurrence in assembly do not sync with teamcenter, resulting in the attributes being lost after re-opening the part. Applying attributes to component instances DOES sync the callout to Teamcenter.

I am trying to modify this program to apply the attributes to component instance rather than occurrence in assembly.

Code:
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
    Sub Main()


        Dim attributesetter As AttributePropertiesBaseBuilder
        Dim theSession As Session = Session.GetSession()
        Dim theUI As UI = UI.GetUI()

        Dim workpart As part = thesession.parts.work
        Dim assm As assemblies.component = workpart.ComponentAssembly.RootComponent
        Dim children() As assemblies.component = assm.getchildren
        Dim callout As String
        Dim seq_num As Integer
        If UBound(children) > -1 Then
            For Each obj As Assemblies.Component In children
                Try

                    seq_num = obj.getstringattribute("DB_SEQUENCE_NO")
                    If CType(seq_num, Integer) < 10 Then

                        OBJ.SETATTRIBUTE("CALLOUT", "00" & CType(seq_num, String))
                    ElseIf CType(seq_num, Integer) < 100 Then

                        OBJ.SETATTRIBUTE("CALLOUT", "0" & CType(seq_num, String))
                    ElseIf CType(seq_num, Integer) < 10000 Then
                        OBJ.SETATTRIBUTE("CALLOUT", CType(seq_num, String))
                    End If
                Catch
                End Try
            Next
        End If


    End Sub
End Module

After looking through the programming help files, I can not find any way to use obj.SetAttribute to apply to component instance, however, I have found that the AttributePropertiesBaseBuilder.ObjectOptions.ComponentInstance may do what I want. Knowing that, I have this code that does not cause an error, but also doesn't work.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI



Module NXJournal
    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim theUI As UI = UI.GetUI()

        Dim attributesetter As AttributePropertiesBaseBuilder

        Dim workpart As part = thesession.parts.work
        Dim assm As assemblies.component = workpart.ComponentAssembly.RootComponent
        Dim children() As assemblies.component = assm.getchildren


        Dim callout As String
        Dim seq_num As Integer
        Dim calloutcheck As String

        If UBound(children) > -1 Then
            For Each obj As Assemblies.Component In children
                Try
                    list.add(obj)
                    seq_num = obj.getstringattribute("DB_SEQUENCE_NO")

                    attributesetter.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.ComponentInstance
                    attributesetter.Title = "Callout"
                    attributesetter.IsArray = "false"
                    attributesetter.StringValue = seq_num

                End Try
            Next
        End If
    End Sub
End Module

I'm not very experienced with visual basic programming, so any help you guys can offer on setting attributes to component instances would be great!
 
Replies continue below

Recommended for you

Try this:

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim workpart As Part = theSession.Parts.Work
        Dim assm As Assemblies.Component = workpart.ComponentAssembly.RootComponent
        Dim children() As Assemblies.Component = assm.GetChildren

        Dim callout As String
        Dim seq_num As Integer
        Dim calloutcheck As String

        If UBound(children) > -1 Then
            For Each obj As Assemblies.Component In children
                Try
                    seq_num = obj.GetStringAttribute("DB_SEQUENCE_NO")

                    Dim objects(0) As NXObject
                    objects(0) = obj
                    Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
                    attributePropertiesBuilder1 = workpart.PropertiesManager.CreateAttributePropertiesBuilder(objects)
                    attributePropertiesBuilder1.ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.Occurrence
                    attributePropertiesBuilder1.DataType = AttributePropertiesBaseBuilder.DataTypeOptions.String
                    attributePropertiesBuilder1.Title = "DB_SEQUENCE_NO"
                    attributePropertiesBuilder1.IsArray = False
                    attributePropertiesBuilder1.StringValue = seq_num.ToString.PadLeft(3, "0"c)
                    Dim nXObject2 As NXObject
                    nXObject2 = attributePropertiesBuilder1.Commit()
                    attributePropertiesBuilder1.Destroy()
                Catch ex As NXException
                    If ex.ErrorCode = 512008 Then
                        'seq_num attribute was not found
                        'add code to handle error

                    Else
                        MsgBox(ex.ErrorCode & ": " & ex.Message)
                        'code to handle other error

                    End If

                End Try
            Next
        End If


    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

    End Function

End Module

www.nxjournaling.com
 
EXCELLENT!

Tweaked 2 things and it works great! Thank you so much! I especially like the "PadLeft(3, "0"c)", our previous method was a little less elegant.

Again, thanks a ton!
 
Status
Not open for further replies.
Back
Top