Add a WorkPlane in an Assembly

Top  Previous  Next

Here is a function to to it based on the existing standard workplanes of an assembly:

 

bool AddFixedWorkPlaneInAsm (CComPtr<AssemblyComponentDefinition>& pAsmCompDef// where the plane will be added

                            const int ikPlaneIdx, // Which of the standard planes to base the new one on

                            const double kOffsetMm, // Offset,

                            const wchar_t* const pszPlaneName)    // Name of the newly created plane

{

   gLogger.Printf (ekLogMsg,L"AddFixedWorkPlaneInAsm %d <%s> starting",ikPlaneIdx,pszPlaneName) ;

   // Get WorkPlanes

   CComPtr<WorkPlanes> pWorkPlanes ;

   HRESULT hRes = pAsmCompDef->get_WorkPlanes(&pWorkPlanes);

   if (FAILED(hRes)) {

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

       return false ;

   }

 

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

   CComPtr<WorkPlane> pAsmStdWorkPlane ;

   hRes = pWorkPlanes->get_Item(CComVariant(ikPlaneIdx),&pAsmStdWorkPlane);

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

       ShowCOMError (hRes,L"AFWPIA, get_Item (workplane %d) failed",ikPlaneIdx) ;

       return false ;

   }

 

   // Get the standard plane to use as a base for the offset plane

   PlanePtr ThePlane = pAsmStdWorkPlane->GetPlane() ;

   PointPtr StdPlaneOrigin = ThePlane->RootPoint ;

 

   CComPtr<TransientGeometry> pTransGeo = GetTransGeomPtr () ;   

 

   // Internally Inventor always uses cm, so convert...

   const double kOffsetInCm = kOffsetMm/10.0 ;

 

   CComPtr<Point> pOrigin ;

   hRes = pTransGeom->CreatePoint (StdPlaneOrigin->X,StdPlaneOrigin->Y,StdPlaneOrigin->Z,&pOrigin);

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

       ShowCOMError (hRes,L"AFWPIA, could not create origin point") ;

       return false ;

   }

 

   CComPtr<UnitVector> uVector;

   CComPtr<UnitVector> yVector;

   switch (ikPlaneIdx) {

       case gikXYPlaneIndex :

           pTransGeo->CreateUnitVector (1.0, 0.0, 0.0, &uVector) ;

           pTransGeo->CreateUnitVector (0.0, 1.0, 0.0, &yVector) ;

           pOrigin->PutZ (kOffsetInCm) ;

           break ;

 

       case gikXZPlaneIndex :

           pTransGeo->CreateUnitVector (1.0, 0.0, 0.0, &uVector) ;

           pTransGeo->CreateUnitVector (0.0, 0.0, 1.0, &yVector) ;

           pOrigin->PutY (kOffsetInCm) ;

           break ;

 

       case gikYZPlaneIndex :

           pTransGeo->CreateUnitVector (0.0, 1.0, 0.0, &uVector) ;

           pTransGeo->CreateUnitVector (0.0, 0.0, 1.0, &yVector) ;

           pOrigin->PutX (kOffsetInCm) ;

           break ;

 

       default :

           gLogger.Printf (ekErrMsg,L"AFWPIA unhandled index:%d", ikPlaneIdx) ;

           // Do something anyway...

           pTransGeo->CreateUnitVector (1.0, 0.0, 0.0, &uVector) ;

           pTransGeo->CreateUnitVector (0.0, 1.0, 0.0, &yVector) ;

           // pOrigin->PutZ (kOffsetInCm) ;

           break ;

   }

 

   CComPtr<WorkPlane> pOffsetWorkPlane;

   hRes = pWorkPlanes->AddFixed(pOrigin,uVector,yVector, VARIANT_FALSE,&pOffsetWorkPlane);

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

       ShowCOMError (hRes,L"AFWPIA, Could not AddFixed plane %d",ikPlaneIdx) ;

       return false ;

   }

 

   CComBSTR bstrName = CString (pszPlaneName) ;

   pOffsetWorkPlane->put_Name (bstrName) ;

 

   gLogger.Printf (ekLogMsg,L"AddFixedWorkPlaneInAsm <%s> ending ok",pszPlaneName) ;

 

   return true ;

}

 

 

 

 

Here's some other less flexible code:

 

       HRESULT Result = NOERROR;

 

       CLSID InvAppClsid;

       Result = CLSIDFromProgID (L"Inventor.Application", &InvAppClsid);

       if (FAILED(Result)) return;

 

       CComPtr<IUnknown> pInvAppUnk;

       Result = ::GetActiveObject(InvAppClsid, NULL, &pInvAppUnk);

       if (FAILED(Result))

       {

           _tprintf_s(_T("*** Could not get hold of an active Inventor application ***\n"));

           return ;

       }

 

       CComPtr<Application> pInvApp;

       Result = pInvAppUnk->QueryInterface (__uuidof(Application), (void **) &pInvApp);

       if (FAILED(Result)) return ;

 

       CComPtr<Document> oDoc;

       CComPtr<AssemblyDocument> pAssyDoc;

       Result = pInvApp->get_ActiveDocument(&oDoc);

       pAssyDoc = oDoc;

 

       CComPtr<TransientGeometry> pTransGeo;

       Result = pInvApp->get_TransientGeometry(&pTransGeo);

 

       CComPtr<Point> pt;

       Result = pTransGeo->CreatePoint(0, 0, -100,&pt);

 

       CComPtr<UnitVector> xVector;

       Result = pTransGeo->CreateUnitVector(1,0, 0, &xVector);

 

       CComPtr<UnitVector> yVector;

       Result = pTransGeo->CreateUnitVector(0, 1, 0, &yVector);

 

       CComPtr<WorkPlane> wPlane;

 

       Result = pAssyDoc->ComponentDefinition->WorkPlanes->AddFixed(pt,xVector,yVector, VARIANT_FALSE,&wPlane);

 

 

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