马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
How to find all fields in a drawing? By Philippe Leefsma
The Named Object Dictionary (NOD) in a database contains an “ACAD_FIELDLIST” entry if your drawing contains any field. This entry holds a record of type “AcDbFieldList” which is unfortunately an object type that isn’t exposed to the ARX or .Net API.
Each AcDbField object is typically accessible from an entity Extension Dictionary, for example if I you create a MText that contains a field, you can access that specific MText and look for the AcDbField in the ExtensionDictionary.
In order to access all fields directly, one way to get the job done is to grab all TypedValue (DxfCode – Value pairs) of our AcDbFieldList, iterate through them and grab the one that have dxf code = 330 and are of type AcDbField.
We need to rely on some good old P/Invoked arx methods, hence that code is version dependent, here is an example for R19:
class ImportsR19 { public struct ads_name { public IntPtr a; public IntPtr b; }; [DllImport("acdb19.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AAY01JVAcDbObjectId@@@Z" public static extern int acdbGetAdsName32( ref ads_name name, ObjectId objId); [DllImport("acdb19.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AEAY01_JVAcDbObjectId@@@Z" public static extern int acdbGetAdsName64( ref ads_name name, ObjectId objId); public static int acdbGetAdsName( ref ads_name name, ObjectId objId) { if (Marshal.SizeOf(IntPtr.Zero) > 4) return acdbGetAdsName64(ref name, objId); return acdbGetAdsName32(ref name, objId); } [DllImport("accore.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "acdbEntGet" public static extern System.IntPtr acdbEntGet( ref ads_name ename); public static System.Collections.Generic.List<TypedValue> acdbEntGetTypedValues(ObjectId id) { System.Collections.Generic.List<TypedValue> result = new System.Collections.Generic.List<TypedValue>(); ads_name name = new ads_name(); int res = acdbGetAdsName(ref name, id); ResultBuffer rb = new ResultBuffer(); Autodesk.AutoCAD.Runtime.Interop.AttachUnmanagedObject( rb, acdbEntGet(ref name), true); ResultBufferEnumerator iter = rb.GetEnumerator(); while (iter.MoveNext()) { result.Add((TypedValue)iter.Current); } return result; } } [CommandMethod("FieldList" public void FieldList() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (var tx = db.TransactionManager.StartTransaction()) { var nod = tx.GetObject( db.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary; if (!nod.Contains("ACAD_FIELDLIST")) { ed.WriteMessage("\nDrawing has no field..."); return; } var id = nod.GetAt("ACAD_FIELDLIST"); List<TypedValue> dxf = ImportsR19.acdbEntGetTypedValues(id); foreach (var entry in dxf) { if (entry.TypeCode == 330) { ObjectId objId = (ObjectId)entry.Value; if (objId.ObjectClass.Name == "AcDbField") { Field field = tx.GetObject( objId, OpenMode.ForWrite) as Field; field.Evaluate(); ed.WriteMessage( "\n - Format: " + field.Format + " Value: " + field.GetStringValue()); } } } } }
|