using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices; // Namespace for the ResultBuffer class
using Autodesk.AutoCAD.EditorInput; // for the Editor
using Autodesk.AutoCAD.Geometry;
namespace SendMouseClick
{
public class LispFunctions
{
#region HeaderComments
/* Sample Lisp Calls (try in the drawing editor):
_$ (SendMouseClick) >> (669.0 667.0) ; Will return the mouse pixel coordinates
_$ (SendMouseClick '(669.0 667.0)) >> (669.0 667.0) ; Will move the the cursor to the specified pixel coordinate(s)
_$ (repeat 6 (SendMouseClick (mapcar '+ '(50. 20.) (SendMouseClick))) (vl-cmdf "_.delay" 200)) ; Will move your cursor
_$ (SendMouseClick '(700 400) "rightdown" "rightup") ; will go to the specified pixel coordinates, and will invoke the right clicks
(defun C:test nil (SendMouseClick "leftdown" "leftup")) ; will simulate left-clicks on invoking the 'test' command
(defun C:test nil (SendMouseClick "leftdown" "leftup" (mapcar '+ '(200 -200) (SendMouseClick)) "leftdown" "leftup")) ; will attempt an selection from 'BL' corner towards 'TR'
(defun InvokeRightClick nil (SendMouseClick "rightdown" "rightup"))
NOTE: You can read the mouse coordinates within using reactors (no need to invoke (grread t))
Sample (test) Lisp Functions:
(defun C:MoveCursorWithKeys ( / s k v )
(princ "\nPress [W/A/S/D] keys to move your cursor <exit>: ")
(if SendMouseClick
(while (not s) (mapcar 'set '(k v) (grread))
(and (/= 2 k) (setq s t))
(and (= 2 k)
(SendMouseClick
(mapcar '+ (SendMouseClick)
(cond
(
(cdr
(assoc (setq v (read (chr v)))
'(
(A -5 0)
(D 5 0)
(W 0 -5)
(S 0 5)
)
)
)
)
( (setq s t) '(0 0) )
)
)
)
)
)
)
(princ)
)
(defun C:ClickOnOptions ( / app )
(setq app (vlax-get-acad-object))
(mapcar (function (lambda (x) (vla-put-WindowState app x))) (list acMin acMax))
(SendMouseClick '(10 10) "leftdown" "leftup" '(215 590) "leftdown" "leftup")
(princ)
)
*/
// (SendMouseClick "rightdown" "rightup" '(10 . 10) '(30 . 30)) // <<- dotted pairs as Point2d arguments, don't support them
#endregion HeaderComments
[LispFunction("SendMouseClick")]
public static TypedValue SendMouseClick(ResultBuffer rbArgs)
{
try
{
if (rbArgs != null)
{
bool isDottedPair = false;
List
<int> coords
= new List
<int>();
foreach (TypedValue tv in rbArgs)
{
switch (tv.TypeCode)
{
case (int)LispDataType.Text:
{
switch (tv.Value.ToString().ToLower())
{
case "leftdown": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown); break; }
case "leftup": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp); break; }
case "middledown": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.MiddleDown); break; }
case "middleup": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.MiddleUp); break; }
case "move": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.Move); break; }
case "absolute": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.Absolute); break; }
case "rightdown": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightDown); break; }
case "rightup": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightUp); break; }
default: { break; }
}
break;
}
case (int)LispDataType.Point2d:
{
Point2d p2d = (Point2d)tv.Value;
try
{
MouseOperations.SetCursorPosition((int)p2d.X, (int)p2d.Y);
}
catch { }
break;
}
default: { break; }
}
}
}
MouseOperations.MousePoint pt = MouseOperations.GetCursorPosition();
return new TypedValue
((int)LispDataType
.Point2d,
new Point2d
(pt
.X, pt
.Y)); }
catch (System.Exception ex)
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(string.Format("\nError: {0}\nStackTrace: {1}", ex.Message, ex.StackTrace));
}
return new TypedValue
((int)LispDataType
.Nil); } // public static void SendMouseClick()
}
}