Back to blog

CAD Automation

How to Write Dimensions to iProperties for All Assembly Parts in Autodesk Inventor Using iLogic

I recently received a question about retrieving part dimensions and writing them to iProperties for every component in an assembly. The task isn’t complex,…

Nikita Gurov

I recently received a question about retrieving part dimensions and writing them to iProperties for every component in an assembly. The task isn’t complex, but it does require some familiarity with the Inventor .NET API.

Sub Main()
    'Check file type
    If Not ThisDoc.Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
    'Loop through ref docs
    For Each refDoc As Document In ThisDoc.Document.AllReferencedDocuments
        'Skip docs other than parts
        If Not refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then Continue For
        'Cast to part document
        Dim prtDoc As PartDocument = refDoc
        'Set part component def
        Dim prtDocDef As PartComponentDefinition = prtDoc.ComponentDefinition
        'Get bounding box
        Dim box As Box = prtDocDef.RangeBox
        'Get units of measure and current length units
        Dim uom As UnitsOfMeasure = prtDoc.UnitsOfMeasure
        Dim lenUnits As UnitsTypeEnum = uom.LengthUnits
        ' Dimensions in database units
        Dim dxDb As Double = box.MaxPoint.X - box.MinPoint.X
        Dim dyDb As Double = box.MaxPoint.Y - box.MinPoint.Y
        Dim dzDb As Double = box.MaxPoint.Z - box.MinPoint.Z
        'Convert box dimensions into current length units
        Dim dx As Double = uom.ConvertUnits(dxDb, UnitsTypeEnum.kCentimeterLengthUnits, lenUnits)
        Dim dy As Double = uom.ConvertUnits(dyDb, UnitsTypeEnum.kCentimeterLengthUnits, lenUnits)
        Dim dz As Double = uom.ConvertUnits(dzDb, UnitsTypeEnum.kCentimeterLengthUnits, lenUnits)
        'Set property values
        SetIPropValue(prtDoc, "Dimension X", dx)
        SetIPropValue(prtDoc, "Dimension Y", dy)
        SetIPropValue(prtDoc, "Dimension Z", dz)
    Next
End Sub

Sub SetIPropValue(doc As Document, propName As String, val As Double)
    'Get custom properties
    Dim custPropSet = doc.PropertySets("User Defined Properties")
    'Get / Add property
    Dim prop As [Property]
    Try
        'Get required property
        prop = custPropSet(propName)
        'Set value, assuming the property value type is correct
        prop.Value = val
    Catch
        'If failed to get - create new
        prop = custPropSet.Add(val, propName)
    End Try
End Sub