RSS

Edit numeric values in Solidworks programmatically

22 Feb

The task is to integrate speech recognition results of cardinal numbers into the modification of model parameters, for instance, the radius of the circle under edition. It is farily easy to use the off-the-shelf cardinal grammar, however the challenge is to update the parameter value in Solidworks.

Solidworks did not offer the handle/reference of the numerical edit box (or the numerical spin box) on the PropertyManagerPage for such purposes. The first task is to get the edit box handle on the fly. There are two approaches to do so:

  1. [DllImport(“user32.dll”)]
    public static extern IntPtr GetForegroundWindow();
    which returns the active window handle.
  2. [DllImport(“user32.dll”)]
    public static extern bool GetCursorPos(ref Point lpPoint); // In Screen coordinate
    [DllImport(“user32”)]
    public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

    Example Code:
    Point P = new Point();
    Utility32.GetCursorPos(ref P);
    IntPtr hwnd = Utility32.WindowFromPoint(P.X, P.Y);

After the handle is obtained, we can set its value using the SetWindowText API, the problem is you can hardly update the value to notify Solidworks such changes. That is, you could not call CEdit::UpdateData(TRUE); !

While you can easily edit the value using keyboard, so… the idea is to simulate the key typing programmatically:
Utility32.SetFocus(hwnd); //P-Invoke
System.Windows.Forms.SendKeys.SendWait(v.ToString()+”\r”); //Simulate key typing. “\r”=Enter

Done! Code Snippet:
Point P = new Point();
Utility32.GetCursorPos(ref P);
IntPtr hwnd = Utility32.WindowFromPoint(P.X, P.Y);
if(hwnd != IntPtr.Zero)
{
Utility32.SetFocus(hwnd);
System.Windows.Forms.SendKeys.SendWait(v.ToString()+”\r”); //Simulate key typing.
//SetWindowText won’t do, because no DDX update is done, while key typing simulations will
//invoke the internal data exchange and update.
}

Advertisements
 
2 Comments

Posted by on February 22, 2008 in CAD

 

2 responses to “Edit numeric values in Solidworks programmatically

  1. Xinyu

    February 27, 2008 at 4:52 am

    Note that the grammar rule for cardinal numbers can be found in Library.grxml in Office Communication Server sub folders (search there and it will be found). Then select the cardinal grammar and paste it into a new grammar. That is it.

     
  2. xinyustudio

    September 27, 2008 at 8:13 pm

    To not only change the text of the edit control, but also select all of the text so that the next input will replace the existing text (instead of appending to the tail), use the code below:

    //Ctrl A: select all of the edit/textbox;
    System.Windows.Forms.SendKeys.SendWait(“^{A}”);

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: