I have to say that I’m having an absolute blast getting to know the new API features in AutoCAD 2015. It turns out I’d barely scratched the surface with my original post about the impact for developers. It’s a release that just keeps on giving. :-)
One new feature that I’ve had a lot of fun playing with is the “cursor badge” capability, something you may have seen blogged about from a user’s perspective. The good news is that the ability to add your own cursor badges is provided in the public API (although I did end up using an internal – but publicly accessible – method to convert to the required image format for the badge itself).
Rather than loading and using a bitmap resource – and yes, I could have added my head to the AutoCAD cursor, but that would have been a bit much now that my blog is there inside the AutoCAD frame – I decided to generate a bitmap programmatically and convert that into the format needed for a cursor badge. If you do end up loading your own bitmap resource for this and find that it’s flipped, you might also try using ConvertBitmapToAcGiImageBGRA32Ex() (instead of ConvertBitmapToAcGiImageBGRA32() that I used), to see if that helps.
I discovered a few useful tricks while putting this together: to draw a transparent background for your badge – and you will want to – it needs to be magenta (R=255, G=0, B=255). I think I knew this before, but then I’d gotten used to thinking about the colour needed for transparent pixels in ribbon icons – R=192, G=192, B=192 – and so had to relearn it. I also found that you need to set a cursor badge’s priority to a value such as 1 or 2 if you want your badge to be replaced by system badges (at least the window selection-related ones) or 3 if you want it to take precedence. There may be other priority values to take into consideration, but that’s what I’ve found out for now.
I decided to add the cursor badge in one command and remove it in another – as much to see the way things work when you start a window selection, as anything – but it would be more typical to add the badge just before starting a jig (say) and remove it immediately afterwards. You’d also probably want to add a smaller cursor badge than the one shown below, of course. :-)
Here’s the C# code:
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Runtime;
using System.Drawing;
namespace CursorBadges
{
public class Commands
{
private ImageBGRA32 _img = null;
[CommandMethod("ACB")]
public void AddCursorBadge()
{
if (_img == null)
{
using (var bmp = new Bitmap(85, 85))
{
using (var g = Graphics.FromImage(bmp))
{
// The transparent colour used for these badges is
// Magenta, I've found. Let's use that as a background
// and then draw three concentric, filled circles
// (looking a bit like the RAF logo)
g.Clear(Color.Magenta);
g.FillEllipse(Brushes.Blue, 5, 5, 60, 60);
g.FillEllipse(Brushes.White, 15, 15, 40, 40);
g.FillEllipse(Brushes.Red, 25, 25, 20, 20);
}
_img = Utils.ConvertBitmapToAcGiImageBGRA32(bmp);
}
}
// Adding the badge with a priority of 1 means that it
// will get replaced by selection-related badges. Changing
// this to 3 is enough to give it precedence over these
// (a higher number still will increase the chances
// of it staying shown)
var cbu = new CursorBadgeUtilities();
cbu.AddSupplementalCursorImage(_img, 1);
}
[CommandMethod("RCB")]
public void RemoveCursorBadge()
{
var cbu = new CursorBadgeUtilities();
if (cbu.HasSupplementalCursorImage() && _img != null)
cbu.RemoveSupplementalCursorImage(_img);
}
}
}
When you run the ACB command you’ll find a badge that looks somewhat like the RAF roundel added to the cursor. Selecting the drawing background will cause it to be replaced by the window selection cursor badges, and you can use the RCB command to remove it completely.