找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2148|回复: 0

[分享] Accessing extension dictionary of an overruled entity while dragging

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-13 02:22:51 | 显示全部楼层 |阅读模式

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

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

×
Accessing extension dictionary of an overruled entity while dragging



By Balaji Ramamoorthy

When an entity that is being overruled is dragged, AutoCAD only creates a shallow copy of it. For this reason, the extension dictionary of the cloned entity is inaccessible from within the overrule class. One way to ensure that we get access to the extension dictionary is to drag the original entity and not its clone using the "TransformOverrule::CloneMeForDragging" and return false. Using this technique is not suggested as it is only meant for use with complex entities that can cause performance issues when cloned for dragging purposes.

The workaround is to override the "TransformOverrule::CloneMeForDragging" and continue to return "true" and use the original entity which is a parameter to this method to access the extension dictionary.

Here is a sample code to demonstrate this.



using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.GraphicsInterface;



public class HelperClass

{

    const String _extDictName = "myExtDict";

    const String _XRecName = "myXRec";

    List<TypedValue> _data = new List<TypedValue>();



    public List<TypedValue> Data

    {

        get { return _data; }

    }



    private static HelperClass _instance;

    public String DictionaryName

    {

        get { return _extDictName; }

    }



    public String XRecordName

    {

        get { return _XRecName; }

    }



    protected HelperClass()

    {

    }



    public static HelperClass GetSingleton

    {

        get

        {

            if (_instance == null)

            {

                _instance = new HelperClass();

            }

            return _instance;

        }

    }



    public void CaptureXRecordData(DBObject obj)

    {

        Xrecord xRec = null;

        ObjectId id = obj.ExtensionDictionary;



        _data.Clear();

        if (! id.IsValid)

            return;



        Database db

        = Application.DocumentManager.MdiActiveDocument.Database;

        using (Transaction tr

            = db.TransactionManager.StartOpenCloseTransaction())

        {

            DBDictionary extDict = tr.GetObject(

                                    id,

                                    OpenMode.ForRead,

                                    false) as DBDictionary;

            if (extDict.Contains(DictionaryName))

            {

                ObjectId dictId = extDict.GetAt(DictionaryName);

                DBDictionary myDict = tr.GetObject(

                                    dictId,

                                    OpenMode.ForRead,

                                    false) as DBDictionary;

                xRec = tr.GetObject(

                                    myDict.GetAt(XRecordName),

                                    OpenMode.ForRead,

                                    false) as Xrecord;



                if (xRec != null)

                {

                    ResultBuffer rb = xRec.Data;

                    if (rb != null)

                    {

                        TypedValue[] tvArray = rb.AsArray();

                        foreach (TypedValue tv in tvArray)

                        {

                            _data.Add(

                                    new TypedValue(

                                                   tv.TypeCode,

                                                   tv.Value

                                                  )

                                     );

                        }

                        rb.Dispose();

                    }

                }

            }

            tr.Commit();

        }

    }

}



public class MyCommands

{

    static CMyDrawableOverrule lineOverrule = null;

    static CMyTransformeOverrule transformOverrule = null;



    [CommandMethod("OT")]

    public void OverruleTest()

