This is a question that came up at the recent Cloud Accelerator in Prague: how can you change the border colour for all raster image objects in the drawing? We could do this by placing the raster image on a particular layer, but the developer was looking for a global override.
The answer ended up being really simple with a DrawableOverrule. Here’s some C# code that does this:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
namespace RasterImageOverrule
{
public class RasterImageDisplayOverrule : DrawableOverrule
{
public short Color { get; set; }
public override void ViewportDraw(Drawable d, ViewportDraw vd)
{
vd.SubEntityTraits.Color = Color;
base.ViewportDraw(d, vd);
}
}
public class Commands
{
// Shared member variable to store our Overrule instance
private static RasterImageDisplayOverrule _orule;
[CommandMethod("RIB")]
public static void RasterImageBorder()
{
var doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null)
return;
var db = doc.Database;
var ed = doc.Editor;
// Select the color index to use
var pio = new PromptIntegerOptions("");
pio.SetMessageAndKeywords(
"\nSelect color index for raster image borders [None]:",
"None"
);
var pir = ed.GetInteger(pio);
// Flag to determined whether to regenerate the model
bool regen = false;
if (pir.Status == PromptStatus.Keyword)
{
if (_orule != null)
{
// Remove overrule
Overrule.RemoveOverrule(
RXObject.GetClass(typeof(RasterImage)), _orule
);
_orule.Dispose();
_orule = null;
// When removing the overrule we know we want to regen
regen = true;
}
}
else if (pir.Status == PromptStatus.OK)
{
// Initialize overrule if first time run
if (_orule == null)
{
// Turn overruling on
_orule = new RasterImageDisplayOverrule();
Overrule.AddOverrule(
RXObject.GetClass(typeof(RasterImage)), _orule, false
);
Overrule.Overruling = true;
}
// Only regen the model if the color index has changed
regen = _orule.Color != (short)pir.Value;
if (regen)
{
_orule.Color = (short)pir.Value;
}
}
// Perform the regen
if (regen)
{
doc.Editor.Regen();
}
}
}
}
The overrule is kept simple because the border is the only part of the raster image entity that cares about the colour setting: the rest is determined by the image definition.
Here’s how the RIB command performs on an image we attach to the current drawing: