找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1125|回复: 0

[分享] How to find all fields in a drawing?

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-10-15 15:32:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
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());                        
                }
            }
        }
    }
}


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-18 23:51 , Processed in 0.394672 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表