You are on page 1of 8

Volume 1 Chapter 17 File Manipulation

17 File Manipulation
The MP post executable is capable of providing several output file streams and temporary file streams.
These can be used to manipulate, order, and access data to be used for a variety of purposes. The
ability to manipulate files makes the MP language very flexible. This chapter presents the various file
streams and formula functions that can be used with them.
See Volume 2, How buffer files work and Volume 2, How subprograms work for related topics.

NC program output files


By default, the MP post executable directs all NC program output to the NC file. It can also direct NC
program output to one or more alternate NC output files:
! sub file
! auxiliary file
! external file
This ability is controlled by setting the numeric variable subout. By setting this variable to the
appropriate value, you can switch the output stream the post processor writes data to.
Note: Be aware that when you set the automatic use of subprograms with the numeric variable
sub_level, switching is done for you based on the predetermined nested level of the subprogram.
When using automatic subprograms, you cannot use the alternate files.

Controlling alternate file defaults


The path, name and extension of the alternate files (and the extension of the standard NC output file as
an initial setting) can be controlled by by the post customization file. This ability allows the reuse of a
particular alternate file with a new file name.

Changing default output file extensions


Use the following string variables to redefine the default output file name extension:
string variable output file extension (default)

sextnc default output file (.nc)


sextsub sub output file (.sub)
sextaux auxiliary output file (.aux)
sextext external output file (.ext)

June 2002 Mastercam Version 9 MP Post Processor Reference Guide 17-1


Chapter 17 File Manipulation Volume 1

The extensions are assigned the default character strings as shown above. This default can be
overwritten by initializing the string variables or by reassigning them on a postline. The new file name
is not used until the procedure to changing the output file is used. The NC file extension cannot be
reassigned, but when initialized in the post, it is read into Mastercam when the post is loaded. For
example:
sextnc ABC # Mastercam reads “ABC” as the NC file extension

See String Variables for more information.

Changing default output file names


Use the following string variables to redefine the default output file name:

string variable output file name

snamesub sub output file name


snameaux auxiliary output file name
snameext external output file name
The file names are assigned the NCI file name by default. This default can be overwritten by
initializing the string variables or by reassigning them on a postline. The new file name is not used
until the procedure to changing the output file is used.
Note: The NC file name is defined in Mastercam.

Setting output file paths


Use the following string variables to set the default output file path from within the post:

string variable output file defined

spathsub sub output file name


spathsaux auxiliary output file name
spathext external output file name
The paths are the current directory (that is, no path is assigned) by default. This default can be
overwritten by initializing the string variables or by reassigning them on a postline. The new path is
not used until the procedure to changing the output file is used.
Note: The NC output file path is defined in Mastercam.

Managing output files


Selecting an output file
You can control where the NC output is written from within the post customization file by using
numeric variables related to the subout variable and the subout variable itself.

17-2 Mastercam Version 9 MP Post Processor Reference Guide June 2002


Volume 1 Chapter 17 File Manipulation

The subout variable acts as a switch to direct the NC code output to one of the following alternate files:
subout value alternate file (and default extension)

0 default output file (.NC)


1 sub output file (.SUB)
2 auxiliary output file (.AUX)
3 external output file (.EXT)
Set the subout variable to the desired value in the post customization file either by initializing it or
setting it on a postline at the point the stream is to be switched. The NC output is then directed to the
alternate file selected by the subout value. For example:
subout : 0 # Direct the output to the NC file
pblock # A postblock
subout = 1 # Re-direct the output to the sub alternate file

Opening output files


The alternate output file must be recognized as a valid stream before it can be opened and accessed
with the subout variable. The file is actually opened on the first attempt to output to the file. The
following numeric variables are used to recognize the alternate files:

numeric variable output file

subprg sub output file


auxprg auxiliary output file
extprg external output file
Note: The NC output file is always open.
The subprg, auxprg and extprg numeric variables have three possible states. Set the appropriate variable
to one of the following values:

variable value action

0 Disable writing to the alternate file.


1 If the alternate file already exists, erase the contents of the file and open the file, or
create the new file.
2 If the alternate file already exists, open the file and append output to the existing
contents of the file, or open a new file.
Set the appropriate variable value by initializing it or by using it in a formula statement, although
returning to the standard NC output file (subout = 0) requires no additional coding. This is required the
first time output is directed to the alternate file only. For example, the sub file can be recognized either
way:

June 2002 Mastercam Version 9 MP Post Processor Reference Guide 17-3


Chapter 17 File Manipulation Volume 1
subprg : 1 # Allow access to the sub file by initializing subprg = 1 (open and clear)
pblock # A postblock
subprg = 1 # Allowing access to the sub file as a formula statement (alternate method to subprg : 1)
subout = 1 # Re-direct the output to the sub file

