OOFEM 3.0
Loading...
Searching...
No Matches
GCodeParser.h
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <sstream>
4#include <string>
5#include <vector>
6#include <map>
7#include <variant>
8#include <optional>
9
10#include "GCodeCommand.h"
11
16{
17public:
23 std::vector<GCodeCommand> parseFile(const std::string &filename)
24 {
25 std::ifstream file(filename);
26 std::vector<GCodeCommand> commands;
27
28 if (file.is_open())
29 {
30 std::string line;
31 while (std::getline(file, line))
32 {
33 removeComments(line); // Remove comments from the line
34 if (!line.empty())
35 {
36 GCodeCommand command = parseLine(line);
37 commands.push_back(command);
38 }
39 }
40 file.close();
41 }
42 else
43 {
44 std::cerr << "Unable to open file: " << filename << std::endl;
45 }
46
47 return commands;
48 }
49
50private:
55 void removeComments(std::string &line)
56 {
57 size_t pos = line.find(';');
58 if (pos != std::string::npos)
59 {
60 line.erase(pos);
61 }
62 }
63
69 GCodeCommand parseLine(const std::string &line)
70 {
71 GCodeCommand command;
72 std::istringstream iss(line);
73 std::string token;
74 iss >> token; // Extract the G-code command (e.g., G1, M104)
75
76 if (!token.empty())
77 {
78 command.setCode(token);
79
80 while (iss >> token)
81 {
82 char letter = token[0];
83 std::string value = token.substr(1);
84 command.addParameter(letter, value);
85 }
86 }
87
88 return command;
89 }
90};
The GCodeCommand class represents a single G-code command.
void setCode(const std::string code)
Set the G-code command.
void addParameter(char letter, const std::string &value)
Method to add a parameter to the G-code command.
The GCodeParser class is used to parse G-code files.
Definition GCodeParser.h:16
std::vector< GCodeCommand > parseFile(const std::string &filename)
Parses a file and returns a vector of GCodeCommand objects.
Definition GCodeParser.h:23
GCodeCommand parseLine(const std::string &line)
Parses a line of G-code and returns a GCodeCommand object.
Definition GCodeParser.h:69
void removeComments(std::string &line)
Removes comments from a line of G-code.
Definition GCodeParser.h:55

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011