Industrial Training - The Best for Less

Fanuc "G" Codes

(Excerpt from Newsgroups: alt.lasers)

A generic Fanuc post processor file that contains it's syntax or whatever for G codes, misc.

# Machine Name : GENERIC FANUC
# Control Name : GENERIC FANUC
# Description : GENERIC FANUC MILL POST
# Associated Post : NONE
# Mill/Turn : NO
# 4-axis/Axis subs. : YES
# 5-axis : NO
# Executable : MP 7.04
#
# WARNING: THIS POST IS GENERIC AND IS INTENDED FOR MODIFICATION TO
# THE MACHINE TOOL REQUIREMENTS AND PERSONAL PREFERENCE.
#
# --------------------------------------------------------------------------
# Revision log:
# --------------------------------------------------------------------------
# Programmers Note:
#
# --------------------------------------------------------------------------
# Features: 
# --------------------------------------------------------------------------
# This post supports Generic Fanuc code output for 3 and 4 axis milling.

# Following Misc. Integers are used:
#
# mi1 - Work coordinate system 
# 0 = Reference return is generated and G92 with the 
# X, Y and Z home positions at file head.
# 1 = Reference return is generated and G92 with the 
# X, Y and Z home positions at each tool.
# 2 = WCS of G54, G55.... based on Mastercam settings.
#
# mi2 - Absolute, or Incremental positioning
# 0 = absolute
# 1 = incremental
#
# mi3 - Select G28 or G30 reference point return.
# 0 = G28, 1 = G30
#
#Canned text:
# Entering cantext on a contour point from within Mastercam allows the
# following functions to enable/disable.
# Cantext value:
# 1 = Ostop = output the "M01" optional stop code
# 2 = Stop = output the "M00" stop code
# 3 = Bld on = turn on block delete codes in NC lines
# 4 = bLd off = turn off block delete codes in NC lines
#
#Milling toolpaths (4 axis)
#Layout:
# The term "Reference View" refers to the coordinate system associated
# with the Top view (Alt-F9, the upper gnomon of the three displayed).
# Create the part drawing with the the axis of rotation about the axis
# of the "Reference View" according to the setting you entered for
# 'rot_on_x'. The Top View (Z plus of the Reference View) indicates
# the position on the part of rotary axis zero. The top view rotated
# about the selected axis as a "single axis rotation" are the only
# legal views for 4 axis milling. Rotation direction around the part
# is positive in the CCW direction when viewed from the plus direction
# of the rotating axis. Set the variable 'rot_ccw_pos' to indicate
# the signed direction. Always set the work origin at the center of
# rotation.
#
#Toolplane Positioning:
# 1) Create the Cplane and Tplane as the rotation of the Top View about 
# the selected axis of the "Reference View". The toolplane is used
# to calculate the position of the rotary axis.
#
#Axis substitution:
# 1) Use the Rotary axis substitution by drawing the geometry flattened
# from the cylinder. Positions are wrapped from and to the cylinder
# by using the Roll function. Rapid positions in the NC parameters
# are relative to the chained geometry. The rotary axis button must
# be active for axis substitution information to be output to the 
# NCI file. The radius of the rotary diameter is added to the all the
# Z position at output. 
# 2) Drill positions can also be substituted in this function.
#
#Rotary 4 axis:
# 1) 4 axis toolpaths can be generated from the Multisurf Rotary 4 axis
# function. Use the Top View for the toolplane and the Cplane to define
# the step over slicing plane (uses depth).
#
#Additional Notes:
# 1) G54 calls are generated where the work offset entry of 0 = G54,
# 1 = G55, etc.
# 2) Metric is applied from the first NCI met_tool variable.
# 3) Incremental mode calculates motion from home position at toolchanges.
# The home position is used to define the last position of the tool
# for all toolchanges.
#
# --------------------------------------------------------------------------
# Debugging and program switches
# --------------------------------------------------------------------------
fastmode : 1 #Posting speed optimizition
bug1 : 2 #0=No display, 1=Generic list box, 2=Editor
bug2 : 30 #Append postline labels, non-zero is column position? 
bug3 : 0 #Append whatline no. to each NC line?
bug4 : 1 #Append NCI line no. to each NC line?
whatno : yes #Do not perform whatline branches? (leave as yes)
strtool_v7 : 2 #Use Version 7 toolname?

arcoutput : 0 #0 = IJK, 1 = R no sign, 2 = R signed neg. over 180
arctype : 2 #Arc center 1=abs, 2=St-Ctr, 3=Ctr-St, 4=unsigned inc.
stagetool : 0 #0 = Do not pre-stage tools, 1 = Stage tools
use_gear : 0 #Output gear selection code, 0=no, 1=yes 
max_speed : 10000 #Maximum spindle speed
min_speed : 50 #Minimum spindle speed
index : 0 #Use index positioning
ctable : 5 #Degrees for each index step with indexing spindle
rot_on_x : 1 #Axis rotating about, 0 = Y axis, 1 = X axis
rot_ccw_pos : 0 #Axis signed direction, 0 = CW positive, 1 = CCW positive
maxfrdeg : 2000 #Limit for feed in deg/min
scaxadrs A #Address for the rotary axis

# --------------------------------------------------------------------------
# INITIALIZE - initialize system variables
# --------------------------------------------------------------------------
arccheck : 1 #Check for small arcs, convert to linear
breakarcs : no #Break arcs into quadrants? 
getnextop : 2 #Build the next variable table
newglobal : 1 #Error checking for global variables
nobrk : no #Omit breakup of x, y & z rapid moves
omitseq : no #Omit sequence no.
progname : 1 #Use uppercase for program name
rotaxtyp : 1 #Rotary axis type for toolplane
seqmax : 9999 #Max. sequence no.
spaces : 0 #No. of spaces to add between fields
tlchng_aft : 1 #Delay call to toolchange until move line 
tooltable : 1 #Read for tool table and pwrtt 

