Skip to main content

Associativity in AutoCAD

CAD applications often implement features or entities which are associative in nature, for example creating an extrude surface in AutoCAD 2011, by extruding a profile creates associative extrude surface. When you modify the profile the extruded surface will update itself to follow the profile.
AutoCAD developers now can develop fascinating applications using new associative framework. There are APIs available in C++ as well as .NET to implement associative features in AutoCAD. You can download the latest ObjectARX to make use of associative framework.
The associative framework in AutoCAD helps us to maintain relationship between objects. Relations are represented as hierarchical Networks of Objects, Actions and Dependencies between them. The core building blocks are:
  • Action - Defines behavior or intelligence of application or feature.
  • Dependency- Maintains associativity/ relations between objects and notifies changes on dependent objects.
  • Network- The associative model
  • Object- A regular AutoCAD AcDbObject/AcDbEntity being controlled by an associative action.
Now let's code a simple action where we want to establish a relationship between a line and a circle, where length of line controls diameter of the circle. For this we need to implement an Action class derived from AcDbAssocActionBody, which provides the evaluation logic.
//A custom action derived from AcDbAssocActionBody, establishes relationship
//between a line and a circle. Length of the line drives diameter of the circle.
class MyActionBody : public AcDbAssocActionBody

virtual void evaluateOverride();

static Acad::ErrorStatus createInstance(AcDbObjectId lineId, AcDbObjectId circleId, AcDbObjectId& actionBodyId);

Establishing relationship between objects:

This action depends on both line and circle, so we need to attach dependencies on both line and circle. Since we are going to read the length of line and write/update diameter of the circle, so we attach read dependency on line and write dependency on circle. These dependencies are owned by our action and they will ensure that line is driving entity and circle is the driven entity.

Acad::ErrorStatus MyActionBody::createInstance(AcDbObjectId lineId, 
    AcDbObjectId circleId, AcDbObjectId& actionBodyId)
    //Get the space to which network/action needs to be added.
    AcDbObjectId ownerSpaceId;
        AcDbObjectPointer< AcDbEntity > pEntity(lineId, AcDb::kForRead);
            return pEntity.openStatus();
        ownerSpaceId = pEntity->ownerId();

    //create a new action and action body on the given space.
    AcDbObjectId actionId;
        MyActionBody::desc(), ownerSpaceId, actionId, actionBodyId);

    AcDbObjectPointer< AcDbAssocAction > pAction(actionId, AcDb::kForWrite);
        return pAction.openStatus();

    //Read dependency on line, to read it's length.
    AcDbAssocDependency* pDep = new AcDbAssocDependency();

    //Write dependency on circle to set it's radius equal to length of line.
    pDep = new AcDbAssocDependency();

    return Acad::eOk;

Evaluation logic:

Now the evaluation of action needs to ensure that the desired behavior is maintained after every evaluation. This requires implementation of evaluateOverride() method on the action body class.

