乐筑天下

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

CAD.NET API一日一练(5)图块改色

[复制链接]

24

主题

42

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
138
发表于 2011-11-3 15:50:00 | 显示全部楼层 |阅读模式
代码已更正,感谢 的指导,谢谢!!!
  1. #region//图块改色
  2.         [CommandMethod("BlockColor")]
  3.         public void BlockColor()
  4.         {
  5.             Document doc = Application.DocumentManager.MdiActiveDocument;
  6.             Database db = doc.Database;
  7.             Editor ed = doc.Editor;
  8.             TypedValue[] filter = new TypedValue[1];
  9.             filter[0] = new TypedValue(0, "insert");
  10.             SelectionFilter ssfilter = new SelectionFilter(filter);
  11.             PromptSelectionResult ssresult = ed.GetSelection(ssfilter);
  12.             if (ssresult.Status != PromptStatus.OK)
  13.             {
  14.                 return;
  15.             }
  16.             PromptIntegerOptions  intoptions=new PromptIntegerOptions ("");
  17.             intoptions.Message = "\n请指定颜色";
  18.             intoptions.AllowArbitraryInput = false;
  19.             intoptions.AllowNone = true ;
  20.             intoptions.DefaultValue = 1;
  21.             intoptions.AllowNegative = false;
  22.                         
  23.             PromptIntegerResult intresult=ed.GetInteger(intoptions);
  24.             if (intresult .Status!=PromptStatus.OK)
  25.             {
  26.                 return;
  27.             }
  28.             List [B] list=new List[B] ();
  29.             
  30.             list = getlist(db, ssresult,intresult,list);
  31.              using (Transaction trans = db.TransactionManager.StartTransaction())
  32.             {
  33.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  34.                 foreach (BlockTableRecord blkr in list)
  35.                 {
  36.                     ModifyColor(blkr, trans, bt, intresult);
  37.                 }         
  38.                 trans.Commit();
  39.             }
  40.             
  41.             
  42.         }
  43.         //获取块定义集合(块表记录集合)
  44.         public List[B] getlist(Database db, PromptSelectionResult ssresult,PromptIntegerResult intresult,  List [B] list)
  45.         {
  46.             using (Transaction trans = db.TransactionManager.StartTransaction())
  47.             {
  48.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  49.                 foreach (ObjectId id in ssresult.Value.GetObjectIds())
  50.                 {
  51.                     BlockReference ent = trans.GetObject(id, OpenMode.ForWrite) as BlockReference;
  52.                     ent.ColorIndex = intresult.Value;
  53.                     BlockTableRecord blkr = trans.GetObject(bt[ent.Name], OpenMode.ForRead) as BlockTableRecord;
  54.                     if (!list.Contains(blkr))
  55.                     {
  56.                         list.Add(blkr);
  57.                     }
  58.                 }
  59.                 trans.Commit();
  60.                 return list;
  61.             }
  62.         }
  63.         //对块定义改色      
  64.         public void ModifyColor(BlockTableRecord blkr, Transaction trans, BlockTable bt, PromptIntegerResult intresult)
  65.         {
  66.             BlockTableRecordEnumerator enu = blkr.GetEnumerator();//获取块枚举器
  67.             while (enu.MoveNext())
  68.             {
  69.                 Entity ent = (Entity)trans.GetObject(enu.Current, OpenMode.ForWrite);
  70.                 ent.ColorIndex = intresult.Value;
  71.                  if (ent is BlockReference)
  72.                 {
  73.                     ent.ColorIndex = intresult.Value;
  74.                     blkr = trans.GetObject(bt[((BlockReference)ent).Name], OpenMode.ForWrite)as BlockTableRecord;
  75.                     ModifyColor(blkr, trans, bt, intresult);
  76.                 }
  77.             }
  78.         }
  79.            
  80.         

回复

使用道具 举报

29

主题

503

帖子

8

银币

中流砥柱

Rank: 25

铜币
619
发表于 2011-11-4 14:39:00 | 显示全部楼层

我不是版主,不过我分析一下
你最后的ent.BlockName得到的其实是上一层的块参照名称,打个比方,你的"块1“内嵌"块2",那么你的
ent.BlockName返回的是"块1",ent.Name才是它的块名"块2",所以导致了死循环,因此应该添加一个转换
BlockReference ent1 = (BlockReference)ent;
然后用ent.Name来读取块名
blkr = trans.GetObject(bt[ent1.Name], OpenMode.ForWrite)as BlockTableRecord;
回复

使用道具 举报

24

主题

42

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
138
发表于 2011-11-4 15:01:00 | 显示全部楼层

哈哈,完全正确,学习了,感谢老师指导
回复

使用道具 举报

0

主题

4

帖子

1

银币

初来乍到

Rank: 1

铜币
4
发表于 2013-6-7 15:11:00 | 显示全部楼层
好贴,一定支持。
回复

使用道具 举报

0

主题

4

帖子

1

银币

初来乍到

Rank: 1

铜币
4
发表于 2013-6-7 15:22:00 | 显示全部楼层
好贴,一定支持。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-14 21:18 , Processed in 0.403795 second(s), 62 queries .

© 2020-2025 乐筑天下

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