admin 发表于 2018-1-10 10:12:25

Catia二次开发源码分享: 获取装配所有组件列表


Catia二次开发源码分享: 获取装配所有组件列表


/* ----------------------------*/
        /* 2. Retrieves Root Product   */
        /* ----------------------------*/
       
        // Begin navigation through the document => start with the RootProduct.
        CATIDocRoots* piDocRootsOnDoc = NULL;
        rc = pDoc->QueryInterface(IID_CATIDocRoots,
                                    (void**) &piDocRootsOnDoc);
        if ( FAILED(rc) ) return 3;
       
        // get the root product which is the first element of root elements
        CATListValCATBaseUnknown_var* pRootProducts =
                piDocRootsOnDoc->GiveDocRoots();
        CATIProduct_var spRootProduct = NULL_var;
       
        if (pRootProducts && pRootProducts->Size())
        {
                spRootProduct = (*pRootProducts);
                delete pRootProducts;
                pRootProducts = NULL;
        }
/** @anchor err_1 piDocRootsOnDoc not set to NULL after release */
        piDocRootsOnDoc->Release();
        piDocRootsOnDoc = NULL;
       
        // Get CATIProduct handle on the root product.
        CATIProduct *piProductOnRoot = NULL;
        rc = spRootProduct->QueryInterface(IID_CATIProduct,
                                             (void**) &piProductOnRoot);
        if ( FAILED(rc) ) return 3;

        /* ---------------------------------------*/
        /* 3. Retrieves children under the root   */
        /* ---------------------------------------*/
       
        int nbOfDirectChidren = piProductOnRoot -> GetChildrenCount() ;
        cout << " Number of direct children under the root = " << nbOfDirectChidren << endl << flush;
       
        // then on a root product, get all the children agregated to it.
        CATListValCATBaseUnknown_var*   ListChildren =
                piProductOnRoot->GetAllChildren();
/** @anchor err_2 piProductOnRoot not set to NULL after release */
        piProductOnRoot -> Release();
        piProductOnRoot = NULL;
        if(NULL != ListChildren)
        {
               
                int numberOfChildren = ListChildren->Size();
                cout << " Number of all children under the root = " << numberOfChildren << endl << flush;

                /* -----------------------------------------------------------*/
                /*4. For each child, get its partNumber, and InstanceName   */
                /* -----------------------------------------------------------*/
                CATIProduct_var spChild = NULL_var;
                for (int i=1;i<=numberOfChildren;i++)
                {
                        spChild = (*ListChildren);
/** @anchor err_3 spChild not tested before use ( if !! ) */
                        if ( NULL_var == spChild ) return 4;
                        CATUnicodeString partNumber = spChild -> GetPartNumber();
                        CATUnicodeString instanceName (" ");
                        rc = spChild -> GetPrdInstanceName ( instanceName ) ;
                        if( FAILED(rc) ) return 4;
                       
                        cout << " child number : " << i << endl << flush;
                        cout << " has as part number : " << partNumber.CastToCharPtr()<< endl << flush;
                        cout << " and as instanceName : " << instanceName.CastToCharPtr() << endl << endl << flush;
                }
                delete ListChildren;
                ListChildren=NULL;
        }       
        /* -------------------------------------------- */
        /*   Ends the session                        */
        /* -------------------------------------------- */
页: [1]
查看完整版本: Catia二次开发源码分享: 获取装配所有组件列表