Add a mate constraint with two planes programatically

Top  Previous  Next

Here is a function which constrains two occurrences by mating two WorkPlanes in the occurrences. It maybe has more error checking than normal, but it helps debug stuff sometimes. Note also that it handles the proxies, so the caller of the function does not need to worry about those:

 

// Given two occurrences which contain workplanes to be mated together

// this function creates a Mate constraint between them

bool AddMateConstraintOfTwoPlanes (CComPtr<AssemblyComponentDefinition>& pAsmCompDef,

                                  CComPtr<ComponentOccurrence>& pOccA,

                                  CComPtr<WorkPlane>& pWorkPlaneA,

                                  CComPtr<ComponentOccurrence>& pOccB,

                                  CComPtr<WorkPlane>& pWorkPlaneB,

                                  const double kOffsetMm/*=0.0*/)

{

   if ((pWorkPlaneA == nullptr) || (pWorkPlaneB == nullptr)) {

       gLogger.Printf(ekErrMsg, L"Errore: (pWorkPlaneA == nullptr) || (pWorkPlaneB == nullptr)");

       return false;

 

   } else if ((pOccA == nullptr) || (pOccB == nullptr)) {

       gLogger.Printf(ekErrMsg, L"Errore: (pOccA == nullptr) || (pOccB == nullptr)");

       return false;

   }

 

   CComPtr<WorkPlaneProxy> pWPProxyA ;

   HRESULT hRes = pOccA->CreateGeometryProxy (pWorkPlaneA,(IDispatch**)&pWPProxyA) ;

   if (FAILED(hRes)) {

       const CString kcsWorkPlaneName = GetWorkPlaneName(pWorkPlaneA);

       const CString kcsOccName = GetOccurrenceName(pOccA);

       ShowCOMError (hRes,L"AMCOTP could not get pWPProxyA, plane=<%s> occurrence=<%s>.",kcsWorkPlaneName,kcsOccName) ;

       return false ;

   }

 

   CComPtr<WorkPlaneProxy> pWPProxyB ;

   pOccB->CreateGeometryProxy (pWorkPlaneB,(IDispatch**)&pWPProxyB) ;

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"AMCOTP could not get pWPProxyB.") ;

       return false ;

   }

 

   // Get the list of constraints of the assembly so you can add a new one

   CComPtr<AssemblyConstraints> pConstraintList ;

   hRes = pAsmCompDef->get_Constraints(&pConstraintList) ;

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"AMCOTP could not get constraints.") ;

       return false ;

   }

 

   CComVariant varOffset(kOffsetMm/10.0); // Inventor wants cm

   CComPtr<MateConstraint> pMateConstraint = nullptr ;  

   hRes = pConstraintList->AddMateConstraint(pWPProxyA, pWPProxyB,

                                             varOffset,

                                             kNoInference,

                                             kNoInference,

                                             gkvarEmpty,

                                             gkvarEmpty,

                                             &pMateConstraint);

   if (FAILED(hRes)) {

       ShowCOMError (hRes,L"AddMateConstraint failed") ;

       return false ;

   }

 

   return true ;

}

 

Note that we use proxies, which give the position of the planes in the coordinate system of the assembly. I assume here that the two occurrences have been taken from the AssemblyComponentDefinition.

 

Another thing to note (and maybe change with FlipNormal) when mating WorkPlanes is the normal. Depending on the part this could flip or rotate your part by 180 degrees.

 

 

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