how to extract coordinates from g code files

I have this text file (g code), with several lines. What I would like to do is read the lines considering only some of them and extract the X Y E coordinates from them. In particular, the text is divided into "sections": it contains the coordinates for 3D printing of 3 parts (3 types of MESH) and for each part the external wall (WALL-OUTER) and the internal matrix are identified. This is for each single LAYER deposited by the extruder. (MESH, WALL-OUTER and LAYER are keywords present in the text)
My goal is to extract the coordinates X Y E referred to
MESH: lattice1, dim elem 0.98, rotaz 30.stl
TYPE: WALL-OUTER
and this for every single LAYER
I want to extract coordinates from G lines between these keywords
I tried to use this script but there is something wrong
filename = 'file g.gcode';
output = strcat('modified', filename);
fid = fopen (filename, 'r');
fod = fopen (output, 'wt');
rgx = [';LAYER ^M\s+S(\S+) ;TYPE:WALL-OUTER ;MESH:lattice1, dim elem 0.98, rotaz 30.stl ^G1\s+X(\S+)\s+Y(\S+)\s+E(\S+)'];
percent = 0.2
text = readlines(filename);
while ~feof (fid);
str = fgetl(fid);
[spl, tkn] = regexp (str, rgx, 'split', 'tokens', 'once'); %seperates numbers from text
vec = str2double(tkn);%converts number (text) into number values
if numel(vec);
vec = vec + percent*numel(vec);
mod = sprintf ('G1 X%g Y%g E%g', vec);
fprintf ('og: %s\nnew: %s\n\n', str, mod);
spl {2, 1} = mod;
spl(cellfun(@isempty, spl)) = [];
end
fprintf (fod, '%s\n', spl{:})
end

4 comentarios

@Walter Roberson Good Morning, in your opinion can I modify to script above to obtain the result that I want? Grazie
Walter Roberson
Walter Roberson el 18 de Ag. de 2022
rgx has four (\S+) so it should be outputing four tokens, so vec should have four entries if the line matched. But your sprintf line only has three %g formats, so the format would have to be repeated automatically for the 4th entry. Are you sure that is what you want? And if it is, why not just code the 4th %g format for clarity ?
Ok I correct, only that the script does not work because it continues to output the same lines as the input g file, while I would like to extract the X Y E coordinates corresponding to the type WALL-OUTER of the part to be printed "lattice1, dim elem 0.98, rotaz 30.stl "and this for each LAYER
@Walter Roberson I extracted ALL the coordinates X Y E from the gcode file with this script
str = fileread('file g.gcode');
rgx = sprintf('\\s+%c([+-]?\\d+\\.?\\d*)','XYE');
tkn = regexp(str,rgx,'tokens');
mat = str2double(vertcat(tkn{:}))
but I want ONLY the coordinates that are written under the lines:
;MESH: lattice1, dim elem 0.98, rotaz 30.stl
;TYPE: WALL-OUTER

Iniciar sesión para comentar.

Respuestas (1)

Mathieu NOE
Mathieu NOE el 24 de Ag. de 2022
hello
tried a few things , this is my suggestion for the time being . Not 100% sure it's the best code , maybe someone else will see potential improvements.
have identified 75 "valid" sections as I assumed what we are loocking for are the data that follows the two lines (in that specific order) :
;TYPE:WALL-OUTER
;MESH:lattice1, dim elem 0.98, rotaz 30.stl
those 75 sets are stored individually in a cell array (mat)
hope it helps !
clc
clearvars
D=readlines('file g.gcode'); % read as string array
ixMESH=find(contains(D,';MESH:lattice1, dim elem 0.98, rotaz 30.stl')); % find the "MESH" lines
ixTYPE=find(contains(D,';TYPE:WALL-OUTER')); % find the "TYPE:WALL-OUTER" lines
eof = numel(D);
% I want ONLY the coordinates that are written under the lines:
% ;MESH: lattice1, dim elem 0.98, rotaz 30.stl
% ;TYPE: WALL-OUTER
% In fact portion of data is organized either after two consecutives lines :
%
% ;TYPE:WALL-OUTER
% ;MESH:lattice1, dim elem 0.98, rotaz 30.stl
% the other case being :
% example 1 :
% ;MESH:lattice1, dim elem 0.98, rotaz 30.stl
% G0 F1285.7 X186.285 Y123.676
% G0 X184.711 Y123.546
% G0 X181.241 Y123.546
% G0 X174.446 Y123.619
% G0 X171.19 Y123.546
% M204 S1000
% ;TYPE:WALL-OUTER
% example 2
% ;MESH:lattice1, dim elem 0.98, rotaz 30.stl
% G0 F9000 X184.711 Y123.684
% G0 X181.241 Y123.684
% G0 X174.431 Y122.806
% G0 X170.914 Y123.684
% ;TYPE:WALL-OUTER
% notice the distance between the two lines vary in case 2 which makes
% things a bit more complicated
%% for the time being the code below works (hopefully) for case 1 where lines come in this order (and consecutives)
% ;TYPE:WALL-OUTER
% ;MESH:lattice1, dim elem 0.98, rotaz 30.stl
ixTYPE(ixTYPE>ixMESH(end)) = []; % remove ixTYPE greater than last value of ixMESH
ixMESH(ixMESH<ixTYPE(1)) = []; % remove ixMESH below fist value of ixTYPE
% define which sections are candidates (mst have the two lines in
% consecutive order) :
ixTYPE_select = [];
for ci = 1:numel(ixTYPE)
ind = find(ixMESH>ixTYPE(ci),1,'first');
delta = ixMESH(ind) - ixTYPE(ci) ;
if delta == 1
ixTYPE_select = [ixTYPE_select ; ixTYPE(ci)];
end
end
% loop over the selected portion of file
for ci = 1:numel(ixTYPE_select)
if ci == numel(ixTYPE_select) % last file section until EOF
str = convertStringsToChars(D(ixTYPE_select(ci):eof,1)); % selected portion of text
else % previous sections
str = convertStringsToChars(D(ixTYPE_select(ci):ixTYPE_select(ci+1),1)); % selected portion of text
end
% your code expanded below
rgx = sprintf('\\s+%c([+-]?\\d+\\.?\\d*)','XYE');
tkn = regexp(str,rgx,'tokens');
% remove empty cells
emptyCells = cellfun('isempty', tkn);
tkn(emptyCells) = [];
% convert cell array of cells to one array
A = vertcat(tkn{:});
B = vertcat(A{:});
mat{ci,1} = str2double(B); % finally ! store the array in one cell (or whatever structure you prefer
end

4 comentarios

Federico Paolucci
Federico Paolucci el 25 de Ag. de 2022
Editada: Federico Paolucci el 25 de Ag. de 2022
Hi @Mathieu NOE thanks for your script! Below, I report a more accurate description of that I want (maybe my previous description is not 100% clear)
I want to obtain is a script capable of loading the g code and dividing it into a structure with cells containing the individual commands: I want to find the strings in which the outer contour of all layers is drawn, initialized by ;TYPE: WALL-OUTER, as a result of which all commands containing the token G1 at the beginning of the command are stored, followed by the X Y Z E coordinates of displacement and possibly also from F. Then, also commands with the token G0 are stored. I should therefore extract the absolute position coordinates, saved in a structure cataloged by layer to which it belongs. the points extracted from the g code must be ordered in order to have a closed path that can be represented by a polygon. the passage from one layer to the next must be indicated by the ;LAYER string, followed by the layer number starting from 0 and the height expressed in mm must be indexed in the first strings of the file through the comment; layer height, for example 0.2 mm. In the g code file there are the coordinates for each piece to be printed, they are distinguishable as the construction of the layer is preceded by the string ;MESH. In particular I want to extract the coordinates for the piece "lattice1, dim elem 0.98, rotaz 30", so it is necessary to locate the string ;MESH: lattice1, dim elem 0.98, rotaz 30.stl
Mathieu NOE
Mathieu NOE el 25 de Ag. de 2022
hello Federico
ok - need more time to diggest all these infos - but this gonna take a while to be finalized and checked ... this is probably a work for a subcontractor rather than some hobbyist (like myself) that spend a few minutes per day on this forum.
Thank you for your availability!
Mathieu NOE
Mathieu NOE el 26 de Ag. de 2022
maybe one thing you could do is paste your g code in word or excel and put some color where you want the data
that would help to visualize which sections you want to retrieve (sometimes more efficient than a long description)

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2022a

Preguntada:

el 18 de Ag. de 2022

Comentada:

el 26 de Ag. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by