Option Strict Off
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Assemblies
Public class PList1
Public Property Part_Name As String
Public Property Attrib_Name As String
Public Property Value_Name As String
Public Sub New(ByVal PartN As String, ByVal AttrN As String, ByVal ValN As String)
Part_Name = PartN
Attrib_Name = AttrN
Value_Name = ValN
End Sub
End Class
Module NXJournal
Public theSession As Session = Session.GetSession()
Public ufs As UFSession = UFSession.GetUFSession()
Public theUISession As UI = UI.GetUI
Public lw As ListingWindow = theSession.ListingWindow
Dim PartList1 as New list (of PList1) ()
Dim List1 as New List (of String)
Sub Main()
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
Dim saveFileName As String
Dim saveDialog As New System.Windows.Forms.SaveFileDialog
Dim objExcel As Object
Dim objWorkbook As Object
Dim excelFileExists As Boolean = False
Dim objWorksheet As Object
Dim colPartname As Integer = 1
Dim colAttribname As Integer = 2
Dim colValueNumber As Integer = 3
Dim rowNumber As Integer = 1
lw.Open
Dim My_attrib1 as String
try
My_attrib1 = workpart.GetStringAttribute("CYL_Code")
Catch ex011 As NXException
If ex011.ErrorCode = 512008 Then
My_attrib1 = "-"
Else
lw.WriteLine("error: " & ex011.ErrorCode & ", " & ex011.Message)
End If
end try
if My_attrib1 = "" or My_attrib1 = " " or My_attrib1 = "-" then
else
if not list1.contains(workpart.name) then
list1.add(Workpart.name)
Dim attr_info() As NXObject.AttributeInformation = workPart.GetUserAttributes
For Each ainfo As NXObject.AttributeInformation In attr_info
'if type is string, do something with the value
If ainfo.Type = NXObject.AttributeType.String and ainfo.title = "CYL_Code" Then
'lw.WriteLine(ainfo.title & " = " & ainfo.StringValue)
Partlist1.add(new PList1(workpart.name, ainfo.title, ainfo.StringValue))
End If
Next
end if
end if
Try
Dim c As ComponentAssembly = dispPart.ComponentAssembly
if not IsNothing(c.RootComponent) then
ReportComponentChildren(c.RootComponent, 0)
lw.writeline(" ")
ReportComponentChildren2(c.RootComponent, 0)
else
lw.WriteLine("Part has no components")
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
if Not IsNothing(Partlist1) AndAlso Partlist1.Count > 0 Then
partlist1.sort(AddressOf Comparer)
lw.writeline(" ")
lw.writeline("--------------------------------------")
lw.writeline("| File name | Attribute name | Value |")
lw.writeline("--------------------------------------")
lw.writeline(" ")
for each comp_ as PList1 in Partlist1
lw.writeline(comp_.Part_Name & " | " & comp_.Attrib_Name & " | " & comp_.Value_Name )
next
With saveDialog
.DefaultExt = "xlsx"
.FileName = "My data"
.Filter = "MS Excel Spreadsheets (*.xlsx) | *.xlsx | All Files (*.*) | *.*"
.FilterIndex = 1
.OverwritePrompt = True
.Title = "Select a file where you'd like to save the exported data."
End With
savedialog.showdialog()
saveFileName = saveDialog.FileName
If saveFileName = "Exported Data" Then
MsgBox("You failed to select a save file. Exiting the macro.")
Exit Sub
End If
objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
theUISession.NXMessageBox.Show("Error", theUISession.NXMessageBox.DialogType.Error, "Could not start Excel, journal exiting")
Exit Sub
End If
If File.Exists(saveFileName) Then
excelFileExists = True
objWorkbook = objExcel.Workbooks.Open(saveFileName)
objWorksheet = objWorkbook.Sheets.Add
Else
objWorkbook = objExcel.Workbooks.Add
objworkbook.saveas(SaveFilename)
objWorksheet = objWorkbook.Sheets(1)
End If
objWorksheet.cells(rowNumber, colPartname).Value = "Part Name"
objWorksheet.cells(rowNumber, colAttribname).Value = "Atrribute name"
objWorksheet.cells(rowNumber, colValueNumber).Value = "Value"
for each comp_ as PList1 in Partlist1
'lw.writeline(comp_.Part_Name & " | " & comp_.Attrib_Name & " | " & comp_.Value_Name )
rowNumber += 1
objWorksheet.cells(rowNumber, colPartname).Value = comp_.Part_Name
objWorksheet.cells(rowNumber, colAttribname).Value = comp_.Attrib_Name
objWorksheet.cells(rowNumber, colValueNumber).Value = comp_.Value_Name
next
objWorksheet.columns("A:M").entirecolumn.autofit
objWorkbook.save
objWorkbook.close
objExcel.quit()
Cleanup(objWorksheet, objWorkbook, objExcel)
MsgBox("Completed the extraction successfully! Check " & saveFileName & " for the data.")
end if
lw.Close
End Sub
Sub Cleanup(ParamArray objs As Object())
GC.Collect()
GC.WaitForPendingFinalizers()
For Each obj As Object In objs
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Next
End Sub
'*****************************************************************************************
' Empty run
'*****************************************************************************************
Sub reportComponentChildren( ByVal comp As Component, ByVal indent As Integer)
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
For Each child As Component In comp.GetChildren()
Dim MyPart As Part = child.Prototype.OwningPart
Try
if child.IsSuppressed = true then
lw.writeline(" Error: " & child.DisplayName & ".prt" & " -> " & "File closed")
Continue for
end if
Catch e1 As Exception
theSession.ListingWindow.WriteLine("Failed: " & e1.ToString)
end try
If LoadComponent(child) Then
Else
'component could not be loaded
End If
reportComponentChildren(child, indent+1)
Next
End Sub
'*****************************************************************************************
' Changing file name
'*****************************************************************************************
Sub reportComponentChildren2( ByVal comp As Component, ByVal indent As Integer)
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
For Each child As Component In comp.GetChildren()
Dim MyPart As Part = child.Prototype.OwningPart
Try
if child.IsSuppressed = true then
'lw.writeline("Error: " & child.DisplayName & ".prt" & " " & "File closed")
'exit sub
'exit for
Continue for
end if
Catch e1 As Exception
theSession.ListingWindow.WriteLine("Failed: " & e1.ToString)
end try
If LoadComponent(child) Then
Dim File_name as string
Dim My_attrib as String
File_name = child.DisplayName()
try
My_attrib = mypart.GetStringAttribute("CYL_Code")
'lw.writeline("File name: " & child.Name)
'lw.writeline("CYL_Code: " & My_atrib)
Catch ex01 As NXException
If ex01.ErrorCode = 512008 Then
My_attrib = "-"
Else
lw.WriteLine("error: " & ex01.ErrorCode & ", " & ex01.Message)
End If
end try
'lw.writeline(child.Name & " , " & My_atrib)
if My_attrib = "" or My_attrib = " " or My_attrib = "-" then
else
if not list1.contains(child.name) then
list1.add(child.name)
'lw.writeline("")
'lw.writeline("component name: " & child.Name)
Dim attr_info() As NXObject.AttributeInformation = myPart.GetUserAttributes
For Each ainfo As NXObject.AttributeInformation In attr_info
'if type is string, do something with the value
If ainfo.Type = NXObject.AttributeType.String and ainfo.title = "CYL_Code" Then
'lw.WriteLine(ainfo.title & " = " & ainfo.StringValue)
Partlist1.add(new PList1(child.name, ainfo.title, ainfo.StringValue))
End If
Next
end if
end if
'lw.writeline("component name: " & child.Name & "-> " & File_name )
'lw.writeline("")
Else
'component could not be loaded
End If
reportComponentChildren2(child, indent + 1)
Next
End Sub
'**********************************************************
Private Function Comparer(ByVal x As Plist1, ByVal y As Plist1) As Integer
Dim result As Integer = x.Part_name.CompareTo(y.Part_name)
If result = 0 Then
result = x.Attrib_Name.CompareTo(y.Attrib_Name)
End If
Return result
End Function
'**********************************************************
' Function loading components
'**********************************************************
Private Function LoadComponent(ByVal theComponent As Component) As Boolean
Dim thePart As Part = theComponent.Prototype.OwningPart
Dim partName As String = ""
Dim refsetName As String = ""
Dim instanceName As String = ""
Dim origin(2) As Double
Dim csysMatrix(8) As Double
Dim transform(3, 3) As Double
Try
If thePart.IsFullyLoaded Then
'component is fully loaded
Else
'component is partially loaded
End If
Return True
Catch ex As NullReferenceException
'component is not loaded
Try
ufs.Assem.AskComponentData(theComponent.Tag, partName, refsetName, instanceName, origin, csysMatrix, transform)
Dim theLoadStatus As PartLoadStatus
theSession.Parts.Open(partName, theLoadStatus)
If theLoadStatus.NumberUnloadedParts > 0 Then
Dim allReadOnly As Boolean = True
For i As Integer = 0 To theLoadStatus.NumberUnloadedParts - 1
If theLoadStatus.GetStatus(i) = 641058 Then
'read-only warning, file loaded ok
Else
'641044: file not found
lw.WriteLine("Error: " & partname & " " & "File not found")
allReadOnly = False
End If
Next
If allReadOnly Then
Return True
Else
'warnings other than read-only...
Return False
End If
Else
Return True
End If
Catch ex2 As NXException
if ex2.message = "File not found" then
lw.WriteLine("Error: " & partname & " " & "File not found")
else
lw.WriteLine("Error: " & partname & " " & ex2.Message)
end if
Return False
End Try
Catch ex As NXException
'unexpected error
lw.WriteLine("error: " & ex.Message)
Return False
End Try
End Function
'**********************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
'**********************************************************
End Module