Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Recommended Posts

I've looked around for a way to exit the script when the user hits the 'escape' key but failed to find a solution.

Can't use 'console.readKey' because Mastercam doesn't have a console.

I also found Mcamreturn (escape pressed) and looked into the UIevents

I found this but it looks like it's pressing the keys, not waiting for keys to be pressed.

//so, I press escape key 3 times or more....
for(int i=0;i<3;i++)
{
keybd_event(VK_ESCAPE, 0, 0, 0);
keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0);
}

Link to comment
Share on other sites
On 12/17/2022 at 9:41 PM, JKLINE said:

I've looked around for a way to exit the script when the user hits the 'escape' key but failed to find a solution.

Can't use 'console.readKey' because Mastercam doesn't have a console.

I also found Mcamreturn (escape pressed) and looked into the UIevents

I found this but it looks like it's pressing the keys, not waiting for keys to be pressed.

//so, I press escape key 3 times or more....
for(int i=0;i<3;i++)
{
keybd_event(VK_ESCAPE, 0, 0, 0);
keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0);
}

you can create a loop :

while(true)

{



//if u want t exit 

break;

};

 

or better exception handling

try

{





//if u want to exit

throw new System.Exception("Exit called");







}

catch(System.Exception e)

{



}

you can also define a function in the script then consume it, remember , in a script you are inside a class of sorts and can define functions in the class

//main script procedure
bool MyScriptProc()
{



//when u want to exit
return false;

}


//call the script function

MyScriptProc();

 

  • Like 1
Link to comment
Share on other sites
2 minutes ago, techbytesoftware.com said:
while(true)

{



//if u want t exit 

break;

};

While (escape key is not pressed){

--commit CHook--

}

return

 

This was my initial thought, but I can't get it to recognize when the escape key is pressed.

I don't want it to exit at a specific time, I want it to exit when the user presses escape.

Link to comment
Share on other sites
3 minutes ago, JKLINE said:

While (escape key is not pressed){

--commit CHook--

}

return

 

This was my initial thought, but I can't get it to recognize when the escape key is pressed.

I don't want it to exit at a specific time, I want it to exit when the user presses escape.

You have to wrangle the function using pinvoke for GetAsyncKeyState

try this

 

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
//exit when the escape key is pressed
		while ((GetAsyncKeyState(0x1B) & 0x8000) == 0)
					{
						
						
					}

 

Link to comment
Share on other sites

That looks like the exact thing I'm looking for :D

Thank you.

-- It doesn't work while waiting on user Input though.

"oh no, I've clicked the wrong geometry, I'd like to exit instead of having to go through the rest of the motions."

 

Thinking "if response == null, return"?

 

Link to comment
Share on other sites
1 minute ago, JKLINE said:

That looks like the exact thing I'm looking for :D

Thank you.

-- It doesn't work while waiting on user Input though.

"oh no, I've clicked the wrong geometry, I'd like to exit instead of having to go through the rest of the motions."

 

Thinking "if response == null, return"?

 

maybe this?

if(response == null)

{

//exit the loop

break;

}

 

Link to comment
Share on other sites
					var depth = -0.100;
                    var roughAngle = 15.0;
                    var finishAngle = 20.0;
					var selectedCutChain = ChainManager.GetMultipleChains("Select Geometry");
                    if (selectedCutChain == null)
                    {
                        return;
                    }
                    DialogManager.AskForNumber("Enter Depth", ref depth);
                    if (depth == null)
                    {
                        return;
                    }
                    DialogManager.AskForAngle("Enter Rough Angle", ref roughAngle);
                    if (roughAngle == null)
                    {
                        return;
                    }
                    DialogManager.AskForAngle("Enter Finish Angle", ref finishAngle);
                    if (finishAngle == null)
                    {
                        return;
                    }

Agreed. Except depth, roughAngle, and finishAngle will never be null

Link to comment
Share on other sites

Solution ::

                var depth = -0.100;
                var roughAngle = 15.0;
                var finishAngle = 20.0;
                var selectedCutChain = ChainManager.GetMultipleChains("Select Geometry");
                if (selectedCutChain == null)
                {
                    return;
                }
                var depthDialog = DialogManager.AskForNumber("Enter Depth", ref depth);
                if (depthDialog == 0)
                {
                    return;
                }
                    var roughAngleDialog = DialogManager.AskForAngle("Enter Rough Angle", ref roughAngle);
                if (roughAngleDialog == 0)
                {
                    return;
                }
                var finishAngleDialog = DialogManager.AskForAngle("Enter Finish Angle", ref finishAngle);
                if (finishAngleDialog == 0)
                {
                    return;
                }

 

Link to comment
Share on other sites
3 minutes ago, JKLINE said:

-- It doesn't work while waiting on user Input though.

 

If you need the UI to be responsive you need to run the message pump


  [
System.Runtime.InteropServices.StructLayout(
     System.Runtime.InteropServices.LayoutKind.Sequential,
    CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
            public struct POINT
            {
                public System.Int32 x;
                public System.Int32 Y;
            }
            [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
            public struct MSG
            {
                public IntPtr hwnd;
                [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
                public int message;
                public IntPtr wParam;
                public IntPtr lParam;
                [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
                public int time;
                public POINT pt;
            }
			[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool TranslateMessage([System.Runtime.InteropServices.In] ref MSG lpMsg);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int GetMessage(out MSG lpMsg, System.IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
						  MSG myMsg;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern System.IntPtr DispatchMessage([System.Runtime.InteropServices.In] ref MSG lpmsg);
//exit when the escape key is pressed
	
					
      while (GetMessage(out myMsg, System.IntPtr.Zero, 0, 0) > 0)
                    {
					if ((GetAsyncKeyState(0x1B) & 0x8000) != 0)
					{
						break;
					}
						
						
                       TranslateMessage(ref myMsg);
                       DispatchMessage(ref myMsg);
                    }
	

 

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...