[求助]如何设置块参照的颜色?
<p>BlockReference 类型的br 用 br.Color = Color.FromColorIndex(ColorMethod.ByBlock, color.ColorIndex);</p><p>设置其颜色后br.Color显示正确了 但是没显示应有的颜色 </p> 不行的 刚试了下 狐哥,您老在哪儿啊呼唤。。。。。。。。。。 本帖最后由 作者 于 2009-10-20 21:07:04 编辑ColorMethod.ByAci 快参照的颜色应该是在快定义中设定的,无法改变,除非你改变块定义的颜色,那么所有该块定义的块参照的颜色也就全部改变了。 <p>可以在块定义的实体设置Color为ByBlock,这样参照的颜色就可以随块改变</p> 这有两段代码,仅供参考。
// 修改块参照的颜色——编辑块定义中的对象颜色为随块.
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,
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();
}
}
// 修改块参照的颜色——编辑块定义中的对象颜色为新颜色.
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,
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();
}
}
<p>从功能上而言</p><p>ent.Color = Color.FromColorIndex(ColorMethod.ByAci, 0);</p><p>等效于</p><p>ent.ColorIndex = 0<br/>即随块</p><p>不过老卢的写法要简洁些</p>
页:
[1]
2