Determining xref hierarchy using ObjectARX

By Balaji Ramamoorthy

To iterate through the drawing and pull out all the attached xrefs and to determine the level of hierarchy of each xref, here is a sample code. For example, an xref named xref1 in a drawing which in turn has xref2 and xref3 attached within it, you can find out if xref1 is the parent, and xref2 and xref3 are the children.

A class, AcDbXrefGraph, facilitates the traversal of a drawing’s xref hierarchy. The graph’s nodes allow access to information corresponding to an xref’s block table record. One stipulation, though, is that the graph maps singular parent-child linkages, even for xrefs that are multiply referenced in the current drawing. Therefore, only one parent xref will be recognized and graphed for each child xref.

The following code steps through the branches of an xref graph and prints the xref node names:

// - AdskXrefGraph._XRG command 
// Command to print Xref graph as a table
static void AdskXrefGraph_XRG(void)
{
    Acad::ErrorStatus es = Acad::eOk;
    AcDbXrefGraph graph;
    AcDbXrefGraphNode *node;
 
    TCHAR status[32];
    TCHAR nested[12];
 
    es = acedGetCurDwgXrefGraph(graph);
 
    acutPrintf(_T("Number of nodes: %dnn"), graph.numNodes());
    acutPrintf
    (
        _T("%3s%10s%15s%10s%12sn"),
        _T("#"), 
        _T("Name"), 
        _T("Status"), 
        _T("BTR ID"), 
        _T("Nested")
    );
 
    for (int i=0; ixrefStatus())
            {
            case AcDb::kXrfResolved :
                _stprintf(status, _T("Resolved"));
                break;
            case AcDb::kXrfUnloaded :
                _stprintf(status, _T("Unloaded"));
                break;
            case AcDb::kXrfUnreferenced :
                _stprintf(status, _T("Unreferenced"));
                break;
            case AcDb::kXrfFileNotFound :
                _stprintf(status, _T("File not found"));
                break;
            case AcDb::kXrfUnresolved :
                _stprintf(status, _T("Unresolved"));
                break;
            default :
                _stprintf
                (
                    status, 
                    _T("Unknown(%d)"), 
                    node->xrefStatus()
                );
            }
 
            if (node->btrId() == 0)
                _stprintf(nested, _T("(root dwg)"));
            else
                _stprintf(    
                            nested, 
                            node->isNested() ? _T("Yes"):_T("No"));
 
            acutPrintf(_T("%3d%10s%15s%10d%12sn"), i, node->name(), status, node->btrId(), nested);
        }
    }
}
 
// - AdskXrefGraph._PXRG command (do not rename)
// Command that prints the Xref graph as a tree
static void AdskXrefGraph_PXRG(void)
{
    AcDbXrefGraph graph;
 
    acedGetCurDwgXrefGraph(graph);
 
    if (graph.numNodes() name());
    if (node->numOut() > 0)
    {
        AcDbXrefGraphNode *nextNode;
        for (int i=0; inumOut(); i++)
        {
            nextNode = (AcDbXrefGraphNode *) node->out(i);
            if (nextNode != NULL)
                printNode(nextNode, numNodes, depth+1);
        }
    }
}

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading