How Subprograms Work: Subprograms in Mastercam Mill and Lathe
How Subprograms Work: Subprograms in Mastercam Mill and Lathe
This chapter provides information on subprograms in Mastercam, the NCI file, and the MP language.
Mastercam subprogram support allows repetitive toolpath motion to be output as subprograms. The
toolpath motion must be identical for a toolpath to be considered a subprogram. When determining
how or what to put in a subprogram, Mastercam does not compare separate geometric chains to
determine if the motion is the same as another chain. Numerous options can cause Mastercam to create
separate subprograms, such as work offsets, multiple chains being selected, and multi passes. For more
information on creating subprograms in Mastercam toolpaths, please refer to Mastercam online help.
Mastercam subprograms allow you to create subprograms through depth cuts, drilling, and transform.
You can also create nested subprograms, where a toolpath that includes non-transform subprogram is
transformed with subprograms active. For example, a contour toolpath with depth cuts that are
subprograms could be translated with transform and subprograms active.
Note: If you are using subprograms in Mastercam Wire, see the “Subprograms in Mastercam Wire”
section.
Both categories support motion with either absolute or incremental positioning in the subprogram.
Transform subprograms
Transform subprograms are created by using Transform toolpaths. Each transformation of the selected
toolpaths or source operations is a subprogram.
Transform subprograms cannot be nested in another transform operation that is marked as a
subprogram. However, non-transform subprograms or non-subprogram toolpaths can be nested in a
transform subprogram. For example, a contour toolpath with depth cuts that are subprograms could be
translated and subprograms could be created using transform.
Transform by operation order
Transform by operation order creates a single subprogram with all source operations in the transform
selection included in the subprogram called at each transform location. When the Unique Subprograms
check box is checked in the Mastercam dialog box, a separate, unique subprogram will be created for
each tool change and each will be called in order at each transform location.
Transform by instance
Transform by instance always creates a subprogram for each tool selected and a single tool transform is
assigned. A single tool operation is performed at each location before the next operation is called.
Note: Transform subprograms are available in Lathe for turning operations. Depth cut and drill
subprograms are only available in Mill operations
Non-transform subprograms
Non-transform subprograms are subprograms generated as a toolpath option. These are toolpath
sections that can be identified as repeating patterns and are supported under the Depth Cuts dialog in
Contour, Pocket, Face and Circle Mill toolpaths. When subprograms are activated through the depth
cuts dialog box, matching depth cuts in the XY plane are marked as subprograms.
Note: Tapered walls and islands are not supported in subprograms.
! The top level is the default NC level. This level is always active if no subs are defined.
! The second level is the transform level. This level is set for all transform subprograms.
! The third level is for all non-transform subprograms. In the absence of the transform level,
third level subprograms are output at the second level.
g 1018 gcode
1 Subprogram number sub_op_id
2 Actual operation id sub_grp_id
3 Transform/non- sub_ref_id 0 Non-transform
tansform indicator
>0 Transform
4 Iteration counter sub_sec_no Transform operations:
<0 = Off
0 = Original
>0 = Copy
Non-transform operations:
<1 = Copy in transform
1 = Original
>1 = Copy
5 Total number of sub_totl_no
instances, 1-based
6 Not used sub_chn no
7 Absolute or sub_inc 0 Absolute
incremental 1 Incremental
8 Transform type sub_trnstyp 0 Mirror
1 Rotate
2 Scale (not used)
3 Translate
9 sub_trnmthd 0 Translate method = Tool plane
1 Translate method = Tool plane with
‘Tool plane origin ONLY’ checked
g 1019 gcode
1 Subprogram number esub_op_id
2 Actual operation id esub_grp_id
3 Transform/non- esub_ref_id 0 Non-transform
transform flag
>0 Transform
4 Iteration counter esub_sec_no Transform operations:
<0 = Off
0 = Original
>0 = Copy
Non-transform operations:
<1 = Copy in transform
1 = Original
>1 = Copy
5 Total number of exub_totl_no
instances
6 Not used esub_chn_no
1018 Interpretation
The post executable uses the 1018 parameters to determine what sub level is being processed. This
level will determine how to set output stream levels. Output can occur at the NC level, transform sub
level or the non-transform sub level. Parameter 1 (subprogram number), parameter 3 (transform/non-
transform indicator), and parameter 4 (iteration counter) trigger the output stream level and are critical
to the basic subprogram functionality.
Note: Subprograms of the non-transform type (sub level) read only the first seven parameters from the
1018 Gcode line.
Subprogram ID (parameter 1)
Parameter 1 (sub_op_id) is the unique subprogram ID and is used to produce the subprogram number.
! In transform subprograms, this value is written to the main_prg_no for output at the transform level.
! In non-transform subprograms, this value is written to the sub_prg_no for output at the non-
transform level.
The unique subprogram ID (sub_op_id) is numbered independently for transform and non-transform
subprograms. In most cases, these are sequentially assigned values for each new pattern. Repeated
patterns then reuse the value assigned to the original pattern.
Subprogram ID assignment exceptions in Mastercam
DRILL (non-transform) – Any point where a drill cycle definition (G81) is cancelled or
assigned a new subprogram ID is assigned and a new unique subprogram is created. This
occurs with jump heights.
Parameter 3 (sub_ref_id) is the subprogram reference ID. This variable is tested to see if it is greater
than zero. If the test is true, the output level is set to transform; otherwise it is set to non-transform.
When a non-transform operation is referenced in a transform operation as a copy, these values will be
signed negative.
Parameter 4 (sub_sec_no) is the subprogram iterator and is used to determine what type of output is to
occur.
! At transform level, parameter 4 (sub_sec_no) is checked to see if it is zero. A value of 0 indicates
that the NC code for the actual subprogram is generated and written along with a call to the
subprogram. Any other value only produces a call to the subprogram.
! At non-transform level, parameter 4 (sub_sec_no) is checked to see if it is 1. A value of 1 indicates
that the NC code for the subprogram is generated and written along with a call to the subprogram.
Any other value only produces a call to the subprogram.
The subprogram iterator (sub_sec_no) controls the output of the original subprogram.
! Transform subprograms write the original when 0 is assigned
! Non-transform subprograms write the original when 1 is assigned. This condition should occur
only once for any defined subprogram pattern. If it occurs more than once, the post outputs
duplicates of the same subprogram, which is an error.
Therefore, non-transform subprograms nested in transform subprograms must have the non-transform
subprogram iterator (sub_sec_no) signed negative in transform patterns where they are copies.
Note: Be aware that the original non-transform subprograms may be outside the first transform
operation so the original transform may contain a copy. This design allows hiding the non-transform
subs without losing the iteration number.
Parameter 5 (sub_totl_no ) is the subprogram total count and is used to track the cumulative number of
repeated patterns.
! In transform, subprograms track the cumulative number of repeated patterns relative to each
transform referencing the original operation. The original operation has parameter 5 assigned the
cumulative count of the first referenced transform. This allows the user to find the end of the
current transform when multiple transforms were assigned.
! In non-transform, subprograms track the number of repeated patterns relative to the current
operation. The total does not increase when copied into transform subprograms.
Critical parameters used to control output levels
1019 Interpretation
The 1019 NCI Gcode parameters are used to return the output stream level to the previous level that
was set prior to 1018 being called. Therefore, 1018 and 1019 are matched pairs and only 'non-
transform' subprograms may nest in transform subprograms. The first six parameters written on the
matching 1018 NCI Gcode line are repeated as the six parameters with the 1019 NCI Gcode line.
Complete 1018 Interpretation
Progno
The predefined variable progno contains the program number for the call level (NC).
Main_prg_no
The predefined variable main_prg_no contains the transform level subprogram ID. The value in
sub_op_id is written to main_prg_no.
Sub_prg_no
The predefined variable sub_prg_no contains the non-transform level subprogram ID. The value in
sub_op_id is written to sub_prg_no.
Merge commands
Mergesub, mergeaux and mergeext are used to combine all the alternate output files into one file.
See Volume 1, File Manipulation for more information.
Clear commands
Clearsub, clearaux and clearext are used to clear the alternate output files.
See Volume 1, File Manipulation for more information.
absinc
The predefined variable absinc controls absolute or incremental subprogram output (0=absolute,
1=incremental). For subprograms, the absinc value is read from the sub_inc parameter on the 1018.
The sub_inc parameter is set from inside the Mastercam settings in the subprogram dialog box. For the
call level (NC), absinc must be set in the post or through some user input. It is not done automatically
based on the settings in the subprogram dialog box. Normally this is done through the use of a
miscellaneous integer.
Predefined postblocks for calls and labels on the 1018 NCI Gcode
Psub_call_m
Psub_call_m is a special postblock used to output the subprogram call to the main level output file. This
postblock is called when:
! 1018 is encountered
! and the command word c_msng is found in the post
! and the transform operation has one tool change in it.
It is desirable to output the tool before the main call at the NC level so the current tool is not called
again.
sub_mny_t
The variable sub_mny_t is read from the NCI to determine if more than one tool is contained in a
transform operation. It is set to 1 when more than one tool is present.
Psub_call_mm
Psub_call_mm is a special postblock used to output the subprogram call to the main level output file.
This postblock is called when:
! 1018 is encountered
! and the command word c_mmlt is found in the post
! and the transform operation has two or more tool changes in it.
It is desirable to output the tool inside the main program (level 1) allowing the tool to be called
repeatedly.
The variable sub_mny_t is read from the NCI to determine if more than one tool is contained in a
transform operation. It is set to 1 when more than one tool is present.
Psub_call_s
The psub_call_s postblock outputs the subprogram call to the sub level. This postblock is called when a
1018 is encountered and non-transform subprograms are active (sub_ref_id <= 0).
Psub_st_m
The psub_st_m postblock outputs the main program number (main_prg_no) and header to the transform
level. This postblock is called after the main level call (psub_call_m or psub_call_mm).
Psub_st_s
The psub_st_s postblock outputs the sub program number (sub_prg_no) and header to the sub level.
This postblock is called after the sub level call (psub_call_s).
Merging
The user must add the sub merge commands if combining all output into the one NC file. Remember
to merge at the NC level, subout set to 0.
Just before the internal call to psub_st_m, psub_st_mm and psub_st_s, the absinc variable is set to the
subprogram setting.
Just before the internal call to psub_end_m, and psub_end_s, the absinc variable is set to the return level
subprogram setting.
Note: If automatic subs are not used, sub_level returns as -1 and the output stream is not switched. The
calls are performed to the predefined postblocks as in automatic processing. Code must be present in
your post for subprogram output to occur.
Additional Information
Disabling long output
A subprogram can be disabled for long output by assigning sub_level to -1 in the postblocks
psub_call_m, psub_call_mm, or psub_call_s. This prevents the current output level from switching and
prevents calls to psub_st_m, psub_st_mm, psub_end_m, psub_st_s, and psub_end_s. After the location of
the normal end code, the automatic subprogram routine is restored
Updating variables
The following variables are updated to their previous setting when returning from a transform sub to
the NC level, which allows the initialized value to be restored:
! sub_trnstyp
! sub_trnmthd
! sub_trnsx
! sub_trnsy
! sub_trnsz
! sub_nxt_t
! sub_nxt_h
! sub_nxt_tid
! sub_mny_t
Note: Do not update these values because they won't restore properly!
Initializing sub_trnstyp
sub_trnstyp is initialized internally to –1. Do not initialize in the post because an error will be generated.
Suppressing output
When the output stream is suppressed:
! The NCI is processed internally.
! Debug (bug2 and/or bug4) forces output of all levels.
! Error messages and leader and trailer code are output.
! All file-merging operations are output to the current level.
! Hexadecimal output is not suppressed.
Variables
There is no standard for subprogram support in a Wire post customization file. How subprograms are
supported is left entirely up to the post writer. The following is a list of variables that may be used for
Wire subprogram support.
! Subtype is a NCI variable that holds the subprogram flag set by Mastercam.
! Subno is a NCI variable that holds the subprogram number.
! Skimpass is a NCI variable that holds the skimcut parameter.
! Subout is a post switch variable that determines what alternate file (.nc, .sub, etc) the NC code is
written to.
See Volume 1, File Manipulation for more information.on using subout and the alternate output files.
See Volume 3, Numeric Variables for more information on all predefined variables.
g 1015 gcode
1 Subroutine type settings subtyp 0 Not a subroutine
1 Write subroutine
2 Call subroutine only
2 Subroutine number subno
3 Thread/Cut flag td_ct_flg 0 No thread or cut
(Not used) 1 Allow thread the wire
in V8.1 2 Allow cut the wire
4 Tab Cut tabcut 0 No tab cut
1 Tab cut
2 Contour with tab cut
5 Wire Trim (wtrim): 0 Trim in control
1 Trim in computer
2 3D tracking
6 Skimcut options skimpass 0 No skimcut
+1 First skimcut pass on a
contour
-1 Subsequent skimcut pass
on a contour
-2 Last skimcut pass on a
contour.
7 Wire cut position X cutx
8 Wire cut position Y cuty
9 XY trimming plane trimplane1
10 UV trimming plane trimplane2
11 register value 1 reg1
12 register value 2 reg2
13 register value 3 reg3
14 register value 4 reg4
15 register value 5 reg5
16 register value 6 reg6
17 register value 7 reg7
18 register value 8 reg8
19 register value 9 reg9
20 register value 10 reg10