How to add a sketch on a part's default workplane programatically

Top  Previous  Next

Every part has 3 default workplanes.

 

Default-Workplanes-Small

 

You can add sketches to them programatically using this function:

 

// Adds a sketch to one of the three standard workplanes in a part

bool AddSketchToStdWorkplane (CComPtr<PlanarSketch>& pNewSketch,

                             CComPtr<PartComponentDefinition>& pPartCompDef,

                             const wchar_t* const pszNewSketchName,

                             const UINT ikWorkPlaneNumber) // gikXIndex, gikYIndex, gikZIndex

{

   if ((pszNewSketchName == nullptr) || (wcslen(pszNewSketchName) == 0)) {

       gLogger.Printf (ekErrMsg,L"ASTSWP, workplane name incorrect") ;

       return false;

   }

 

   CComPtr<WorkPlane> pWorkPlane;

   bool bOk = GetStdWorkPlaneByIndex(pWorkPlane, ikWorkPlaneNumber, pPartCompDef);

   if (!bOk) {

       return false;

   }

 

   // Get the list of sketches

   CComPtr<PlanarSketches> pSketchList ;

   HRESULT hRes = pPartCompDef->get_Sketches (&pSketchList);

   if (FAILED(hRes)) {

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

       return false;

   }

 

 

   hRes = pSketchList->Add (_variant_t((IDispatch *)pWorkPlane),VARIANT_FALSE,&pNewSketch) ;

   if (FAILED(hRes)) {

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

       return false;

   }

 

   pNewSketch->put_Name (CComBSTR (pszNewSketchName)) ;

 

   return true;

}

 

The last part of the code above shows you how to get the name of a sketch using get_Name. The following diagram shows the indices to get the standard workplanes:

 

Get-Workplane-Programatically

You can also use: gikXIndex, gikYIndex, gikZIndex

 

This code...

 

   // Create a part

   CComPtr<PartDocument> pPartDoc;

   bool bOk = CreateNewPart(GetInvAppPtr(),pPartDoc,L"Test");

   if (!bOk) {

       return;

   }

 

   CComPtr<PartComponentDefinition> pPartDef;

   HRESULT hRes = pPartDoc->get_ComponentDefinition(&pPartDef);

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

       return;

   }

 

 

   for (UINT i = gikXIndex; i <= gikZIndex; i++) {

       // Add a sketch

       CString kcsSketchName;

       kcsSketchName.Format(L"Sketch%d", i);

       CComPtr<PlanarSketch> pNewSketch;

       AddSketchToStdWorkplane(pNewSketch, pPartDef, kcsSketchName, i);

       AddCircleToSketch(pNewSketch, i * 1, i * 2, i * 3);

   }

 

 

...creates these three sketches...

 

Planes-And-Sketches

 

 

 

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!)