how to extract coordinates from g code files

48 views (last 30 days)
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 Comments
Federico Paolucci
Federico Paolucci on 18 Aug 2022
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
Federico Paolucci
Federico Paolucci on 19 Aug 2022
@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

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 24 Aug 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 Comments
Mathieu NOE
Mathieu NOE on 26 Aug 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)

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!