% Written by Francis Esmonde-White (francis@esmonde-white.com), comments added 28 December, 2010.
% Based on http://www.mathworks.com/matlabcentral/fileexchange/7118-readdxf-read-in-an-ascii-dxf-file

INPUT_FILENAME = 'logo2.dxf';
OUTPUT_FILENAME = 'ewlogo.gc';

fid = fopen(INPUT_FILENAME); % this opens the DXF file written by Inkscape
goodtext = fread(fid); % read in the whole DXF file
fclose(fid); % close the file.

line_ind=strfind(goodtext',['LINE', char(10)]); % find all lines in the DXF files that have the text "LINE"

merged_ind=sort([line_ind, numel(goodtext)]); % make a list of all the "LINE" positions, end it with the total number of lines

figure; % Open a figure window
hold on; % add lines to the figure, rather than replace them.

fid = fopen(OUTPUT_FILENAME,'w');  % Open the G-code file to export to
fprintf(fid,'G00 Z0.5\n'); % Print the first line of the G-code file (this raises the cutting head to +0.5 on the z-axis

for segment_ind=1:(numel(merged_ind)-1); % for each segment in the DXF file
    segment=goodtext(merged_ind(segment_ind):(merged_ind(segment_ind+1)-1)); % Find the length of text for the current line segment
    if goodtext(merged_ind(segment_ind)-1)=='P' % If this segment includes a point statement
        disp('SPLINE')
        xpos_ind=strfind(segment',char([49,    48,    10])); % find the X coordinate
        ypos_ind=strfind(segment',char([50,    48,    10])); % find the Y coordinate
        
        badx=[];
        bady=[];
        for i=1:numel(xpos_ind);
            xt=segment(xpos_ind(i)-1);
            if ((xt~=10)&(xt~=32))
                badx=[badx,i];
            end
        end        
        for i=1:numel(ypos_ind);
            yt=segment(ypos_ind(i)-1);
            if((yt~=10)&(yt~=32))
                bady=[bady,i];
            end
        end
        xpos_ind(badx)=[];
        ypos_ind(bady)=[];

        xpos_ind=xpos_ind+3;
        ypos_ind=ypos_ind+3;

        for i=1:numel(xpos_ind);
            xpos_end(i)=find(segment(xpos_ind(i)+(1:15))==10,1,'first');
            ypos_end(i)=find(segment(ypos_ind(i)+(1:15))==10,1,'first');
        end

        xpos_end=xpos_end+xpos_ind-1;
        ypos_end=ypos_end+ypos_ind-1;
        
        clear xpos ypos

        for i=1:numel(xpos_ind);
            xpos(i)=str2num(char(segment(xpos_ind(i):xpos_end(i))')); % read in the X coordinate
            ypos(i)=str2num(char(segment(ypos_ind(i):ypos_end(i))')); % read in the Y coordinate
        end

        xpos=xpos/25.4; % convert the x position from millimeters to inches
        ypos=ypos/25.4; % convert the y position from millimeters to inches

        plot(xpos,ypos,'r-') % plot the new line segment

    else
        disp('LINE') % this statement includes a line segment
        x1pos_ind=strfind(segment',char([10, 49,    48,    10]))+3;
        y1pos_ind=strfind(segment',char([10, 50,    48,    10]))+3;

        x2pos_ind=strfind(segment',char([10, 49,    49,    10]))+3;
        y2pos_ind=strfind(segment',char([10, 50,    49,    10]))+3;

        x1pos=str2num(char(segment(x1pos_ind+(0:9))'));
        y1pos=str2num(char(segment(y1pos_ind+(0:9))'));
        x2pos=str2num(char(segment(x2pos_ind+(0:9))'));
        y2pos=str2num(char(segment(y2pos_ind+(0:9))'));

        fprintf(fid,'G01 X%8.6f  Y%8.6f\n',x1pos/25.4,y1pos/25.4); % output the new x & y coordinate for the beginning of the line
        fprintf(fid,'G01 Z0.00\n'); % bring the milling head down to z = 0
        fprintf(fid,'G01 X%8.6f  Y%8.6f\n',x2pos/25.4,y2pos/25.4);  % output the new x & y coordinate for the end of the line
        fprintf(fid,'G00 Z0.5\n'); % bring the milling head back up to z = +0.5

        plot([x1pos,x2pos]/25.4,[y1pos,y2pos]/25.4,'b-') % display the new line in the figure

    end
end

fclose(fid); % close the g-code file