#Common variables and strings
zero : 0 #Define constant
one : 1 #Define constant
two : 2 #Define constant
c9k : 9999 #Define constant
frc_cinit : 0 #Force C axis reset and C axis codes at each toolchange
#overrides the modality based on postflg
speed : 0 #Absolute value of ss
bld : 0 #Block delete active
result : 0 #Return value for functions
sav_spc : 0 #Save spaces
sav_gcode : 0 #Gcode saved 
absinc : 0 #Absolute/Incremental 
ctype : 0 #Cut type 
cflag : 0 #1 = in path 

#Position control variables
x_mult : 1 #Multiplier for output on X axis 
y_mult : 1 #Multiplier for output on Y axis 
z_mult : 1 #Multiplier for output on Z axis 

#English constant variables, adjust according to metric/english setting
conversion : 12 #Unit conversion value, do not change
ltol : .002 #Length tolerance for arccheck
vtol : .0001 #System tolerance
maxfeedpm : 500 #Limit for feed in unit/min

pconsmet #Metric constant variables
conversion = 1000
ltol = .05 
vtol = .001
maxfeedpm = 10000 

# --------------------------------------------------------------------------
# Format statements - n=nonmodal, l=leading, t=trailing, i=inc, d=delta
# --------------------------------------------------------------------------
#Default english position format statements
fs 1 0.4 #Decimal, absolute, 4 place, default for initialize (:)
fs 2 0.4 #Decimal, absolute, 4 place
fs 3 0.4d #Decimal, delta, 4 place
#Metric position format statements
fs 22 0.3 #Decimal, absolute, 3 place
fs 23 0.3d #Decimal, delta, 3 place
#Common format statements
fs 4 1 0 #Integer, not leading
fs 5 2 0l #Integer, force two leading
fs 6 3 0l #Integer, force three leading
fs 7 4 0l #Integer, force four leading
fs 8 0 4t #No decimal, absolute, four trailing
fs 9 0.1 #Decimal, absolute, 1 place
fs 10 0.2 #Decimal, absolute, 2 place
fs 11 0.3 #Decimal, absolute, 3 place
fs 12 0.4 #Decimal, absolute, 4 place
fs 13 0.5 #Decimal, absolute, 5 place
fs 14 0.3d #Decimal, delta, 3 place

# --------------------------------------------------------------------------
#Address string definitions
srad "R"
srminus "R-"
sblank

# --------------------------------------------------------------------------
# Motion G code selection
sg00 G0 #Rapid
sg01 G1 #Linear feed
sg02 G2 #Circular interpolation CW 
sg03 G3 #Circular interpolation CCW 
sg04 G4 #Dwell
sgcode #Target for string

fstrsel sg00 gcode sgcode 
# --------------------------------------------------------------------------
# Select work plane G code
sg17 G17 #XY plane code 
sg19 G19 #YZ plane code 
sg18 G18 #XZ plane code 
sgplane #Target string

fstrsel sg17 plane sgplane 
# --------------------------------------------------------------------------
#Select english/metric code 
sg20 G20 #Inch code
sg21 G21 #Metric code
smetric #Target string 

fstrsel sg20 met_tool smetric 
# --------------------------------------------------------------------------
#Select reference return code 
sg28 G28 #First reference point return
sg30 G30 #Second reference point return
sg28ref #Target string

fstrsel sg28 mi3 sg28ref 
# --------------------------------------------------------------------------
# Cutter compensation G code selection
scc0 G40 #Cancel cutter compensation
scc1 G41 #Cutter compensation left
scc2 G42 #Cutter compensation right
sccomp #Target for string

fstrsel scc0 cc_pos sccomp
# --------------------------------------------------------------------------
# Select incremental or absolute G code 
sg90 G90 #Absolute code
sg91 G91 #Incremental code
sgabsinc #Target string 

fstrsel sg90 absinc sgabsinc 
# --------------------------------------------------------------------------
# Generate string for spindle 
sm04 M4 #Spindle reverse 
sm05 M5 #Spindle off 
sm03 M3 #Spindle forward 
spindle #Target for string

fstrsel sm04 spdir1 spindle
# --------------------------------------------------------------------------
# Coolant M code selection
sm09 M9 #Coolant Off
sm08 M8 #Coolant Flood 
sm08_1 M8 #Coolant Mist
sm08_2 M8 #Coolant Tool
scoolant #Target for string

fstrsel sm09 coolant scoolant 
# --------------------------------------------------------------------------
# Table rotation direction 
sindx_m M22 #Rotate CCW code 
sindx_p M21 #Rotate CW code
sindx_m2 M22 #Rotate CCW code 
sindx_mc #Target for string

fstrsel sindx_m indx_mc sindx_mc 
# --------------------------------------------------------------------------
# Define the gear selection code
flktbl 1 3 #Lookup table definition - table no. - no. entries
40 0 #Low gear range
41 400 #Med gear range
42 2250 #Hi gear range
# --------------------------------------------------------------------------
# Toolchange NC output
# --------------------------------------------------------------------------
fmt O 7 progno #Program number
fmt T 4 t #Tool No
fmt T 4 first_tool #First Tool Used 
fmt T 4 next_tool #Next Tool Used 
fmt D 4 tloffno #Diameter Offset No
fmt H 4 tlngno #Length Offset No
fmt G 4 g_wcs #WCS G address
fmt P 4 p_wcs #WCS P address
fmt S 4 speed #Spindle Speed
fmt M 4 gear #Gear range
fmt 4 indx_out #Index position
# --------------------------------------------------------------------------
fmt "TOOL - " 4 tnote # Note format
fmt " DIA. OFF. - " 4 toffnote # Note format
fmt " LEN. - " 4 tlngnote # Note format
fmt " DIA. - " 1 tldia # Note format
# --------------------------------------------------------------------------
ptoolcomment #Comment for tool
tnote = t 
toffnote = tloffno
tlngnote = tlngno
"(", pstrtool, *tnote, *toffnote, *tlngnote, *tldia, ")", e 

pstrtool #Comment for tool
if strtool <> sblank, pstrtool2

pstrtool2 #Comment for tool
strtool = ucase(strtool)
*strtool, " "

pcomment #Comment from manual entry
pcomment2

pcomment2 #Comment from manual entry
scomm = ucase (scomm)
if gcode = 1007, "(", scomm, ")"
else, "(", scomm, ")", e

pheader #Start of file 
"%"
*progno

psof0 #Start of file for tool zero 
psof

psof #Start of file for non-zero tool number 
nextcflg = c9k
plast_recd
rc1 = one
gcode1 = rbuf ( one, rc1 )
if ntools = one, stagetool = two #skip single tool output
gcode = zero
cc_pos = zero
pspindle
pcom_moveb
"(PROGRAM NAME - ", progname, ")"
"(DATE=DD-MM-YY - ", date, " TIME=HH:MM - ", time, ")"
ptoolcomment
comment
pbld, n, *smetric 
pbld, n, *sgcode, sgplane, "G40", "G49", "G80", *sgabsinc
if mi1 <= one, pg92_rtrnz, pg92_rtrn, pg92_g92 
absinc = mi2
if stagetool = zero, pbld, n, *t, "M6"
if stagetool = one, pbld, n, *next_tool, "M6"
pindex
pbld, n, *sgcode, *sgabsinc, pwcs, pfxout, pfyout, pfcout,
*speed, *spindle, pgear, pcan1
pbld, n, "G43", *tlngno, pfzout, scoolant
pcom_movea

ptlchg0 #Call from NCI null tool change (tool number repeats) 

pctype
prv_x = vequ (xa)
pspindle
comment
pbld, n, sgplane, e 
if prv_spdir1 <> spdir1, pbld, n, "M5", e
if prv_speed <> speed | prv_spdir1 <> spdir1,
pbld, n, *speed, *spindle, pgear, e 
pbld, n, scoolant,e

ptlchg #Tool change 
gcode = zero
pspindle
pcom_moveb
if mi1 = one, pg92_rtrn, pg92_g92 
absinc = mi2
pbld, n, "M01", e
ptoolcomment
comment
if stagetool = zero, pbld, n, *t, "M6"
if stagetool = one, pbld, n, *next_tool, "M6"
pindex
pbld, n, *sgcode, *sgabsinc, pwcs, pfxout, pfyout, pfcout,
*speed, *spindle, pgear, pcan1
pbld, n, "G43", *tlngno, pfzout, scoolant
pcom_movea

ptlchg1002 #Call at actual toolchange 
pctype
absinc = mi2
prv_feed = c9k
linarc = zero
brklinestype = zero
xh = xh * x_mult
yh = yh * y_mult
zh = zh * z_mult
prv_xia = vequ (xh) 
if abs(ctype) = two & rotdia = zero, proterror

ptoolend #End of tool path 
gcode1 = rbuf ( one, rc1 )
if gcode1 <> 1000, ptoolend_t

ptoolend_t #End of tool path, toolchange 
gcode = zero
coolant = zero
absinc = one
if cc_pos <> zero, cc_pos = zero 
pbld, n, sccomp, "M5", e
pbld, n, sgabsinc, sgcode, *sg28ref, "Z0.", scoolant, e
if mi2 = one, pbld, n, *sg28ref, "X0.", "Y0.", protretinc, e
else, protretabs

protretinc #Reset the C axis revolution counter
if frc_cinit = one, presetrev

protretabs #Reset the C axis revolution counter
if frc_cinit = one, pbld, n, *sg28ref, presetrev, e

presetrev #Reset the C axis revolution counter
rev = zero
cabs = zero
prv_csav = zero
prv_cabs = zero
*scaxadrs
sav_spc = spaces
spaces = zero
"0."
spaces = sav_spc

peof0 #End of file for tool zero 
peof

peof #End of file for non-zero tool 
ptoolend_t
comment
pbld, n, "M30" 
"%"

pwcs #G54+ coordinate setting
if mi1 > 1, pwcs_g54

pwcs_g54 #G54 coordinate setting
if workofs < 6, g_wcs = workofs + 54
else, p_wcs = workofs - 5
if workofs < 6, g_wcs
else, 'G54.1', p_wcs
!workofs

pfwcs #Force G54+ coordinate setting and position
absinc = zero
pbld, n, sgabsinc, pwcs_g54, pfxout, pfyout, pfzout, e
absinc = mi2

pg92_rtrnz #Z home position return 
absinc = one
pfbld, n, sgabsinc, *sg28ref, "Z0.", e

pg92_rtrn #XY home position return 
pfbld, n, *sg28ref, "X0.", "Y0.", e

pg92_g92 #G92 coordinate setting at tool change
pfbld, n, "G92", *xh, *yh, *zh, e

pgear #Find spindle gear
if use_gear = one, pgear2

pgear2 #Find spindle gear and output
gear = frange ( 1, speed )
*gear

pcan #Canned text - before output call

