Code covered by the BSD License  

Highlights from
CNCCoder

image thumbnail

CNCCoder

by

 

This is a basic CNC coder which allow user to write CNC codes.

newCNC
function newCNC
figure('MenuBar','none','Name','CNCReader','NumberTitle','off','Position',[50, 100, 1200, 600]);
% 
CNCCode=[];


%Txt1 = uicontrol('Style','Text','Position',[200,100,400,400]);
Txt2 = uicontrol('Style','Text','Position',[700,100,400,400]);
InitializePressed =uicontrol('Style','PushButton','String','Initialize','Position',[20,500,110,20],...
  'CallBack',@IPressed);
% This is for clicking on txtbox
ListBox =uicontrol('Style','ListBox','String',CNCCode,'Position',[200,100,400,400],... 
    'CallBack', @ListBoxPressed);

StopTheProgram=uicontrol('Style','PushButton','String','Stop the program','Position',[20,480,110,20],...
  'CallBack',@StopTheProgramPressed);

EndofProgram=uicontrol('Style','PushButton','String','End of Program','Position',[20,460,110,20],...
  'CallBack',@EndofProgramPressed);
SpindleClockwise=uicontrol('Style','PushButton','String','Spindle Clockwise','Position',[20,440,110,20],...
  'CallBack',@SpindleClockwisePressed);
SpindleCClockwise=uicontrol('Style','PushButton','String','Spindle C.Clockwise','Position',[20,420,110,20],...
  'CallBack',@SpindleCClockwisePressed);
SpindleOff=uicontrol('Style','PushButton','String','Spindle Off','Position',[20,400,110,20],...
  'CallBack',@SpindleOffPressed);
CoolantOn=uicontrol('Style','PushButton','String','Coolant On','Position',[20,380,110,20],...
  'CallBack',@CoolantOnPressed);
CoolantOff=uicontrol('Style','PushButton','String','Coolant Off','Position',[20,360,110,20],...
  'CallBack',@CoolantOffPressed);
RapidMove= uicontrol('Style','PushButton','String','Rapid move','Position',[20,340,110,20],...
   'CallBack',@RapidMovePressed);
FeedUpDown=uicontrol('Style','PushButton','String','Feed Up/Down','Position',[20,320,110,20],...
  'CallBack',@FeedUpDownPressed);
FeedDiagon=uicontrol('Style','PushButton','String','Feed Diagonally','Position',[20,300,110,20],...
  'CallBack',@FeedDiagPressed);
Arc= uicontrol('Style','PushButton','String','Arc','Position',[20,280,110,20],...
  'CallBack',@ArcPressed);
SetX=uicontrol('Style','PushButton','String','Set X','Position',[20,260,110,20],...
  'CallBack',@SetXPressed);
SetY=uicontrol('Style','PushButton','String','Set Y','Position',[20,240,110,20],...
  'CallBack',@SetYPressed);
Dwell=uicontrol('Style','PushButton','String','Dwell','Position',[20,220,110,20],...
  'CallBack',@DwellPressed);
uicontrol('Style','PushButton','String','CLEAR ALL!','Position',[20,200,110,20],...
  'CallBack',@CLEARALL);
SaveCNC=uicontrol('Style','PushButton','String','Copy Commands','Position',[20,180,110,20],...
  'CallBack',@SaveCNCPressed);
About=uicontrol('Style','PushButton','String','About','Position',[20,160,110,20],...
  'CallBack',@AboutPressed);
uicontrol('Style','PushButton','String','Exit','Position',[20,140,110,20],...
  'CallBack',@ExitPressed);
function IPressed(h, eventdata)
prompt = {'WorkSpiece X','WorkSpiece Y','WorkSpiece Z','Tool','X startpoint','Y startpoint','Z startpoint','[I]nch/[M]ilimiter','[A]bsolute/[I]ncrement'};
dlg_title = 'WorkSpiece Size [X Y Z], Tool, Startpoints, ';
num_lines = 1;
%       Xn Yn  Zn  Tool Xs  Ys  Zs  Inch/Milimeter
def = {'1','1','1','3','0','0','0','I','A'};
%       1   2   3   4   5   6   7   8
global XnN;
global YnN;
global ZnN;
global XsN;
global YsN;
global ZsN;

