Displayed Part Properties to Excel

Hello,

I am wondering if somebody could help me out with this. I would like to write the “Displayed Part Properties” to an excel file using VB. I'm using NX9.

Thanks,
Marty

The example code below is very rudimentary; the Excel file must exist before running this journal (edit the value of the constant that points to the Excel file near the top of the journal code). The journal will write all the user attributes of the display part to the Excel file.

'NXJournaling.com
'April 13, 2015

'Export user attributes from the display part to an Excel file.
'The Excel file must exist before running this journal.
'Change the reference to the Excel file before running this journal.

'display_attributes_to_excel

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim theUISession As UI = UI.GetUI
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

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

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

Dim theAttributes() As NXObject.AttributeInformation
theAttributes = displayPart.GetUserAttributes

'change excelFileName to meet your needs
'create the excel file before running this journal
Const excelFileName As String = "C:\Temp\display_part_attributes.xlsx"
Dim row As Long = 1
Dim column As Long = 1

'create Excel object
Dim objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

'open Excel file
Dim objWorkbook = objExcel.Workbooks.Open(excelFileName)
If objWorkbook Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not open Excel file: " & excelFileName & ControlChars.NewLine & "journal exiting.")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

objExcel.visible = True

Dim title As String
Dim value As String

For Each temp As NXObject.AttributeInformation In theAttributes

title = temp.Title
value = ""
'lw.WriteLine("title: " & title)

Select Case temp.Type
Case Is = NXObject.AttributeType.Boolean
value = temp.BooleanValue.ToString
Case Is = NXObject.AttributeType.Integer
value = temp.IntegerValue.ToString
Case Is = NXObject.AttributeType.Real
value = temp.RealValue.ToString
Case Is = NXObject.AttributeType.String
value = temp.StringValue
Case Is = NXObject.AttributeType.Time
value = temp.TimeValue.ToString
Case Else
value = "value type not supported by this journal"
End Select

'lw.WriteLine("value: " & value)

'lw.WriteLine("")

objExcel.Cells(row, 1) = title
objExcel.cells(row, 2) = value
row += 1

Next

'objExcel.Quit()
objWorkbook = Nothing
objExcel = Nothing

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

Good morning,

Thanks for your help.
I ran this program and it's only outputting part of the information. This program outputs the following to the excel file:

MATERIAL_MISSING_ASSIGNMENTS TRUE
MATERIAL_MULTIPLE_ASSIGNED FALSE
MaterialMultipleAssigned FALSE
MaterialMissingAssignments TRUE

This excel file is columns A through B and 4 rows.

This is what I get when I do it manually in NX. File - Properties - Displayed Part Properties:


Title/Alias Value Units Template Type Read-only Inherited
Materials
MaterialMissingAssignments TRUE String Attribute is locked
MaterialMultipleAssigned FALSE String Attribute is locked
DB Component Instance
CALLOUT This attribute holds the name of the property holding the Callout value. String Attribute is writable
PLIST_IGNORE_MEMBER The presence of this attribute removes the component from the parts list. String Attribute is writable
PLIST_IGNORE_SUBASSEMBLY The presence of this attribute removes the component assembly from the parts list. String Attribute is writable
REFERENCE_COMPONENT The presence of this attribute means that the component is for reference only, it's not part of this assembly. If set as a part attribute, the part will be reference only in all assemblies. String Attribute is writable
Materials
MATERIAL_MISSING_ASSIGNMENTS TRUE This field specifies the character string that is used as the attribute title when there are bodies in the part that do not have materials assigned. String Attribute is owned by another object
MATERIAL_MULTIPLE_ASSIGNED FALSE This field specifies the character string that is used as the attribute title when there are multiple part materials assigned in the part. String Attribute is owned by another object
MATERIAL_PREFERRED This field specifies the character string that is used as the attribute title for the preferred part material. String Attribute is writable
All Unset

This excel file is columns A through G and 14 rows.

Sorry, I don't know how to attach an excel file to this post or paste from SnagIt to make this easy to read.

The code above only works on user attributes that have a value (they are not in the 'unset' group).

It appears to me that these other fields have a value in them. Is there a way to extract that data?

Which attribute, in particular, are you interested in? Also, are you running Teamcenter?
I ask, because some of the system attributes may need to be handled differently...