And now appending to the auxiliary alternate file:


auxprg : 1 # Allow access to the aux file by initializing auxprg = 1 (open and clear)
pblock # A postblock
auxprg = 1 # Allow access to the aux file as a formula statement (alternate method to auxprg : 1)
subout = 2 # Re-direct the output to the aux file

The above code snippet displays the value set for erasing the contents of the alternate file when the file
exists. The following code displays the setting to append the NC output to the contents of the file if it
exists.
subprg : 2 # Allow access to the sub file by initializing subprg = 2 (open and append)
pblock # A postblock with the numeric variable
subprg = 2 # Allow access to the sub file as a formula statement
subout = 1 # Re-direct the output to the sub file

Note: Automatic subprograms enable the alternate files, so it is not necessary to set these variables in
posts supporting the subprograms.

Merging output files


You can append (merge) an alternate file into another output file (including the standard NC output
file) by using one of the merge commands:

command variable file to be merged

mergesub Merge sub output file.


mergeaux Merge auxiliary output file.
mergeext Merge external output file.
To merge an alternate file, enter the command variable for the appropriate alternate file into the post
customization file as a postline at the point where you want to merge the file. The alternate file can be
merged at any time and will be appended to the target file. By merging before all posting is completed,
the contents of the merged file are inserted into the target file.
You cannot merge a file into itself, so the subout variable cannot be set to write to the same alternate
file. For example, if subout were set to 1, attempting to mergesub would generate an error because a file
cannot be merged into itself. The following is not valid:
pblock # A postblock
subout = 1 # Direct the output to the sub alternate file
mergesub # Append the sub file to the NC file

17-4 Mastercam Version 9 MP Post Processor Reference Guide June 2002


Volume 1 Chapter 17 File Manipulation

The following is a legal use of the merge function:


pblock # A postblock
subout = 0 # Direct the output to the NC file
mergesub # Append the sub file to the NC file

Clearing output files


You can clear or empty alternate files of current contents by using the appropriate numeric variable for
the alternate file:

command variable file to be emptied

clearsub Empty sub output file.


clearaux Empty auxiliary output file.
clearext Empty external output file.
To clear an alternate file, enter the command variable for the appropriate alternate file into the post
customization file as a postline at the point where you want to clear the file. For example:
pblock # A postblock
subout = 0 # Direct the output to the NC file
clearsub # Empty the sub alternate file

Changing the output file


Use the following numeric variables to close the alternate file and reopen it with a new file path, name
or extension. The file opening method can also be modified before the command variable is
encountered on the postline. If the file name is not changed and the file opening method numeric
variables (subprg, auxprg and extprg) are 1, the file is emptied.

command variable file to be closed and reopened

newsub Close then open a new sub output file.


newaux Close then open a new auxiliary output file.
newext Close then open a new external output file.
For example, the name is changed for the alternate file “sub file”. In the following code example, the
alternate file for the sub file is closed with the contents intact and reopened with the new file name as a
new file by the command variable:
psubname # Rename the ISO name
subout = 0
sisoend = no2asc(isono)
snamesub = snamenc + sisoend
subout = 1
subprg = 1
newsub

June 2002 Mastercam Version 9 MP Post Processor Reference Guide 17-5


Chapter 17 File Manipulation Volume 1

Note: To close a specified file rather than the current file, change the output file path, name, and or
extension before using these command variables in a postline.

Closing output files


You can close the NC output file and the alternate files (.sub, .aux, .ext) with the file function fclose. If
the argument passed in the function is a string variable that is the file name and extension of the NC file
or one of the alternate files, the file with the matching name is closed. See Formulas for more
information.
string1 "file.sub"
pblock # A postblock
real_result = fclose (string1) # Close the matching file name

General file manipulation formulas


The following formula functions can be used to manipulate any files including the alternate files:

Formula Use

fexist Determine if a file exists.


fclose Close a file.
remove Remove a file from the disk.
rename Rename a file on the disk.
These functions are normally used together to perform actions such as renaming an output file to an
existing disk file. These tools can be used in conjunction with other MP features, such as buffer files,
to perform a high level of file manipulation.
See Volume 3, Formulas and Volume 2, How buffer files work for more information.

General file manipulation techniques


Following are examples of techniques that can be used to manipulate files using the MP language.
Because MP is a language, there are many possible applications. These examples provide a general
idea of what is possible.

Closing and reopening an alternate file with subprograms


