tymol
tymol

Reputation: 1

Problem regarding converting text to g-code in C#

So I have a problem regardind converting text to G-Code commands (G-Code commands supported by GRBL firmware). I almost did a good conversion, but not really. My export function looks like this right now:

private void ExportButton_Click(object sender, EventArgs e)
        {
            // Step 1: Retrieve the displayed text
            string textToConvert = displayLabel.Text;
            if (string.IsNullOrWhiteSpace(displayLabel.Text))
            {
                MessageBox.Show("Text box is empty. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                // Step 2: Convert the text to outlines curves
                GraphicsPath textPath = new GraphicsPath();
                textPath.AddString(textToConvert, displayLabel.Font.FontFamily, (int)displayLabel.Font.Style, displayLabel.Font.Size, new Point(0, 0), StringFormat.GenericTypographic);

                // Step 4: Generate G-code instructions
                const float scaleToMM = 5.0f; // Convert pixels to millimeters
                string gcodeFilePath = "output.gcode";

                using (StreamWriter writer = new StreamWriter(gcodeFilePath))
                {
                    writer.WriteLine("G90"); // Use absolute coordinates mode
                    writer.WriteLine("G17");
                    writer.WriteLine("F200");
                    writer.WriteLine("M3 S50");

                    // Move to the starting point at (0,0)
                    writer.WriteLine("G0 X0 Y0");
                    writer.WriteLine("G1 X0 Y0");

                    PointF lastMoveToPoint = PointF.Empty;
                    PointF lastLetterEndPoint = PointF.Empty;

                    foreach (PointF point in textPath.PathPoints)
                    {
                        writer.WriteLine($"G1 X{point.X * scaleToMM:F2} Y{point.Y * scaleToMM:F2}");

                        lastMoveToPoint = point;
                        lastLetterEndPoint = point;
                    }

                    // Return to the origin point at (0,0) after finishing the word
                    writer.WriteLine("G1 X0 Y0");
                    writer.WriteLine("G0 X0 Y0");
                    writer.WriteLine("M5");
                }

                MessageBox.Show("G-code export complete.");
            }
        }

Besides the fact that is upside down (but it's not a problem for me right now), the cutting path isn't the greatest, letters are being cut at half or are left unfinished. What can I do to achieve a better cutting path? I looked over C# functions from GraphicsPath library and I saw that AddBezier might do the job but I don't know how to use it in this context.

An example when I try to convert the text "text" is shown here

Thank you!

I tried using the AddBezier function but with no success. Also, there are 2 G-Code commands, G2 and G3 which are used to do arcs clockwise and counterclockwise, but I didn't do something better either with them

Upvotes: 0

Views: 182

Answers (0)

Related Questions