Listing and checking constraints |
Top Previous Next |
This function goes over the constraints in an assembly checking that a given named constraint does not already exist:
bool ConstraintNameAlreadyExists (const CString& csNameToCheck, CComPtr<AssemblyComponentDefinition>& pAsmCompDef) { // Get the list of constraints of the assembly CComPtr<AssemblyConstraints> pConstraintList = nullptr ; HRESULT hRes = pAsmCompDef->get_Constraints(&pConstraintList) ; if (FAILED(hRes) || (pConstraintList == nullptr)) { ShowCOMError (hRes,L"ConstraintNameAlreadyExists could not get constraints.") ; return true ; // pretend it exists hopefully to stop further processing }
const long ikNumConstraints = pConstraintList->GetCount() ; for (int iConstraint = 1 ; iConstraint <= ikNumConstraints ; iConstraint++) { CComPtr<AssemblyConstraint> pConstraint = nullptr ; hRes = pConstraintList->get_Item(CComVariant(iConstraint),&pConstraint) ; if (FAILED(hRes) || (pConstraint == nullptr)) { ShowCOMError (hRes,L"ConstraintNameAlreadyExists could not get constraint %d.",iConstraint) ; return true ; // pretend it exists hopefully to stop further processing }
CComBSTR bstrConstraintName ; pConstraint->get_Name(&bstrConstraintName) ; TRACE (L"Checking constraint <%s>\n",bstrConstraintName) ; if (CString(bstrConstraintName).CompareNoCase(csNameToCheck) == 0) { // A constraint with the given name already exists return true ; } }
// If we get here then there are no constraints with the same name as csNameToCheck return false ; }
By the way, when your assemblies get even a bit complex it will probably help you to name the constraints you add, using put_Name.
|
Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)