用.NET阻止AutoCAD对象被选中

原文:Preventing AutoCAD objects from being selected using .NET

上一篇文章中我们看到如何阻止实体在选择中被高亮显示。这一片文章我们将会看到如何阻止实体被选中。再次感谢 Balaji Ramamoorthyt 提供的代码

我们采用的基本方案和上一篇相似,我们维护一份我们想阻止被选择的对象的DXF名称列表。但是它可以很容易的被改进成用其他的条件来讲对象从选择集中删除

如果我们使用单个实体选择函数(GetEntity())而不是用GetSelection(),我们可能需要使用另外一种方式来过滤:我们可能需要用PrompEntityOptions.AddAllowedClass()来允许特定的类型。但是我们也可以使用这一技术用于实体选择。尤其是我们用其他的条件(例如不是基于实体类型)来阻止实体被选择

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using System.Text;
 
namespace PreventSelection
{
  public class Test : IExtensionApplication
  {
    void OnSelectionAdded(object sender, SelectionAddedEventArgs e)
    {
      ObjectId[] addedIds = e.AddedObjects.GetObjectIds();
      for (int i=0; i < addedIds.Length; i++)
      {
        ObjectId oid = addedIds[i];
 
        if (IsInList(oid.ObjectClass.DxfName))
        {
          e.Remove(i);
        }
      }
    }
 
    [CommandMethod("US")]
    public static void Unselect()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;
 
      // Print the list of currently unhighlighted classes
 
      ed.WriteMessage(ListToPrint());
 
      // Get the type to add to the list
 
      PromptResult pr =
        ed.GetString(
          "\nEnter the type of object to stop from " +
          "being selected: "
        );
      if (pr.Status != PromptStatus.OK)
        return;
 
      if (IsInList(pr.StringResult))
      {
        ed.WriteMessage("\nItem already in the list.");
      }
      else
      {
        AddToList(pr.StringResult);
        ed.WriteMessage("\nItem added to the list.");
      }
    }
 
    // Would call this command RS, but it's taken by RSCRIPT,
    // so using the somewhat unwieldy UUS, instead
 
    [CommandMethod("UUS")]
    public static void Ununselect()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;
 
      // Print the list of currently unhighlighted classes
 
      ed.WriteMessage(ListToPrint());
 
      // Get the type to remove from the list
 
      PromptResult pr =
        ed.GetString(
          "\nEnter the type of object to remove from the " +
          "list: "
        );
      if (pr.Status != PromptStatus.OK)
        return;
 
      if (!IsInList(pr.StringResult))
      {
        ed.WriteMessage("\nItem not currently in the list.");
      }
      else
      {
        RemoveFromList(pr.StringResult);
        ed.WriteMessage("\nItem removed from the list.");
      }
    }
 
    void IExtensionApplication.Initialize()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;
 
      ed.SelectionAdded +=
        new SelectionAddedEventHandler(OnSelectionAdded);
    }
 
    void IExtensionApplication.Terminate()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;
 
      ed.SelectionAdded -=
        new SelectionAddedEventHandler(OnSelectionAdded);
    }
 
    // The list of types to unhighlight
 
    static List<string> _unhighlighted = new List<string>();
 
    // Add a type to the list
 
    public static void AddToList(string name)
    {
      string upper = name.ToUpper();
      if (!_unhighlighted.Contains(upper))
      {
        _unhighlighted.Add(upper);
      }
    }
 
    // Remove a type from the list
 
    public static void RemoveFromList(string name)
    {
      string upper = name.ToUpper();
      if (_unhighlighted.Contains(upper))
      {
        _unhighlighted.Remove(upper);
      }
    }
 
    // Check whether the list contains a type
 
    public static bool IsInList(string name)
    {
      return _unhighlighted.Contains(name.ToUpper());
    }
 
    // Get a string printing the contents of the list
 
    public static string ListToPrint()
    {
      string toPrint;
 
      if (_unhighlighted.Count == 0)
      {
        toPrint =
          "\nThere are currently no objects in the list " +
          "to stop from being selected.";
      }
      else
      {
        StringBuilder sb =
          new StringBuilder(
            "\nObjects of these types will not be selected:"
          );
        foreach (string name in _unhighlighted)
        {
          sb.Append(" " + name);
        }
        toPrint = sb.ToString();
      }
 
      return toPrint;
    }
  }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值