Occurrences and Component Definitions

Top  Previous  Next

An occurrence is like an XREF insert in AutoCAD. Occurrences exist, for example, inside assemblies. It is an instance of a part. The diagram below shows that an assembly has inside it an AssemblyComponentDefinition, and inside that is a list of occurrences (among ther things):




Here is some code to list the occurrences in an assembly:


// Get assembly component definition

CComPtr<AssemblyComponentDefinition> pAssemblyCompDef;

HRESULT Result = pAssemblyDoc->get_ComponentDefinition(&pAssemblyCompDef);


// Get the ComponentOccurrences collection for the assembly document

CComPtr<ComponentOccurrences> pOccs;

Result = pAssemblyCompDef->get_Occurrences(&pOccs);


long lNoOccs; 

Result = pOccs->get_Count(&lNoOccs);


TRACE("There are %d occurrences\n",lNoOccs);


//iterate through the current level of occurrences

long lOccCount;

for (lOccCount=1; lOccCount <= lNoOccs; ++lOccCount) {


    // Get the nth occurrence...

    CComPtr<ComponentOccurrence> pCompOcc;

    Result = pOccs->get_Item(lOccCount, & pCompOcc);


    //get the display name from the component occurrence

    CComBSTR bstrDisplayName;

    Result = pCompOcc->get_Name(&bstrDisplayName);

    TRACE("Component occurrence %02d <%s>\n",lOccCount,bstrDisplayName);


    // See if there are any other occurrences below the current one...

    CComPtr<ComponentOccurrencesEnumerator> pEnumOccurrences;

    Result = pCompOcc->get_SubOccurrences(&pEnumOccurrences);


    if (SUCCEEDED(Result) && (pEnumOccurrences->Count > 0))  {

        TRACE("   has %d sub occurrences\n",pEnumOccurrences->Count) ;




I've not placed any error checking in the previous code.


How to get the name of an occurrence.





Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)