CATIA-V5-在装配体中新建零件并插入

// ------------------------------------------------
/**

  • @brief 插入新的零件

  • @param[in] 无

  • @param[out] ospNewPart 插入新的零件

  • @retval TRUE:成功;FALSE:失败

  • @note [详细说明]

  • @attention [注意事项说明]
    */
    // ------------------------------------------------
    CATBoolean YFBOCreMarkLinesCmd::InsertNewPart(
    CATISpecObject_var& ospNewPart
    )
    {
    CATFrmEditor *pEditor = CATFrmEditor::GetCurrentEditor();
    if (pEditor==NULL)
    {
    return FALSE;
    }
    CATDocument *pDoc = NULL;
    pDoc = pEditor->GetDocument();
    if (NULL == pDoc)
    {
    return FALSE;
    }

    // Search for the document's root product (RootProduct)
    CATUnicodeString ustrTemp = "";
    CATIProduct* piProductOnRoot = NULL;
    CATIDocRoots* piDocRootsOnDoc = NULL;
    CATISpecObject_var spSpecOnDocRoots = NULL_var;
    HRESULT rc = pDoc->QueryInterface(IID_CATIDocRoots,(void) &piDocRootsOnDoc);
    if ( SUCCEEDED(rc) )
    {
    CATListValCATBaseUnknown_var pRootProducts = piDocRootsOnDoc->GiveDocRoots();
    if (NULL != pRootProducts)
    {
    if (pRootProducts->Size())
    {
    CATBaseUnknown_var spBaseTemp = (
    pRootProducts)[1];
    if ( NULL_var != spBaseTemp )
    {
    spBaseTemp->QueryInterface(IID_CATIProduct,(void
    ) &piProductOnRoot);
    }
    spSpecOnDocRoots = spBaseTemp;
    }
    delete pRootProducts;
    pRootProducts = NULL;
    }

    piDocRootsOnDoc->Release();
    piDocRootsOnDoc=NULL;
    

    }

    if (NULL == piProductOnRoot)
    {
    return FALSE;
    }
    else
    {
    // Adds a new CATPart under the root product.
    // creates a new part document
    CATDocument *pPartDoc1 = NULL;
    rc = CATDocumentServices::New("Part", pPartDoc1);
    if ( FAILED(rc) || NULL == pPartDoc1 )
    {
    return FALSE;
    }
    else
    {
    // 遍历所有Product
    CATUnicodeString ustrAliasName = "";
    CATUnicodeString ustrSubString = "";
    int iFindPos = 0;
    int iNum = 0;
    int iTempNum = 0;
    CATUnicodeString ustrPartName = "MakeLinePart_";
    CATListValCATBaseUnknown_var pChildProducts = piProductOnRoot->GetAllChildren();
    if (NULL == pChildProducts)
    {
    return FALSE;
    }
    if (pChildProducts->Size() != 0)
    {
    for (int iLoop = 1; iLoop <= pChildProducts->Size();iLoop++)
    {
    CATBaseUnknown_var spChildProductTemp = (
    pChildProducts)[iLoop];
    CATIAlias_var spiAs = spChildProductTemp;
    if (spiAs == NULL_var)
    {
    continue;
    }
    ustrAliasName = spiAs->GetAlias();
    iFindPos = ustrAliasName.SearchSubString(ustrPartName);
    if (iFindPos != -1)
    {
    iTempNum = ustrAliasName.GetLengthInChar() - ustrPartName.GetLengthInChar();
    ustrSubString=ustrAliasName.SubString(iFindPos + ustrPartName.GetLengthInChar(),iTempNum);
    // 将尾数转化为数字
    ustrSubString.ConvertToNum(&iTempNum);
    if (iTempNum > iNum)
    {
    iNum = iTempNum;
    }
    }
    }

            ustrTemp = "";
            iNum = iNum + 1;
            ustrTemp.BuildFromNum(iNum);
            ustrPartName = ustrPartName + ustrTemp;
        }
    
        CATIProduct *piInstanceProd1 = NULL;
        // imports the new document in root product
        CATIDocRoots *piDocRootsOnNewDoc = NULL;
        rc = pPartDoc1->QueryInterface(IID_CATIDocRoots, (void**) &piDocRootsOnNewDoc);
        if (SUCCEEDED(rc))
        {
            rc = E_FAIL;
            CATListValCATBaseUnknown_var * pRootProducts = piDocRootsOnNewDoc->GiveDocRoots();
            if (NULL != pRootProducts)
            {
                if (pRootProducts->Size() > 0)
                {  
                    CATIProduct_var spRootProduct = (*pRootProducts)[1];
                    if (NULL_var != spRootProduct)
                    {
                        // 设定零件编号
                        spRootProduct->SetPartNumber(ustrPartName);
                        CATIProduct_var spProduct = piProductOnRoot->AddProduct(spRootProduct);
                        if (NULL_var !=  spProduct )
                        {
                            rc = spProduct->QueryInterface(IID_CATIProduct, 
                                (void**) &piInstanceProd1);
                            if (NULL == piInstanceProd1 || FAILED(rc))
                            {
                                return FALSE;
                            }
                        }
                    }
                }
                delete pRootProducts;
                pRootProducts = NULL;
            }
            piDocRootsOnNewDoc->Release();
            piDocRootsOnNewDoc=NULL;
        }
        if ( NULL == piInstanceProd1 ) 
        {
            return FALSE;
        }
    
        CATILinkableObject_var spLinkInstProduct;      
        if(SUCCEEDED(piInstanceProd1->GetShapeRep(spLinkInstProduct, "Default", CATPrd3D, TRUE)))
        {
            CATIPrtPart *pPartInstProduct = NULL;
            rc = spLinkInstProduct->QueryInterface(IID_CATIPrtPart, (void **) &pPartInstProduct);
            if (NULL == pPartInstProduct || FAILED(rc))
            {
                return FALSE;
            }
            CATISpecObject_var spNewPart = pPartInstProduct;
            if (NULL_var == spNewPart)
            {
                return FALSE;
            }
            // 输出插入的零件
            ospNewPart = spNewPart;
        }
    }
    
    // 刷新装配树
    CATISpecObject* pSpecOnRoot = spSpecOnDocRoots;
    if (NULL == pSpecOnRoot)
    {
        return FALSE;
    }
    CATIRedrawEvent_var spRedrawOnRoot(pSpecOnRoot);
    if (NULL_var == spRedrawOnRoot)
    {
        return FALSE;
    }
    spRedrawOnRoot->Redraw();
    
    // 刷新模型
    CATModify pModifyOnRoot(pSpecOnRoot);
    CATIModelEvents_var spMeOnRoot = pSpecOnRoot;
    if (NULL_var == spMeOnRoot)
    {
        return FALSE;
    }
    spMeOnRoot->Dispatch(pModifyOnRoot);
    if (NULL != pSpecOnRoot)
    {
        pSpecOnRoot->Release();
        pSpecOnRoot = NULL;
    }
    

    }

    return TRUE;
    }

你可能感兴趣的:(CATIA-V5-在装配体中新建零件并插入)