pcan1 #Canned text - with move

pcan2 #Canned text - after output call
if opcode1 = 3 & dwell <> zero & gcode = one, pdwell1
if cantext = one, pbld, n, "M01", e
if cantext = two, pbld, n, "M00", e

pspindle #Spindle speed calculations for RPM
speed = abs (ss1)
if speed > max_speed, speed = max_speed 
if speed < min_speed, speed = min_speed 

pctype #Determine the cut type
#Multisurf Rotary 4 axis or 11 gcode = one
#Axis substitution = two
ctype = zero
if nextop = 11, ctype = one 
if (rotaxis = one & rot_on_x = one)
| (rotaxis = two & rot_on_x = zero), psuberror 
if rotaxis = one | rotaxis = two, ctype = two 
if rot_on_x = zero, ctype = -ctype

# --------------------------------------------------------------------------
# Motion NC output
# --------------------------------------------------------------------------
#The variables for absolute output are xabs, yabs, zabs.
#The variables for incremental output are xinc, yinc, zinc.
#They are found from the variables xa, ya, za.
#To prevent disturbing xa, ya, za they are used to calculate the output
#variables with multipliers.
fmt N 4 n #Sequence number
fmt X 2 xabs #X position output
fmt Y 2 yabs #Y position output
fmt Z 2 zabs #Z position output
fmt X 3 xinc #X position output
fmt Y 3 yinc #Y position output
fmt Z 3 zinc #Z position output
fmt 11 cabs #C axis position
fmt 14 cinc #C axis position
fmt I 3 iout #Arc center description in X
fmt J 3 jout #Arc center description in Y
fmt K 3 kout #Arc center description in Z
fmt R 2 arcrad #Arc Radius
fmt F 10 feed #Feedrate
fmt P 11 dwell #Dwell
# --------------------------------------------------------------------------
prapidout #Output to NC of linear movement - rapid 
pbld, n, sgplane, sgcode, pwcs, pccdia, pxout, pyout, pzout, pcout,
pcan1, e

plinout #Output to NC of linear movement - feed 
pbld, n, sgplane, sgcode, pwcs, pccdia, pxout, pyout, pzout, pcout, pfr,
pcan1, e 

pcirout #Output to NC of circular interpolation 

pbld, n, sgplane, sgcode, pwcs, pccdia, pxout, pyout, pzout, pcout, parc,
pfr, pcan1, e 

prapid #Output to NC of linear movement - rapid 
pcom_moveb
if prv_workofs <> workofs & mi1 > 1, pfwcs
ppos_cax_lin 
prapidout
pcom_movea

pzrapid #Output to NC of linear movement - rapid Z only 
prapid

plin #Output to NC of linear movement - feed 
pcom_moveb
if prv_workofs <> workofs & mi1 > 1, pfwcs
if ctype = zero, ppos_cax_lin
plinout
pcom_movea

pz #Output to NC of linear movement - feed Z only 
plin

pmx #Output to NC of Multisurf Rotary 
if fr = -2, gcode = zero
else, gcode = one
if gcode = one, plin
else, prapid

pcir #Output to NC of circular interpolation 

pcom_moveb
if prv_workofs <> workofs & mi1 > 1, pfwcs
if ctype = zero, ppos_cax_lin
pcirout
pcom_movea

ppos_cax_lin #Position the rotary axis before move - rapid
if ctype = zero & fmtrnd(prv_cabs) <> fmtrnd(cabs), ppos_cax_lin2
pindex

ppos_cax_lin2 #Position the rotary axis before move - rapid
sav_gcode = gcode
gcode = zero
pbld, n, sgcode, pcout, e
gcode = sav_gcode

pdwl_spd #Call from NCI gcode 4
ss1 = ss
spdir1 = spdir + one 
pspindle
comment
pdwell1
if prv_spdir1 <> spdir1, pbld, n, "M5", e
if prv_speed <> speed | prv_spdir1 <> spdir1,
pbld, n, *speed, *spindle, pgear, e 

pdwell2 #Output to NC of dwell 
gcode = 4
pbld, n, *sgcode, *dwell, e 

pdwell1 #Check for zero dwell 
if fmtrnd(dwell) <> zero, pdwell2

pcom_moveb #Common motion preparation routines, before 
xa = vequ(x)
pxyzcout
ps_prvx
comment
pcan

pcom_movea #Common motion preparation routines, after 
pcan2
pe_prvx
if cend = one, cflag = zero

# --------------------------------------------------------------------------
# Motion output components
# --------------------------------------------------------------------------
pbld #Canned text - block delete
if cantext = 3, bld = one
if cantext = 4, bld = zero
if bld = one, '/'

pfbld #Force - block delete
"/" 

pccdia #Cutter Compensation
#Force Dxx# if prv_cc_pos <> cc_pos & cc_pos <> zero, prv_tloffno = c9k
sccomp
if cc_pos > zero, tloffno

pfxout #Force X axis output
if absinc = zero, *xabs, !xinc
else, *xinc, !xabs

pxout #X output
if absinc = zero, xabs, !xinc
else, xinc, !xabs

pfyout #Force Y axis output
if absinc = zero, *yabs, !yinc
else, *yinc, !yabs

pyout #Y output
if absinc = zero, yabs, !yinc
else, yinc, !yabs

pfzout #Force Z axis output
if absinc = zero, *zabs, !zinc
else, *zinc, !zabs

pzout #Z output
if absinc = zero, zabs, !zinc
else, zinc, !zabs

pfcout #Force C axis output 
if index = zero, pcout2

pfcout2 #Force C axis output
if absinc = zero, *cabs
else, *cinc

pcout #C axis output
if index = zero, pcout2

pcout2 #C axis output
if absinc = zero, cabs, !cinc
else, cinc, !cabs

parc2 #Arc output for R
if abs(sweep) <= 180 | arcoutput = one, result = nwadrs(srad, arcrad)
else, result = nwadrs(srminus, arcrad)
*arcrad

parc0 #Arc output for I, J, K 
iout = i * x_mult
jout = j * y_mult
kout = k * z_mult
iout, jout, kout

parc #Select the arc output
if arcoutput = zero, parc0
else, parc2

pindex #Index output
if index = one & prv_indx_out <> indx_out,
pbld, n, *sindx_mc, *indx_out, e
if index = one, !cabs, !cinc
!indx_out

pffr #Output feedrate, force
*feed

pfr #Output feedrate
feed

# --------------------------------------------------------------------------
# Drilling
# --------------------------------------------------------------------------
usecandrill : yes #Use canned cycle for drill
usecanpeck : yes #Use canned cycle for Peck
usecanchip : yes #Use canned cycle for Chip Break
usecantap : yes #Use canned cycle for Tap
usecanbore1 : yes #Use canned cycle for Bore1
usecanbore2 : yes #Use canned cycle for Bore2
usecanmisc1 : yes #Use canned cycle for Misc1
usecanmisc2 : yes #Use canned cycle for Misc2
# --------------------------------------------------------------------------
fmt Q 2 peck1 #First peck increment (positive)
fmt 2 peck2 #Second or last peck (positive)
fmt Q 2 shftdrl #Fine bore tool shift
fmt R 2 refht_a #Reference height
fmt R 3 refht_i #Reference height

drlgsel : -1 #Drill Select Initialize
drillref : 0 #Select drill reference
peckacel : 0 #Fractional percent to reduce peck2 when usecan.. : no
drlgcode : 0 #Save Gcode in drill 

# Canned drill cycle string select
sg81 G81 #drill - no dwell 
sg81d G82 #drill - with dwell 
sg83 G83 #peck drill - no dwell 
sg83d G83 #peck drill - with dwell 
sg73 G73 #chip break - no dwell 
sg73d G73 #chip break - with dwell 
sg84 G84 #tap - right hand
sg84d G74 #tap - left hand
sg85 G85 #bore #1 - no dwell 
sg85d G89 #bore #1 - with dwell 
sg86 G86 #bore #2 - no dwell 
sg86d G86 #bore #2 - with dwell 
sgm1 G76 #misc #1 - no dwell
sgm1d G76 #misc #1 - with dwell
sgm2 G81 #misc #2 - no dwell
sgm2d G82 #misc #2 - with dwell
sgdrill #Target for string

fstrsel sg81 drlgsel sgdrill 
# -------------------------------------------------------------------------- 
#Canned drill cycle reference height
sg98 G98 #Reference at initht
sg99 G99 #Reference at refht 
sgdrlref #Target for string

fstrsel sg98 drillref sgdrlref # Drill cycle G string select
# --------------------------------------------------------------------------
pdrill0 #Drill, motion test
gcode = zero
znci = initht * x_mult

pdrlcommonb #Canned Drill Cycle common call, before
if cstart = one, cflag = one
if drillcyc = 3, drlgsel = fsg2 (ss1) + drillcyc * 2
else, drlgsel = fsg2 (dwell) + drillcyc * 2 
if initht <> refht, drillref = 0 
else, drillref = 1
z = depth * z_mult
if absinc = one, prv_zia = refht * z_mult
feed = fr_pos
prv_dwell = zero
@dwell
pcom_moveb

pdrlcommona #Canned Drill Cycle common call, after
pcom_movea
z = initht * z_mult
za = initht * z_mult
prv_zia = initht * z_mult
pxyzcout
prv_gcode = -1

prdrlout #R drill position
refht_a = refht * z_mult
refht_i = (refht - initht) * z_mult 
if absinc = zero, *refht_a
else, *refht_i

pdrill #Canned Drill Cycle
pdrlcommonb
pbld, n, *sgdrlref, *sgdrill, pxout, pyout, pfzout, pcout, prdrlout,
dwell, pffr, e 
pdrlcommona

ppeck #Canned Peck Drill Cycle
pdrlcommonb
pbld, n, *sgdrlref, *sgdrill, pxout, pyout, pfzout, pcout, prdrlout, *peck1,
pffr, e 
pdrlcommona

pchpbrk #Canned Chip Break Cycle
pdrlcommonb
pbld, n, *sgdrlref, *sgdrill, pxout, pyout, pfzout, pcout, prdrlout, *peck1,
pffr, e 
pdrlcommona

ptap #Canned Tap Cycle
pdrlcommonb
pbld, n, *sgdrlref, *sgdrill, pxout, pyout, pfzout, pcout, prdrlout,
pffr, e 
pdrlcommona

pbore1 #Canned Bore #1 Cycle
pdrlcommonb
pbld, n, *sgdrlref, *sgdrill, pxout, pyout, pfzout, pcout, prdrlout,
dwell, pffr, e 
pdrlcommona

pbore2 #Canned Bore #2 Cycle
pdrlcommonb
pbld, n, *sgdrlref, *sgdrill, pxout, pyout, pfzout, pcout, prdrlout,
pffr, e 
pdrlcommona

pmisc1 #Canned Misc #1 Cycle
pdrlcommonb
pbld, n, *sgdrlref, *sgdrill, pxout, pyout, pfzout, pcout, prdrlout,
shftdrl, dwell, pffr, e 
pdrlcommona

pmisc2 #Canned Misc #2 Cycle (User Option)
pdrill

prdrlout_2 #R drill position
refht_a = refht * z_mult
refht_i = (refht - initht) * z_mult 
if absinc = zero, refht_a
else, refht_i

pdrill_2 #Canned Drill Cycle
pdrlcommonb
pbld, n, pxout, pyout, pzout, pcout, prdrlout_2, dwell, e 
pdrlcommona

ppeck_2 #Canned Peck Drill Cycle
pdrill_2

pchpbrk_2 #Canned Chip Break Cycle
pdrill_2

ptap_2 #Canned Tap Cycle
pdrill_2

pbore1_2 #Canned Bore #1 Cycle
pdrill_2

pbore2_2 #Canned Bore #2 Cycle
pdrill_2

pmisc1_2 #Canned Misc #1 Cycle
pdrill_2

pmisc2_2 #Canned Misc #2 Cycle
pdrill_2

pcanceldc #Cancel canned drill cycle
pbld, n, "G80", e

# --------------------------------------------------------------------------
# Position calculations
# --------------------------------------------------------------------------
#Unshifted machine position
xa : 0 #Absolute unshifted value for X
ya : 0 #Absolute unshifted value for Y
za : 0 #Absolute unshifted value for Z
#Incremental calculations
xia : 0 #Formated absolute value for X
yia : 0 #Formated absolute value for Y
zia : 0 #Formated absolute value for Z
#Rotary/Index output
csav : 0 #C saved value
prvcabs : 0 #Saved cout calculation from deg.
ctol : 225 #Tolerance in deg. before rev flag changes
cdelta : 0 #Calculation for angle change
rev : 0 #Calculation for deg/min
ixtol : .01 #Tolerance in deg. for index error
indx_dlt : 0 #Rotation direction calculation
indx_dlt2 : 0 #Rotation direction calculation
indx_mc : 0 #Rotation direction calculation
indx_out : 0 #Rotation direction calculation
aaxisx : 1 #A rotation vector
aaxisy : 0
aaxisz : 0
baxisx : 0 #B rotation vector
baxisy : 1
baxisz : 0
#Feed output control variables
frdegstp : 10 #Step limit for feed in deg/min
frdelta : 0 #Calculation for deg/min
frdeg : 0 #Feedrate deg/min actual
prvfrdeg : 0 #Feedrate deg/min actual
cldelta : 0 #Calculation for deg/min
ldelta : 0 #Calculation for deg/min
circum : 0 #Calculation for deg/min
# --------------------------------------------------------------------------
#Incremental calculations
ps_prvx #Incremental calculations, start 
xia = fmtrnd(xabs)
yia = fmtrnd(yabs)
zia = fmtrnd(zabs)
xinc = vsub (xia, prv_xia)
cinc = cabs - prv_cabs

pe_prvx #Incremental calculations, end 
!xia, !yia, !zia
!x, !y, !z

#Pre-process rotary motion control flags
plin0 #Linear movement, mill motion test 

if cstart = one, cflag = one
linarc = zero
brklinestype = zero
if abs(ctype) = two, plin0_1
pcc_upd

plin0_1 #Set brklinestype 
if ctype = two & cflag = one, brklinestype = 4
if ctype = -2 & cflag = one, brklinestype = 5

pcir0 #Circular interpolation, mill arc motion test 

if cstart = one, cflag = one
linarc = zero
brklinestype = zero
if abs(ctype) = two, linarc = one
pcc_upd

pcc_upd #Update cc_pos for cutter comp. control 

if ccomp <> 1 & nextcflg = 1000 & (nextccomp = two | nextccomp = 3),
cc_pos = nextccomp - one
if ((nextop = 1011 & ccomp = 4 & nextcflg2 = 1000 & prv_cc_pos = cc_pos2)
| (nextcflg = 1000 & ccomp = one & (nextccomp - one) = prv_cc_pos)
| (ccomp = 4 & cend = one)), cc_pos = prv_cc_pos

#Map coordinates
pxyzcout #Output values 
if ctype = zero, pxyzcout_0 #Tool plane positioning
if abs(ctype) = one, pxyzcout1 #Multisurf Rotary 
if abs(ctype) = two, pxyzcout2 #Axis substitution
xabs = xabs * x_mult
yabs = yabs * y_mult
zabs = zabs * z_mult
pcoutrev
pindxcalc
pfcalc
!xa, !ya, !za 
prvcabs = fmtrnd(cabs)

#Toolplane positioning, ctype = 0
pxyzcout_0 #Multisurf rotary axis motion about X axis
xabs = vequ (xa) 
csav = c 
if rot_ccw_pos = one, csav = -csav

#Multisurf Rotary, ctype = 1
pxyzcout_1 #Multisurf rotary axis motion about X axis
csav = atan2 (-vtooly, vtoolz) 
axisx = vequ (aaxisx) 

pxyzcout-1 #Multisurf rotary axis motion about Y axis
csav = atan2 (vtoolx, vtoolz) 
axisx = vequ (baxisx) 

pxyzcout1 #Multisurf rotary axis motion
if ctype = one, pxyzcout_1 #Multisurf Rotary about X
else, pxyzcout-1 #Multisurf Rotary about Y
xabs = rotp (-csav, xa) 
if rot_ccw_pos = zero, csav = -csav

#Y axis substitution, ctype = 2, directions are ok
pxyzcout_2 #Output values for Y axis substitution, top view
xabs = xa * x_mult
yabs = zero
zabs = (za + (rotdia / two)) * z_mult
csav = ya * (360 / (pi * rotdia))

pxyzcout-2 #Output values for X axis substitution, top view
xabs = zero
yabs = ya * x_mult
zabs = (za + (rotdia / two)) * z_mult
csav = xa * (360 / (pi * rotdia))

pxyzcout2 #Axis substitution
if ctype = two, pxyzcout_2 #Y axis substitution
if ctype = -2, pxyzcout-2 #X axis substitution
if rot_ccw_pos = one, csav = -csav

#Index calculations
pindxcalc #Index move calculations
if frac (abs(csav)/ctable) > ixtol & index = one, pindxerror
indx_dlt = prvcabs - cabs
indx_dlt2 = abs (indx_dlt)
indx_mc = zero
if indx_dlt < zero, indx_mc = one
if indx_dlt2 > 180, indx_mc = indx_mc + one 
indx_out = int(abs(csav)) 

#Rotary axis revolution calculation
pcoutrev2 #Add or subtract 360 degree for position
if cdelta > zero, rev = rev - one
else, rev = rev + one

pcoutrev1 #If motion exceeds ctol, add wind-up
if abs(cdelta) > ctol, pcoutrev2 

pcoutrev #Modify for wind-up
cdelta = csav - prv_csav
pcoutrev1
cabs = rev * 360 + csav
!csav

#Feedrate calculation
pfcalc #Feedrate calculations
if fmtrnd(cabs) = prvcabs | index = one
| (abs(ctype) = two & cflag = zero), pfcalc2
else, pfcalc_deg

pfcalc2 #Feedrate 
feed = fr_pos
if feed > maxfeedpm, feed = maxfeedpm
prvfrdeg = feed

pfcalc_deg #Feedrate deg/min
circum = xa * two * pi 
if circum = zero, circum = c9k #Don't allow Zero
ldelta = 
sqrt(((xabs-prv_xabs)/x_mult)^2+((yabs-prv_yabs)/y_mult)^2+((zabs-prv_zabs)/z_
mult)^2)
cldelta = sqrt((((abs(cabs - prvcabs))/360)*circum)^2 + ldelta^2)
if cldelta = zero, cldelta = c9k
if ldelta = zero, cldelta = cdelta
frdeg = abs(cdelta / cldelta) * abs(fr_pos * (360 / circum))
frdelta = abs(frdeg - prvfrdeg)
if frdelta > frdegstp | prv_feed = c9k, pfcalc_deg2 
if frdeg > maxfrdeg, feed = maxfrdeg 

pfcalc_deg2 #Feedrate deg/min control
prvfrdeg = frdeg #Control output of frdeg 
feed = frdeg #Control output of frdeg 


# --------------------------------------------------------------------------
# Post setup and metric format conversion
# --------------------------------------------------------------------------
pq #Setup post based on switch settings
if stagetool = one, bldnxtool = one

psetup #Setup post based on NCI settings
if arctype = one | arctype = 4, pijkengabs
else, pijkengdlt
if met_tool = one, pmetric
#Apply rotary axis address
result = nwadrs(scaxadrs, cabs)
result = nwadrs(scaxadrs, cinc)
result = nwadrs(scaxadrs, indx_out)

pmetric #Metric format, English is default
pconsmet
if arctype = one | arctype = 4, pijkmetabs
else, pijkmetdlt
#Metric conversion for position values
result = newfs(22, xabs)
result = newfs(22, yabs)
result = newfs(22, zabs)
result = newfs(23, xinc)
result = newfs(23, yinc)
result = newfs(23, zinc)
result = newfs(22, arcrad)
result = newfs(9, feed)
#Drill calculations metric conversion
result = newfs(22, peck1)
result = newfs(22, peck2)
result = newfs(22, dwell)
result = newfs(22, shftdrl)
result = newfs(22, refht_a)
result = newfs(23, refht_i)

pijkmetabs #Metric ijk format, absolute
result = newfs(22, iout)
result = newfs(22, jout)
result = newfs(22, kout)

pijkmetdlt #Metric ijk format, delta
result = newfs(23, iout)
result = newfs(23, jout)
result = newfs(23, kout)

pijkengabs #English ijk format, absolute
result = newfs(2, i)
result = newfs(2, j)
result = newfs(2, k)

pijkengdlt #English ijk format, delta
result = newfs(3, i)
result = newfs(3, j)
result = newfs(3, k)

# --------------------------------------------------------------------------
# Tooltable look ahead routine
# --------------------------------------------------------------------------
# Toolchange information buffer - (var#, 1 for current, 2 for next)
wc1 : 1 #Initial count for write buffer 1 
rc1 : 1 #Initial count for read buffer 1
size1 : 0 #Buffer 1 size 

gcode1 : 0 #Buffer 1
cc_pos1 : 0 #Buffer 1
cc_pos2 : 0 #Buffer 1
opcode1 : 0 #Buffer 1
ss1 : 0 #Buffer 1
spdir1 : 0 #Buffer 1
nextdc1 : 0 #Buffer 1
nextcflg2 : 0 #Buffer 1

fbuf 1 0 8 0 #Buffer 1

upd_pmv : 0 #Saved count for the first point motion (0 is off)
# --------------------------------------------------------------------------
ppmv_recd2 #Update the toolchange point move, variables to update in prv_
!cc_pos1, !opcode1, !ss1, !spdir1, !nextdc1
rc1 = upd_pmv
gcode1 = rbuf ( one, rc1 )
cc_pos1 = prv_cc_pos1
opcode1 = prv_opcode1
ss1 = prv_ss1
spdir1 = prv_spdir1
nextdc1 = prv_nextdc1
rc1 = upd_pmv 
gcode1 = wbuf ( one, rc1 ) #rc1 is used to write also
upd_pmv = zero

ppmv_recd #Update the point move if null or cancel if toolchange
if gcode1 > 1000, upd_pmv = zero
else, ppmv_recd2

plast_recd #Update the last record with the current information (2)
rc1 = wc1 - one
gcode1 = rbuf ( one, rc1 )
if nextccomp = zero | nextccomp = one | nextccomp = 4, cc_pos2 = zero
if nextccomp = two, cc_pos2 = one
if nextccomp = 3, cc_pos2 = two
nextcflg2 = nextcflg
rc1 = wc1 - one #rc1 is used to write also
gcode1 = wbuf ( one, rc1 )
if upd_pmv <> zero & prv_opcode <> 15, ppmv_recd 

pcur_recd #Write to the current tool record (1)
gcode1 = gcode
cc_pos1 = cc_pos
if opcode = 3, cc_pos1 = zero
opcode1 = opcode
ss1 = ss
spdir1 = spdir + one 
nextdc1 = nextdc
gcode1 = wbuf ( one, wc1 )

pwrtt #Buffer toolchange information
if gcode = 1001, psetup
#test for toolchange point move (opcode 15) update
if gcode <> 1001, plast_recd
pcur_recd
if gcode > 1000 & opcode = 15, upd_pmv = wc1 - one
!opcode

# --------------------------------------------------------------------------
# Error messages
# --------------------------------------------------------------------------
psuberror #Arc output not allowed
"ERROR - WRONG AXIS USED IN AXIS SUBSTITUTION", e

proterror #Rotaxis diameter not set
"ERROR-ROTARY AXIS DIAMETER VALUE NOT SET", e
rotdia = .1 #Avoid divide error

pindxerror #Index increments are not correct
"ERROR-INDEX BY ", *ctable, " DEGREES", e

# --------------------------------------------------------------------------
# Numbered questions for Mastercam
# --------------------------------------------------------------------------
38. Rapid feedrate? 300.0
80. Communications port number for receive and transmit (1 or 2) ? 2
81. Data rate (110,150,300,600,1200,2400,4800,9600,14400,19200,38400)? 300 
82. Parity (E/O/N)? E
83. Data bits (7 or 8)? 7
84. Stop bits (1 or 2)? 2
85. Strip line feeds? N
86. Delay after end of line (seconds)? 0
87. Ascii, Eia, or Binary (A/E/B)? A
88. Echo keyboard to screen in terminal emulation? n
89. Strip carriage returns? N
90. Drive and subdirectory for NC and Material files?
91. Name of executable post processor? MP
92. Name of reverse post processor? RP
93. Reverse post PST file name? RPFAN
100. Number of places BEFORE the decimal point for sequence numbers? 3
101. Number of places AFTER the decimal point for sequence numbers? 0
103. Maximum spindle speed? 5000
107. Average time for tool change (seconds)? 2.0
160. Display first and last entity in toolpath when simulating cutter 
compensation in control? y
161. Enable Home Position button? y
162. Enable Reference Point button? n
163. Enable Misc. Values button? y
164. Enable Rotary Axis button? n
165. Enable Tool Plane button? y
166. Enable Construction Plane button? y
167. Enable Tool Display button? y
168. Check tplane during automatic work origin creation? n

#110. Default tool library? TOOLS.TL7

# --------------------------------------------------------------------------
# Default Miscellaneous Real Values
# --------------------------------------------------------------------------
201. Default miscellaneous real variable 1 (mr1)? 0.0
202. Default miscellaneous real variable 2 (mr2)? 0.0
203. Default miscellaneous real variable 3 (mr3)? 0.0
204. Default miscellaneous real variable 4 (mr4)? 0.0
205. Default miscellaneous real variable 5 (mr5)? 0.0
206. Default miscellaneous real variable 6 (mr6)? 0.0
207. Default miscellaneous real variable 7 (mr7)? 0.0
208. Default miscellaneous real variable 8 (mr8)? 0.0
209. Default miscellaneous real variable 9 (mr9)? 0.0
210. Default miscellaneous real variable 10 (mr10)? 0.0

# --------------------------------------------------------------------------
# Default Miscellaneous Integer Values
# --------------------------------------------------------------------------
301. Work Coordinates [0-1=G92, 2=G54's] (mi1)? 2
302. Absolute or Incremental [0=ABS, 1=INC] (mi2)? 0
303. Reference Return [0=G28, 1=G30] (mi3)? 0
304. Miscellaneous integer variable 4 (mi4)? 0
305. Miscellaneous integer variable 5 (mi5)? 0
306. Miscellaneous integer variable 6 (mi6)? 0
307. Miscellaneous integer variable 7 (mi7)? 0
308. Miscellaneous integer variable 8 (mi8)? 0
309. Miscellaneous integer variable 9 (mi9)? 0
310. Miscellaneous integer variable 10 (mi10)? 0

# --------------------------------------------------------------------------
# Configuration File association parameters (default is "y")
# --------------------------------------------------------------------------
400. Name of associated cfg file? 
401. Read SYSTEM COLORS section? y
402. Read ALLOCATIONS section? y
403. Read TOLERANCES section? y
404. Read DATA PATHS section? y
405. Read COMMUNICATIONS section? y
406. Read DRAFT SETTINGS section? y
407. Read MISCELLANEOUS section? y
408. Read NC SETTINGS section? y
409. Read DIALOG SCRIPTS section? y
410. Read DESIGN SETTINGS section? y
411. Read PLOTTER SETTINGS section? y
412. Read ALT-KEY ASSIGNMENTS section? y
413. Read CAD section? y
414. Read START/EXIT section? y
415. Read SCREEN section? y
416. Read FILE NAMES section? y