No Teamcenter.
All of the attributes that show up when I do the following "File - Properties". Right click in the "Value" area, export to worksheet. All the information that is in that worksheet is what I want. My goal is to duplicate that info.

I've not had time to dig into this issue, but I plan on returning to it when my schedule allows.

Thank you, I appreciate your help.

Good morning, I was wondering if you had a chance to dig into this any further?

Here's the latest code I have; the formatting is not exactly the same as what you get with the "export to spredsheet" command but the information is the same.


Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim theUISession As UI = UI.GetUI
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

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

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

Dim theAttributes() As NXObject.AttributeInformation
theAttributes = displayPart.GetUserAttributes(True)

'change excelFileName to meet your needs
'create the excel file before running this journal
Const excelFileName As String = "C:\Temp\display_part_attributes.xlsx"
Dim row As Long = 1
Dim column As Long = 1

'create Excel object
Dim objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

'open Excel file
Dim objWorkbook = objExcel.Workbooks.Open(excelFileName)
If objWorkbook Is Nothing Then
theUISession.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, "Could not open Excel file: " & excelFileName & ControlChars.NewLine & "journal exiting.")
theSession.UndoToMark(markId1, "journal")
Exit Sub
End If

objExcel.visible = True

'create title row
objExcel.Cells(row, 1) = "Title"
objExcel.cells(row, 2) = "Value"
objExcel.cells(row, 3) = "Units"
objExcel.cells(row, 4) = "Category"
objExcel.cells(row, 5) = "Type"
objExcel.cells(row, 6) = "Read-only"
objExcel.cells(row, 7) = "Inherited"
row += 1

Dim title As String
Dim value As String
Dim attType As String
Dim attReadOnly As String
Dim attInherited As String

For Each temp As NXObject.AttributeInformation In theAttributes

title = temp.Title
value = ""
'lw.WriteLine("title: " & title)

Select Case temp.Type
Case Is = NXObject.AttributeType.Boolean
value = temp.BooleanValue.ToString
attType = "Boolean"
Case Is = NXObject.AttributeType.Integer
value = temp.IntegerValue.ToString
attType = "Integer"
Case Is = NXObject.AttributeType.Real
value = temp.RealValue.ToString
attType = "Real"
Case Is = NXObject.AttributeType.String
value = temp.StringValue
attType = "String"
Case Is = NXObject.AttributeType.Time
value = temp.TimeValue.ToString
attType = "Time"
Case Else
value = "value type not supported by this journal"
attType = "N/A"
End Select

'lw.WriteLine("value: " & value)

'lw.WriteLine("")

objExcel.Cells(row, 1) = title
objExcel.cells(row, 2) = value
If Not IsNothing(temp.Unit) Then
objExcel.cells(row, 3) = temp.Unit.Name
End If
objExcel.cells(row, 4) = temp.Category
objExcel.cells(row, 5) = attType
objExcel.cells(row, 6) = temp.Locked.ToString
objExcel.cells(row, 7) = temp.Inherited.ToString

row += 1

Next

'objExcel.Quit()
objWorkbook = Nothing
objExcel = Nothing

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

Thank you, that is the information I'm looking for.

Where can I get a copy of the "SNAP Reference Guide"? I have the "Getting Started with Snap" manual.

The SNAP reference guide is part of the NX help documentation. Recent versions of NX do NOT install the "programming tools" help files by default, make sure you have them installed. If the programming help is not installed, re-run the help doc installer and make sure the "programming tools" option is selected.

After they are installed, you can find the SNAP reference guide by opening NX help, scroll to the bottom of the list of topics and choose "Programming tools", on the page that displays next, and click the link to display the programming help. The programming tools help page will appear and the links to the SNAP documentation can easily be found in the navigation menu on the left.

Thanks. I need to get that installed.

The SNAP Reference Guide is a CHM file. It normally goes hand-in-hand with the "Getting Started with SNAP" guide (which is a PDF file). It's odd that you have one but not the other. I would expect that the install process will either install both of them or neither of them.

Beware that CHM files are a pain. They pose a security risk, so recent versions of Windows try quite hard to stop you from viewing them. You may have to fiddle with your security settings.