I'm running a CAM software package that uses C# macro coding to activate tasks.
I need to create a macro that will rename a tool loaded in my spindle. For example, if I had Tool #1 (T1) in the spindle when I shut the machine down, but the CAM software says I don't have a tool loaded on restart, I'd want to change the stated T0 on screen to T1. I'd like to press the button on my UCCNC screen and have it ask me what tool number I want to use for the renaming.
I have the following code someone generously created for me that generates the input box. However this is for a tool change, and not a tool renaming.
string val = exec.TextQuestion("Enter loaded tool number.");
string toolch = "M6 T" + val;
exec.Code(toolch);
which executes a tool change.
Here is C# code where I can manually enter T61 T(#) in the manual input line in UCCNC and specify T1 to get the loaded tool renamed to T1. This works fine but I'd like to attach this to a macro so I only have to push a screen button and then enter the tool number.
int newTool = exec.Getnewtool();
if (newTool < 0)
{
return;
}
else
{
exec.Setcurrenttool(newTool);
}
I've tried combing the two command sequences in numerous ways similar to what I've pasted below. None have worked though. All have given me a code error message in UCCNC.
string val = exec.TextQuestion("Enter loaded tool number.");
string Setcurrenttool(newTool); = "M61 T" + val;
exec.Setcurrenttool(newTool);
Could someone help me out here?
Thanks,
BH
Nikunj SatasiyaPosted Jul 27, 2024, 7:36 PM
Hi BH,
It looks like you're trying to combine the code for asking a user input and setting the current tool in the UCCNC software using C#. Let's correct and refine the code snippet to achieve your goal of renaming the tool via a macro.
From your description, it sounds like you want to:
Here's a corrected version of the macro code that should work for this purpose:
Explanation:
exec.TextQuestion("Enter loaded tool number."): This line will display a dialog box asking the user to input the tool number. The response is stored in the
userInputvariable.int.TryParse(userInput, out int newToolNumber): This attempts to convert the user input (a string) into an integer. If successful,
newToolNumberwill hold the integer value, and the method returnstrue. If the conversion fails (e.g., if the user enters a non-numeric value), it returnsfalse.exec.Setcurrenttool(newToolNumber): This line is called only if the input conversion is successful. It sets the current tool to the user-specified number.
exec.Message("Invalid tool number. Please enter a valid number."): This is an error handling mechanism to inform the user if the entered value was not a valid number.
This revised macro should more reliably prompt the user for a tool number and set it as the current tool without trying to change the tool as your previous attempt did. Make sure to test this in a safe environment to ensure it interacts with the UCCNC software as expected.