这有两段代码,仅供参考。- // 修改块参照的颜色——编辑块定义中的对象颜色为随块.
- [CommandMethod("test1")]
- public void MyTest1()
- {
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- Database db = HostApplicationServices.WorkingDatabase;
- PromptEntityOptions opt = new PromptEntityOptions("\n请选择块参照");
- opt.SetRejectMessage("您选择的不是块参照,请重新选择!");
- opt.AddAllowedClass(typeof(BlockReference), true);
- PromptEntityResult res = ed.GetEntity(opt);
- if (res.Status != PromptStatus.OK)
- {
- return;
- }
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockReference ent = (BlockReference)trans.GetObject(res.ObjectId,
- OpenMode.ForWrite);
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
- OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[ent.Name],
- OpenMode.ForWrite);
- BlockTableRecordEnumerator blkRefEnumerator = btr.GetEnumerator();
- while (blkRefEnumerator.MoveNext())
- {
- Entity ee = (Entity)trans.GetObject(blkRefEnumerator.Current,
- OpenMode.ForWrite);
- if (ee.ColorIndex != 0)
- {
- ee.ColorIndex = 0;
- }
- }
- ent.ColorIndex = 1;
- trans.Commit();
- }
- }
- // 修改块参照的颜色——编辑块定义中的对象颜色为新颜色.
- [CommandMethod("test2")]
- public void MyTest2()
- {
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- Database db = HostApplicationServices.WorkingDatabase;
- PromptEntityOptions opt = new PromptEntityOptions("\n请选择块参照");
- opt.SetRejectMessage("您选择的不是块参照,请重新选择!");
- opt.AddAllowedClass(typeof(BlockReference), true);
- PromptEntityResult res = ed.GetEntity(opt);
- if (res.Status != PromptStatus.OK)
- {
- return;
- }
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockReference ent = (BlockReference)trans.GetObject(res.ObjectId,
- OpenMode.ForRead);
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
- OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[ent.Name],
- OpenMode.ForWrite);
- BlockTableRecordEnumerator blkRefEnumerator = btr.GetEnumerator();
- while (blkRefEnumerator.MoveNext())
- {
- Entity ee = (Entity)trans.GetObject(blkRefEnumerator.Current,
- OpenMode.ForWrite);
- ee.ColorIndex = 1;
- }
- ed.Regen();
- trans.Commit();
- }
- }
|