Implementing an AutoCAD command to print graphics to the screen using .NET
This recent request came into my inbox from our discussion group support team:
I know that this is not strictly an AutoCAD 2012 issue, BUT as her CAD teacher it’s my job to do the best I can for all of my students so I’m reaching out and asking for help.
I have a severely disabled student; she can only move her head (and that is severely limited). She has a head mouse to point with. She can left and right click her mouse by using a puffer tube in her mouth. But she can’t scroll or pan easily, (Center mouse wheel something easy for us). I have enlarged the icons, enlarged the text, high contrast background, moved the command line up to the ribbon, and she uses an onscreen keyboard (the default keyboard that comes with windows). But inputting numbers, zooming and panning is very hard; even with icons it is painfully slow. Note this is a main streamed student, who is at peer level. She has a full time nurse and aid to help her.
I have a second monitor mounted close to me so I can see what she is doing, but I don’t have an easy way for her to signal me when she needs help. Anyone know how to write a lisp program to flash a red square to catch my attention?
This is an interesting request, and one whose solution I have to believe would be helpful to others in similar situations (as well as to those who just have an interest in the chosen approach).
There were a couple of ways that stood out, in terms of addressing this requirement:
- One using AutoCAD transient graphics to display text on AutoCAD’s drawing canvas
- One painting graphics directly to the screen, not using an AutoCAD-specific API
I opted for the second approach, as I considered it would be of more potential use for products other than AutoCAD. It creates an empty, transparent form the size of the screen which paints a red border around itself and some simple text centered on it. The form flashes on and off five times (although it could very easily be displayed indefinitely, too).
Here’s the C# code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using System.Drawing;
using System.Windows.Forms;
using System;
namespace GetAttention
{
public class HelpForm : Form
{
// Percentage of screen height for border
const int bdrPrc = 20;
const string msg = "Help!";
public HelpForm()
{
TopMost = true;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.Plum;
TransparencyKey = Color.Plum;
Width = Screen.PrimaryScreen.Bounds.Width;
Height = Screen.PrimaryScreen.Bounds.Height;
Paint += new PaintEventHandler(HelpForm_Paint);
}
void HelpForm_Paint(object sender, PaintEventArgs e)
{
// Calculate the actual border size in pixels
int bdrWid = Height * bdrPrc / 100;
// Our border will be around the whole screen
Rectangle border = new Rectangle(0, 0, Width, Height);
// Draw the border
e.Graphics.DrawRectangle(
new Pen(Brushes.Red, bdrWid), border
);
// Our text will be centered in the border
System.Drawing.Font f = new Font("Arial", bdrWid);
SizeF sz = e.Graphics.MeasureString(msg, f);
int wid = (int)sz.Width;
int hgt = (int)sz.Height;
Rectangle rect =
new Rectangle(
(Width - wid) / 2, (Height - hgt) / 2,
(int)(wid * 1.2), hgt
);
e.Graphics.DrawString(msg, f, Brushes.Red, rect);
}
}
public class Commands
{
private HelpForm _form = null;
private Timer _timer = null;
private int _times = 0;
void Timer_Tick(object sender, EventArgs e)
{
// Once the timer has ticked 10 times (and the form
// displayed 5 times), dispose of the form
if (_times++ >= 10)
{
_form.Hide();
_form.Dispose();
_form = null;
_timer.Stop();
_timer.Dispose();
_timer = null;
_times = 0;
}
else
{
// If we haven't reached ten ticks, toggle the form's
// display on/off
if (_form.Visible)
_form.Hide();
else
_form.Show();
}
}
[CommandMethod("HELPME")]
public void RequestHelp()
{
// Create our form
_form = new HelpForm();
_form.Show();
// Start the timer, ticking once per second
_timer = new Timer() { Interval = 1000, Enabled = true };
_timer.Tick += new EventHandler(Timer_Tick);
}
}
}
Here are the results of running the HELPME command:
Recent Comments
Archives
More...