AnsweredAssumed Answered

Create a point in hole feature of a component in an assembly

Question asked by Wasim Ahmed on Jan 14, 2015

Description: A hole feature has 2 sketches(one for location and the other for hole defining). This hole feature is placed in a component in an assembly.

I would like to place a point at location x, y, z. I have first used Component transform followed by its inverse and then model to sketch transform.

The code I am posting does create the point at the right location but the hole doesn't generate .


    HRESULT hr = S_OK;


       CComPtr<IMathUtility> PMathUtil;

       hr = pSwApp->IGetMathUtility(&PMathUtil);


       CComPtr<IModelDoc2> swModel;

       hr = pSwApp->get_IActiveDoc2(&swModel);


       CComPtr<ISelectionMgr> pSelMgr;

       hr = swModel->get_ISelectionManager(&pSelMgr);


       CComPtr<IModelDocExtension> pModExtn;

       hr = swModel->get_Extension(&pModExtn);



       CComBSTR CompName(L"L-1@00/LDS-1@L");                                        //Selecting the component in the assembly

       CComBSTR Type2(L"COMPONENT");

       hr = pModExtn->SelectByID2(CompName, Type2, 0.0, 0.0, 0.0, VARIANT_FALSE, 0, NULL, 0, &VBStat);


       long Ind , Mark ;

       CComPtr<IDispatch> pDisp;

       Ind = 1; Mark = -1;

       hr = pSelMgr->GetSelectedObject6(Ind, Mark, &pDisp);


       CComPtr<IComponent2> pComp;

       hr = pDisp->QueryInterface(&pComp);


       CComPtr<IMathTransform> pCompTrFrom;                                                  //Getting its transform

       hr = pComp->get_Transform2(&pCompTrFrom);



       hr = pCompTrFrom->Inverse(&pDisp);                                                            //Getting its inverse



       hr = pDisp->QueryInterface(&pCompTrFrom);


       double A[3] = {-.050, .030, -.085};

       VARIANT var3;

       var3.vt = VT_ARRAY|VT_R8;

       var3.parray = SafeArrayCreateVector(VT_R8, 0l, 3l);

       long i = 0;

       hr = SafeArrayPutElement(var3.parray, &i, &A[0]);

       hr = SafeArrayPutElement(var3.parray, &++i, &A[1]);

       hr = SafeArrayPutElement(var3.parray, &++i, &A[2]);



       hr = PMathUtil->CreatePoint(var3, &pDisp);


       CComPtr<IMathPoint> pMthPnt;

       hr = pDisp->QueryInterface(&pMthPnt);



       hr = pMthPnt->MultiplyTransform(pCompTrFrom, &pDisp);



       hr = pDisp->QueryInterface(&pMthPnt);


       VARIANT Var;

       hr = pMthPnt->get_ArrayData(&Var);


       CComBSTR Name(L"Sketch3@L-1@00/LDS-1@L");                                                                 //Selecting the sketch in the hole feature of the component

       CComBSTR Type(L"SKETCH");                                                                               

       hr = pModExtn->SelectByID2(Name, Type, VARIANT_FALSE, 0.0, 0.0, 0.0, 0, NULL, 0, &VBStat);



       hr = pSelMgr->GetSelectedObject6(Ind, Mark, &pDisp);


       CComPtr<IFeature> pFeat;

       hr = pDisp->QueryInterface(&pFeat);



       hr = pFeat->GetSpecificFeature2(&pDisp);


       CComPtr<ISketch> pSketch;

       hr = pDisp->QueryInterface(&pSketch);//got sketch


       CComPtr<IMathTransform> pMathTransform;

       hr = pSketch->get_ModelToSketchTransform(&pMathTransform);                                             //getting the model to sketch transform



       hr = pMthPnt->MultiplyTransform(pMathTransform, &pDisp);



       hr = pDisp->QueryInterface(&pMthPnt);


       CComVariant VarMn;

       hr = pMthPnt->get_ArrayData(&VarMn);


       long Sz, lBnd;

       hr = GetSafeArrayElementSize(&VarMn, Sz, lBnd);

       double B[3];


       for(long i = lBnd; i < Sz; i++)


            hr = SafeArrayGetElement(VarMn.parray, &i, &B[i]);


       hr = swModel->EditSketch();                                                       Entering the edit sketch mode

       hr = swModel->ClearSelection2(VARIANT_TRUE);

       CComPtr<ISketchManager> pSkhMgr;

       hr = swModel->get_SketchManager(&pSkhMgr);

       hr = pSkhMgr->put_AddToDB(VARIANT_TRUE);

       CComPtr<ISketchPoint> pSkhPnt;

       hr = pSkhMgr->CreatePoint(B[0], B[1], B[2], &pSkhPnt);               //Creating the sketch point


       hr = pSkhMgr->put_AddToDB(VARIANT_FALSE);

       hr = pSkhMgr->InsertSketch(VARIANT_TRUE);