Using a palette from .NET to display properties of multiple AutoCAD objects

20 篇文章 0 订阅


After a brief interlude we're back on the series of posts showing how to implement basic user-interfaces inside AutoCAD using .NET. Here's the series so far:

In this post we're going to swap out the modeless form we've been using in the last few posts in the series and replace it with an instance of AutoCAD's in-built palette class (Autodesk.AutoCAD.Windows.PaletteSet).

Firstly, why bother? Well, the PaletteSet class is realy cool: it provides docking, auto-hide, transparency and fixes the annoying focus-related issues we see with normal modeless dialogs.

And the best is that you get all this basically for free - the implementation work needed is really minimal. I started by copying liberal amounts of code from the DockingPalette sample on the ObjectARX SDK, and then deleted what wasn't needed for this project (which turned out to be most of it).

Here's the updated Command implementation. This really has very minor changes, as the palette implementation is all hidden inside the new TypeViewerPalette class.

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using System;

using CustomDialogs;


namespace CustomDialogs

{

  public class Commands : IExtensionApplication

  {

    static TypeViewerPalette tvp;


    public void Initialize()

    {

      tvp = new TypeViewerPalette();


      DocumentCollection dm =

        Application.DocumentManager;

      dm.DocumentCreated +=

        new DocumentCollectionEventHandler(OnDocumentCreated);

      foreach (Document doc in dm)

      {

        doc.Editor.PointMonitor +=

          new PointMonitorEventHandler(OnMonitorPoint);

      }

    }


    public void Terminate()

    {

      try

      {

        DocumentCollection dm =

          Application.DocumentManager;

        if (dm != null)

        {

          Editor ed = dm.MdiActiveDocument.Editor;

          ed.PointMonitor -=

            new PointMonitorEventHandler(OnMonitorPoint);

        }

      }

      catch (System.Exception)

      {

        // The editor may no longer

        // be available on unload

      }

    }


    private void OnDocumentCreated(

      object sender,

      DocumentCollectionEventArgs e

    )

    {

      e.Document.Editor.PointMonitor +=

        new PointMonitorEventHandler(OnMonitorPoint);

    }


    private void OnMonitorPoint(

      object sender,

      PointMonitorEventArgs e

    )

    {

      FullSubentityPath[] paths =

        e.Context.GetPickedEntities();

      if (paths.Length <= 0)

      {

        tvp.SetObjectId(ObjectId.Null);

        return;

      };


      ObjectIdCollection idc = new ObjectIdCollection();

      foreach (FullSubentityPath path in paths)

      {

        // Just add the first ID in the list from each path

        ObjectId[] ids = path.GetObjectIds();

        idc.Add(ids[0]);

      }

      tvp.SetObjectIds(idc);

    }


    [CommandMethod("vt",CommandFlags.UsePickSet)]

    public void ViewType()

    {

      tvp.Show();

    }

  }

}

As for the TypeViewerPalette class: I started by migrating the SetObjectId[s]()/SetObjectText() protocol across from the old TypeViewerForm class - the most complicated part of which involved exposing the contents of our palette (which we define and load as a User Control) via a member variable that can be accessed from SetObjectText(). Other than that it was all just copy & paste.

Here's the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Interop;

using Autodesk.AutoCAD.Interop.Common;

using Autodesk.AutoCAD.Windows;

using TypeViewer;


namespace CustomDialogs

{

  public class TypeViewerPalette

  {

    // We cannot derive from PaletteSet

    // so we contain it

    static PaletteSet ps;


    // We need to make the textbox available

    // via a static member

    static TypeViewerControl tvc;


    public TypeViewerPalette()

    {

      tvc = new TypeViewerControl();

    }


    public void Show()

    {

      if (ps == null)

      {

        ps = new PaletteSet("Type Viewer");

        ps.Style =

          PaletteSetStyles.NameEditable |

          PaletteSetStyles.ShowPropertiesMenu |

          PaletteSetStyles.ShowAutoHideButton |

          PaletteSetStyles.ShowCloseButton;

        ps.MinimumSize =

          new System.Drawing.Size(300, 300);

        ps.Add("Type Viewer 1", tvc);

      }

      ps.Visible = true;

    }


    public void SetObjectText(string text)

    {

      tvc.typeTextBox.Text = text;

    }


    public void SetObjectIds(ObjectIdCollection ids)

    {

      if (ids.Count < 0)

      {

        SetObjectText("");

      }

      else

      {

        Document doc =

          Autodesk.AutoCAD.ApplicationServices.

            Application.DocumentManager.MdiActiveDocument;

        DocumentLock loc =

          doc.LockDocument();

        using (loc)

        {

          string info =

            "Number of objects: " +

            ids.Count.ToString() + "/r/n";

          Transaction tr =

            doc.TransactionManager.StartTransaction();

          using (tr)

          {

            foreach (ObjectId id in ids)

            {

              Entity ent =

                (Entity)tr.GetObject(id, OpenMode.ForRead);

              Solid3d sol = ent as Solid3d;

              if (sol != null)

              {

                Acad3DSolid oSol =

                  (Acad3DSolid)sol.AcadObject;


                // Put in a try-catch block, as it's possible

                // for solids to not support this property,

                // it seems (better safe than sorry)

                try

                {

                  string solidType = oSol.SolidType;

                  info +=

                    ent.GetType().ToString() +

                    " (" + solidType + ") : " +

                    ent.ColorIndex.ToString() + "/r/n";

                }

                catch (System.Exception)

                {

                  info +=

                    ent.GetType().ToString() +

                    " : " +

                    ent.ColorIndex.ToString() + "/r/n";

                }

              }

              else

              {

                info +=

                  ent.GetType().ToString() +

                  " : " +

                  ent.ColorIndex.ToString() + "/r/n";

              }

            }

            tr.Commit();

          }

          SetObjectText(info);

        }

      }

    }


    public void SetObjectId(ObjectId id)

    {

      if (id == ObjectId.Null)

      {

        SetObjectText("");

      }

      else

      {

        Document doc =

          Autodesk.AutoCAD.ApplicationServices.

            Application.DocumentManager.MdiActiveDocument;

        DocumentLock loc =

          doc.LockDocument();

        using (loc)

        {

          Transaction tr =

            doc.TransactionManager.StartTransaction();

          using (tr)

          {

            DBObject obj =

              tr.GetObject(id, OpenMode.ForRead);

            SetObjectText(obj.GetType().ToString());

            tr.Commit();

          }

        }

      }

    }

  }

}

Here's what you get when you run the VT command and manipulate the palette's docking/transparency before hovering over a set of drawing objects:

Palette_1

You can download the source for this project from here .

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值