- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.