Inserting an extrusion in a Part

Top  Previous  Next

You need to have a part .ipt document open for the following function to work, and that .ipt must have a sketch called Sketch1 already existing inside it. I've tested it with an empty Sketch1.


I've removed much of the hRes checking, but you should do that checking:


static HRESULT AddSolid(CComPtr<Application> pInvApp)


   CComPtr<Document> piDoc;

   HRESULT hRes = pInvApp->get_ActiveDocument(&piDoc);


   CComQIPtr<PartDocument> pPartDoc(piDoc); // you should check that pPartDoc does not result in nullptr


   CComPtr<PartComponentDefinition> piPartCompDef;

   hRes = pPartDoc->get_ComponentDefinition(&piPartCompDef);


   CComPtr<TransientGeometry> oTG;

   hRes = pInvApp->get_TransientGeometry(&oTG);


   CComPtr<Point2d> oCenterP;

   hRes = oTG->CreatePoint2d(0, 0, &oCenterP);


   CComPtr<PlanarSketch> pSketch = NULL;

   hRes = piPartCompDef->Sketches->get_Item(CComVariant(_T("Sketch1")), &pSketch);


   CComPtr<SketchCircle> pSketchCircle = NULL;

   hRes = pSketch->SketchCircles->AddByCenterRadius(oCenterP,2);


   CComPtr<Profile> pProfile = NULL;

   CComVariant pSegs;

   CComVariant preserve;

   hRes = pSketch->Profiles->AddForSolid(VARIANT_TRUE,pSegs,preserve,&pProfile);


   CComPtr<PartFeatures> pPartFs;

   hRes = piPartCompDef->get_Features(&pPartFs);


   CComPtr<ExtrudeFeatures> pExtrudeFs;

   hRes = pPartFs->get_ExtrudeFeatures(&pExtrudeFs);


   CComPtr<ExtrudeDefinition> pExtrudeDef;

   hRes = pExtrudeFs->CreateExtrudeDefinition(pProfile,kJoinOperation,&pExtrudeDef);




   CComPtr<ExtrudeFeature> pExtrude;

   hRes = pExtrudeFs->Add (pExtrudeDef);

   if (FAILED(hRes)) {

       TRACE ("AddSolid but could Add ExtrudeDefinition\n");

       return hRes ;



   TRACE ("AddSolid finished, extrusion added ok.\n");


   return hRes;



Good luck young man.

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