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 .

 
以下是一些常见的AutoCAD软件API模板: 1. 命令模板 ```C++ void myCommand() { // 在这里编写自己的代码 acutPrintf(_T("Hello World!\n")); } void initApp() { acedRegCmds->addCommand(_T("MYGROUP"), _T("MYCOMMAND"), _T("MYCOMMAND"), ACRX_CMD_MODAL, myCommand); } void unloadApp() { acedRegCmds->removeGroup(_T("MYGROUP")); } extern "C" AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* appId) { switch (msg) { case AcRx::kInitAppMsg: acrxDynamicLinker->unlockApplication(appId); acrxRegisterAppMDIAware(appId); initApp(); break; case AcRx::kUnloadAppMsg: unloadApp(); break; default: break; } return AcRx::kRetOK; } ``` 2. 实体模板 ```C++ class MyLine : public AcDbLine { public: ACRX_DECLARE_MEMBERS(MyLine); MyLine(); MyLine(const AcGePoint3d& start, const AcGePoint3d& end); virtual Acad::ErrorStatus dwgInFields(AcDbDwgFiler* filer) override; virtual Acad::ErrorStatus dwgOutFields(AcDbDwgFiler* filer) const override; virtual Acad::ErrorStatus dxfInFields(AcDbDxfFiler* filer) override; virtual Acad::ErrorStatus dxfOutFields(AcDbDxfFiler* filer) const override; virtual void subList() const override; }; ACRX_DECLARE_MEMBERS(MyLine); MyLine::MyLine() : AcDbLine() {} MyLine::MyLine(const AcGePoint3d& start, const AcGePoint3d& end) : AcDbLine(start, end) {} Acad::ErrorStatus MyLine::dwgInFields(AcDbDwgFiler* filer) { assertWriteEnabled(); Acad::ErrorStatus es; if ((es = AcDbLine::dwgInFields(filer)) != Acad::eOk) { return es; } return filer->filerStatus(); } Acad::ErrorStatus MyLine::dwgOutFields(AcDbDwgFiler* filer) const { assertReadEnabled(); Acad::ErrorStatus es; if ((es = AcDbLine::dwgOutFields(filer)) != Acad::eOk) { return es; } return filer->filerStatus(); } Acad::ErrorStatus MyLine::dxfInFields(AcDbDxfFiler* filer) { assertWriteEnabled(); Acad::ErrorStatus es; if ((es = AcDbLine::dxfInFields(filer)) != Acad::eOk) { return es; } return filer->filerStatus(); } Acad::ErrorStatus MyLine::dxfOutFields(AcDbDxfFiler* filer) const { assertReadEnabled(); Acad::ErrorStatus es; if ((es = AcDbLine::dxfOutFields(filer)) != Acad::eOk) { return es; } return filer->filerStatus(); } void MyLine::subList() const { AcDbLine::subList(); } void initApp() { acrxClassDictionary->add(AcDbLine::desc(), MyLine::desc()); } void unloadApp() { acrxClassDictionary->remove(AcDbLine::desc()); } extern "C" AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* appId) { switch (msg) { case AcRx::kInitAppMsg: acrxDynamicLinker->unlockApplication(appId); acrxRegisterAppMDIAware(appId); initApp(); break; case AcRx::kUnloadAppMsg: unloadApp(); break; default: break; } return AcRx::kRetOK; } ``` 这个模板定义了一个自定义的实体类`MyLine`,继承自AutoCAD的`AcDbLine`类,并重载了一些函数,用于存储和读取自定义数据。在初始化函数中,将自定义实体类添加到AutoCAD的类字典中,以便在AutoCAD中创建和编辑这个实体。开发人员可以根据具体需求修改和扩展这个模板。 3. 界面模板 ```C++ class MyPalette : public AcPane { public: MyPalette(AcRx::AppInfo* appInfo); virtual ~MyPalette(); virtual void OnCreate(); virtual void OnDestroy(); private: AcDbDatabase* m_pDb; CWnd* m_pWnd; }; MyPalette::MyPalette(AcRx::AppInfo* appInfo) : AcPane(appInfo), m_pDb(nullptr), m_pWnd(nullptr) {} MyPalette::~MyPalette() { if (m_pDb) { delete m_pDb; } } void MyPalette::OnCreate() { // 创建数据库 m_pDb = new AcDbDatabase; m_pDb->readDwgFile(_T("Template.dwg")); // 创建窗口 m_pWnd = new CWnd; m_pWnd->Create(nullptr, _T("My Palette"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 200, 300), this, 0); // 添加控件 m_pWnd->CreateButton(_T("Button 1"), CRect(10, 10, 90, 30), 1); m_pWnd->CreateButton(_T("Button 2"), CRect(10, 50, 90, 70), 2); } void MyPalette::OnDestroy() { if (m_pWnd) { m_pWnd->DestroyWindow(); delete m_pWnd; m_pWnd = nullptr; } if (m_pDb) { m_pDb->closeInput(true); delete m_pDb; m_pDb = nullptr; } } void initApp() { // 创建面板 AcRx::AppInfo* appInfo = acrxGetAppInfo(); MyPalette* pPalette = new MyPalette(appInfo); acedAddObjectToPane(pPalette, AcPane::kFloating); } void unloadApp() { } extern "C" AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* appId) { switch (msg) { case AcRx::kInitAppMsg: acrxDynamicLinker->unlockApplication(appId); acrxRegisterAppMDIAware(appId); initApp(); break; case AcRx::kUnloadAppMsg: unloadApp(); break; default: break; } return AcRx::kRetOK; } ``` 这个模板定义了一个自定义的界面类`MyPalette`,继承自AutoCAD的`AcPane`类,并重载了一些函数,用于创建和销毁自定义面板。在初始化函数中,创建自定义面板,并添加到AutoCAD的界面中,以便在AutoCAD中打开和关闭这个面板。开发人员可以根据具体需求修改和扩展这个模板。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值