Associative Framework: How to delete constraints applied to a specific entity?

By Philippe Leefsma

Using the Associative framework is not a straightforward task. Some of the components, such as constraints applied to one entity can be buried inside the framework.

The following examples illustrates how to delete all constraints applied to a specific entity and also deleting only some specific constraint types.

 

Here is how to delete all constraints applied to a given entity. There are several ways to get this done, but if you want to get rid of all constraints on an entity at once, the most direct way is as follow:

Acad::ErrorStatus deleteAllConstraintsOnEntity(const AcDbObjectId& entityId)

{

        Acad::ErrorStatus es;

 

        AcDbObjectPointer  pEntity(entityId, AcDb::kForWrite);

 

        if((es = pEntity.openStatus()) != Acad::eOk)

                return es;

 

        AcDbObjectIdArray actionIds;

        if((es = AcDbAssocAction::getActionsDependentOnObject(

                pEntity, true, true, actionIds))!= Acad::eOk)

                return es;

 

        AcDbObjectIdArray dependencyIds;

        if((es = AcDbAssocDependency::getDependenciesOnObject(

                pEntity, true, true, dependencyIds))!= Acad::eOk)

      &#
160;        
return es;

 

        for(int i = 0; i < actionIds.length(); ++i)

        {

             AcDbObjectPointer constraintGroup(actionIds[i], AcDb::kForWrite);

 

             if(constraintGroup.openStatus() != Acad::eOk)

                 continue;

               

             for(int j = 0; j < dependencyIds.length(); ++j)

             {

                if((es = constraintGroup->deleteConstrainedGeometry(

                    dependencyIds[j])) != Acad::eOk)

                {

                        acutPrintf(

                         L"\nFailed to delete constrained geometry…");

                }

             }

        }

 

        return es;     

}

The second example illustrates how to delete fix constraints applied to a line. In that case we need to access the constrained points of the constrained curve, because this is on them that the fix constraint is applied:

 

Acad::ErrorStatus deleteConstraintsOnEntity(

        const AcDbObjectId& entityId,

        const AcRxClass* constraintDesc)

{

        Acad::ErrorStatus es;

 

        AcDbObjectPointer  pEntity(entityId, AcDb::kForWrite);

 

        if((es = pEntity.openStatus()) != Acad::eOk)

                return es;

 

        AcDbObjectIdArray actionIds;

        if((es = AcDbAssocAction::getActionsDependentOnObject(

                pEntity, true, true, actionIds))!= Acad::eOk)

                return es;

 

        AcDbObjectIdArray dependencyIds;

        if((es = AcDbAssocDependency::getDependenciesOnObject(

                pEntity, true, true, dependencyIds))!= Acad::eOk)

                return es;

 

        for(int i = 0; i < actionIds.length(); ++i)

        {

                AcDbObjectPointer constraintGroup(

                    actionIds[i], AcDb::kForWrite);

 

                if(constraintGroup.openStatus() != Acad::eOk)

                        continue;

               

                for(int j = 0; j < dependencyIds.length(); ++j)

                {

                        AcDbObjectPointer geomDependency(

                            dependencyIds[j], AcDb::kForRead);

 

                        if(geomDependency.openStatus() != Acad::eOk)

                                continue;

                       

                        AcConstrainedGeometry* pConsGeom = NULL;

                        if((es = constraintGroup->getConstrainedGeometry(

                                geomDependency, pConsGeom)) != Acad::eOk)

                        {

                                acutPrintf(

                                 L"\nFailed to get constrained geometry…");

                                continue;

                        }

 

                        if(pConsGeom->isKindOf(AcConstrainedCurve::desc()))

                        {

                                AcConstrainedCurve* pConsCurve =

                                    AcConstrainedCurve::cast(pConsGeom);

 

                                AcArray apImplicitPoints;

                                pConsCurve->getConstrainedImplicitPoints(

                                    apImplicitPoints);

 

                                for(int k= 0; k<apImplicitPoints.length(); ++k)

                                {

                                   AcArray apConstraints;

                                   if((es = apImplicitPoints[k]->

                                      getConnectedConstraints(

                                        apConstraints)) != Acad::eOk)

                                        {

                                           acutPrintf(

                                    L"\nFailed to get connected constraints…");

                                                continue;

                                        }

 

                                    for(int l=0;l<apConstraints.length(); ++l){

                                      

                               if(apConstraints[l]->isA()==constraintDesc)

                                    {

                                      if((es=constraintGroup->deleteConstraint(

                                        apConstraints[l])) != Acad::eOk)

                                        {

                                         acutPrintf(

                                          L"\nFailed to delete constraint…");

                                        }

                                    }

                                 }

                            }

                        }

                }

        }

 

        return es;     

}

 

static void ADSConstraintModeling_DeleteEntConstr(void)

{

        Acad::ErrorStatus es;

 

        AcDbObjectId id;

        ads_name name;

        ads_point pt;

 

        if(acedEntSel(L"\nSelect Entity: ", name, pt) != RTNORM)

        {      

                return;

        }

       

        acdbGetObjectId(id, name);

        //es = deleteAllConstraintsOnEntity((id);

        es = deleteConstraintsOnEntity(id, AcFixedConstraint ::desc());

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”>}


Comments

2 responses to “Associative Framework: How to delete constraints applied to a specific entity?”

  1. Hi Philippe,
    any chance to extend your awesome ADNAssocConstraintAPI with that functions?
    Regards Matthias (sorry for being anonymous, I couldn’t sign in)

  2. Hi Matthias, hope you are doing well, I will take a look at that once AU is over, too much things at the moment…
    Cheers,
    Philippe.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading