Professional Documents
Culture Documents
117 File Manipulation
117 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.
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
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)
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.
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.
Formula Use
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
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.
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)
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.
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