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 code Help Please

Status
Not open for further replies.

Kenja824

Automotive
Nov 5, 2014
949
I have received some code from you to add attributes to a .prt file in the past. Now they are looking for something different.

We already have a "Tool_ID" attribute. The value is different from file to file.

Now I need to have a journal that can create a "NPM" Attribute but the value would be whatever is in the Tool-ID attribute's value.

So I guess it copies the Tool_ID attributes value and creates a new NPM attribute with that same value.




I have a feeling they will get to a point that we have ten different journals to do what one could do in a tenth the amount of code. lol
 
Replies continue below

Recommended for you

Well I figured out something that works for this. I found other code that did something similar and I axed my way through it to make it do what I needed. I know it could be a ton cleaner and it would have been preferable if it didn't make it into an expression, but the end results were good enough.

I did not remove the lines I blocked out for the purpose of someone who knows code better might have an easier time seeing what it was meant to do before I butchered it. lol

----------------------------

'Build a new part attribute from existing part attributes.
'When run on a part with string part attributes named "first" and "second", this journal will create a new part attribute ("new") that combines the existing attributes.
'new = TOOL_ID & "-" & second

Option Strict Off
Imports System
Imports NXOpen

Module new_attribute_from_attributes
Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow
' Dim workPart As NXOpen.Part = theSession.Parts.Work

Sub Tool ID Attr()

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

lw.Open()

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

Dim attributePropertiesBuilder1 As NXOpen.AttributePropertiesBuilder = Nothing
attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(theSession.Parts.Work, {theSession.Parts.Work}, NXOpen.AttributePropertiesBuilder.OperationType.None)

attributePropertiesBuilder1.IsArray = False

attributePropertiesBuilder1.DataType = NXOpen.AttributePropertiesBaseBuilder.DataTypeOptions.String

attributePropertiesBuilder1.Category = "VALIANT"
attributePropertiesBuilder1.Title = "NPM"


Dim expression1 As NXOpen.Expression = Nothing
expression1 = theSession.Parts.Work.Expressions.CreateSystemExpression("String", """""")

Dim expression2 As NXOpen.Expression = Nothing
expression2 = theSession.Parts.Work.Expressions.GetAttributeExpression(theSession.Parts.Work, "TOOL_ID", NXOpen.NXObject.AttributeType.String, -1)

Dim expression3 As NXOpen.Expression = Nothing
expression3 = theSession.Parts.Work.Expressions.GetAttributeExpression(theSession.Parts.Work, "second", NXOpen.NXObject.AttributeType.String, -1)

lw.WriteLine("NPM rhs: " & expression2.Name & " + """"")
expression1.RightHandSide = expression2.Name & " + """""

' lw.WriteLine("NPM rhs: " & expression2.Name & " + ""-"" + " & expression3.Name)
' expression1.RightHandSide = expression2.Name & " + ""-"" + " & expression3.Name

attributePropertiesBuilder1.Expression = expression1


' Dim expression4 As NXOpen.Expression = CType(workPart.Expressions.FindObject("p19"), NXOpen.Expression)

' attributePropertiesBuilder1.Expression = expression1

' attributePropertiesBuilder1.IsArray = False

' attributePropertiesBuilder1.IsArray = False

' Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
' markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Break Expression Link")

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = attributePropertiesBuilder1.Commit()

Dim id1 As NXOpen.Session.UndoMarkId = Nothing
id1 = theSession.GetNewestUndoMark(NXOpen.Session.MarkVisibility.Visible)

Dim nErrs1 As Integer = Nothing
nErrs1 = theSession.UpdateManager.DoUpdate(id1)

attributePropertiesBuilder1.Destroy()

lw.Close()

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
 
Kenja824 said:
Now I need to have a journal that can create a "NPM" Attribute but the value would be whatever is in the Tool-ID attribute's value.

So I guess it copies the Tool_ID attributes value and creates a new NPM attribute with that same value.

Did you want the NPM attribute to be associative to the Tool-ID value - when the Tool-ID changes, the NPM updates to the same value? Or is NPM an independent attribute that just gets the same initial value as Tool-ID - future changes to NPM and Tool-ID don't affect each other?

www.nxjournaling.com
 
TBH, I dont know. lol

To my knowledge, they should always be the same but I don't think either is ever expected to change. I think they just want to be able to run the code and have it copy whatever is on the Tool_ID over to the NPM attribute.

When I showed him what this code did, he was happy with this even, so I don't think it needs to be associative. I have to say though, its pretty cool that is even possible. I didn't know it was. lol

 
Now I feel stupid. lol I did not realize that the expression was linking the NPM attribute to the TOOL_ID attribute. I thought you were saying you could do that if I wanted. lol

I will have to let my leader know that they are currently linked and see if he likes that or not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor