Is changin attributes of blocks in an array independently possible?

Is changin attributes of blocks in an array independently possible?

shricharana_bharadwaj
Enthusiast Enthusiast
339 Views
2 Replies
Message 1 of 3

Is changin attributes of blocks in an array independently possible?

shricharana_bharadwaj
Enthusiast
Enthusiast

Hi, 

 

I want to add count to an attribute in an block that is in an array. Is it possible?

 

I tried to use the AssocArray as BlockReference and then get the BlockTableRecord to iterate over the blocks, but does not work.  the BTR has no objectIds to iterate over.

I know the position can be changed, so I'm wondering if this is possible as well.

 

This is the way I have tried. 

Thanks in advance for any help.

string blockName = "T";
string tag = "TESTATT";
using (Transaction tr = Active.Document.TransactionManager.StartUndoGroupTransaction())
{
    try
    {
        BlockTable bt = (BlockTable)tr.GetObject(Active.Database.BlockTableId, OpenMode.ForRead);
        BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
        BlockReference br = BlockOperations.InsertBlock(tr, bt, modelSpace, blockName, Point3d.Origin);
        br.SetAttributeValue(tr, tag, "Original");
        BlockTableRecord btRec = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
        foreach (ObjectId id in btRec)
        {
            Active.Editor.WriteMessage(id.ToString());
        }
        AssocArrayRectangularParameters param = new AssocArrayRectangularParameters(100, 100, 0, 10, 10, 0, 0, 0);

        VertexRef verref = new VertexRef(br.Position);
        AssocArray array = AssocArray.CreateArray(new ObjectIdCollection { br.ObjectId }, verref, param);

        BlockReference arrayBlock = (BlockReference)tr.GetObject(array.EntityId, OpenMode.ForWrite);
        BlockTableRecord arrayBT = (BlockTableRecord)tr.GetObject(arrayBlock.BlockTableRecord, OpenMode.ForWrite);
        foreach (ObjectId id in arrayBT)
        {
            BlockReference item = (BlockReference)tr.GetObject(id, OpenMode.ForWrite);
            item.SetAttributeValue(tr, tag, "Array");
        }

        tr.Commit();
    }
    catch (System.Exception ex)
    {
        Active.WriteError(ex.Message);
        tr.Abort();
    }
    
}

 

0 Likes
Accepted solutions (1)
340 Views
2 Replies
Replies (2)
Message 2 of 3

ActivistInvestor
Mentor
Mentor
Accepted solution

No, it is not possible because there is not more than one occurrence of the Block with the attribute reference in the array.

 

When you create an associative array, you are not creating copies of the objects that you want to array, You are creating multiple insertions of an anonymous block containing a single copy of the objects you want to array. 

 

The situation is the same as you would have if you were to insert a block with attributes into another block, and then create multiple insertions of that other block. There is a single insertion of the block and attribute nested in the block who's insertions are copied, so you cannot change the attribute in each of those insertions/copies. 

Message 3 of 3

shricharana_bharadwaj
Enthusiast
Enthusiast

I see. I understand arrays much better now. That's why when editing the source it opens for edit similar to block refedit. 

 

Thank you for the clarification.

Much appreciated!

0 Likes