Issue with Reading Object Data of a BlockReference

Issue with Reading Object Data of a BlockReference

mohammed_chbabi
Participant Participant
695 Views
2 Replies
Message 1 of 3

Issue with Reading Object Data of a BlockReference

mohammed_chbabi
Participant
Participant

I'm encountering an issue while attempting to read ObjectData (OD) from entities in my DWG file that are of type BlockReference.

The code runs fine for other entities like lines or polylines, but when I use the same approach for a BlockReference, I receive the following error on line 32:

 

 

 

   at Autodesk.Gis.Map.ObjectData.Records.get_Record()
   at Autodesk.Gis.Map.ObjectData.Records.RecordEnumerator.get_Current()

 

 

 

 

I'm unsure why this is happening, as I haven’t seen any documentation suggesting that reading ObjectData should be different for BlockReference compared to lines or polylines. Has anyone experienced this issue, or does anyone know if there’s something specific I need to do for BlockReference objects when retrieving ObjectData?

Here’s the code snippet I'm using:

 

 

 

 

public void ExtractObjectDataFromBlockReference()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        try
        {
            // Prompt the user to select a BlockReference
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a Block Reference: ");
            peo.SetRejectMessage("\nMust be a BlockReference.");
            peo.AddAllowedClass(typeof(BlockReference), false);
            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status == PromptStatus.OK)
            {
                // Get the BlockReference object
                BlockReference blockRef = tr.GetObject(per.ObjectId, OpenMode.ForRead) as BlockReference;

                if (blockRef != null)
                {
                    // Access the Object Data Manager
                    Tables odTables = HostMapApplicationServices.Application.ActiveProject.ODTables;

                    // Get the Object Data records associated with this block
                    using (Records odRecords = odTables.GetObjectRecords(0, blockRef.ObjectId, Autodesk.Gis.Map.Constants.OpenMode.OpenForRead, false))
                    {
                        if (odRecords != null && odRecords.Count > 0)
                        {
                            foreach (Record odRecord in odRecords)
                            {
                                Autodesk.Gis.Map.ObjectData.Table odTable = odTables[odRecord.TableName];
                                ed.WriteMessage($"\nTable Name: {odTable.Name}");

                                // Iterate through the fields and values in the Object Data record
                                for (int i = 0; i < odTable.FieldDefinitions.Count; i++)
                                {
                                    FieldDefinition field = odTable.FieldDefinitions[i];
                                    string fieldName = field.Name;
                                    var fieldValue = odRecord[i].StrValue;
                                    ed.WriteMessage($"\nField: {fieldName}, Value: {fieldValue}");
                                }
                            }
                        }
                    }
                }
            }

            tr.Commit();
        }
        catch (System.Exception ex)
        {
            ed.WriteMessage($"\nError: {ex.Message}");
        }
    }
}

 

 

Any insights or suggestions would be greatly appreciated. Thanks in advance to anyone who takes the time to help!

 

EDIT:

Environment: AutoCAD 2024 C3D
.NET 4.7.2

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

norman.yuan
Mentor
Mentor
Accepted solution

Well, the code worked on my side as expected (also C3D2024). 

 

Have you tried the code on different block references with the ObjectData from the same ODTable or different ODTable attached? or tested the code with different drawing; or event recreated the ODTable, or created a new ODTable and tested? Just in case you have corrupted in the drawing for some reasons.

 

BTW, there is no need to start a Transaction and open the entity before read the ObjectData from an entity. AutoCAD Map API manages the transaction internally in the Table[s].GetObjectRecords() method call. That is, in your code, start a transaction and open the entity/BlockReference only be useful if you need the entity to be opened beyond the need of reading ObjectData records.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

mohammed_chbabi
Participant
Participant

Thanks for your reply Norman, at the moment I used your tip to add custom Object Data to a Block Reference and indeed my code works as it should. Because the file is just delivered to us by another team I have asked to look into this.

0 Likes