answer = inputdlg(prompt,dlg_title,num_lines,def);
  Xn=answer{1};
  Yn=answer{2};
  Zn=answer{3};
  Xs=answer{5};
  Ys=answer{6};
  Zs=answer{7};
 InchCen=answer{8};
       
        GetTool=strcat('M06 T',answer{4});
         CNCCode=strvcat(CNCCode,GetTool);

XnN=str2num(Xn);
XsN=str2num(Xs);
YnN=str2num(Yn);
YsN=str2num(Ys);
ZsN=str2num(Zs);
ZnN=str2num(Zn);

         if ((XsN>XnN) |(YsN>YnN) | (ZsN>ZnN)) %#ok<OR2>
            msgbox('Data entered is Not Valid!')
            return
         end
        if CNCCode==' ';
            CNCCode=[];
        end
        if (answer{9}=='I') ||(answer{9}=='i')
            CNCCode=strvcat(CNCCode,'G91 ');
            SystemInfo='Increment';
        else
            SystemInfo='Absolute';
            CNCCode=strvcat(CNCCode,'G90 ');
        end
        
        if (InchCen=='I') ||(InchCen=='i')
            CNCCode=strvcat(CNCCode,'G20 ');
            SystemMeasure='Inch';
        else
            SystemMeasure='Milimeter';
            CNCCode=strvcat(CNCCode,'G21 ');
        end
        
        button = questdlg('Working Plane?','title','XY','YZ','XZ','XY');
        switch button
            case 'XY'
                plan='G17';
            case 'YZ'
                plan='G19';
            case 'XZ'
                plan='G18';
        end
promptInfo0='Workspiece:';
promptInfo1=['Xn=',Xn,' Yn=',Yn,' Zn=',Zn];
p3=' ';
promptinfo2='Start Points:';
promptInfo=['Xs=',Xs,' Ys=',Ys,' Zs=',Zs];
p4=' ';
Tool=['Tool#',answer{4}];
sye=['System Measure: ',SystemMeasure];
ppp=strvcat(promptInfo0,promptInfo1,p3,promptinfo2,promptInfo,p4,Tool,sye,'Plane:',button,SystemInfo);
set(Txt2,'String',ppp);

CNCCode=strvcat(CNCCode,plan);

%msgbox (ppp,'Info');

    set(ListBox, 'String',CNCCode );
end  
  function StopTheProgramPressed(h, eventdata)
   CNCCode=strvcat(CNCCode,'M0');
      set(ListBox, 'String',CNCCode );
  end
function EndofProgramPressed(h, eventdata)
   CNCCode=strvcat(CNCCode,'M30');
      set(ListBox, 'String',CNCCode );
end
function SpindleClockwisePressed(h, eventdata)
 rpm=[];
   numlines=1 ;
rpm = inputdlg('Speed:','Speed',numlines);

        speed=strcat('M3 S',rpm{1});
        CNCCode=strvcat(CNCCode,speed);
    
      set(ListBox, 'String',CNCCode );

end
function SpindleCClockwisePressed(h, eventdata)
 rpm=[];
   numlines=1 ;
rpm2 = inputdlg('Speed:','Speed',numlines);

        speed2=strcat('M4 S',rpm2{1});
        CNCCode=strvcat(CNCCode,speed2);
    
      set(ListBox, 'String',CNCCode );

end

function SpindleOffPressed(h, eventdata)
      CNCCode=strvcat(CNCCode,'M5');    
      set(ListBox, 'String',CNCCode );

end
function CoolantOnPressed(h, eventdata)
      CNCCode=strvcat(CNCCode,'M8');    
      set(ListBox, 'String',CNCCode );

end
function CoolantOffPressed(h, eventdata)
      CNCCode=strvcat(CNCCode,'M9');    
      set(ListBox, 'String',CNCCode );

end
function RapidMovePressed(h, eventdata)
global XnN;
global YnN;
global ZnN;
global XsN;
global YsN;
global ZsN;
    prompt = {'X','Y'};
dlg_title = 'Rapid Move To [X,Y] ';
num_lines = 1;
def = {'1','1'};
answ = inputdlg(prompt,dlg_title,num_lines,def);    
    
        if ((str2num(answ{1})>XnN)|(str2num(answ{2})>YnN))
            msgbox('Out of range')
            return
        end
        Rapid=['G00 X',answ{1},' Y',answ{2}];
        CNCCode=strvcat(CNCCode,Rapid);  
      
      set(ListBox, 'String',CNCCode );

