Add a plane by offset from another plane

Top  Previous  Next

Note that in theory these two functions work, but currenty in Inventor 2015, you cannot add planes programatically inside assemblies. For that use the function at the very end of this page.

 

static bool CreateSketchOffsetFromXYPlane (CComPtr<PlanarSketch>& pNewSketch,   // The sketch created, output

                                          CComPtr<WorkPlanes> pWorkPlanes,     // Where the sketch added

                                          CComPtr<PlanarSketches> pSketches,   // The list to we add sketch

                                          const double kOffset,                // How far to offset

                                          const wchar_t* const pszSketchName)  // Name of the newsketch

{

   // Get hold of one of the WorkPlanes. Valid indices are 1L 2L 3L for standard workplanes

   CComPtr<WorkPlane> pXYWorkPlane  ;

   HRESULT hRes = pWorkPlanes->get_Item(_variant_t(3L, VT_I4),&pXYWorkPlane);

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

       ShowCOMError (hRes,L"CXYPOS, get_Item (workplane) failed") ;

       return false ;

   }

 

   // Create a workplane parallel to the standard XY Plane...

   CComPtr<WorkPlane> pOffsetWorkPlane  ;

   hRes = pWorkPlanes->AddByPlaneAndOffset (_variant_t((IDispatch *)pXYWorkPlane), // "starting" plane

                                            CComVariant(kOffset),   // how much to offset

                                            VARIANT_TRUE,       // Construction

                                            &pOffsetWorkPlane) ; // Return value

 

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

       ShowCOMError (hRes,L"CXYPOS, could not add workplane with offset, pOffsetWorkPlane=%p",pOffsetWorkPlane) ;

       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"CXYPOS, AddSketch failed") ;

       return false ;

   }

 

   pNewSketch->put_Name (CComBSTR (pszSketchName)) ;

 

   CComBSTR bstrSketchName ;

   pNewSketch->get_Name (&bstrSketchName) ;

 

   TRACE (L"The sketch is now called %s\n",bstrSketchName) ;

 

   return true ;

}

 

/**************************************************************************************************/

 

bool CreateSketchOffsetFromXYPlaneInPart (CComPtr<PartComponentDefinition>& pPartComponentDefinition,  // where the sketch will be added

                                         CComPtr<PlanarSketch>& pNewSketch,     // The sketch created, an output

                                         const double kOffset,

                                         const wchar_t* const pszSketchName)    // Name of the newly created sketch

{

   // Get PlanarSketches, the list of sketches

   CComPtr<PlanarSketches> pSketches  ;

   HRESULT hRes = pPartComponentDefinition->get_Sketches(&pSketches);

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

       ShowCOMError (hRes,L"CreateXYPlaneSketch, get_Sketches failed") ;

       return false ;

   }

 

   // Get standard default WorkPlanes, probably 3 only initially in the list

   CComPtr<WorkPlanes> pWorkPlanes  ;

   hRes = pPartComponentDefinition->get_WorkPlanes(&pWorkPlanes);

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

       ShowCOMError (hRes,L"CreateXYPlaneSketch, get_WorkPlanes failed") ;

       return false ;

   }

 

   bool bOk = CreateSketchOffsetFromXYPlane (pNewSketch,   // The sketch created, an output

                                             pWorkPlanes,  // Where the sketch will be added

                                             pSketches,    // List to which we must add the sketch

                                             kOffset,      // How far to offset

                                             pszSketchName) ;  // Name of the newly created sketch

 

   return bOk ;

}

 

/*************************************************************************************************/

 

 

bool CreateSketchOffsetFromXYPlaneInAsm (CComPtr<AssemblyComponentDefinition>& pAsmCompDef,  // where the sketch will be added

                                        CComPtr<PlanarSketch>& pNewSketch,     // The sketch created, an output

                                        const double kOffset,

                                        const wchar_t* const pszSketchName)    // Name of the newly created sketch

{

   // Get PlanarSketches, the list of sketches

   CComPtr<PlanarSketches> pSketches  ;

   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  ;

   hRes = pAsmCompDef->get_WorkPlanes(&pWorkPlanes);

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

       ShowCOMError (hRes,L"CXYPOSIA, get_WorkPlanes failed") ;

       return false ;

   }

 

   bool bOk = CreateSketchOffsetFromXYPlane (pNewSketch,   // The sketch created, an output

                                             pWorkPlanes,  // Where the sketch will be added

                                             pSketches,    // list to which we must add the sketch

                                             kOffset,      // How far to offset

                                             pszSketchName) ;  // Name of the newly created sketch

 

   return bOk ;

}

 

/*************************************************************************************************/

 

Here is a function which will work in assemblies:

 

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  ;

    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  ;

    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  ;

    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 = GetTransGeomPtr () ;    

    CComPtr<Point> pOrigin  ;

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

        TRACE (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 ;

}

 

 

 

 

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