Author Topic: Simulate left mouse click  (Read 6319 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Simulate left mouse click
« on: December 21, 2017, 09:22:58 AM »
I have a point in variable pt...

How can I simulate that I left mouse clicked on that point (of course in ACAD) through ALISP ?

P.S. I want something that will select single sub object, but unfortunately (vl-cmdf "_.SELECT" "_SU" "_non" pt "") doesn't work...
« Last Edit: December 21, 2017, 09:30:06 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: Simulate left mouse click
« Reply #1 on: December 25, 2017, 12:49:14 PM »

Hi Marko,
with "" the object is deselected

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Re: Simulate left mouse click
« Reply #2 on: December 25, 2017, 02:58:05 PM »

Hi Marko,
with "" the object is deselected

Never mind Gian, I've solved my task differently... It was just ask for fun and to see if there is some possibility to achieve what I want... But now I don't mind...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Simulate left mouse click
« Reply #3 on: December 28, 2017, 06:46:52 PM »
Going off memory, but if you want to select a sub-entity by a point, I think there is a function 'nentselp' with will allow you to do it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Re: Simulate left mouse click
« Reply #4 on: December 28, 2017, 09:26:28 PM »
Going off memory, but if you want to select a sub-entity by a point, I think there is a function 'nentselp' with will allow you to do it.

No, (nentselp) won't work for complex 3d solids... As I stated, I solved my task by checking numbers of selected entities through (vl-cmdf "_.SELECT" "_SU" "_CP" p1 p2 p3 p4 "") and then (setvar 'cmdecho 1), (vl-cmdf "_.SELECT") => (setq li (getvar 'lastprompt)), (setvar 'cmdecho 0)... Then I inspected "li" variable - lastprompt and do things according to number of sub objects selected (for ex. (vl-cmdf "_.ERASE"))... But then again it's a long story and I don't want to post my code...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Re: Simulate left mouse click
« Reply #5 on: January 23, 2019, 09:37:31 AM »
Hi, its me again...
I want to revive this topic as it isn't solved yet...
Now to the story : beside simulation of pure left mouse click, I want to be able to automate this task :
I have list of 3D points and mesh entity I want to modify... Now, each point from point list represent corresponding vertex on mesh... So, I want to be able to pass this point list to the function that will grip select vertex of mesh and process all points from point list so that finally I have gripped with red marker all vetrices from mesh that were supplied in point list... I would then be able to stretch vertices all in one go simultaneusly... Now how can I do that... I don't want to click+shift on each vertex - I want to speed and possibly avoid mistakes by automating this task...

Thanks...
BTW. I find that stretch command is somewhat cumbersome as it don't have implemented selection by points in releation to current UCS... (ssget) works only according to current view direction, so if you are in 3D view, you just can't supply "_CP" or "_WP" method of (ssget) in relation of current UCS orientation - it's normal... So if you are in 3D - with "_CP" or "_WP" you supply, selection will be made to all entities that are inside polygon projection to current 3D view - not normal vector of UCS... If someone knows how to solve this it would be also very useful... I mean for selection, you can quickly change view to explan of UCS, get selection, restore views and then (sssetfirst nil (ssget "_P")), but if you want stretch, there is no way you can start command, change views to explan of UCS, and restore view to 3D with resuming stretching command... There is no way you can grip correct vertices that are inside "_WP" or "_CP" of explan of UCS and perform stretch command resume from restored 3D view... So some opinion, please, or direction of possible right path for solving this issues...

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Simulate left mouse click
« Reply #6 on: January 23, 2019, 04:02:38 PM »
Hi Marko,
After briefly reading your main request, perhaps its time to incorporate some (.dll) assemblies in order to finally achieve some mouse/cursor manipulation in LISP.
The suggested MouseOperations class from here (stackoverflow) by @radbyx (If I'm not mistaken) looks great, hence consider the following -

MouseOperations.cs
Code - C#: [Select]
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. // https://stackoverflow.com/questions/2416748/how-do-you-simulate-mouse-click-in-c
  5.  
  6. namespace SendMouseClick
  7. {
  8.  
  9.   public class MouseOperations
  10.   {
  11.     [Flags]
  12.     public enum MouseEventFlags
  13.     {
  14.       LeftDown = 0x00000002,
  15.       LeftUp = 0x00000004,
  16.       MiddleDown = 0x00000020,
  17.       MiddleUp = 0x00000040,
  18.       Move = 0x00000001,
  19.       Absolute = 0x00008000,
  20.       RightDown = 0x00000008,
  21.       RightUp = 0x00000010
  22.     }
  23.    
  24.     [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
  25.     [return: MarshalAs(UnmanagedType.Bool)]
  26.     private static extern bool SetCursorPos(int x, int y);
  27.    
  28.     [DllImport("user32.dll")]
  29.     [return: MarshalAs(UnmanagedType.Bool)]
  30.     private static extern bool GetCursorPos(out MousePoint lpMousePoint);
  31.    
  32.     [DllImport("user32.dll")]
  33.     private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
  34.    
  35.     public static void SetCursorPosition(int x, int y)
  36.     {
  37.       SetCursorPos(x, y);
  38.     }
  39.    
  40.     public static void SetCursorPosition(MousePoint point)
  41.     {
  42.       SetCursorPos(point.X, point.Y);
  43.     }
  44.    
  45.     public static MousePoint GetCursorPosition()
  46.     {
  47.       MousePoint currentMousePoint;
  48.       var gotPoint = GetCursorPos(out currentMousePoint);
  49.       if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
  50.       return currentMousePoint;
  51.     }
  52.    
  53.     public static void MouseEvent(MouseEventFlags value)
  54.     {
  55.       MousePoint position = GetCursorPosition();
  56.      
  57.       mouse_event
  58.       ((int)value,
  59.       position.X,
  60.       position.Y,
  61.       0,
  62.       0)
  63.       ;
  64.     }
  65.    
  66.     [StructLayout(LayoutKind.Sequential)]
  67.     public struct MousePoint
  68.     {
  69.       public int X;
  70.       public int Y;
  71.      
  72.       public MousePoint(int x, int y)
  73.       {
  74.         X = x;
  75.         Y = y;
  76.       }
  77.     }
  78.   }
  79.  
  80. }


LispFunctions.cs (and LISP usage examples included in the header)
Code - C#: [Select]
  1. using System.Collections.Generic;
  2.  
  3. using Autodesk.AutoCAD.Runtime;
  4. using Autodesk.AutoCAD.DatabaseServices; // Namespace for the ResultBuffer class
  5. using Autodesk.AutoCAD.EditorInput; // for the Editor
  6. using Autodesk.AutoCAD.Geometry;
  7.  
  8. namespace SendMouseClick
  9. {
  10.   public class LispFunctions
  11.   {
  12.        #region HeaderComments
  13.       /* Sample Lisp Calls (try in the drawing editor):
  14.           _$ (SendMouseClick) >> (669.0 667.0) ; Will return the mouse pixel coordinates
  15.           _$ (SendMouseClick '(669.0 667.0)) >> (669.0 667.0) ; Will move the the cursor to the specified pixel coordinate(s)
  16.           _$ (repeat 6  (SendMouseClick (mapcar '+ '(50. 20.) (SendMouseClick))) (vl-cmdf "_.delay" 200)) ; Will move your cursor
  17.           _$ (SendMouseClick '(700  400) "rightdown" "rightup") ; will go to the specified pixel coordinates, and will invoke the right clicks
  18.           (defun C:test nil (SendMouseClick "leftdown" "leftup")) ; will simulate left-clicks on invoking the 'test' command
  19.           (defun C:test nil (SendMouseClick "leftdown" "leftup" (mapcar '+ '(200 -200) (SendMouseClick)) "leftdown" "leftup")) ; will attempt an selection from 'BL' corner towards 'TR'
  20.           (defun InvokeRightClick nil (SendMouseClick "rightdown" "rightup"))
  21.           NOTE: You can read the mouse coordinates within using reactors (no need to invoke (grread t))
  22.  
  23.           Sample (test) Lisp Functions:
  24.          
  25.           (defun C:MoveCursorWithKeys ( / s k v )
  26.             (princ "\nPress [W/A/S/D] keys to move your cursor <exit>: ")
  27.             (if SendMouseClick
  28.               (while (not s) (mapcar 'set '(k v) (grread))
  29.                 (and (/= 2 k) (setq s t))
  30.                 (and (= 2 k)
  31.                   (SendMouseClick
  32.                     (mapcar '+ (SendMouseClick)
  33.                       (cond
  34.                         (
  35.                           (cdr
  36.                             (assoc (setq v (read (chr v)))
  37.                               '(
  38.                                 (A -5 0)
  39.                                 (D 5 0)
  40.                                 (W 0 -5)
  41.                                 (S 0 5)
  42.                               )
  43.                             )
  44.                           )
  45.                         )
  46.                         ( (setq s t) '(0 0) )
  47.                       )
  48.                     )
  49.                   )
  50.                 )
  51.               )
  52.             )
  53.             (princ)
  54.           )
  55.  
  56.  
  57.           (defun C:ClickOnOptions ( / app )
  58.             (setq app (vlax-get-acad-object))
  59.             (mapcar (function (lambda (x) (vla-put-WindowState app x))) (list acMin acMax))
  60.             (SendMouseClick '(10 10) "leftdown" "leftup" '(215 590) "leftdown" "leftup")
  61.             (princ)
  62.           )
  63.  
  64.       */
  65.       // (SendMouseClick "rightdown" "rightup" '(10 . 10) '(30 . 30)) // <<- dotted pairs as Point2d arguments, don't support them
  66.     #endregion HeaderComments
  67.     [LispFunction("SendMouseClick")]
  68.     public static TypedValue SendMouseClick(ResultBuffer rbArgs)
  69.     {
  70.       try
  71.       {
  72.         if (rbArgs != null)
  73.         {
  74.           bool isDottedPair = false;
  75.           List<int> coords = new List<int>();
  76.          
  77.           foreach (TypedValue tv in rbArgs)
  78.           {
  79.             switch (tv.TypeCode)
  80.             {
  81.               case (int)LispDataType.Text:
  82.               {
  83.                 switch (tv.Value.ToString().ToLower())
  84.                 {
  85.                   case "leftdown": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown); break; }
  86.                   case "leftup": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp); break; }
  87.                   case "middledown": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.MiddleDown); break; }
  88.                   case "middleup": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.MiddleUp); break; }
  89.                   case "move": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.Move); break; }
  90.                   case "absolute": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.Absolute); break; }
  91.                   case "rightdown": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightDown); break; }
  92.                   case "rightup": { MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightUp); break; }
  93.                   default: { break; }
  94.                 }
  95.                 break;
  96.               }
  97.               case (int)LispDataType.Point2d:
  98.               {
  99.                 Point2d p2d = (Point2d)tv.Value;
  100.                 try
  101.                 {
  102.                   MouseOperations.SetCursorPosition((int)p2d.X, (int)p2d.Y);
  103.                 }
  104.                 catch { }
  105.                 break;
  106.               }
  107.              
  108.               default: { break; }
  109.             }
  110.           }
  111.         }
  112.         MouseOperations.MousePoint pt = MouseOperations.GetCursorPosition();
  113.         return new TypedValue((int)LispDataType.Point2d, new Point2d(pt.X, pt.Y));
  114.       }
  115.       catch (System.Exception ex)
  116.       {
  117.         Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  118.         ed.WriteMessage(string.Format("\nError: {0}\nStackTrace: {1}", ex.Message, ex.StackTrace));
  119.        
  120.       }
  121.       return new TypedValue((int)LispDataType.Nil);
  122.     } // public static void SendMouseClick()
  123.   }
  124. }

NOTE:
I'm a .NET newbie, so the coding probably may be insecure or funny.. so a potential input from the .NET guys would be great!
And I know that this is the LISP section and I may be wrong on posting non-lisp codes here, but aside from grread or the Mouse*Click events, I don't know anything else regarding the mouse cursor.

EDIT:
Oh and I'm including a stripped code-commented blocks from the above code, so one might wonder why I have declared isDottedPair and the List<int> coords -
Code - C#: [Select]
  1.   /* NO ! I won't support dotted pairs as Point2d arguments
  2.   case (int)LispDataType.DottedPair:
  3.       {
  4.           isDottedPair = false;
  5.           coords.Clear();
  6.           break;
  7.       }
  8.   case (int)LispDataType.ListBegin:
  9.       {
  10.           isDottedPair = true;
  11.           coords.Clear();
  12.           break;
  13.       }
  14.   case (int)LispDataType.ListEnd:
  15.       {
  16.           isDottedPair = false;
  17.           coords.Clear();
  18.           break;
  19.       }
  20.   case (int)LispDataType.Int16:
  21.       {
  22.           if (isDottedPair)
  23.           {
  24.               if (Int32.TryParse(tv.Value.ToString(), out int coord))
  25.               {
  26.                   coords.Add(coord);
  27.               }
  28.               if (coords.Count == 2)
  29.               {
  30.                   try
  31.                   {
  32.                       MouseOperations.SetCursorPosition(coords[0], coords[1]);
  33.                   }
  34.                   catch { }
  35.                   isDottedPair = false;
  36.                   coords.Clear();
  37.               }
  38.           }
  39.           break;
  40.       }
  41.   */
« Last Edit: January 23, 2019, 04:07:25 PM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Re: Simulate left mouse click
« Reply #7 on: January 24, 2019, 02:30:20 AM »
Thanks Grrr for the input, but unforteunately I don't have installed Visual Studio or Sharp Develop to compile the code to *.dll... I suppose that then I would need to recompile code for various versions of AutoCAD (I often use A2014, and for something more demanding A2018)... So if you want you could do it, but I don't see how would you pass beside left click and also shift down key simulation - > grips have to be marked red color... For my last request I did find solution, but still cumbersome, for instance I'll explain it : you have to have 2 viewports; one with top view and one with 3D view from which you want to do stretching... Then you initiate stretch from top viewport and there you pass (ssget "_CP" ptlst) or "_WP" - I think "_CP" is better when stretching is task to be doing and after selection is acquired, you pass with mouse to 3D viewport and finish stretching there (of course in lisp you have to have (while (< 0 (getvar 'cmdactive)) (vl-cmdf "\\")) syntax after calling (vl-cmdf "_.stretch" (ssget "_CP" ptlst)) to be sure you can sucessfuly finish command)... But this is for me cumbersome as I used to work in one main single viewport, so I am open for something more closer to my way of working in CAD... I don't know if this all makes some sense, but I think that this tip and snippet may be useful... Now we'll see if something better could be implemented in newer CAD version, but as always I am not to optimistic - here all members are doing workarounds to achieve goals... Sad but true...
« Last Edit: January 24, 2019, 05:29:35 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Simulate left mouse click
« Reply #8 on: January 24, 2019, 04:51:02 AM »
I have list of 3D points and mesh entity I want to modify...
if it's a mesh why don't you just entmod it?

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Re: Simulate left mouse click
« Reply #9 on: January 24, 2019, 05:30:40 AM »
I have list of 3D points and mesh entity I want to modify...
if it's a mesh why don't you just entmod it?

Well I am little picky, I want to see how it looks while stretching...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Simulate left mouse click
« Reply #10 on: January 24, 2019, 08:09:34 AM »
Well I am little picky, I want to see how it looks while stretching...
use grread and entmod the entity on the fly

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Re: Simulate left mouse click
« Reply #11 on: January 24, 2019, 10:36:08 AM »
Well I am little picky, I want to see how it looks while stretching...
use grread and entmod the entity on the fly

You are persitent... But, no I won't entmod it using grread... I want to see stretching along Z axis of UCS which is impossible by grread... I could thogh try to simulate that, but no, pure stretching with preview is all needed on the fly...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3259
  • Marko Ribar, architect
Re: Simulate left mouse click
« Reply #12 on: August 06, 2021, 11:22:36 AM »
I want to revive this topic...
Has someone achieved something regarding this topic issue and my comments?
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube