What is a Post-Processor?
A post-processor is a custom piece of software that translates instructions from CAM software into commands that can be understood by machines. These instructions are usually passed through a text file, where each line represents a movement, procedure, or function that the machine should perform. While G-code is the standard machine language, not all machines are the same (e.g., a laser cutter versus a lathe), so it is often necessary to use a post-processor to fine-tune the output from the CAM software.
How Does It Work?
Within CAM software, all the information needed to create a toolpath is stored in as standardized a format as possible. A CAM toolpath object contains a list of coordinates and direction vectors that the tool should follow, along with process inputs at each point such as movement type (rapid/feed), feed rates, spindle speeds, and tool properties like diameter and length, as well as any other relevant information for the machining task.
When it’s time to generate the NC file for a specific machine, the post-processor loops through all of this information and writes the correct sequence of commands into the NC file, which the machine will read, interpret, and execute.
Illustrating the concept with C#:
We have a (very) simplified toolpath structure:
class ArrayOfMove
{
List<Move> Moves; // List of moves
}
Where each move has the following structure:
class Move
{
string Type; // "Rapid"/"Linear"/...
double TargetX; // X target position
double TargetY; // Y target position
double TargetZ; // Z target position
}
The post-processor can be constructed as a function that takes such a Toolpath as input and prints the corresponding G-code into the NC file:
void PostProcess(ArrayOfMove toolPath)
{
// Initialize file content variable
string fileContent = "";
// Loop through each move in the toolpath
foreach (Move move in toolPath.Moves)
{
// Check the move type: G1 for linear feed, G0 for rapid
if (move.Type == "Linear")
fileContent += "G1"; // Feed move
else
fileContent += "G0"; // Rapid move
// Add the X, Y, and Z coordinates to the file content
fileContent += " X" + move.X.ToString();
fileContent += " Y" + move.Y.ToString();
fileContent += " Z" + move.Z.ToString();
// Add a new line after each move command
fileContent += "\r\n";
}
// Write the complete file content to a .nc file
File.WriteAllText("C:\\myNCFile.nc", fileContent);
}
Building custom post-processor with NCnetic.
When NCnetic or any backplotter reads an NC file, it performs the reverse operation of a post-processor. The NC file is translated back into its standard native structure.
It is then possible to reprocess the file and output a new file format.
To do this, an ’export’ function is available, supporting XML, JSON, or CSV formats. In the ‘Options & Settings’ window, go to the ‘EXPORT’ section and select the ‘EXPORT FILE TYPE’. You can enable the ‘FILE DIALOG’ property (set it to true) to open a file-saving dialog window.
The output file can then be processed by a custom application by checking the ‘RUN EXE’ property and specifying an *.exe file in the ‘EXE PATH’ property.
When the ’export’ is performed, the executable is called with the exported file as an argument, which is equivalent to performing this action in a command prompt:
c:\MyCustomExe.exe "c:\ExportedFile.xml"
Additionally, it is possible to call a script by enabling the ‘INCLUDE SCRIPT’ property and specifying a script file in the ‘SCRIPT PATH’ property. For example, running Python with a *.py script during ’export’ is equivalent to performing this action in a command prompt:
python c:\MyPythonScript.py "c:\ExportedFile.xml"
The executable should read the exported file, extract relevant informations, and write (post-process) a new output file.
Practical code sample: DXF post-processor
The following code is the Program.cs content of a .Net console application. It will convert the XML export into a DXF file in the XY view (Use ‘EXPORT=XML’ & ‘FILE DIALOG=true’).
Download link: https://www.ncnetic.com/Program.cs.txt