Recording XY points from a webcam

Post anything you want to discuss with others about the software.

Recording XY points from a webcam

Postby uspl » Sat Mar 25, 2023 3:14 am

Hi

I am starting to learn Ucamcopy but in the interim I am just using regular polygonal shapes.
Is there a way to record a series of points from either the digitise or probe screens while using the webcam?
Planet CNC has this function built in but I don't want to licence another package.

In practice:
Open the camera window
Jog to the first point
Click "record"
Jog to the next point
Click "record"
Rinse and repeat then export point list to a DXF file

Thanks in advance for suggestions.
uspl
 
Posts: 9
Joined: Sun Oct 09, 2022 2:06 am

Re: Recording XY points from a webcam

Postby wmgeorge » Sat Mar 25, 2023 8:32 pm

Never heard of this feature but I am interested and following.
wmgeorge
 
Posts: 68
Joined: Wed Jan 11, 2023 11:55 pm

Re: Recording XY points from a webcam

Postby uspl » Sun Mar 26, 2023 12:44 am

@wmgeorge

I had some software that worked (sort of) with Mach3 years ago but cannot for the life of me find it now.

I am just getting to grips with UCCNC and was thinking maybe this could be done with a macro that uses a loop of message boxes to collect the data points by reading the DRO values and writing them to an array then outputing that array to a CSV file for import as a point list into CamBam. Does anyone know if the macro feature have the ability to write to an array?
uspl
 
Posts: 9
Joined: Sun Oct 09, 2022 2:06 am

Re: Recording XY points from a webcam

Postby uspl » Sun Mar 26, 2023 1:26 am

Through the power of ChatGPT I got the following code, that AI freaks me out, I would not want to be a coder when it really gets smart.

I asked it:
I want a C# macro that uses a message box to capture a series of input values then write those values to a CSV file
Can you help me


The reply was:
Sure! Here's an example of a C# macro that prompts the user to enter a series of values using message boxes and then writes those values to a CSV file:

I attached this to a button as Macro 20000 but unfortunately UCCNC gives me the very unhelpful result of:
Script error: M20000 has error(s) and cannot be executed.

Anyone have any ideas ?


Code: Select all
#define SHOW_MESSAGE_BOX

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

class Program
{
    static void Main(string[] args)
    {
        string fileName = "data.csv";

        // Initialize a list to hold the points
        List<Point> points = new List<Point>();

        // Prompt the user to enter the first point
        Point point = GetInputPoint("Enter point 1:");

        // Loop to ask the user if they want to add more points
        while (true)
        {
            points.Add(point);

            // Ask the user if they want to add another point
            DialogResult result = MessageBox.Show("Add another point?", "Input", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                point = GetInputPoint($"Enter point {points.Count + 1}:");
            }
            else
            {
                break;
            }
        }

        // Write the points to a CSV file
        using (StreamWriter writer = new StreamWriter(fileName))
        {
            foreach (Point p in points)
            {
                writer.WriteLine($"{p.X},{p.Y},{p.Value}");
            }
        }

        // Show a message box to confirm that the points were written to the file
        MessageBox.Show($"The points were written to the file {fileName}");
    }

    static Point GetInputPoint(string message)
    {
        int x;
        int y;
        int value;

        // Prompt the user to enter the X, Y, and Value values using message boxes
        #if SHOW_MESSAGE_BOX
        string inputX = MessageBox.Show($"{message}\nEnter X:", "Input", MessageBoxButtons.OKCancel).ToString();
        if (inputX == "Cancel")
        {
            Environment.Exit(0);
        }
        string inputY = MessageBox.Show("Enter Y:", "Input", MessageBoxButtons.OKCancel).ToString();
        if (inputY == "Cancel")
        {
            Environment.Exit(0);
        }
        string inputValue = MessageBox.Show("Enter Value:", "Input", MessageBoxButtons.OKCancel).ToString();
        if (inputValue == "Cancel")
        {
            Environment.Exit(0);
        }
        #else
        Console.WriteLine(message);
        string inputX = Console.ReadLine();
        string inputY = Console.ReadLine();
        string inputValue = Console.ReadLine();
        #endif

        // Try to parse the X, Y, and Value values as integers
        if (!int.TryParse(inputX, out x) || !int.TryParse(inputY, out y) || !int.TryParse(inputValue, out value))
        {
            // If the input is not valid, prompt the user again
            MessageBox.Show("Invalid input. Please enter integer values for X, Y, and Value.");
            return GetInputPoint(message);
        }

        return new Point(x, y, value);
    }
}

