找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2188|回复: 0

[分享] Using a palette from .NET to display properties of multiple AutoCAD objects

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-27 22:22:16 | 显示全部楼层 |阅读模式

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

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

×
Using a palette from .NET to display properties of multiple AutoCAD objects                                                        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.
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;
  5. using System;
  6. using CustomDialogs;
  7. namespace CustomDialogs
  8. {
  9.   public class Commands : IExtensionApplication
  10.   {
  11.     static TypeViewerPalette tvp;
  12.     public void Initialize()
  13.     {
  14.       tvp = new TypeViewerPalette();
  15.       DocumentCollection dm =
  16.         Application.DocumentManager;
  17.       dm.DocumentCreated +=
  18.         new DocumentCollectionEventHandler(OnDocumentCreated);
  19.       foreach (Document doc in dm)
  20.       {
  21.         doc.Editor.PointMonitor +=
  22.           new PointMonitorEventHandler(OnMonitorPoint);
  23.       }
  24.     }
  25.     public void Terminate()
  26.     {
  27.       try
  28.       {
  29.         DocumentCollection dm =
  30.           Application.DocumentManager;
  31.         if (dm != null)
  32.         {
  33.           Editor ed = dm.MdiActiveDocument.Editor;
  34.           ed.PointMonitor -=
  35.             new PointMonitorEventHandler(OnMonitorPoint);
  36.         }
  37.       }
  38.       catch (System.Exception)
  39.       {
  40.         // The editor may no longer
  41.         // be available on unload
  42.       }
  43.     }
  44.     private void OnDocumentCreated(
  45.       object sender,
  46.       DocumentCollectionEventArgs e
  47.     )
  48.     {
  49.       e.Document.Editor.PointMonitor +=
  50.         new PointMonitorEventHandler(OnMonitorPoint);
  51.     }
  52.     private void OnMonitorPoint(
  53.       object sender,
  54.       PointMonitorEventArgs e
  55.     )
  56.     {
  57.       FullSubentityPath[] paths =
  58.         e.Context.GetPickedEntities();
  59.       if (paths.Length <= 0)
  60.       {
  61.         tvp.SetObjectId(ObjectId.Null);
  62.         return;
  63.       };
  64.       ObjectIdCollection idc = new ObjectIdCollection();
  65.       foreach (FullSubentityPath path in paths)
  66.       {
  67.         // Just add the first ID in the list from each path
  68.         ObjectId[] ids = path.GetObjectIds();
  69.         idc.Add(ids[0]);
  70.       }
  71.       tvp.SetObjectIds(idc);
  72.     }
  73.     [CommandMethod("vt",CommandFlags.UsePickSet)]
  74.     public void ViewType()
  75.     {
  76.       tvp.Show();
  77.     }
  78.   }
  79. }

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.
  1. using Autodesk.AutoCAD.ApplicationServices;

  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Interop;
  5. using Autodesk.AutoCAD.Interop.Common;
  6. using Autodesk.AutoCAD.Windows;
  7. using TypeViewer;

  8. namespace CustomDialogs

  9. {
  10.   public class TypeViewerPalette
  11.   {
  12.     // We cannot derive from PaletteSet
  13.     // so we contain it
  14.     static PaletteSet ps;
  15.     // We need to make the textbox available
  16.     // via a static member
  17.     static TypeViewerControl tvc;
  18.     public TypeViewerPalette()
  19.     {
  20.       tvc = new TypeViewerControl();
  21.     }
  22.     public void Show()
  23.     {
  24.       if (ps == null)
  25.       {
  26.         ps = new PaletteSet("Type Viewer");
  27.         ps.Style =
  28.           PaletteSetStyles.NameEditable |
  29.           PaletteSetStyles.ShowPropertiesMenu |
  30.           PaletteSetStyles.ShowAutoHideButton |
  31.           PaletteSetStyles.ShowCloseButton;
  32.         ps.MinimumSize =
  33.           new System.Drawing.Size(300, 300);
  34.         ps.Add("Type Viewer 1", tvc);
  35.       }
  36.       ps.Visible = true;
  37.     }
  38.     public void SetObjectText(string text)
  39.     {
  40.       tvc.typeTextBox.Text = text;
  41.     }
  42.     public void SetObjectIds(ObjectIdCollection ids)
  43.     {
  44.       if (ids.Count < 0)
  45.       {
  46.         SetObjectText("");
  47.       }
  48.       else
  49.       {
  50.         Document doc =
  51.           Autodesk.AutoCAD.ApplicationServices.
  52.             Application.DocumentManager.MdiActiveDocument;
  53.         DocumentLock loc =
  54.           doc.LockDocument();
  55.         using (loc)
  56.         {
  57.           string info =
  58.             "Number of objects: " +
  59.             ids.Count.ToString() + "\r\n";
  60.           Transaction tr =
  61.             doc.TransactionManager.StartTransaction();
  62.           using (tr)
  63.           {
  64.             foreach (ObjectId id in ids)
  65.             {
  66.               Entity ent =
  67.                 (Entity)tr.GetObject(id, OpenMode.ForRead);
  68.               Solid3d sol = ent as Solid3d;
  69.               if (sol != null)
  70.               {
  71.                 Acad3DSolid oSol =
  72.                   (Acad3DSolid)sol.AcadObject;
  73.                 // Put in a try-catch block, as it's possible
  74.                 // for solids to not support this property,
  75.                 // it seems (better safe than sorry)
  76.                 try
  77.                 {
  78.                   string solidType = oSol.SolidType;
  79.                   info +=
  80.                     ent.GetType().ToString() +
  81.                     " (" + solidType + ") : " +
  82.                     ent.ColorIndex.ToString() + "\r\n";
  83.                 }
  84.                 catch (System.Exception)
  85.                 {
  86.                   info +=
  87.                     ent.GetType().ToString() +
  88.                     " : " +
  89.                     ent.ColorIndex.ToString() + "\r\n";
  90.                 }
  91.               }
  92.               else
  93.               {
  94.                 info +=
  95.                   ent.GetType().ToString() +
  96.                   " : " +
  97.                   ent.ColorIndex.ToString() + "\r\n";
  98.               }
  99.             }
  100.             tr.Commit();
  101.           }
  102.           SetObjectText(info);
  103.         }
  104.       }
  105.     }
  106.     public void SetObjectId(ObjectId id)
  107.     {
  108.       if (id == ObjectId.Null)
  109.       {
  110.         SetObjectText("");
  111.       }
  112.       else
  113.       {
  114.         Document doc =
  115.           Autodesk.AutoCAD.ApplicationServices.
  116.             Application.DocumentManager.MdiActiveDocument;
  117.         DocumentLock loc =
  118.           doc.LockDocument();
  119.         using (loc)
  120.         {
  121.           Transaction tr =
  122.             doc.TransactionManager.StartTransaction();
  123.           using (tr)
  124.           {
  125.             DBObject obj =
  126.               tr.GetObject(id, OpenMode.ForRead);
  127.             SetObjectText(obj.GetType().ToString());
  128.             tr.Commit();
  129.           }
  130.         }
  131.       }
  132.     }
  133.   }
  134. }

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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