With automatic subprograms, it is often desirable to have separate files generated for each subprogram.
To do this, you must rename the alternate output file for each new subprogram written. Because a
subprogram is called to be written at the postblocks psub_st_m and psub_st_s, a call to the example
postblocks psubname_m and psubname_s can be made from the appropriate postblock. You must filter
the calls so that the call is made only on the initial subprogram.
See Volume 2, How subprograms work for more information.

17-6 Mastercam Version 9 MP Post Processor Reference Guide June 2002


Volume 1 Chapter 17 File Manipulation

For example:
psub_st_m # Header in main level
if sub_sec_no = zero, psubname_m
*main_prg_no, "(PROGRAM - ", progname, ")", e
psub_st_s # Header in sub level
if sub_sec_no = one, psubname_s
*sub_prg_no, "(PROGRAM - ", progname, ")", e

This example creates a unique file name for each subprogram by appending a letter to the NC file
name. Initialize the following user-defined variables for use in the code snippet.
snameend # String for character to append
ascii_no : 65 # Ascii equivalent of character to append

The postblocks and the postlines in them assign a new subprogram name (alternate file name) when
they are called. Notice that the main subprogram is automatically assigned the sub file and the sub
subprogram was assigned the auxiliary sub file.
psubname_m # Name the main subprogram
snameend = no2asc(ascii_no)
snamesub = snamenc + snameend
newsub
ascii_no = ascii_no + 1

psubname_s # Name the sub subprogram


snameend = no2asc(ascii_no)
snamesub = snamenc + snameend
newaux
ascii_no = ascii_no + 1

The postlines first convert the ascii_no value to a character (initially to A). The character in the
resulting alternate file name is appended to the NC file name. Next, the command variable is
encountered, which closes the currently open file and reopens the stream with the new file name. The
last action is to index the numeric variable ascii_no to get the next character when either postblock is
called again.

Renaming the NC file after posting


When a job is posted from Mastercam, the NC file name is passed as an argument and cannot be
changed until after posting is complete. You can do this by using the formula functions in the MP
language. It is assumed that either the user or the post has assigned the new file name.
snewname # User defined string with the new file name
sprgname # User defined string with the program file name

For example:
ppost # The predefined postblock called after files are closed at the end of posting
sprgname = spathnc + snamenc + sextnc
result = remove(snewname)
result = rename(sprgname, snewname)

June 2002 Mastercam Version 9 MP Post Processor Reference Guide 17-7


Chapter 17 File Manipulation Volume 1

The above code snippet first builds the NC files path and file name with the extension. Next, the new
file is removed from the disk. (This is a precaution. If the file is on disk, it cannot be used to rename
another file.) Finally, the NC file is renamed with the new file name.

Opening an alternate file as a buffer file


You can reopen an alternate file (NC output) as a buffer file to further process the resulting NC code.
This might be required, for example, for renumbering or adding additional code based on the resulting
output.
The following example demonstrates the MP code required to reopen an alternate file as a buffer. First,
define some variables and the buffer file to be used:
result : 0 #A variable for a return value
rc1 :1 # A variable for the buffer read count
sz1 :1 #A variable for the size of the buffer
s_extname #A string variable for the entire ext file name
stringbuf #A string variable for the buffer read
fbuf 1 0 80 1 #Define buffer 1, initialize flag set to open a file

In the postblock pheader, set the output to the external alternate file:
pheader # Predefined postblock at file header
extprg = 1 # Open (and clear) the .EXT file
subout = 3 # Direct post output into this file

In the postblock peof, switch the output stream to the NC output and build the external file name. Next,
close the external file and the name is assigned to the first buffer file. The buffer file (formerly the
external file) is checked for the size and then the file is read and written to the NC output. This
continues until all the records are read because of the variables used in the “while” loop. The rc1
variable used for the read counter is automatically incremented by the rbuf command each time a
record is read. (Normally you would have code in the “while” loop to manipulate the read record.).
peof #End of file
n, "M30", e
s_extname = "C:\MCAM9\MILL\NC\" # Assign the path for the EXT file
s_extname = s_extname + snameext + sextext # Build up the complete file name
result = fclose(s_extname) # Close the EXT file
sbufname1 = s_extname # Now, open this file as a Buffer
subout = 0 # Set output stream to (main) NC file
size1 = rbuf(1, 0) # Get the size (# of records) in the buffer file by reading record ZERO
while rc1 <= size1, # Loop through the entire buffer file
[
stringbuf = rbuf(1, rc1) # Read a record (line) from the buffer file (rc1 auto increments)
stringbuf, e #This writes the record to the NC output file
]

See Volume 3, Formulas and Volume 2, How buffer files work for more information

17-8 Mastercam Version 9 MP Post Processor Reference Guide June 2002

You might also like