class Point
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Value { get; set; }

    public Point(int x, int y, int value)
    {
        X = x;
        Y = y;
        Value = value;
    }
}
uspl
 
Posts: 9
Joined: Sun Oct 09, 2022 2:06 am

Re: Recording XY points from a webcam

Postby uspl » Sun Mar 26, 2023 2:12 am

Yes, I do realise I am spamming myself :)

Updated code but still won't run. Any suggestions most gratefully received.

Code: Select all
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

public class Program
{
    public static void Main()
    {
        string filePath = "C:\\points.csv";
        List<string> pointData = new List<string>();

        bool morePoints = true;
        while (morePoints)
        {
            string xString = "";
            string yString = "";
            bool validX = false;
            bool validY = false;

            while (!validX)
            {
                xString = MessageBox.Show("Enter X value for point:", "Point Data Entry", MessageBoxButtons.OKCancel).ToString();

                if (xString == "Cancel")
                {
                    morePoints = false;
                    break;
                }

                double x;
                if (double.TryParse(xString, out x) && x >= 0 && x <= 999.99)
                {
                    validX = true;
                }
                else
                {
                    MessageBox.Show("Invalid input. Please enter a number between 0 and 999.99.");
                }
            }

            if (!morePoints)
            {
                break;
            }

            while (!validY)
            {
                yString = MessageBox.Show("Enter Y value for point:", "Point Data Entry", MessageBoxButtons.OKCancel).ToString();

                if (yString == "Cancel")
                {
                    morePoints = false;
                    break;
                }

                double y;
                if (double.TryParse(yString, out y) && y >= 0 && y <= 999.99)
                {
                    validY = true;
                }
                else
                {
                    MessageBox.Show("Invalid input. Please enter a number between 0 and 999.99.");
                }
            }

            if (!morePoints)
            {
                break;
            }

            pointData.Add(xString + "," + yString);

            DialogResult result = MessageBox.Show("Add another point?", "Point Data Entry", MessageBoxButtons.YesNo);
            if (result == DialogResult.No)
            {
                morePoints = false;
            }
        }

        if (pointData.Count > 0)
        {
            File.WriteAllLines(filePath, pointData);
            MessageBox.Show("Data saved to " + filePath);
        }
        else
        {
            MessageBox.Show("No data to save.");
        }
    }
}
uspl
 
Posts: 9
Joined: Sun Oct 09, 2022 2:06 am

Re: Recording XY points from a webcam

Postby uspl » Sun Mar 26, 2023 11:29 pm

SOLVED !!!

1. Never ever use C# when you can use VB
2. Found this little gem in the Macro detailed instruction file
If the first line of the macro code contains the #VB directive then the software uses the VisualBasic compiler to compile the macro instead of the C# compiler.
3. Don't overthink things, why prompt the user to enter co-ordinates when the machine knows exactly what they are?

And the result is this working code. Hope someone finds it useful, I just stuck it on a button and press that every time I want to record a point.

Code: Select all
#VB
Dim Xpos As Double = exec.GetXmachpos() ' Get the current machine coordinates
Dim Ypos As Double = exec.GetYmachpos() ' Get the current machine coordinates
Dim filePath As String = "C:\UCCNC\DXF.txt" 'Change this as suits your System
Dim file As System.IO.StreamWriter

MessageBox.Show(Xpos & " " & Ypos,"Point added to file")

file = My.Computer.FileSystem.OpenTextFileWriter(filePath, True)
file.WriteLine(Xpos & "," & Ypos &",0") 'Appended a trailing zero so CamBam will accept this as a valid point list file
file.Close()
uspl
 
Posts: 9
Joined: Sun Oct 09, 2022 2:06 am


Return to General discussion about the UCCNC software

Who is online

Users browsing this forum: Bing [Bot] and 22 guests