Create Virtual Component by Code

By Xiaodong Liang

The virtual component is defined by a VirtualComponentDefinition. Through the definition, you can also get the information of component settings.

Sub VirtualC()
    ' assume an assembly document is opened
    Dim oAssDoc As AssemblyDocument
    Set oAssDoc = ThisApplication.activeDocument
    Dim oAssDef As AssemblyComponentDefinition
    Set oAssDef = oAssDoc.ComponentDefinition
    Dim oMatrix As matrix
    Set oMatrix = ThisApplication.TransientGeometry.CreateMatrix

    ' add one virtual occurrence
    Dim oNewOcc As ComponentOccurrence
    Set oNewOcc = oAssDef.Occurrences.AddVirtual("MyVirtual", oMatrix)
    Dim oCVirtualCompDef As VirtualComponentDefinition
    Set oCVirtualCompDef = oNewOcc.Definition
    ' get BOMStructure
    Debug.Print "BOMStructure of Virtual Component: " & oCVirtualCompDef.BOMStructure

    Dim oQuantityType As BOMQuantityTypeEnum
    Dim oBaseQuantity As Variant
     ' get QuantityType and Evaluated Quantity
    Call oCVirtualCompDef.BOMQuantity.GetBaseQuantity(oQuantityType, oBaseQuantity)
    Dim oEvaluatedQuantityType As BOMQuantityTypeEnum
     Debug.Print "QuantityType: " & oEvaluatedQuantityType
    Dim oEvaluatedQuantity As Double
    oEvaluatedQuantity = oCVirtualCompDef.BOMQuantity.GetEvaluatedBaseQuantity(oEvaluatedQuantityType)
    Debug.Print "Quantity: " & oEvaluatedQuantity
End Sub

image


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading