Listing the contents of a model view BOM

Top  Previous  Next

Once you have an assembly comp def you need to get hold of the BOM and then the BOM views. There are usually three, you need to look for the model view. Then you can list the contents:

 

   // Find the BOM of this assembly

   CComPtr<BOM> pBOM;

   HRESULT hRes = pAsmCompDef->get_BOM(&pBOM);

   if (FAILED(hRes) || (pBOM == nullptr)) {

       ShowCOMError(hRes,L"ALUICBOM, Could not get BOM");

       return ;

   }

 

   // There are usually three types of view

   CComPtr<BOMViews> pBOMViews;

   hRes = pBOM->get_BOMViews(&pBOMViews);

   if (FAILED(hRes) || (pBOMViews == nullptr)) {

       ShowCOMError(hRes,L"ALUICBOM, Could not get BOMViews");

       return ;

   }

 

   long iNumBomViews;

   pBOMViews->get_Count(&iNumBomViews);

 

   for (long bv = 1; bv <= iNumBomViews; bv++) {

       CComVariant varNum(bv);

       CComPtr<BOMView> pBomView;

       pBOMViews->get_Item(varNum, &pBomView);

 

       BOMViewTypeEnum eBVTYpe;

       pBomView->get_ViewType(&eBVTYpe);

 

       if (eBVTYpe != kModelDataBOMViewType) {

           continue; // I'm only interested in the first tab, the model BOM

       }

 

       gLogger.Printf(ekLogMsg, L"BOM Type %d == kModelDataBOMViewType", eBVTYpe);

 

       CComPtr<BOMRowsEnumerator> BomRows;

       pBomView->get_BOMRows(&BomRows);

 

       // Look at each row in the bom

       for (long iRow = 1; iRow <= BomRows->Count; iRow++) {

           CComPtr<BOMRow> ThisBomRow;

           BomRows->get_Item(iRow, &ThisBomRow);

 

           // Of this row, what is the component? Usually there is only one...

           CComPtr<ComponentDefinitionsEnumerator> pDefinitionsEnum;

           ThisBomRow->get_ComponentDefinitions(&pDefinitionsEnum);

 

           // ...assuming there is only one I get the first

           CComPtr<ComponentDefinition> pCompDef;

           pDefinitionsEnum->get_Item(1, &pCompDef) ;

 

           // Get the definition of the part listed in this BOM row

           CComPtr<PartComponentDefinition> pPartCompDef;

           pPartCompDef = pCompDef;

           if (pPartCompDef == nullptr) {

               continue;

           }

 

           CString csPartName = GetPartName(pPartCompDef);

 

           ... etc etc...

       }

   }

 

You may find this function useful:

 

// Returns the BOM view in pBomView and true if all went well

bool GetModelDataBOMView(CComPtr<BOMView>& pBomView, // return value

                        CComPtr<AssemblyComponentDefinition>& pAsmCompDef)

{

   // Find the BOM of this assembly

   CComPtr<BOM> pBOM;

   HRESULT hRes = pAsmCompDef->get_BOM(&pBOM);

   if (FAILED(hRes) || (pBOM == nullptr)) {

       ShowCOMError(hRes,L"GMDBOMV, Could not get BOM");

       return false ;

   }

 

   // There are usually three types of view

   CComPtr<BOMViews> pBOMViews;

   hRes = pBOM->get_BOMViews(&pBOMViews);

   if (FAILED(hRes) || (pBOMViews == nullptr)) {

       ShowCOMError(hRes,L"GMDBOMV, Could not get BOMViews");

       return false ;

   }

 

   long iNumBomViews;

   pBOMViews->get_Count(&iNumBomViews);

 

   gLogger.Printf(ekLogMsg, "There are %d BOM views", iNumBomViews);

 

   for (long bv = 1; bv <= iNumBomViews; bv++) {

       CComVariant varNum(bv);

       pBOMViews->get_Item(varNum, &pBomView);

 

       BOMViewTypeEnum eBVTYpe;

       pBomView->get_ViewType(&eBVTYpe);

 

       if (eBVTYpe == kModelDataBOMViewType) {

           return true ; // Done! Finished! Found!

       }

   }

 

   return false;

 

}

/**********************************************************************************************/

 

 

 

 

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