颜色修改在CAD中属于比较简单的操作,但是当面临块嵌套的情况时,要想彻底修改其颜色可能会非常困难。
本文聚集在修改嵌套块的颜色上,封装相关接口。
递归修改块中元素的颜色模式
将块中所有元素的颜色模式修改为ByBlock模式,最后修改块参照颜色即可实现,这里使用到递归算法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public static void SetBlockEntitiesColor(this Document doc, BlockReference block) { using (Transaction trans = doc.TransactionManager.StartTransaction()) { BlockTableRecord btr = trans.GetObject(block.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord; foreach (ObjectId id in btr) { Entity ent2 = (Entity)trans.GetObject(id, OpenMode.ForWrite); ent2.ColorIndex = 0; if (ent2 is BlockReference) { BlockReference block1 = (BlockReference)ent2; SetBlockEntitiesColor(doc, block1); } } trans.Commit(); } }
|
修改块参照颜色
1 2 3 4 5 6
| public static void ChangeBlockReferenceColor(this Document doc, BlockReference block, System.Windows.Media.Color blockColor) { SetBlockEntitiesColor(doc, block); cadColors.Color color = cadColors.Color.FromRgb(blockColor.R, blockColor.G, blockColor.B); block.Color = color; }
|