- 积分
- 2124
- 明经币
- 个
- 注册时间
- 2011-4-12
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 mrhvslisp 于 2011-11-4 15:05 编辑
代码已更正,感谢sailorcwx 的指导,谢谢!!!
- #region//图块改色
- [CommandMethod("BlockColor")]
- public void BlockColor()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- TypedValue[] filter = new TypedValue[1];
- filter[0] = new TypedValue(0, "insert");
- SelectionFilter ssfilter = new SelectionFilter(filter);
- PromptSelectionResult ssresult = ed.GetSelection(ssfilter);
- if (ssresult.Status != PromptStatus.OK)
- {
- return;
- }
- PromptIntegerOptions intoptions=new PromptIntegerOptions ("");
- intoptions.Message = "\n请指定颜色";
- intoptions.AllowArbitraryInput = false;
- intoptions.AllowNone = true ;
- intoptions.DefaultValue = 1;
- intoptions.AllowNegative = false;
-
- PromptIntegerResult intresult=ed.GetInteger(intoptions);
- if (intresult .Status!=PromptStatus.OK)
- {
- return;
- }
- List <BlockTableRecord> list=new List<BlockTableRecord> ();
-
- list = getlist(db, ssresult,intresult,list);
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- foreach (BlockTableRecord blkr in list)
- {
- ModifyColor(blkr, trans, bt, intresult);
- }
- trans.Commit();
- }
-
-
- }
- //获取块定义集合(块表记录集合)
- public List<BlockTableRecord> getlist(Database db, PromptSelectionResult ssresult,PromptIntegerResult intresult, List <BlockTableRecord> list)
- {
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- foreach (ObjectId id in ssresult.Value.GetObjectIds())
- {
- BlockReference ent = trans.GetObject(id, OpenMode.ForWrite) as BlockReference;
- ent.ColorIndex = intresult.Value;
- BlockTableRecord blkr = trans.GetObject(bt[ent.Name], OpenMode.ForRead) as BlockTableRecord;
- if (!list.Contains(blkr))
- {
- list.Add(blkr);
- }
- }
- trans.Commit();
- return list;
- }
- }
- //对块定义改色
- public void ModifyColor(BlockTableRecord blkr, Transaction trans, BlockTable bt, PromptIntegerResult intresult)
- {
- BlockTableRecordEnumerator enu = blkr.GetEnumerator();//获取块枚举器
- while (enu.MoveNext())
- {
- Entity ent = (Entity)trans.GetObject(enu.Current, OpenMode.ForWrite);
- ent.ColorIndex = intresult.Value;
- if (ent is BlockReference)
- {
- ent.ColorIndex = intresult.Value;
- blkr = trans.GetObject(bt[((BlockReference)ent).Name], OpenMode.ForWrite)as BlockTableRecord;
- ModifyColor(blkr, trans, bt, intresult);
- }
- }
- }
-
-
|
|