So I got this code an made to work exporting oItemNumberProperty, odescripProperty.Value, oPartNumberProperty.Value and oRevNumberValue. When I added oSource1Property = oCustomPropertySet.Item("Source 1") Source 1 is a custom iproperty field I have in my stuctured BOM. It now throws this error. At that line.
Error on line 116 in rule: ExportBOM, in document: 20.0000061.iam
Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
System.Runtime.InteropServices.COMException (0x80004005):
Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at ThisRule.QueryBOMRowProperties(BOMRowsEnumerator oBOMRows, String JobSpreadsheetName, Int32 iCurrRow) in external rule: ExportBOM:line 116
at ThisRule.Main() in external rule: ExportBOM:line 60
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
Below is my full code.
Sub Main()
'Declarations
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument
' Set a reference to the BOM
Dim oAssyCompDef As AssemblyComponentDefinition
oAssyCompDef = oAssyDoc.ComponentDefinition
'Get Path of current document
Dim sPath As String = ThisDoc.Path
'Set master level Levelof Detail. Needs to be in Master LOD for BOM Manipulation
Dim oLOD As LevelOfDetailRepresentation
Try
oLOD = oAssyCompDef.RepresentationsManager.LevelOfDetailrepresentations.Item("Master")
Catch ex As System.ArgumentException
Finally
oLOD.Activate(True)
End Try
'Get BOM From Assembly
Dim oBOM As BOM
oBOM = oAssyCompDef.BOM
Logger.Trace("oBOM is set")
oBOM.StructuredViewFirstLevelOnly = False
oBOM.StructuredViewEnabled = True
'oBOM.PartsOnlyViewEnabled = True
Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Structured")
Logger.Trace("oStructuredBOMView is set")
'BOM Export
'Copy Job Information Spredshet Form
Dim sJobBOMTempName As String = String.Concat(sPath, "\_BOM_Template.xlsx")
Dim sJobBOMName As String = String.Concat(ThisDoc.PathAndFileName(False), "-BOM.xlsx")
Logger.Trace("sJobBOMName: " & sJobBOMName)
Try
IO.File.Copy(sJobBOMTempName, sJobBOMName, True)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Dim sMySheet As String = "BOM"
Static iCurrRow As Integer = 6
GoExcel.DisplayAlerts = False
GoExcel.Open(sJobBOMName, sMySheet)
'Write BOM Values To Excel
Call QueryBOMRowProperties(oBOMView.BOMRows, sJobBOMName, iCurrRow)
oAssyDoc.Save
'Save Excel
GoExcel.Save
GoExcel.Close
'Set to iLogic LevelOFDetail
Try
oLOD = oAssyCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("iLogic")
Catch ex As System.ArgumentException
oLOD = oAssyCompDef.RepresentationsManager.LevelOfDetailRepresentations.Add("iLogic")
Finally
oLOD.Activate(True)
End Try
'Message Box
Dim sMessage As String = "BOM Export Complete"
Dim sCaption As String = "BOM Export Complete"
Dim oButtons As MessageBoxButtons = MessageBoxButtons.OK
Dim oIcons As MessageBoxIcon = MessageBoxIcon.Information
Dim oResult As DialogResult
' Display the Message Box
oResult = MessageBox.Show(sMessage, sCaption, oButtons, oIcons)
End Sub
Public Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, JobSpreadsheetName As String, iCurrRow As Integer)
'Iterate through the contents of BOM rows
Static CurrentRow As Integer = iCurrRow
Dim i As Long
For i = 1 To oBOMRows.Count
'Get Current row
Dim oRow As BOMRow
oRow = oBOMRows.Item(i)
'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As ComponentDefinition
oCompDef = oRow.ComponentDefinitions.Item(1)
Dim oPropSets As PropertySets
oPropSets = oCompDef.Document.PropertySets
oDesignTrackingPropertySet = oPropSets.Item("Design Tracking Properties")
oInventorSummaryPropertySet = oPropSets.Item("Inventor Summary Information")
oCustomPropertySet = oPropSets.Item("Inventor User defined Properties")
'Get the file properties that are required
oItemNumberProperty = oRow.ItemNumber
oDescripProperty = oDesignTrackingPropertySet.Item("Description")
oPartNumberProperty = oDesignTrackingPropertySet.Item("Part Number")
oRevNumberProperty = oInventorSummaryPropertySet.Item("Revision Number")
oSource1Property = oCustomPropertySet.Item("SOURCE 1")
Trace.WriteLine("oItemNumberProperty: " & oItemNumberProperty)
Trace.WriteLine("oDescripProperty: " & oDescripProperty.Value)
Trace.WriteLine("oPartNumberProperty: " & oPartNumberProperty.Value)
Trace.WriteLine("oRevNumberProperty: " & oRevNumberProperty.Value)
Trace.WriteLine("oSource1Property: " & oSource1Property.Value)
GoExcel.CellValue("A"&CurrentRow) = oItemNumberProperty
GoExcel.CellValue("B"&CurrentRow) = oDescripProperty.Value
GoExcel.CellValue("C" & CurrentRow) = oPartNumberProperty.Value
GoExcel.CellValue("D" & CurrentRow) = oRevNumberProperty.Value
GoExcel.CellValue("E" & CurrentRow) = oSource1Property.Value
'increment row by 1
CurrentRow = CurrentRow + 1
'Recursively itererate child rows if present
If Not oRow.ChildRows Is Nothing Then
Call QueryBOMRowProperties(oRow.ChildRows, JobSpreadsheetName, CurrentRow)
Else
End If
Next i
End Sub