Thanks to Wolfgang Ruthensteiner for suggesting this excellent topic a comment to this previous post. Here's Wonfgang's question:
How do I read back the field code with C# (from an attribute e.g.)?
I am linking room-label blocks with polylines, using fields inside an attribute to display the polyline's area property.
Later I want to find out programatically, which polyline a certain block is linked to by evaluating the field in the attribute (extracting the objectId).
This was actually quite tricky, and one I needed the help of our old friend, ArxDbg, to solve (see here for some information on this very useful ObjectARX sample). I should say up-front that there may well be a simpler way to access the information - the below technique is to some degree relying on the database structure (which might be considered an implementation detail). I may be missing a higher-level API providing a simpler way to access the information, but there you have it.
The full text of the field expression is stored in an AcDbField object (which is accesible through the Autodesk.AutoCAD.DatabaseServices.Field) which exists inside a field dictionary in the text object's (or attribute's) extension dictionary. So here's what needs to happen:
- Select the MText object (I chose to use MText in the below code, as it was a bit more work to allow attribute selection within a block - left as an exercise for the reader :-)
- Open the MText object's extension dictionary
- Open the nested field dictionary
- Access the field object stored therein
At this stage you have your text string with all the uninterpreted field codes. For those of you that are interested, I remember an important decision at the time we implemented fields in AutoCAD: that we should maintain the existing protocol and not return uninterpreted field codes from the standard text access properties/methods. This was largely to avoid migration issues for applications that depended on the data to be returned in its evaluated form. But it clearly means a bit more work if you want to get at the underlying codes.
So once we have our codes, we then want to get back to the "referred" object(s). I implemented a simple function that parses a string for the following sub-string:
... where XXX is a string representing the ObjectId. The code then uses a conversion function to get an integer from the string, and create an ObjectId from the integer. We return the ID to the calling function, where we can then open it and find out more about it.
So that's the description - here's the C# code implementing it:
public class Commands
static public void GetFieldLink()
Document doc =
Database db = doc.Database;
Editor ed = doc.Editor;
// Ask the user to select an attribute or an mtext
PromptEntityOptions opt =
"\nSelect an MText object containing field(s): "
"\nObject must be MText."
PromptEntityResult res =
if (res.Status == PromptStatus.OK)
Transaction tr =
// Check the entity is an MText object
DBObject obj =
MText mt = obj as MText;
if (mt != null)
"\nMText object does not contain fields."
// Open the extension dictionary
DBDictionary extDict =
const string fldDictName = "ACAD_FIELD";
const string fldEntryName = "TEXT";
// Get the field dictionary
ObjectId fldDictId =
if (fldDictId != ObjectId.Null)
DBDictionary fldDict =
// Get the field itself
ObjectId fldId =
if (fldId != ObjectId.Null)
Field fld = obj as Field;
if (fld != null)
// And finally get the string
// including the field codes
string fldCode = fld.GetFieldCode();
"\nField code: "
// Loop, using our helper function
// to find the object references
if (fldCode != "")
// Print the ObjectId
"\nFound Object ID: "
// ... and the type of the object
", which is an object of type "
} while (fldCode != "");
// Extract an ObjectId from a field string
// and return the remainder of the string
static public string FindObjectId(
out ObjectId objId
const string prefix = "%<\\_ObjId ";
const string suffix = ">%";
// Find the location of the prefix string
int preLoc = text.IndexOf(prefix);
if (preLoc > 0)
// Find the location of the ID itself
int idLoc = preLoc + prefix.Length;
// Get the remaining string
string remains = text.Substring(idLoc);
// Find the location of the suffix
int sufLoc = remains.IndexOf(suffix);
// Extract the ID string and get the ObjectId
string id = remains.Remove(sufLoc);
objId = new ObjectId(Convert.ToInt32(id));
// Return the remainder, to allow extraction
// of any remaining IDs
return remains.Substring(sufLoc + suffix.Length);
objId = ObjectId.Null;
Here's what happens when we run the code. Firstly I went and created a simple, closed polyline and a circle. I then created a single MText object with field codes accessing the other two objects' areas:
I then run the GFL command and select the MText object:
Select an MText object containing field(s):
Field code: Area of the circle: \AcObjProp Object(%<\_ObjId
2130239616>%).Area\P\PArea of the polyline: \AcObjProp Object(%<\_ObjId
Found Object ID: (2130239616), which is an object of type
Found Object ID: (2130239624), which is an object of type
As you can see, we've been able to find and extract information from the objects referred to by fields in an MText object.