I'm replying because I've found an alternative which might help others. I needed a function which, in an assembly, created a sketch offset from one of the standard planes. Here's the full C++ function:
bool CreateSketchOffsetFromXYPlaneInAsm (CComPtr<AssemblyComponentDefinition>& pAsmCompDef, // where the sketch will be added
CComPtr<PlanarSketch>& pNewSketch, // The sketch created, an output
const double kOffsetMm,
const wchar_t* const pszSketchName) // Name of the newly created sketch
{
// Get PlanarSketches, the list of sketches
CComPtr<PlanarSketches> pSketches = nullptr ;
HRESULT hRes = pAsmCompDef->get_Sketches(&pSketches);
if (FAILED(hRes) || (pSketches == nullptr)) {
ShowCOMError (hRes,L"CXYPOSIA, get_Sketches failed") ;
return false ;
}
// Get standard default WorkPlanes, probably 3 only initially in the list
CComPtr<WorkPlanes> pWorkPlanes = nullptr ;
hRes = pAsmCompDef->get_WorkPlanes(&pWorkPlanes);
if (FAILED(hRes) || (pWorkPlanes == nullptr)) {
ShowCOMError (hRes,L"CXYPOSIA, get_WorkPlanes failed") ;
return false ;
}
// Get hold of one of the WorkPlanes. Valid indices are 1L 2L 3L for standard workplanes
CComPtr<WorkPlane> pXYWorkPlane = nullptr ;
hRes = pWorkPlanes->get_Item(_variant_t(3L, VT_I4),&pXYWorkPlane);
if (FAILED(hRes) || (pXYWorkPlane == nullptr)) {
ShowCOMError (hRes,L"CXYPOSIA, get_Item (workplane) failed") ;
return false ;
}
CComPtr<TransientGeometry> pTransGeom = theApp.GetTransGeomPtr () ;
CComPtr<Point> pOrigin = nullptr ;
hRes = pTransGeom->CreatePoint (0,0,0,&pOrigin);
CComPtr<UnitVector> UnitXVector ;
pTransGeom->CreateUnitVector (1.0,0,0,&UnitXVector) ;
CComPtr<UnitVector> UnitYVector ;
pTransGeom->CreateUnitVector (0.0,1.0,0,&UnitYVector) ;
// Internally Inventor always uses cm, so convert...
const double kOffsetInCm = kOffsetMm/10.0 ;
pOrigin->PutZ (kOffsetInCm) ;
CComPtr<WorkPlane> pOffsetWorkPlane ;
hRes = pWorkPlanes->AddFixed(pOrigin,UnitXVector,UnitYVector,VARIANT_FALSE,&pOffsetWorkPlane) ;
if (pOffsetWorkPlane == nullptr) {
gLogger.Printf (ekErrMsg,L"CXYPOSIA, Could not AddFixed") ;
return false ;
}
// Now actually create a sketch and get a pointer to it in one go...
hRes = pSketches->Add (_variant_t((IDispatch *)pOffsetWorkPlane),
VARIANT_FALSE,
&pNewSketch);
if (FAILED(hRes)) {
ShowCOMError (hRes,L"CXYPOSIA, AddSketch failed") ;
return false ;
}
pNewSketch->put_Name (BSTR(pszSketchName)) ;
return true ;
}