找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1704|回复: 0

[分享] 用.NET阻止AutoCAD对象被选中

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-10-19 22:10:30 | 显示全部楼层 |阅读模式

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

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

×
原文:Preventing AutoCAD objects from being selected using .NET
上一篇文章中我们看到如何阻止实体在选择中被高亮显示。这一片文章我们将会看到如何阻止实体被选中。再次感谢 Balaji Ramamoorthyt 提供的代码
我们采用的基本方案和上一篇相似,我们维护一份我们想阻止被选择的对象的DXF名称列表。但是它可以很容易的被改进成用其他的条件来讲对象从选择集中删除
如果我们使用单个实体选择函数(GetEntity())而不是用GetSelection(),我们可能需要使用另外一种方式来过滤:我们可能需要用PrompEntityOptions.AddAllowedClass()来允许特定的类型。但是我们也可以使用这一技术用于实体选择。尤其是我们用其他的条件(例如不是基于实体类型)来阻止实体被选择


  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;
  5. using System.Collections.Generic;
  6. using System.Text;

  7. namespace PreventSelection
  8. {
  9.   public class Test : IExtensionApplication
  10.   {
  11.     void OnSelectionAdded(object sender, SelectionAddedEventArgs e)
  12.     {
  13.       ObjectId[] addedIds = e.AddedObjects.GetObjectIds();
  14.       for (int i=0; i < addedIds.Length; i++)
  15.       {
  16.         ObjectId oid = addedIds[i];

  17.         if (IsInList(oid.ObjectClass.DxfName))
  18.         {
  19.           e.Remove(i);
  20.         }
  21.       }
  22.     }

  23.     [CommandMethod("US")]
  24.     public static void Unselect()
  25.     {
  26.       Document doc = Application.DocumentManager.MdiActiveDocument;
  27.       Editor ed = doc.Editor;

  28.       // Print the list of currently unhighlighted classes

  29.       ed.WriteMessage(ListToPrint());

  30.       // Get the type to add to the list

  31.       PromptResult pr =
  32.         ed.GetString(
  33.           "\nEnter the type of object to stop from " +
  34.           "being selected: "
  35.         );
  36.       if (pr.Status != PromptStatus.OK)
  37.         return;

  38.       if (IsInList(pr.StringResult))
  39.       {
  40.         ed.WriteMessage("\nItem already in the list.");
  41.       }
  42.       else
  43.       {
  44.         AddToList(pr.StringResult);
  45.         ed.WriteMessage("\nItem added to the list.");
  46.       }
  47.     }

  48.     // Would call this command RS, but it's taken by RSCRIPT,
  49.     // so using the somewhat unwieldy UUS, instead

  50.     [CommandMethod("UUS")]
  51.     public static void Ununselect()
  52.     {
  53.       Document doc = Application.DocumentManager.MdiActiveDocument;
  54.       Editor ed = doc.Editor;

  55.       // Print the list of currently unhighlighted classes

  56.       ed.WriteMessage(ListToPrint());

  57.       // Get the type to remove from the list

  58.       PromptResult pr =
  59.         ed.GetString(
  60.           "\nEnter the type of object to remove from the " +
  61.           "list: "
  62.         );
  63.       if (pr.Status != PromptStatus.OK)
  64.         return;

  65.       if (!IsInList(pr.StringResult))
  66.       {
  67.         ed.WriteMessage("\nItem not currently in the list.");
  68.       }
  69.       else
  70.       {
  71.         RemoveFromList(pr.StringResult);
  72.         ed.WriteMessage("\nItem removed from the list.");
  73.       }
  74.     }

  75.     void IExtensionApplication.Initialize()
  76.     {
  77.       Document doc = Application.DocumentManager.MdiActiveDocument;
  78.       Editor ed = doc.Editor;

  79.       ed.SelectionAdded +=
  80.         new SelectionAddedEventHandler(OnSelectionAdded);
  81.     }

  82.     void IExtensionApplication.Terminate()
  83.     {
  84.       Document doc = Application.DocumentManager.MdiActiveDocument;
  85.       Editor ed = doc.Editor;

  86.       ed.SelectionAdded -=
  87.         new SelectionAddedEventHandler(OnSelectionAdded);
  88.     }

  89.     // The list of types to unhighlight

  90.     static List<string> _unhighlighted = new List<string>();

  91.     // Add a type to the list

  92.     public static void AddToList(string name)
  93.     {
  94.       string upper = name.ToUpper();
  95.       if (!_unhighlighted.Contains(upper))
  96.       {
  97.         _unhighlighted.Add(upper);
  98.       }
  99.     }

  100.     // Remove a type from the list

  101.     public static void RemoveFromList(string name)
  102.     {
  103.       string upper = name.ToUpper();
  104.       if (_unhighlighted.Contains(upper))
  105.       {
  106.         _unhighlighted.Remove(upper);
  107.       }
  108.     }

  109.     // Check whether the list contains a type

  110.     public static bool IsInList(string name)
  111.     {
  112.       return _unhighlighted.Contains(name.ToUpper());
  113.     }

  114.     // Get a string printing the contents of the list

  115.     public static string ListToPrint()
  116.     {
  117.       string toPrint;

  118.       if (_unhighlighted.Count == 0)
  119.       {
  120.         toPrint =
  121.           "\nThere are currently no objects in the list " +
  122.           "to stop from being selected.";
  123.       }
  124.       else
  125.       {
  126.         StringBuilder sb =
  127.           new StringBuilder(
  128.             "\nObjects of these types will not be selected:"
  129.           );
  130.         foreach (string name in _unhighlighted)
  131.         {
  132.           sb.Append(" " + name);
  133.         }
  134.         toPrint = sb.ToString();
  135.       }

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

本版积分规则

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

GMT+8, 2025-9-6 03:19 , Processed in 0.212269 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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