Hi,
I am hopign someone will take this on as a simple little project as it would be of great help to me.

I have a CNC plasma machine which cuts sheets which are about 4ft by 10ft. however, sometimes we only use about 4*4ft of the sheet. I would like to create a program that finds the maximum length the cutter goes, and then cuts the sheet off one inch or so further than that.
The machine is controlled by g code, which is stored in text like files.
the following explains the action of a line of code

N190 G00 X6.0000 Y1.2500
(line number) (command) (x position) (Y position)

I am looking to be able to have a program that reads through all of the lines of code and picks out the largest x value, and then adds the following lines.

G01 X(max) Y0.0000
G01 X(max) Y47.7500

any help or tutorials on this would be greatly appreciated, or if someone wanted to try this out as a project it would be even better. i have attached a sample g code file. it can be opened with notepad.

thanks

nick

Dani AI

Generated

Short expert addendum for . The task is easy to automate in C++. is right that a scripting language makes a quick proof-of-concept, but a small C++ utility produces a portable executable. and were on the money — below is a minimal, safe approach and a compact example that can be adapted to different machines.

Approach: stream the g-code file line-by-line, extract every X value with a regex that accepts uppercase/lowercase and decimals, track the largest X, add a safety margin, then write a copy that appends two travel moves at that computed X. Key cautions: create a backup before changing files, insert added moves before any program terminator, match the machine's preferred command format (rapid vs feed), and do a dry-run on scrap stock or in a simulator.

#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <limits>
#include <iomanip>

int main(int argc, char** argv) {
    if (argc < 2) { std::cerr << "Usage: " << argv[0] << " input.gcode [margin]\n"; return 1; }
    std::string inpath = argv[1];
    double margin = (argc > 2) ? std::stod(argv[2]) : 1.0; // safety gap in same units as file
    const double sheet_min_y = 0.0, sheet_max_y = 48.0; // set actual sheet extents

    std::ifstream in(inpath);
    if (!in) { std::cerr << "Can't open file\n"; return 2; }

    std::regex rx(R"([Xx]\s*([+-]?(?:\d+\.\d*|\.\d+|\d+)))");
    std::string line; double max_x = -std::numeric_limits<double>::infinity();
    while (std::getline(in, line)) {
        std::smatch m; auto s = line;
        while (std::regex_search(s, m, rx)) {
            double x = std::stod(m[1].str());
            if (x > max_x) max_x = x;
            s = m.suffix().str();
        }
    }
    if (!std::isfinite(max_x)) { std::cerr << "No X coordinates found\n"; return 3; }
    double cut_x = max_x + margin;

    in.clear(); in.seekg(0);
    std::ofstream out(inpath + ".withcut");
    while (std::getline(in, line)) out << line << '\n';
    out << std::fixed << std::setprecision(4);
    out << "G1 X" << cut_x << " Y" << sheet_min_y << '\n';
    out << "G1 X" << cut_x << " Y" << sheet_max_y << '\n';
    std::cout << "Wrote: " << inpath << ".withcut\n";
    return 0;
}

Notes: ensure the units in the file (inches vs mm) match the margin; handle negative coordinates if the origin is offset; increase regex robustness if the file contains unusual comment formats; round output to the controller precision. Always verify the modified file in a simulator or with a harmless dry-run before actual cutting.

Recommended Answers

All 3 Replies

Sounds like you need to post in Job Offers -- someone might help you if you want to pay $$$ for their work.

commented: So right! So right! I'm volunteering though..... +1

Sounds trivial, if you drop the requirement for it to be done in C++

Yeah, you probably should have posted this in job offers. I'm on top of it though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.