How to retrieve and remove associative array

How to retrieve and remove associative array

bwallaceZR7XZ
Participant Participant
380 Views
3 Replies
Message 1 of 4

How to retrieve and remove associative array

bwallaceZR7XZ
Participant
Participant

Hello,

Apologies if this has an easy solution..

I am programmatically creating multiple polar arrays in AutoCAD using AssocArray.CreateArray. Each array is based on a Circle entity and follows standard AssocArrayPolarParameters.

After saving and reopening the drawing, I need the ability to remove specific polar arrays programmatically.

1) How do i retrieve the AssocArray? What identifier would I need to store to retrieve it, there is no handle?
2) Once i have retrieved my AssocArray, how do I remove it and the circles from the drawing? I see there is a DeleteItem method, I could just loop round the number of circles and remove each one?

Many thanks,
Bethan

0 Likes
381 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant

Hi,

The AssocArray instance returned by the AssocArray.CreateArray static method has an EntityId property which is the ObjectId of the BlockReference representing the array. You can store the Handle of this block reference (or its ObjectId as Soft/Hard Pointer in an Xrecord).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4

bwallaceZR7XZ
Participant
Participant

Thanks for the reply.

I was having some difficulties removing the associated array, after deleting the block reference I could still see the polar array on the drawing.
What worked for me was erasing the assoc actions, I believe this is severing the links in between the items in the array, then removing the block.

ObjectIdCollection dependentActions = new ObjectIdCollection();
AssocAction.GetActionsDependentOnObject(obj, true, true, dependentActions);

foreach (ObjectId actionId in dependentActions)
{
    AssocAction assocAction = tr.GetObject(actionId, OpenMode.ForWrite) as AssocAction;
    if (assocAction != null)
    {
        // Step 2: Invalidate the associative action before deleting
        assocAction.Status = AssocStatus.ErasedAssocStatus;
    }
}

 

0 Likes
Message 4 of 4

_gile
Consultant
Consultant

What if you simply erase the block reference (and possibly purge its definition)?

Here's an example

private static void EraseAndPurgeAssocArray(AssocArray assocArray)
{
    using(var tr = new OpenCloseTransaction())
    {
        var arrayBr = (BlockReference)tr.GetObject(assocArray.EntityId, OpenMode.ForWrite);
        var arrayBtr = (BlockTableRecord)tr.GetObject(arrayBr.BlockTableRecord, OpenMode.ForRead);
        // Erase the blockRefernce
        arrayBr.Erase();

        // Purge the block definition if there's no other reference
        if (arrayBtr.GetBlockReferenceIds(true, false).Count == 0)
        {
            var entitiesBr = (BlockReference)tr.GetObject(arrayBtr.Cast<ObjectId>().First(), OpenMode.ForWrite);
            var entitiesBtr = (BlockTableRecord)tr.GetObject(entitiesBr.BlockTableRecord, OpenMode.ForWrite);
            entitiesBtr.Erase();
            arrayBtr.UpgradeOpen();
            arrayBtr.Erase();
        }
        tr.Commit();
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes