using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AnimateClock.MyCommands))]
#pragma warning disable 0618
namespace AnimateClock
{
public class MyCommands
{
[CommandMethod("AnimateClock")]
public void AnimateClockHandler()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
Database db = doc.Database;
Point3d center = (Point3d) Application.GetSystemVariable("VIEWCTR");
double height = (double) Application.GetSystemVariable("VIEWSIZE");
Point2d scrsize = (Point2d)Application.GetSystemVariable("SCREENSIZE");
double width = height * (scrsize.X / scrsize.Y);
double radius = Math.Min(height, width) * 0.45;
ObjectId axisId = ObjectId.Null;
using (BlockTable bt = db.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
{
using (BlockTableRecord btr = bt[BlockTableRecord.ModelSpace].Open(OpenMode.ForWrite) as BlockTableRecord)
{
using (Line axis = new Line (center, center + new Vector3d(0, radius, 0)))
{
axisId = btr.AppendEntity(axis);
}
}
}
MyMessageFilter filter = new MyMessageFilter();
System.Windows.Forms.Application.AddMessageFilter(filter);
if (!axisId.IsNull)
{
double ang = 0;
while (!filter.bCanceled)
{
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
Line line = tr.GetObject(axisId, OpenMode.ForWrite) as Line;
line.EndPoint = line.StartPoint + new Vector3d(0, radius, 0).RotateBy(ang, Vector3d.ZAxis);
//doc.TransactionManager.QueueForGraphicsFlush();
//doc.TransactionManager.FlushGraphics();
tr.Commit();
}
//ed.UpdateScreen();
Autodesk.AutoCAD.Internal.Utils.FlushGraphics();
System.Threading.Thread.Sleep(1000);
System.Windows.Forms.Application.DoEvents();
ang -= Math.PI / 30;
if (ang < 0) ang += 2 * Math.PI;
}
}
System.Windows.Forms.Application.RemoveMessageFilter(filter);
}
public class MyMessageFilter : System.Windows.Forms.IMessageFilter
{
public const int WM_KEYDOWN = 0x0100;
public bool bCanceled = false;
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_KEYDOWN)
{
// Check for the Escape keypress
System.Windows.Forms.Keys kc
= (System.Windows.Forms.Keys)
(int)m.WParam & System.Windows.Forms.Keys.KeyCode;
if (m.Msg == WM_KEYDOWN &&
kc == System.Windows.Forms.Keys.Escape)
{
bCanceled = true;
}
// Return true to filter all keypresses
return true;
}
// Return false to let other messages through
return false;
}
}
}
}