Add an iMate (based on a point) into an assembly

Top  Previous  Next

Here is a function to do it:

 

bool AddMateiMateAtAsmWorkPoint(const CString& kcsiMateName,

                               CComPtr<AssemblyComponentDefinition> pAsmCompDef,

                               const CString& kcsWorkPointName)

{

   CComPtr<iMateDefinitions> piMateDefinitions = nullptr ;

   HRESULT hRes = pAsmCompDef->get_iMateDefinitions(&piMateDefinitions);

   if (FAILED(hRes)) {

       TRACE ("AMMAAWP: Failed to get iMate definitions");

       return false ;

   }

 

   CComPtr<WorkPoint> pWorkPoint;

   const bool kbWpOk = GetWorkPointByName(pWorkPoint, kcsWorkPointName, pAsmCompDef);

   if (!kbWpOk) {

       TRACE ("AMMAAWP: Failed to get work point called <%s>",kcsWorkPointName);

       return false;

   }

 

   // Zero offset

   CComVariant varOffset = CComVariant(0.0);

 

   CComPtr<MateiMateDefinition> pMateiMateDef;

   hRes = piMateDefinitions->AddMateiMateDefinition(pWorkPoint,

                                                    varOffset,

                                                    kNoInference,

                                                    gkvarEmpty,

                                                    CComBSTR(kcsiMateName),

                                                    gkvarEmpty,

                                                    &pMateiMateDef);

   if (FAILED(hRes)) {

       TRACE (L"AMMAAWP:Failed to add iMate definition at work point.",kcsWorkPointName);

       return false;

   }

 

   return true;

}

 

Here is some example code which also opens the assembly...

 

   CComPtr<AssemblyComponentDefinition> pAsmCompDef;

   CComPtr<AssemblyDocument> pAsmDoc;

   const CString kcsFullAsmFileName(L"C:\\TEST.IAM") ;

 

   const bool kbOpenedOk = OpenAssembly (pAsmCompDef,pAsmDoc,kcsFullAsmFileName) ;

   if (!kbOpenedOk) {

       TRACE("Failed to open");

       return;

   }

 

   CComPtr<iMateDefinitions> piMateDefinitions = nullptr ;

   HRESULT hRes = pAsmCompDef->get_iMateDefinitions(&piMateDefinitions);

   if (FAILED(hRes)) {

       TRACE("Failed to get imate definitions");

       return;

   }

 

   CComPtr<WorkPoint> pWorkPoint;

   const bool kbWpOk = GetWorkPointByName(pWorkPoint, L"ZeroPoint", pAsmCompDef);

   if (!kbWpOk) {

       TRACE("Could not find work point");

       return;

   }

 

   CComVariant varOffset = CComVariant(0.0);

 

   CComPtr<MateiMateDefinition> pMateiMateDef;

   hRes = piMateDefinitions->AddMateiMateDefinition(pWorkPoint,

                                                    varOffset,

                                                    kNoInference,

                                                    gkvarEmpty,

                                                    CComBSTR("Pippo"),

                                                    gkvarEmpty, &pMateiMateDef);

   if (FAILED(hRes)) {

       TRACE("Failed to add imate definition.");

   }

 

   pAsmDoc->Close(VARIANT_FALSE);

 

See what I mean?

 

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