void MyActionBody::evaluateOverride()
    AcDbAssocEvaluationCallback* pCallback = this->currentEvaluationCallback();

    AcDbObjectIdArray depIds;
    Acad::ErrorStatus es = getDependencies(true, true, depIds);
    AcDbObjectId lineId, circleId;
    for(int i = 0; i < depIds.length(); ++i)
        AcDbObjectPointer< AcDbAssocDependency > pDep(depIds[i], AcDb::kForRead);
            //Reporting error to the callback.
            if(NULL != pCallback)
                AcDbObjectPointer< AcDbAssocAction > pAction(this->parentAction(), AcDb::kForRead);
                pCallback->setActionEvaluationErrorStatus(pAction, pDep.openStatus(), depIds[i]);

        if(pDep->status() == kErasedAssocStatus)
            //If any dependent object is erased, the corresponding dependency
            //will be mared as kErasedAssocStatus. For this example, we need to
            //erase the action if dependency is to be erased.

        if(!pDep->isWriteDependency()) //read only dependency
            lineId = pDep->dependentOnObject();
        else if(!pDep->isReadDependency()) //write only dependency
            circleId = pDep->dependentOnObject();

    assert(!lineId.isNull() && !circleId.isNull());
    if(lineId.isNull() || circleId.isNull())
        return; //you may report some error here.
    //Evaluate dependencies

    double length = 0.0;
    //Use AcDbAssocObjectPointer to open line for read, so that
    //we get the cloned copy created by dragger, when line is dragged.
    AcDbAssocObjectPointer< AcDbLine > pLine(lineId, AcDb::kForRead);
        return; //you may report some error here.

    AcGePoint3d start, end;
    es = pLine->getStartPoint(start);
    es = pLine->getEndPoint(end);

    length = start.distanceTo(end);

    //Use AcDbAssocObjectPointer to open circle for write, so that
    //we get the clone of circle managed by dragger used to draws updated 
    //circle when line is dragged.
    AcDbAssocObjectPointer< AcDbCircle > pCircle(circleId, AcDb::kForWrite);

    setStatus(kIsUpToDateAssocStatus); //Finally set the status as uptodate.


  1. Interesting stuff Sharad:)

    Couple of questions:
    1. What does evaluateDependencies do in ln 46?

    2. If we want the same dependency between circle and line, for example, change in circles diameter, should modify the length of the line too. Both should be "write" dependencies? In that case how would we know which dependencyId corresponds to which object in evaluateOverride?

  2. Thanks Kartik for visiting my blog. Here are the answers for your questions.

    1. Each dependency may have some evaluation logic and the simplest logic can be to set it's state as uptodate. evaluateDependencies() will mark each dependency status uptodate after evaluation. In certain cases dependencies may cach some values, for example in the above example network evaluation will be take place even if you change the color of line. Since you just wanted to track changes in length, you can have your custom dependency attached on line that caches the value of length. This custom dependency can update it's cache when evaluateDependencies is called. I'll explain dependecies in more detail in my next blog.

    2. If want to update line length to circle diameter as well, then you need to attach read and write dependencies to both line and circle. In that situation you can look at the objectClass() of dependentOnObject to know the type of object, else you may choose to cache object id of line and circle with your custom action. But I prefer not to duplicate the information as long as possible.

  3. Hi Sharad,
    I am an Architect, working in Spore for last 7 years. I would like to get in touch with you regarding Guidance on Computational Design. Would really appreciate if you could give me your contact details. my email id is


Post a Comment

Popular posts from this blog

Linear Regression in Dynamo

Today we are collecting more data than ever but making sense out of the data is very challenging. Machine learning helps us analyze the data and build an analytical model to help us with future prediction. To create a machine learning prediction model we usually develop a hypothesis based on our observation of the collected data and fine tune the model to reduce the cost function by training the model. One of the very simple hypothesis we can develop by assuming a linear relationship between input and output parameters.

Suppose we have data on housing price and we assume that housing prices are linearly related to the floor area of the house then we can use linear regression to predict the price of a house with specific floor area. One of the most important steps towards building the hypothesis is being able to visualize the data and understand the trend. So let first draw a scatter plot of our data as shown in the figure below. I am using Grapher package to draw the scatter plot.

What’s Intent?

Every action has equal and opposite reaction but today we need to know the intent behind an action to react properly. If you can understand the intent you are smart. So what’s the intent, how do you understand it? It’s a thought, an idea, a concept which can be derived from the context by reading between the lines or understanding the gestures. It’s understanding of “I in you”.
Understanding intent leads to providing the best solution for a problem, it leads to innovation. For example, searching for a restaurant on mobile phone has an intent and providing the best search result based on the context/location is innovation. In design paradigm every entity drawn on the canvas has some intent and it can be modified meaningfully only if one understand the intent of the entity. But when author is changed or the tools to modify these entities are changed, most often intents are lost. Capturing intent from actions develops knowledge base and developing knowledge base helps us understand the …