Add a work point at a sketch point

Top  Previous  Next

Here's a function to place a workpoint in space at the same position as a point in a sketch. This is what we want to do:

 

Workpoint-from-sketchpoint

 

This is how to do it:

 

HRESULT AddWorkPointAtSketchPoint (CComPtr<PartComponentDefinition>& pPartCompDef,

                                  const wchar_t* const pszNewPointName,

                                  const wchar_t* const pszSketchName)

/*

Adds a workpoint in space at the first point in a named sketch

*/

{

   CComPtr<WorkPoints> pWorkPointsList ;

   HRESULT hRes = pPartCompDef->get_WorkPoints (&pWorkPointsList) ;

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

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

   }

 

   CComPtr<PlanarSketch> pSketch ;

   hRes = GetSketchByName (pSketch,pszSketchName,pPartCompDef) ;

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

       return ReturnAndShowCOMError (hRes,L"AddWorkPointAtSketchPoint, could not get sketch") ;

   }

 

   CComPtr<SketchPoints> pSkPoints;

   pSketch->get_SketchPoints (&pSkPoints) ;

   long iNumPoints ;

   hRes = pSkPoints->get_Count (&iNumPoints) ;

   if (FAILED(hRes) || (iNumPoints < 1)) {

       return ReturnAndShowCOMError (hRes,L"AddWorkPointAtSketchPoint, not enough points in sketc") ;

   }

 

   CComPtr<SketchPoint> pSketchPoint ;

   hRes = pSkPoints->get_Item (1,&pSketchPoint) ;

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

       return ReturnAndShowCOMError (hRes,L"AddWorkPointAtSketchPoint, get sketch point failed") ; 

   }

 

   CComPtr<WorkPoint> pWorkPoint ;

   hRes = pWorkPointsList->AddByPoint (_variant_t((IDispatch *)pSketchPoint),VARIANT_FALSE,&pWorkPoint) ;

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

       return ReturnAndShowCOMError (hRes,L"AddWorkPointAtSketchPoint, Add workpoint failed") ; 

   }

 

   pWorkPoint->put_Name (CComBSTR (pszNewPointName)) ;

 

   return (hRes) ;

}

 

This function assumes that there is at least 1 points in the sketch, and takes the first. The function also takes a name for the workpoint because I find that giving signifigant names to objects in Inventor makes them easier to find.

 

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