Listing members in an iPartFactory programatically

Top  Previous  Next

Here is how to list members of an iPartFactory, starting with the PartDocument:

 

   // Open the standard part...

   CComPtr<PartComponentDefinition> pTroncPartCompDef = nullptr ;

   CComPtr<PartDocument> pTroncPartDoc = nullptr ;

   HRESULT hRes = OpenPart (pTroncPartCompDef,pTroncPartDoc,gkcsTroncFactory,pInvApp) ;

 

   // Check that it is an iPartFactory...

   if (pTroncPartCompDef->IsiPartFactory == VARIANT_FALSE) {

       gLogger.Printf (ekImmediateErrorMsg,L"%s non e' un iPartFactory",gkcsTroncFactory) ;

       // This is NOT an iPartFactory

       return ;

   }

 

   /*

    * Get the iPartFactory from the component definition...

    */

   CComPtr<iPartFactory> piPartFactory = nullptr ;

   hRes =pTroncPartCompDef->get_iPartFactory (&piPartFactory) ;

 

   /*

    * Get the rows of the factory, each row corresponds to a "member"...

    */

   CComPtr<iPartTableRows> piPartTableRows = nullptr ;

   piPartFactory->get_TableRows (&piPartTableRows) ;

 

   /*

    * Loop over the members in the table, printing Member...

    */

   for (int iRow = 1 ; iRow <= piPartTableRows->Count ; iRow++) {

       CComPtr<iPartTableRow> piPartRow ;

       piPartTableRows->get_Item (iRow,&piPartRow) ;

 

       const int ikNumColumns = piPartRow->GetCount() ;

       BSTR bstrMember = nullptr ;

       piPartRow->get_MemberName (&bstrMember) ;

 

       TRACE (L"    Row %03d:%s has %d things\n",iRow,LPCSTR(bstrMember),ikNumColumns) ;

       for (int iCol = 1 ; iCol <= ikNumColumns ; iCol++) {

           CComPtr<iPartTableCell> piPartTableCell = nullptr ;

           piPartRow->get_Item (CComVariant(iCol),&piPartTableCell) ;

 

           CComBSTR bstrCellString ;

           piPartTableCell->get_Value (&bstrCellString) ;

           TRACE (L"   iRow %d iCol %d has value %s\n",iRow,iCol,LPWSTR(bstrCellString)) ;

       }

 

       ::SysFreeString (bstrMember) ;

   }

 

As usual there's not as much error checking in the source above as there should be. That last inner loop could use a function like this:

 

wchar_t* GetStringFromiPartTableRowCell (wchar_t* const szCellContents,

                                         size_t ikBufferSize,

                                         const UINT ikCol, 

                                         CComPtr<iPartTableRow> piPartRow)  

{

    if (int(ikCol) > piPartRow->GetCount()) {

        szCellContents[0] = char(0) ;

        return szCellContents ;

    }

 

    CComPtr<iPartTableCell> piPartTableCell  ;

    piPartRow->get_Item (CComVariant(ikCol),&piPartTableCell) ;

 

    CComBSTR bstrCellString ; 

    piPartTableCell->get_Value (&bstrCellString) ;

    wcscpy_s (szCellContents,ikBufferSize,LPWSTR(bstrCellString)) ;

 

    return szCellContents ;

}

 

 

 

 

See also iPartMember. and

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