找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 652|回复: 2

[分享] 使用.NET获取一个图层上的所有实体

[复制链接]

已领礼包: 1个

财富等级: 恭喜发财

发表于 2017-5-21 08:25:02 | 显示全部楼层 |阅读模式

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

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

×
[C#] 纯文本查看 复制代码
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

namespace EntitySelection
{
  public class Commands
  {
    [CommandMethod("EOL")]
    static public void EntitiesOnLayer()
    {
      Document doc =
        Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;

      PromptResult pr =
        ed.GetString("\nEnter name of layer: ");
      if (pr.Status == PromptStatus.OK)
      {
        ObjectIdCollection ents =
          GetEntitiesOnLayer(pr.StringResult);
        ed.WriteMessage(
          "\nFound {0} entit{1} on layer {2}",
          ents.Count,
          (ents.Count == 1 ? "y" : "ies"),
          pr.StringResult
        );
      }
    }

    private static ObjectIdCollection
      GetEntitiesOnLayer(string layerName)
    {
      Document doc =
        Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;

      // Build a filter list so that only entities
      // on the specified layer are selected

      TypedValue[] tvs =
        new TypedValue[1] {
            new TypedValue(
              (int)DxfCode.LayerName,
              layerName
            )
          };
      SelectionFilter sf =
        new SelectionFilter(tvs);
      PromptSelectionResult psr =
        ed.SelectAll(sf);

      if (psr.Status == PromptStatus.OK)
        return
          new ObjectIdCollection(
            psr.Value.GetObjectIds()
          );
      else
        return new ObjectIdCollection();
    }
  }
}


另外一个

[C#] 纯文本查看 复制代码
// 访问AutoCAD程序对象。
using Autodesk.AutoCAD.ApplicationServices;

// 访问 the AutoCAD 编辑器。
using Autodesk.AutoCAD.EditorInput;

// 命令注册。
using Autodesk.AutoCAD.Runtime;

// 访问数据库对象。
using Autodesk.AutoCAD.DatabaseServices;

namespace EntitySelection
{
    public class Commands
    {
        [CommandMethod("SelectOnlayer")]
        static public void SelectEnt()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptResult pr =
             ed.GetString("\n请输入图层名称: ");
            if (pr.Status == PromptStatus.OK)
            {
                TypedValue[] filList = new TypedValue[1];
                filList[0] = new TypedValue((int)DxfCode.LayerName, pr.StringResult);
                SelectionFilter filter = new SelectionFilter(filList);
                PromptSelectionResult ssRes = ed.SelectAll(filter);
                if (ssRes.Status == PromptStatus.OK)
                {
                    SelectionSet SS = ssRes.Value;
                    int nCount = SS.Count;
                    ed.WriteMessage("在{0}层上找到{1}个实体", pr.StringResult, nCount);
                }
            }
        }
    }
}


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

已领礼包: 15个

财富等级: 恭喜发财

发表于 2017-11-14 15:46:15 | 显示全部楼层
好东西,学习了
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2018-1-9 08:58:05 | 显示全部楼层
haole ,zenme wode ping dazi yongbule
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-3-29 03:40 , Processed in 0.349802 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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