Add a (planar) sketch to a workplane programatically

Top  Previous  Next

Here is a function which will add a new sketch to a named workplane in a PartComponentDefinition:

 

HRESULT AddSketchToWorkplane (CComPtr<PartComponentDefinition>& pPartCompDef,

                             const wchar_t* const pszNewSketchName,

                             const wchar_t* const pszOldWorkPlaneName)

{

   // Get the workplanes list

   CComPtr<WorkPlanes> pWorkPlaneList ;

   HRESULT hRes = pPartCompDef->get_WorkPlanes(&pWorkPlaneList);

   if (FAILED(hRes) || (pWorkPlaneList == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"AddSketchToWorkplane, could not get workplane list") ;

   }

 

   // Get the workplane to which we will add the sketch...

   CComPtr<WorkPlane> pWorkPlane ;

   hRes = pWorkPlaneList->get_Item (CComVariant(pszOldWorkPlaneName), &pWorkPlane);

   if (FAILED(hRes) || (pWorkPlaneList == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"AddSketchToWorkplane, could find specific workplane") ;

   }

 

   // Get the list of sketches

   CComPtr<PlanarSketches> pSketchList ;

   hRes = pPartCompDef->get_Sketches (&pSketchList);

   if (FAILED(hRes) || (pSketchList == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"AddSketchToWorkplane, could not get planar sketches") ;

   }

 

   // Create a new sketch inside the list of sketches

   CComPtr<PlanarSketch> pNewSketch ; // return value of following call...

   hRes = pSketchList->Add (_variant_t((IDispatch *)pWorkPlane),

                            VARIANT_FALSE, // Ignored when based on a workplane

                            &pNewSketch) ;

   if (FAILED(hRes) || (pNewSketch == nullptr)) {

       return ReturnAndShowCOMError (hRes,L"AddSketchToWorkplane, could not get planar sketches") ;

   }

 

   pNewSketch->put_Name (CComBSTR (pszNewSketchName)) ;

 

   return (S_OK) ;

}

 

See also this page.

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