end
    function FeedUpDownPressed(h,eventdata)
        global ZnN;
        prompt={'Z','FeedRate'};
        dlg_title='Feed Up/Down';
        numines=1;
        AnsFeed=inputdlg(prompt,dlg_title,numines);
        
       if (str2num(AnsFeed{1})>ZnN)
            msgbox('Out of range')
        return
        end      
        AnsFeed{1},AnsFeed{2}
        CNCCode=strvcat(CNCCode,strcat('G01 Z',AnsFeed{1},' F',AnsFeed{2}));
        set(ListBox, 'String',CNCCode );
    end;
    function FeedDiagPressed(h,eventdata)
    prompt={'X','Y','FeedRate'};
    dlg='Feed Diagonaly';
   
        AnsFeedD=inputdlg(prompt,dlg,1);
        global XnN;
        global YnN;
        xr=AnsFeedD{1};
        yr=AnsFeedD{2};
        if (str2num(xr)>XnN)|(str2num(yr)>YnN)
            msgbox('Out of range')
        return
        end
        Ra=['G01 X',xr,' Y',yr];
        CNCCode=strvcat(CNCCode,strcat(Ra,' F',AnsFeedD{3}));
        set(ListBox,'String',CNCCode);
    end

    function ArcPressed(h,eventdata)
        prompt={'X','Y','R','FeedRate','1-ClockWise/ 2-CClockWise'};
        global XnN;
        global YnN;
        dlg='Arc';
        AnsArc=inputdlg(prompt,dlg,1);
        xr=AnsArc{1};
        yr=AnsArc{2};
        CW=AnsArc{5};
        if (str2num(xr)>XnN)|(str2num(yr)>YnN)
            msgbox('Out of range')
        return
        end
        
        
        
        if (CW=='1' );
            C='G02';
        else (CW=='2');
            C='G03';
        end
        Rapid=[C,' X',xr,' Y',yr, ' R',AnsArc{3}, ' F',AnsArc{4}];
        CNCCode=strvcat(CNCCode, Rapid);
        set(ListBox,'String',CNCCode);
    end
    function SetXPressed(h,eventdata)
    prompt={'X'};
    dlg=' Set X';
    X=inputdlg(prompt,dlg,1);
    
     global XnN;
        if (str2num(X{1})>XnN)
            msgbox('Out of range')
        return
        end
        SetX=['X',X{1}];
        CNCCode=strvcat(CNCCode,SetX);
        set(ListBox,'String',CNCCode);
    end
    function SetYPressed(h,eventdata)
    prompt={'Y'};
    dlg=' Set Y';
    Y=inputdlg(prompt,dlg,1);
    
     global YnN;
        if (str2num(Y{1})>YnN)
            msgbox('Out of range')
        return
        end
        SetY=['Y',Y{1}];
        CNCCode=strvcat(CNCCode,SetY);
        set(ListBox,'String',CNCCode);
    end
    function DwellPressed(h,eventdata)
        prompt={'Dwell time in Milisecond:'};
        dlg='Dwell';
        def={'500'};
        P=inputdlg(prompt,dlg,1,def);
        vv=['G04 P',P{1}];
        CNCCode=strvcat(CNCCode, vv);
    set(ListBox,'String',CNCCode);
    end
    function CLEARALL(h,eventdata)
q=questdlg('Do you want to Clear All?','Warning','Yes','No','No');
if strcmp(q,'Yes')
    CNCCode='';
 set(ListBox,'String',CNCCode);
 set(Txt2,'String','');
end
    end
    
    function SaveCNCPressed(h,eventdata)
    clc
    CNCCode
        msgbox('Data Copied to Command Window')

    end

    function ListBoxPressed(varargin)
    
    List = get(ListBox, 'String');
     Val = get(ListBox, 'Value');
     hitch=CNCCode(Val,:);
     str=[ 'Remove ',hitch];
     button = questdlg(str,'Warning!');
     if button=='Yes'
         CNCCode(Val,:)=' ';
     end
     set(ListBox,'String',CNCCode);
        
    end
    function AboutPressed(h,eventdata)
        a='Copyright (c) 2011';
        b='Sagheb Kohpayeh Araghi';
        c='MultiMedia University ';
        d='Faculty of Engineering';
        f=strvcat(a,b,c,d);
        msgbox (f,'About');
    end

    function ExitPressed(h,eventdata)
     button = questdlg('Do you want to quit?','Warning!');
     if strcmp(button,'Yes')
         close all
     end
     
        
    end
    
end

Contact us