API - Erstellen einer Layout-Stückliste mit Factory-API

Eine iLogic-Regel, die Factory Design-Eigenschaften und Inventor iProperties extrahiert und zu einem Bericht in Excel kombiniert

Erstellen Sie eine iLogic-Regel, die eine Stückliste eines Layouts nach Excel exportieren kann, die Objekteigenschaften (z. B. Objektbeschriftungen) mit ausgewählten Inventor-Bauteileigenschaften und benutzerdefinierten iProperties kombiniert.

AddReference "Autodesk.Factory.PublicAPI.dll"
Imports Autodesk.Factory.PublicAPI.Currency.v2
Sub Main()
    PrepareExcelFile()
    SetValuesInExcel()

    MessageBox.Show ("iLogic sample done")
End Sub

Sub SetValuesInExcel()
    Const COLUMN_InstanceID As String = "A"
    Const COLUMN_InstanceName As String = "B"
    Const COLUMN_InstanceAssetTag As String = "C"
    Const COLUMN_PartNumber As String = "D"

    GoExcel.CellValue(COLUMN_InstanceID + "1") = "Factory Instance ID"
    GoExcel.CellValue(COLUMN_InstanceName + "1") = "Factory Instance Name"
    GoExcel.CellValue(COLUMN_InstanceAssetTag + "1") = "Factory Asset Tag"
    GoExcel.CellValue(COLUMN_PartNumber + "1") = "iProperty Part Number"

    Dim occ As ComponentOccurrence
    Dim instances = Autodesk.Factory.PublicAPI.API.Instance.GetAssetInstances(ThisDoc.Document)
    Dim row = 2
    For Each instance In instances
        occ = instance.NativeObject
        GoExcel.CellValue(COLUMN_InstanceID + row.ToString) = instance.InstanceID
        GoExcel.CellValue(COLUMN_InstanceName + row.ToString) = instance.InstanceName
        GoExcel.CellValue(COLUMN_InstanceAssetTag + row.ToString) = GetAssetTag(instance)
        GoExcel.CellValue(COLUMN_PartNumber + row.ToString) = iProperties.Value(occ.Name, "Project", "Part Number")
        row +=1
    Next

    GoExcel.Save
    GoExcel.Close
End Sub    

Function GetAssetTag(instance As IAssetInstance)
    GetAssetTag = ""
    For Each grp In instance.PropertyGroups
        If (grp.Name = "AssetTag") Then
            For Each prop In grp.Properties
                If (prop.Name = "Asset Tag") Then
                    GetAssetTag = prop.Value
                    Exit Function
                End If
            Next 
        End If
    Next
End Function

Sub PrepareExcelFile
    fileName = System.Environment.ExpandEnvironmentVariables("%temp%\FactoryData.xlsx")
    If (System.IO.File.Exists(fileName)) 
        System.IO.File.Delete (fileName)
    End If

    excelApp = CreateObject("Excel.Application")
    excelApp.Visible = True
    excelApp.DisplayAlerts = False
    excelWorkbook = excelApp.Workbooks.Add
    excelWorkbook.SaveAs (fileName)
    excelWorkbook.Close
    GoExcel.Open(fileName, "Sheet1")
    excelApp = Nothing
End Sub