Programmatically determine BlockReference.BlockName of AttributeCollection

Programmatically determine BlockReference.BlockName of AttributeCollection

Anonymous
Not applicable
1,805 Views
2 Replies
Message 1 of 3

Programmatically determine BlockReference.BlockName of AttributeCollection

Anonymous
Not applicable

Here is a quick background for what I am trying to do:

 

Essentially, I need to loop through and update a set of attributes which match the following set of requisites:

    1. The AttributeCollection must contain a provided Tag name

    2. The AttributeCollection must be a property on a BlockReference which matches a specific Block name

    3. The AttributeCollection must be a property on a BlockReference which is a DynamicBlock

 

I believe I have issues 1 and 3 solved, but 2 is throwing me off. While looping through the BlockReferences, the BlockReference which contains the AttributeCollection does not have a Name which matches what is displayed in the Enhanced Attribute Editor. For example, the .NET code will report that the BlockReference.Name is something similar to *U16, and BlockReference.BlockName is *Model_Space, when the Enhanced Attribute Editor states that "Block: My_Expected_Block_Name". What I really need to do is tie the BlockReference back to "My_Expected_Block_Name". 

 

Am I looking in the wrong spot for matching an AttributeCollection to a specific Block Name, or using the wrong properties? Any direction I can get would be helpful. I don't know my way around AutoCAD very well, as I am just stepping in to assist another user with some .NET programming.

 

If more information is necessary, let me know - I will post the code I am using to loop below.

 

 

Code:

 

// Assume that "db" is a Transaction which has been opened by a StartTransaction() call

// Assume that "AutoCADHelper.CurrentAcadDb" is DocumentManager.MdiActiveDocument.Database

var blockTable = (BlockTable)db.GetObject(AutoCADHelper.CurrentAcadDb.BlockTableId, OpenMode.ForRead);
foreach (ObjectId blockTableId in blockTable) {
    var blockTableRecord = db.GetObject(blockTableId, OpenMode.ForRead) as BlockTableRecord;
    if (blockTableRecord != null) {
        foreach (ObjectId blockTableRecordId in blockTableRecord) {
            var blockReference = db.GetObject(blockTableRecordId, OpenMode.ForWrite) as BlockReference;
            if (blockReference != null && blockReference.IsDynamicBlock) {
                Debug.WriteLine(" blockReference.Name: " + blockReference.Name); // I would assume one of these would be the expected name
                Debug.WriteLine(" blockReference.BlockName: " + blockReference.BlockName); // but I am clearly wrong. 🙂

            }

        }

    }

}

 

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

norman.yuan
Mentor
Mentor
Accepted solution

It looks like you need to understanding a bit more about dynamic block.

 

I believe what you are after is BlockReference.Name, not BlockReference.BlockName. BlockName property refers to blockreference's owner (a blocktablerecord, either ModelSpace, or one of the PaperSpace corresponding to a layout).

 

For regular blockreference, its Name property is the same name as the name of a BlockTableRecord (block definition), from which it is referenced to. However, if the block definition is a dynamic block, the BlockReference's name may not be the same as its block definition, depending on how the dynamic properties are set. It name is very likely be a anonymous block name, something like *Uxxx". You need to use Blockreference.DynamicBlockTableRecord to find its dynamic blcok definition and then its name.

 

To find effective name (as iin VBA) of a blockreference, you do:

 

//tran is an open Transaction

//modelSpaceBlockTableRecord is the BlockTableRecord for ModelSpace

foreach (ObjectId id in modelSpaceBlockTableRecord)

{

    string blockName;

    BlockReference bref=tran.GetObject(id, OpenMode.ForRead) as BlockReference;

    if (bref!=null)

    {

        if (bref.IsDynamicBlock)

        {

             BlockTableRecord br=(BlockTableRecord)tran.GetObject(bref.DynamicBlockTableRecord,OpenMode.ForRead);

             blockName=br.Name;

        }

        else

        {

            blockName=bref.Name;

        }

        Debug.WriteLine("Found block reference: " + blockName);

    }

}

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

Anonymous
Not applicable

I certainly do need more understanding. 🙂 So thank you! That's very helpful to me. 

0 Likes