    {

        Editor ed

        = Application.DocumentManager.MdiActiveDocument.Editor;

        PromptEntityResult per;

        per = ed.GetEntity("Pick a line to overrule");

        if(per.Status != PromptStatus.OK)

            return;



        Database db = HostApplicationServices.WorkingDatabase;

        using (Transaction tr

                    = db.TransactionManager.StartTransaction())

        {

            Entity ent = (Entity)tr.GetObject(

                            per.ObjectId, OpenMode.ForWrite);



            RegAppTable regTable = (RegAppTable)tr.GetObject(

                    db.RegAppTableId, OpenMode.ForRead, false);



            if (!regTable.Has("TEST"))

            {

                regTable.UpgradeOpen();

                RegAppTableRecord app = new RegAppTableRecord();

                app.Name = "TEST";

                regTable.Add(app);

                tr.AddNewlyCreatedDBObject(app, true);

            }

            ent.XData = new ResultBuffer(

                                new TypedValue(1001, "TEST"),

                                new TypedValue(1070, 100));



            string extDictName = "myExtDict";

            string xRecName = "myXRec";



            if (ent.ExtensionDictionary == ObjectId.Null)

            {

                ent.CreateExtensionDictionary();

            }



            DBDictionary xDict = tr.GetObject(

                            ent.ExtensionDictionary,

                            OpenMode.ForWrite) as DBDictionary;



            xDict.TreatElementsAsHard = true;



            if (!xDict.Contains(extDictName))

            {

                DBDictionary newDict = new DBDictionary();

                newDict.TreatElementsAsHard = true;



                xDict.SetAt(extDictName, newDict);

                tr.AddNewlyCreatedDBObject(newDict, true);



                Xrecord xRec = new Xrecord();



                xRec.Data = new ResultBuffer(

                            new TypedValue((int)DxfCode.Text,

                                            "Autodesk"));



                newDict.SetAt(xRecName, xRec);

                tr.AddNewlyCreatedDBObject(xRec, true);

            }

            tr.Commit();

        }



        if (lineOverrule == null)

        {

            transformOverrule = new CMyTransformeOverrule();

            Overrule.AddOverrule(

                RXObject.GetClass(typeof(Line)),

                transformOverrule,

                true);



            transformOverrule.SetXDataFilter("TEST");



            lineOverrule = new CMyDrawableOverrule();

            Overrule.AddOverrule(

                RXObject.GetClass(typeof(Line)),

                lineOverrule,

                true);



            lineOverrule.SetXDataFilter("TEST");



            Overrule.Overruling = true;

        }



        Application.DocumentManager.MdiActiveDocument.Editor.Regen();

    }

}



public class CMyDrawableOverrule : DrawableOverrule

{

    [DllImport("acdb19.dll",

                CharSet = CharSet.Unicode,

                CallingConvention = CallingConvention.Cdecl,

                EntryPoint = "?fromAcDbTextStyle@@YA?AW4ErrorStatus@Acad@@AEAVAcGiTextStyle@@AEBVAcDbObjectId@@@Z")]



    private static extern Autodesk.AutoCAD.Runtime.ErrorStatus

        fromAcDbTextStyle(System.IntPtr style, ref ObjectId id);



    public override bool WorldDraw(Drawable drawable, WorldDraw wd)

    {

        base.WorldDraw(drawable, wd);



        Line line = drawable as Line;

        if (line == null)

            return true;



        using (LineSegment3d seg = new LineSegment3d(

                    line.StartPoint, line.EndPoint))

        {

            if (wd.IsDragging)

            {

                wd.SubEntityTraits.Color = 1;



                // Retrieve the extension dictionary

                // values retrieved using helper class

                List<TypedValue> tvList

                       = HelperClass.GetSingleton.Data;



                foreach (TypedValue tv in tvList)

                {

                    ObjectId textStyleId =

                    Application.DocumentManager.MdiActiveDocument.Database.Textstyle;



                    Autodesk.AutoCAD.GraphicsInterface.TextStyle iStyle

                    = new Autodesk.AutoCAD.GraphicsInterface.TextStyle();



                    if (fromAcDbTextStyle

                        (

                            iStyle.UnmanagedObject,

                            ref textStyleId

                        ) == Autodesk.AutoCAD.Runtime.ErrorStatus.OK)

                    {

                        wd.Geometry.Text(

                                        seg.MidPoint,

                                        line.Normal,

                                        Vector3d.XAxis,

                                        tv.Value.ToString(),

                                        true, iStyle);

                    }

                }

            }

            else

                wd.SubEntityTraits.Color = 2;



            wd.Geometry.Circle(seg.MidPoint,

                               seg.Length * 0.25,

                               line.Normal);

        }

        return true;

    }

}



public class CMyTransformeOverrule : TransformOverrule

{

    public override bool CloneMeForDragging(Entity entity)

    {

        // Retrieve the extension dictionary from the original

        // entity. While dragging, the clone created is a

        //shallow clone and the extension dictionary isnt accessible

        HelperClass.GetSingleton.CaptureXRecordData(entity);



        // Permit cloning while dragging

        return true;

    }

}


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

本版积分规则

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

GMT+8, 2024-5-23 01:35 , Processed in 0.366691 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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