乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 46|回复: 0

C#练习遍历块,删除块内指定名称的块

[复制链接]

20

主题

121

帖子

11

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
201
发表于 2020-2-24 23:09:00 | 显示全部楼层 |阅读模式
net模块总帖子比较少,正在练习,分享一下
  1. //查找图块并删除
  2.    [CommandMethod("sctk")]
  3.         public void Mydeltk()
  4.         {
  5.             Document doc = Application.DocumentManager.MdiActiveDocument;
  6.             Database db = doc.Database;
  7.             Editor ed = doc.Editor;
  8.             PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter your name: ");
  9.             pStrOpts.AllowSpaces = true;
  10.             PromptResult pStrRes = ed.GetString(pStrOpts);
  11.             if (pStrRes.Status != PromptStatus.OK) return;
  12.             Application.ShowAlertDialog("The name entered was: " +
  13.                               pStrRes.StringResult);
  14.             String Blockname = pStrRes.StringResult;
  15.             //定义过滤器
  16.             TypedValue[] filList = new TypedValue[1];
  17.             filList[0] = new TypedValue(0, "Insert");
  18.             //filList[1] = new TypedValue(2, br.Name);
  19.             SelectionFilter filter = new SelectionFilter(filList);
  20.             //选择对象
  21.       PromptSelectionResult res = ed.SelectAll(filter);
  22.             if(res.Status != PromptStatus.OK) return;
  23.             Transaction trans = db.TransactionManager.StartTransaction();
  24.             using (trans)
  25.             {
  26.                 try
  27.                 {
  28.                 //
  29.                   //遍历选择集
  30.                 foreach (ObjectId id in res.Value.GetObjectIds())
  31.                  {
  32.                      BlockReference Selent = (BlockReference)trans.GetObject(id, OpenMode.ForWrite);
  33.                     // BlockReference br = ent as BlockReference;
  34.                      // Selent.Color = Color.FromColorIndex(ColorMethod.ByLayer, 256);
  35.                      DelBlockSubEntity(ed, trans, Selent, Blockname);
  36.                   }
  37.                   //ed.WriteMessage("\n一共查找了" + res.Value.Count );
  38.                   trans.Commit();
  39.                 }
  40.                 catch (Autodesk..Runtime.Exception e)
  41.                 {
  42.                     // Something went wrong
  43.                     ed.WriteMessage(e.ToString());
  44.                 }
  45.             }
  46.         }
  47.              [hide]   ///
  48.         ///  删除图块内部指定名称的图块
  49.         ///
  50.         ///
  51.         /// 块参照
  52.       /// 图块名称
  53.         private static void DelBlockSubEntity(Editor ed, Transaction trans, BlockReference blockObject, String Blockname)
  54.         {
  55.             //获取图块的块表记录
  56.             BlockTableRecord btrBlock = trans.GetObject(blockObject.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
  57.             //遍历图块里的所有实体对象
  58.             foreach (ObjectId BlcokId in btrBlock)
  59.             {
  60.                 Entity BlockEntity = trans.GetObject(BlcokId, OpenMode.ForRead) as Entity;
  61.                 if (BlockEntity is BlockReference)
  62.                 {
  63.                   BlockReference ReEntity = (BlockReference)BlockEntity;
  64.                  //获取图块中的所有实体对象
  65.                   if (ReEntity.Name == Blockname)
  66.                   {
  67.                    Entity BlockEntity2 = trans.GetObject(BlcokId, OpenMode.ForWrite) as Entity;
  68.                    BlockEntity2.Erase();
  69.                    Application.ShowAlertDialog("The name entered was: " + Blockname + "删除了哦!");
  70.                   }
  71.                  DelBlockSubEntity(ed, trans, (BlockReference)BlockEntity, (String)Blockname);
  72.                 }
  73.             }
  74. [/hide]}
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 02:40 , Processed in 0.421746 second(s), 54 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表