I can use the following C++ code to loop over the constraints in an assembly:
for (int i = 1 ; i <= pConstraintList->Count ; i++) { CComPtr<AssemblyConstraint> pConstraint = nullptr ; pConstraintList->get_Item (CComVariant(i),&pConstraint) ; TRACE ("Type is %d\n",pConstraint->GetType()) ; CComQIPtr<FlushConstraint> pFlush (pConstraint) ; if (nullptr != pFlush) { IDispatchPtr pEnt1 = pFlush->EntityOne ; IDispatchPtr pEnt2 = pFlush->EntityTwo ; TRACE (L"Two parts are %p %p\n",pEnt1,pEnt2) ; // The question is, how to convert to PartDocument pointers??? } }
But how do I convert from EntityOne to (for example) a PartDocument. I've experimented with all sorts of casts and conversions, but cannot get a CComPtr<PartDocument> from an IDispatchPtr
Solved! Go to Solution.
Solved by adam.nagy. Go to Solution.
Hi,
You cannot cast EntityOne to a PartDocument because it is a FaceProxy. Just wrote an article that should be of help:
http://adndevblog.typepad.com/manufacturing/2013/10/discover-object-model.html
Cheers,
Ok, thanks for the link, now I undestand better. The 2 entities can also be WorkPlaneProxy not just FaceProxy.
I've changed my sources and can now get to the document,file, part etc like this:
for (int i = 1 ; i <= pConstraintList->Count ; i++) { CComPtr<AssemblyConstraint> pConstraint = nullptr ; pConstraintList->get_Item (CComVariant(i),&pConstraint) ; TRACE ("Type is %d\n",pConstraint->GetType()) ; CComQIPtr<MateConstraint> pMate (pConstraint) ; if (nullptr != pMate) { IDispatchPtr pEnt1 = pMate->EntityOne ; IDispatchPtr pEnt2 = pMate->EntityTwo ; 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") ; } } }
Can the entities be anything else, apart from Face or WorkPlane proxies?
Hi,
Yes, I forgot about WorkPlane's.
Well, it would need to be something with a plane, so I think it's only face and workplane.
But if you go down the other route: Constraint > AffectedOccurrenceOne >... then it does not even matter what EntityOne is.
Cheers,