找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1002|回复: 0

[分享] Changing the colour of nested AutoCAD entities through .NET

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-27 00:10:18 | 显示全部楼层 |阅读模式

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

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

×
Changing the colour of nested AutoCAD entities through .NET                                                        Someone asked me earlier today how to iteratively change the color of entities inside blocks.
The following code uses a recursive helper function to iterate down through the contents of a block, changing the various entities (other than block references, for which we simply recurse) to a specified colour.
  1. using Autodesk.AutoCAD.ApplicationServices;

  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.AutoCAD.Colors;
  6. namespace BlockTest
  7. {
  8.   public class BlockCmds
  9.   {
  10.     [CommandMethod("CC")]
  11.     public void ChangeColor()
  12.     {
  13.       Document doc =
  14.         Application.DocumentManager.MdiActiveDocument;
  15.       Database db = doc.Database;
  16.       Editor ed = doc.Editor;
  17.       PromptIntegerResult pr =
  18.         ed.GetInteger(
  19.           "\nEnter color index to change all entities to: "
  20.         );
  21.       if (pr.Status == PromptStatus.OK)
  22.       {
  23.         short newColorIndex = (short)pr.Value;
  24.         ObjectId msId;
  25.         Transaction tr =
  26.           doc.TransactionManager.StartTransaction();
  27.         using (tr)
  28.         {
  29.           BlockTable bt =
  30.             (BlockTable)tr.GetObject(
  31.               db.BlockTableId,
  32.               OpenMode.ForRead
  33.             );
  34.           msId =
  35.             bt[BlockTableRecord.ModelSpace];
  36.           // Not needed, but quicker than aborting
  37.           tr.Commit();
  38.         }
  39.         int count =
  40.           ChangeNestedEntitiesToColor(msId, newColorIndex);
  41.         ed.Regen();
  42.         ed.WriteMessage(
  43.           "\nChanged {0} entit{1} to color {2}.",
  44.           count,
  45.           count == 1 ? "y" : "ies",
  46.           newColorIndex
  47.         );
  48.       }
  49.     }
  50.     private int ChangeNestedEntitiesToColor(
  51.       ObjectId btrId, short colorIndex)
  52.     {
  53.       int changedCount = 0;
  54.       Document doc =
  55.         Application.DocumentManager.MdiActiveDocument;
  56.       Database db = doc.Database;
  57.       Editor ed = doc.Editor;
  58.       Transaction tr =
  59.         doc.TransactionManager.StartTransaction();
  60.       using (tr)
  61.       {
  62.         BlockTableRecord btr =
  63.           (BlockTableRecord)tr.GetObject(
  64.             btrId,
  65.             OpenMode.ForRead
  66.           );
  67.         foreach (ObjectId entId in btr)
  68.         {
  69.           Entity ent =
  70.             tr.GetObject(entId, OpenMode.ForRead)
  71.             as Entity;
  72.           if (ent != null)
  73.           {
  74.             BlockReference br = ent as BlockReference;
  75.             if (br != null)
  76.             {
  77.               // Recurse for nested blocks
  78.               changedCount +=
  79.                 ChangeNestedEntitiesToColor(
  80.                   br.BlockTableRecord,
  81.                   colorIndex
  82.                 );
  83.             }
  84.             else
  85.             {
  86.               if (ent.ColorIndex != colorIndex)
  87.               {
  88.                 changedCount++;
  89.                 // Entity is only open for read
  90.                 ent.UpgradeOpen();
  91.                 ent.ColorIndex = colorIndex;
  92.                 ent.DowngradeOpen();
  93.               }
  94.             }
  95.           }
  96.         }
  97.         tr.Commit();
  98.       }
  99.       return changedCount;
  100.     }
  101.   }
  102. }


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

本版积分规则

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

GMT+8, 2024-5-22 10:40 , Processed in 0.357016 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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