Enforcing geometric invariants in AutoCAD with AcDbAssocAction

AssocHexBolt demo HEXBOLTADD and HEXBOLTEDIT in AutoCAD. HEXBOLTADD creates a hex bolt head from a nominal diameter (ISO 4017) -HEXBOLTEDIT changes the diameter and the hex outline plus its across-flats dimension update together via AcDbAssocAction.

When one input changes, every dependent object should update with it. A hex bolt head makes that behavior easy to see: the nominal diameter determines both the head’s across-flats width and its height. If the associative update is wrong, the outline and its dimension can disagree.

I built AssocHexBolt with the ObjectARX associative framework to demonstrate that update path. It follows the pattern used by Autodesk’s AssocFillet sample, where a radius drives an arc, but applies it to a shape whose dimensions come from a standard.

The command surface

Command: HEXBOLTADD
Specify hex bolt head position: 10,10,10
Nominal bolt diameter (mm) <6>: 12
M12 bolt -> head across-flats=18.0 mm, height=7.5 mm

Command: HEXBOLTEDIT
Select hex bolt head to edit: <pick the hex outline>
Nominal bolt diameter (mm) <12>: 20
M20 bolt -> head across-flats=30.0 mm, height=12.5 mm

The edit updates the existing objects in place; it does not create another hex head.

Standard dimensions, not a continuous formula

Bolt sizes are discrete. A formula such as “diameter times a factor plus an offset” would only approximate the standard dimensions. This implementation uses a lookup table of supported nominal sizes and their across-flats width (s) and head height (k):

struct Iso4017Entry { double d, s, k; };
const Iso4017Entry kIso4017Table[] = {
    { 3.0,  5.5,  2.0 },
    { 5.0,  8.0,  3.5 },
    { 6.0, 10.0,  4.0 },
    { 8.0, 13.0,  5.3 },
    {10.0, 16.0,  6.4 },
    {12.0, 18.0,  7.5 },
    {16.0, 24.0, 10.0 },
    {20.0, 30.0, 12.5 },
};

This is a subset of ISO 4017 sizes, not a complete size table. The current implementation snaps the entered diameter to the nearest listed size. If the command should accept only exact supported sizes, it should validate the input instead of silently snapping it.

The diameter is stored as a named value parameter, with an optional expression, following the same general pattern as the radius in AssocFillet:

double HexBoltActionBody::getDiameter(AcString &expressionOut) const
{
    expressionOut = L"";
    AcDbEvalVariant value;
    AcString tmp;
    if (!eOkVerify(getValueParam(kDiameterParamName, value, expressionOut, tmp)))
        return 0.0;
    double diameter = 0.0;
    return eOkVerify(value.getValue(diameter)) ? fabs(diameter) : 0.0;
}

Acad::ErrorStatus HexBoltActionBody::setDiameter(double diameter, const AcString &expression)
{
    AcString tmp;
    return setValueParam(kDiameterParamName, fabs(diameter), expression, L"", tmp, true);
}

Building the hex from one number

For a regular hexagon, the across-flats width s and circumradius R are related by s = R * sqrt(3). The code below computes six vertices, 60 degrees apart:

static void buildHexVertices(AcDbPolyline *pPline,
                             const AcGePoint3d &center,
                             double s)
{
    const double circumRadius = s / 1.7320508075688772; // s / sqrt(3)
    for (int i = 0; i < 6; i++)
    {
        const double angle = i * (M_PI / 3.0);
        const AcGePoint2d vertex(center.x + circumRadius * cos(angle),
                                  center.y + circumRadius * sin(angle));
        if ((int)pPline->numVerts() <= i)
            pPline->addVertexAt(i, vertex);
        else
            pPline->setPointAt(i, vertex);
    }
    pPline->setClosed(true);
}

This snippet shows the XY vertex calculation. The complete implementation also needs to place the polyline in the intended plane and account for its elevation or coordinate system; AcDbPolyline vertices are 2D.

The head height k is applied through the polyline’s Thickness property. That gives the outline an extrusion for 3D viewing:

AcDbAssocObjectPointer<AcDbPolyline> pHexHead(getHexHeadId(), kForWrite);
buildHexVertices(pHexHead, mPosition, s);
pHexHead->setThickness(k);

The action uses AcDbAssocObjectPointer to open the dependent entity. During a drag, AutoCAD can provide this pointer with a temporary, non-database-resident clone, so the action can update the preview without modifying the database-resident entity mid-drag.

A second output: a dimension derived from the same parameter

The action also updates an AcDbAlignedDimension between two opposite hex flats. The dimension points are computed from the same across-flats value used to build the hex:

static void computeDimensionGeometry(const AcGePoint3d &center, double s,
                                      AcGePoint3d &xLine1Out,
                                      AcGePoint3d &xLine2Out,
                                      AcGePoint3d &dimLinePointOut)
{
    // Opposite hex edges are s apart. Their midpoints provide
    // dimension points for the across-flats distance.
    xLine1Out = hexFlatMidpoint(center, s, 0);
    xLine2Out = hexFlatMidpoint(center, s, 3);
    // ... perpendicular offset for dimLinePointOut omitted here
}

AcDbAssocObjectPointer<AcDbAlignedDimension> pDim(getDimensionId(), kForWrite);
pDim->setXLine1Point(xLine1);
pDim->setXLine2Point(xLine2);
pDim->setDimLinePoint(dimLinePt);

Change the nominal size from M10 to M20 and the dimension text changes from 16.00 to 30.00. That gives the user a second visible output to inspect alongside the outline. Since both outputs are derived from the same parameter, though, the dimension is not an independent proof that the polyline has the correct width. A separate measurement of the resulting geometry would be needed for that check.

When this approach fits

A dynamic block is useful when the geometry and its permitted variations can be authored in a block definition. An associative action fits a different workflow: custom code creates the geometry from parameters and updates it during evaluation.

Here, the hex and dimension are generated in code from the diameter parameter through evaluateOverride(). If the design later adds a counterbore, bolt circle, or washer, that geometry and its relationships can be added to the action’s implementation.

Why this matters more now

The associative framework evaluates dependent actions after their inputs change. What has changed is who, or what, may request the change. A generative or agentic tool can propose a drawing edit, and its request may be wrong.

The framework can report whether this action evaluated successfully. If a dependent write fails or an expected object is missing, the action can report the error and set its status to kFailedToEvaluateAssocStatus:

AcDbAssocObjectPointer<AcDbPolyline> pHexHead(getHexHeadId(), kForWrite);
if (!eOkVerify(err = pHexHead.openStatus()))
    goto Done;
// ...
Done:
if (err == eOk)
{
    setStatus(kIsUpToDateAssocStatus);
}
else
{
    AcDbObjectPointer<AcDbAssocAction> pAction(parentAction(), kForRead);
    currentEvaluationCallback()->setActionEvaluationErrorStatus(pAction, err);
    setStatus(kFailedToEvaluateAssocStatus);
}

That status gives a calling tool something concrete to check before it trusts the result. It does not, by itself, prove that the input was appropriate, that every geometric rule was implemented correctly, or that partial updates were rolled back. Those guarantees need to come from the action’s validation and error-handling logic. The network already refuses to sign off on a broken state; it still depends on the action’s own checks to know when the state is broken.

Source

Built against the ObjectARX 2027 SDK, based on the AssocFillet sample in samples/entity/AssocFillet. The listed dimensions were cross-checked against mechcodex.com’s hex bolt reference table and engineeringhardware.com’s M3 page. Full buildable project:

View on GitHub Gist