Listing constraints in an assembly programatically

Top  Previous  Next

Here is a code fragment showing how to list constraints and cast EntityOne and EntityTwo into appropriate proxies. Graphically the constraint and two proxies look like this:

 

Constraint-EntityOne-EntityTwo

 

 

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

   CComPtr<AssemblyConstraints> pConstraintList = nullptr ;

   hRes = pAssemblyCompDef->get_Constraints(&pConstraintList) ;

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

       TRACE (L"Could not get constraints.") ;

       return ;

   }

 

   // List the two entities of each MateConstraint...

   for (int i = 1 ; i <= pConstraintList->Count ; i++) {

       CComPtr<AssemblyConstraint> pConstraint = nullptr ;

       pConstraintList->get_Item (CComVariant(i),&pConstraint) ;

       CComQIPtr<MateConstraint> pMate (pConstraint) ;

       if (nullptr != pMate) {

           // This is a mate constraing, show more data about it...

           IDispatchPtr pEnt1 = pMate->EntityOne  ;

           IDispatchPtr pEnt2 = pMate->EntityTwo  ;

           TRACE (L"  Two parts are %p %p\n",pEnt1,pEnt2) ;

           CComQIPtr<FaceProxy> pFace1 = CComQIPtr<FaceProxy>(pEnt1);

           CComQIPtr<WorkPlaneProxy> pWP1 = CComQIPtr<WorkPlaneProxy>(pEnt1);

           if (pFace1 != nullptr) {

               TRACE (L"  Ent1 is a face proxy...\n") ;

 

           } else if (pWP1 != nullptr) {

               TRACE (L"  Ent1 is a workplane proxy...\n") ;

               CComPtr<ComponentOccurrence> pContOcc = pWP1->ContainingOccurrence ;

               if (pContOcc != nullptr) {

                   CComPtr<ReferencedFileDescriptor> pFileDesc = nullptr ;

                   pContOcc->get_ReferencedFileDescriptor (&pFileDesc) ;

                   if (pFileDesc != nullptr) {

                       CComBSTR bstrFullFileName ;

                       pFileDesc->get_FullFileName (&bstrFullFileName) ;

                       TRACE (L"  Ent1 full file name is %s\n",bstrFullFileName) ;

                   }

               }

 

           } else {

               TRACE (L"  Ent1 This is neither a face or workplane proxy\n") ;

           }

       }

   }

 

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