找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 667|回复: 2

Finding out if a layer contains entities

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-9 22:08:24 | 显示全部楼层 |阅读模式

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

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

×


问题:
Is there a quick way to find out if there are entities associated with a layer?

解答:
I have a simple solution for you. We can use the Purge() method of the class Database to check whether the layer is able to be purged or not. If it is, it is not associated with any objects; if not, it is. The Purge() method accepts an ObjectId collection argument. If the layers in the collection are able to be purged, they will remain in the collection; otherwise will be removed from the collection. Therefore, we can check whether the layer id is still in the collection or not to know whether it is able to be purged or not. Here, we can just check whether the count of the collection is zero or not since we can add the layer to the collection.

The following code does so exactly. Please make a layer “1” and draw something on it before testing the command.
  1. [CommandMethod("CheckLayerDependence")]
  2. public void CheckLayerDependence()
  3. {
  4.   Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  5.   try
  6.   {
  7.     Database db = HostApplicationServices.WorkingDatabase;

  8.     using (Transaction tr = db.TransactionManager.StartTransaction())
  9.     {
  10.       ObjectId ltId = db.LayerTableId;
  11.       LayerTable lt = (LayerTable)tr.GetObject(ltId, OpenMode.ForRead);
  12.       if ( lt.Has("1") )
  13.       {
  14.         ObjectId ltrId = lt["1"];

  15.         ObjectIdCollection idCol = new ObjectIdCollection();
  16.         idCol.Add(ltrId);
  17.         db.Purge(idCol);

  18.         if (idCol.Count != 0)
  19.         {
  20.           ed.WriteMessage("\nThe layer 1 does not have dependences.");
  21.         }
  22.         else
  23.         {
  24.           ed.WriteMessage("\nThe layer 1 have dependences.");
  25.         }
  26.       }
  27.       else
  28.       {
  29.         ed.WriteMessage("\nThe layer 1 does not exist!");
  30.       }

  31.       tr.Commit();
  32.     }
  33.   }
  34.   catch (System.Exception ex)
  35.   {
  36.     ed.WriteMessage(ex.Message);
  37.   }
  38. }


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

已领礼包: 4365个

财富等级: 富可敌国

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

使用道具 举报

已领礼包: 4365个

财富等级: 富可敌国

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-5 12:44 , Processed in 0.300547 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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