Contents
- Index
- Previous
- Next
Example of Manually Created NC Code
This is an illustration of code for 2.5D machining. In 2.5D machining, the cutter plunges into the billet to the required depth. All cutting then takes place in the X-Y plane

The drawing shows a billet, X=110, Y=90 and the paths followed by the centre line of a 5mm slot drill cutting a slot 2mm deep and then a circle 3mm deep. This is followed by a 6mm slot drill cutting a rectangle 4mm deep. The work origin, X=0, Y=0, Z=0, is at the bottom left-hand corner of the drawing. Although not shown on the drawing, the billet is 12mm deep (Z).

The picture shows a pictorial view of the finished product. The top, left, front corner of the billet is the work origin, X=0, Y=0, Z=0.
Below is the code used by the mill to machine the product. The notes in and following the code explain its structure. Note: code would not normally contain the comments which are preceded by an apostrophe. Also the operation (Op) number comments preceded by an asterisk are optional but are included here to help the explanation.
%
*Date - 08 JAN 02
*Designer - PETER
*Name of Part - RECTANGLE AND CIRCLE
*Material - MDF
*BILLET LENGTH X - 110
*BILLET WIDTH Y - 90
*BILLET DEPTH Z - 12
*(OP 1 TOOL 5 5MM SLOT DRILL)
N10 T5 'Select tool.
N20 S2000 'Set speed.
N30 G0 Z6 'Rapid move to Safe Height for Horizontal Rapid Movement.
N40 G0 X69 Y45 'Rapid move across until above material entry point.
N50 G0 Z2 'Rapid move down to height for change to feed rate.
N60 G1 X69 Y45 Z-2 F75 'Feed into material to a depth of 2mm.
N70 G1 X41 Y45 Z-2 F300 'Cut the slot.
*(OP 2 TOOL 5 5MM SLOT DRILL)
N80 G0 Z6 'Rapid move up to Safe Height for Horizontal Rapid Movement.
N90 G0 X55 Y70 'Rapid move across until above material entry point.
N100 G0 Z2 'Rapid move down to height for change to feed rate.
N110 G1 X55 Y70 Z-3 F75 'Feed into material to a depth of 3mm.
N120 G2 X55 Y20 R25 F300 'Cut the circle: two G2 moves. Could define circle in one line using I J format code.
N130 G2 X55 Y70 R25 F300
N140 G0 Z6 'Rapid move up to Safe Height for Horizontal Rapid Movement.
N150 G0 Z50 'Rapid move up to Tool Change Height.
*(OP 3 TOOL 6 6MM SLOT DRILL)
N160 T6 'Select tool
N170 S2000 'Set speed
N180 G0 Z6 'Rapid move down to Safe Height for Horizontal Rapid Movement.
N190 G0 X10 Y10 'Rapid move across until above material entry point.
N200 G0 Z2 'Rapid move down to height for change to feed rate.
N210 G1 X10 Y10 Z-4 F75 'Feed into material to a depth of 4mm.
N220 G1 X100 Y10 Z-4 F300 'Cut the rectangle: four G1 moves.
N230 G1 X100 Y80 Z-4 F300
N240 G1 X10 Y80 Z-4 F300
N250 G1 X10 Y10 Z-4 F300
N260 G0 Z6 'Rapid move up to Safe Height for Horizontal Rapid Movement.
N270 G0 Z50 'Rapid move up to Tool Change Height.
%
Writing the Code
The code can be prepared in the WELmill NC code editor (under the Tools menu) and saved with the file extension wmc (eg rectangle and circle.wmc) in the MillData folder.
If the code is prepared with a word processor, it must be saved as a text file with no formatting information and with the file extension wmc (eg rectangle and circle.wmc).
Alternatively, specialised NC code editor software is available. Try an internet search for freebies. The EdgeCAM NC Editor is available for use for a year from www.pathtrace.com
The code files are saved somewhere convenient, for example, in the recommended folder called MillData.
Code Rules and Explanation
% sign: the code must always start with the % sign. Absolutely nothing else is allowed on this % sign line.
The end of the code listing should also be marked by a % sign. This is optional but its use will reassure a user that the code listing is complete.
The % sign can be placed on a line by itself after the last line of code, eg
N123 G0 Z50
%
or, it can be preceded by a line number, eg
N123 G0 Z50
N124%
And, in the latter case, it can have a comment on the same line, eg
N123 G0 Z50
N124% 'end of code
* sign: notes about the part can be put in a file header. Each line of the header must start with the * sign. Notes can also be inserted in the code listing but the line must start with *.
' sign: comments can be on a line of code, as shown, but must be preceded by this sign. Comments can be tabbed across, as in the example above.
N: code lines (blocks) may begin with N followed by a number. Line numbers may be omitted but the code is easier to read if they are used. In the example code above, while the N numbers increment in tens, the increments can be any size. The maximum number of lines (blocks) is 2^31 (2 to the power of 31 - a big number!).
The first six lines (blocks) of code must always have the pattern shown in the listing above:
First line (N10): declares the first tool number, always preceded by the letter T.
Second line (N20): declares the spindle speed, always preceded by the letter S.
Third line (N30): is rapid movement, G0, only in Z, to the Safe Height for Rapid Movement (see below for an explanation), 6mm in this case.
Fourth line (N40): is rapid movement, G0, only in X and Y, to the position above the billet where the tool will eventually enter the material.
Fifth line (N50): is rapid movement, G0, only in Z, towards the billet until the end of the tool is some small distance from its top surface, 2mm in this case, often called the Rapid Down To Distance (see below for an explanation).
Sixth line (N60): the tool moves, in Z only, into the billet until it is below the surface, in this case by 2mm (Z-2). This is a G1 cutting move at, in this case, 75mm/min (the F value). Notice that the X and Y values are the same as in the fourth line. Notice also that the feed rate, F, in the Z direction is 0.25 of the feed rate in the X-Y direction. This is a good rule of thumb.
In line N70 the slot is cut.
Study the remaining code and the comments.
N70 to N110: Note how the 5mm cutter moves from the end of the slot to the start of the circle.
N120 to N130: define the cutting of the circle. G2 means that the circle is cut in a clockwise (CW) direction. G3 would mean that the circle is cut counter-clockwise (CCW). Note that, because the R field format is being used, two lines (blocks) of code are required to define the circle. Each line of code gives a semicircle. In WELmill, the R field format allows an arc to have up to 180 degrees included angle. The arc can start anywhere and finish anywhere as long as the included angle is not greater than 180 degrees. (A circle can be split into three or more R field format arcs if that is more convenient.)
Instead of the R field format, I and J fields could be used. Only one line of code is required to fully define an arc up to and including a circle. The I and J values are the distances, in X and Y respectively, from the start of the arc to its centre and must be preceded by a minus sign when necessary.
N140 onwards: note how the tool change from the 5mm to the 6mm tool is handled.
N260 onwards: note how the file concludes.
Definitions
Safe Height for Rapid Movement: when the tool traverses rapidly above the billet to get above wherever it will eventually plunge into the material, it must do so without fouling the billet, vice, clamps, projections or obstacles. The end of the tool must always be at a Safe Height for Rapid Movement. This can be any convenient, safe positive Z distance above the top surface of the billet: the top surface is always Z=0.
Rapid Down To Distance: when the tool has done a Safe Height for Rapid Movement, it will traverse at rapid speed down towards the billet surface. The end of the tool must not hit and/or enter the billet at rapid speed. It must slow down to the feed rate at some small distance above the top of the billet. This distance is the Rapid Down To Distance and is always a positive Z value, a height above the billet.
Tool Change Height: this is any convenient height of the end of the tool above the top of the billet. It is determined by how much space is required to be able to remove the longest tool and its tool holder from the milling head. 50mm is about the minimum required. Do not make the tool change height so great that the milling head hits its upper end stop. Be specially careful if the top of the billet is high above the mill table.
Safety
Hitting End Stops: this must be avoided. If an end stop is hit, the controlling PC will continue to send movement information but the table or milling head will not move. As there is no positional feedback from the mill to the PC, no corrective action can be taken and thereafter there will be a mismatch between where the PC "thinks" the tool is and its actual position. The job is ruined and material is wasted.
Billet Size and Working Envelope
CAD Software as an Aid to NC Code Preparation
CNC File Specification for WELmill
PC Specification for Running WELmill
WELmill NC code file extension