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 - Journal to break Attribute link 1

Status
Not open for further replies.

Kenja824

Automotive
Nov 5, 2014
949
I have a journal that was given me to copy one file attribute and create a new attribute with the same value. The problem with what I need to use it for now is that it creates the new attribute as a linked expression. For this new project it cannot be linked.

My goal ---
I need to have a journal go into File Attributes, create TOOL_ID2 attribute, and make its value match whatever the TOOL_ID attribute's value has.
I then need to change the TOOL_ID attribute's value to a specific number. "HMS12345S_123S" would work.

The user needs to then run a program on the file

Then I am guessing it would be another journal would reverse the process of the first Journal. Change the existing TOOL_ID value back to the original number that is now in TOOL_ID2.

Next it wouldn't be necessary but I would like to delete the TOOL_ID2 attribute at the end of the second attribute.


The problem is we have a program we run our files to do something. A new customer we have uses completely different types of TOOL_ID values and our program needs a specific format to the value. So we are looking to change the TOOL_ID to a number that will allow the program to run, then after we run it, change the TOOL_ID back to the original number again.

With my current journal for copying an attribute it is linked to the original and so when we change the TOOL_ID to a value that works, the TOOL_ID2 automatically changes and we lose the original number.

I am not any good with code but if I can add to the journal I have to break the link, I think I might be able to make the rest work with two separate journals. One before the program is ran and a second journal to change the attribute back after the program is run.
 
Replies continue below

Recommended for you

Oh, I tried recording a journal to go in and break the attribute link. When I do it manually, it keeps the same value when I break it. When I run the journal back, it records the original number and so it always gives that same number to the attribute link I break. I tried commenting out the code that had the number value typed into it but then it leaves the broken link with a blank value. Running the journal back with new numbers wont keep the new number in it.
 
If I understand correctly, for a certain customer's files you want to run a journal to prep the file, then run the main journal, then run another journal to undo the first journal. That sounds like a lot of opportunity for error when you forget to run the prep or cleanup journal. Perhaps it would be better to modify your current journal to account for this customer's TOOL_ID format?

www.nxjournaling.com
 
The program we need to run on our tools is a .dll file. If I try to edit that, it looks like a scrambled mess of repeated little "NUL" words. The problem is this program was originally designed to expect a TOOL_ID in a specific format. ABC12345S_123S

We have a new customer's jobs that have completely different types of TOOL_ID values.

So I need to make a button that we can hit and it will copy their TOOL_ID value to a TOOL_ID2, then issue the TOOL_ID a generic value like ABC12345S_123S.

Then our guys can run the .dll program to do whatever it needs to do.

Then they would have another button that reverses the first button and puts their TOOL_ID value back in place and deletes the TOOL_ID2. (Though the Tool_ID2 would not need to be deleted I guess. It wouldn't hurt if it was there.)

Unfortunately the code I have that copies one attribute and creates another with the same value only crates a new attribute that is an expression linked to the first attribute. So if I add code to change the original TOOL_ID, the new TOOL_ID2 automatically updates to match and I lose the original value.

So I either need to copy one attribute to another without it being a linked expression, or I need to figure out how to have a journal kill the link. If it helps, the link that would need to be killed will always be on the same attribute "TOOL_ID2".

As an extra thought, if a journal code could be created to launch a .dll file, then I might be able to just make one button that does it all, but I would still need to figure out the original problem anyway and I didnt want to ask for more than necessary. lol
 
Ok, I was assuming that you had control over the program source code.

Here is a journal that will create a new string attribute named "TOOL_ID2" and will copy the value of "TOOL_ID":

Code:
Option Strict Off
Imports System
Imports NXOpen

Module CopyAttribute

    Dim theSession As Session = Session.GetSession()

    Sub Main()

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Const undoMarkName As String = "copy attribute"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Const oldAttributeName As String = "TOOL_ID"
        Const newAttributeName As String = "TOOL_ID2"
        Dim attributeInfo As NXObject.AttributeInformation

        If workPart.HasUserAttribute(oldAttributeName, NXObject.AttributeType.String, -1) Then
            attributeInfo = workPart.GetUserAttribute(oldAttributeName, NXObject.AttributeType.String, -1)
            'lw.WriteLine("attribute value: " & attributeInfo.StringValue)
            AddPartStringAttribute(workPart, newAttributeName, attributeInfo.StringValue)
            AddPartStringAttribute(workPart, oldAttributeName, "HMS12345S_123S")
        Else
            lw.WriteLine("the work part does not have an attribute named: " & oldAttributeName)
        End If


        lw.Close()

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


And here is a version that will copy the value from "TOOL_ID2" to "TOOL_ID" and then delete "TOOL_ID2".

Code:
Option Strict Off
Imports System
Imports NXOpen

Module RevertAttribute

    Dim theSession As Session = Session.GetSession()

    Sub Main()

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Const undoMarkName As String = "revert attribute value"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Const oldAttributeName As String = "TOOL_ID2"
        Const newAttributeName As String = "TOOL_ID"
        Dim attributeInfo As NXObject.AttributeInformation

        If workPart.HasUserAttribute(oldAttributeName, NXObject.AttributeType.String, -1) Then
            attributeInfo = workPart.GetUserAttribute(oldAttributeName, NXObject.AttributeType.String, -1)
            'lw.WriteLine("attribute value: " & attributeInfo.StringValue)
            AddPartStringAttribute(workPart, newAttributeName, attributeInfo.StringValue)
            workPart.DeleteUserAttribute(NXObject.AttributeType.String, oldAttributeName, True, Update.Option.Now)
        Else
            lw.WriteLine("the work part does not have an attribute named: " & oldAttributeName)
        End If


        lw.Close()

    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 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
 
Wow, this is perfect cowski.

Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor