Post Processor
When a DXF file or a predefined shape is to be converted into G-code and imported into the system, the Post Processor macro is used. When the operator opens a DXF file or one of the predefined shapes, HSC Studio sequentially scans the shapes and directs them to the Post Processor macro. The Post Processor macro, like other C# macros, should be created in the macros section under the name PostPro. HSC Studio includes a predefined post class to transfer the relevant values to this macro program and accumulate the generated G-codes. This class can be accessed within the macro program without the need for any assignments. The created macro must contain a function named PostProCmd. HSC Studio sends the outputs to this function as a string.
public void PostProCmd(string cmd)
{
// Post processor redirections should be made here.
}
Post Processor command list:
Init
Finish
Comment
BeginLayer
EndLayer
Line
Arc
StartDistance
EndDistance
post class:
public float Tolerance = 0.1F;
public int CompSide = 0;
public float StartX = 0;
public float StartY = 0;
public float StartZ = 0;
public float EndX = 0;
public float EndY = 0;
public float EndZ = 0;
public float ArcCenterX = 0;
public float ArcCenterY = 0;
public float ArcRadius = 0;
public bool ArcDir = false;
public float ZRetract = 0;
public float ZSafePos = 0;
public float ZCutPos = 0;
public int Tool = 0;
public float Feed = 0;
public float ZFeed = 0;
public int SpindleDir = 0;
public int SpindleSpeed = 0;
public string Comment = "";
public string LayerName = "";
public string Label = "";
/// <summary>
/// Adds the given string to the Output variable
/// </summary>
/// <param name="s"> The string to be added to the Output variable </param>
/// <returns></returns>
public void Add(string s)
/// <summary>
/// Adds the given string to the Output variable,
//appending newline characters at the end
/// </summary>
/// <param name="s"> The string to be added to the Output variable, with newline ///characters appended </param>
/// <returns></returns>
public void AddLine(string s)
/// <summary>
/// Formats the given double value and command, then returns it as a string
/// </summary>
/// <param name="cmd"></param>
/// <param name="n"></param>
/// <returns></returns>
public string Cmd(string cmd, float n, int precision)
Example Post Processor Macro:
// Local variables
int precision = 4; // Number of decimal places
int compSide = 0; // Tool radius compensation direction(0: OFF,1:LEFT,2: RIGHT)
int spdDir = 0; // Last spindle rotation direction
int spdSpeed = 0; // Last spindle speed
int tool = 0; // Last tool
float curX = float.MinValue; // Last processed X position
float curY = float.MinValue; // Last processed Y position
float curZ = float.MinValue; // Last processed Z position
float curF = float.MinValue; // Last given feedrate command
int NCounter = 1;
bool g50_1 = false;
/// <summary>
/// Leave blank
/// </summary>
public void Main()
{
}
/// <summary>
/// Main method for conversion
/// <param name="cmd">Command</param>
/// </summary>
public void PostProCmd(string cmd)
{
switch (cmd)
{
case "Init":
Init();
break;
case "Finish":
Finish();
break;
case "Comment":
Comment();
break;
case "BeginLayer":
BeginLayer();
break;
case "EndLayer":
EndLayer();
break;
case "Line":
Line();
break;
case "Arc":
Arc();
break;
case "StartDistance":
break;
case "EndDistance":
break;
default:
break;
}
}
/// <summary>
/// Called just before starting to generate the G-code file
/// Required initial assignments can be made here
/// </summary>
private void Init()
{
compSide = 0;
feed = 0;
spdDir = 0;
spdSpeed = 0;
tool = 0;
curX = float.MinValue;
curY = float.MinValue;
curZ = float.MinValue;
curF = float.MinValue;
post.AddLine("(THIS FILE IS CREATED BY HSC STUDIO)");
post.AddLine("(-- www.hsckontrol.com ---)");
post.AddLine("");
post.AddLine("G54 G90 G00 G40 G49 G80 G21");
post.AddLine("");
}
/// <summary>
/// Code block to be written at the end of the file
/// </summary>
private void Finish()
{
// Turn off tool radius compensation (if enabled)
if (compSide != 0) post.AddLine("G40");
// Spindle stop
post.AddLine("M5");
// To the Z reference position
post.AddLine("G53 G90 G00 Z0.");
post.AddLine("M30 (END OF PROGRAM)");
post.AddLine("%");
// Save to the G-code file
string file_name = "C:\\shape.cnc";
using (StreamWriter writer = new StreamWriter(file_name, false))
{
writer.Write(post.Output);
}
// Copy to Pulser
CopyGCode(file_name);
}
/// <summary>
/// Description
/// <param name="post.Comment"> Description content </param>
/// </summary>
private void Comment()
{
post.AddLine("(" + post.Comment + ")");
}
/// <summary>
/// A new layer is being started
/// </summary>
private void BeginLayer()
{
}
/// <summary>
/// Layer completed
/// </summary>
private void EndLayer()
{
}
/// <summary>
/// Code block required for linear cutting motion
/// <param name="post.StartX"> Starting X coordinate of the motion </param>
/// <param name="post.StartY"> Starting Y coordinate of the motion </param>
/// <param name="post.StartZ"> Starting Z coordinate of the motion </param>
/// <param name="post.EndX"> Target X coordinate of the motion </param>
/// <param name="post.EndY"> Target Y coordinate of the motion </param>
/// <param name="post.EndZ"> Target Z coordinate of the motion </param>
/// </summary>
private void Line()
{
// Frame lines are skipped
if (post.Label == "Border") return;
// If necessary, a rapid move to the starting point of the motion
RapidMoveToStartPos();
// Move to the Z starting coordinate
if (curZ != post.StartZ)
{
// Slow move to the Z axis cutting start coordinate
post.AddLine("G01" + post.Cmd("Z", post.StartZ, precision) +
post.Cmd("F", post.ZFeed, precision));
curZ = post.StartZ;
}
// Pouring start has been completed
if (g50_1 == true)
{
post.Add("G50.1 ");
g50_1 = false;
}
// Linear cutting command
post.Add("G01 ");
// X target
if (post.StartX != post.EndX)
post.Add(post.Cmd("X", post.EndX, precision));
// Y target
if (post.StartY != post.EndY)
post.Add(post.Cmd("Y", post.EndY, precision));
// Z target
if (post.StartZ != post.EndZ)
post.Add(post.Cmd("Z", post.EndZ, precision));
// F target
if (curF != post.Feed)
post.Add(post.Cmd("F", post.Feed, 1));
// End of The Line
post.AddLine("");
// Save the current position
curX = post.EndX;
curY = post.EndY;
curZ = post.EndZ;
curF = post.Feed;
}
/// <summary>
/// Code block required for circular cutting motion
/// <param name="post.StartX"> Starting X coordinate of the movement </param>
/// <param name="post.StartY"> Starting Y coordinate of the movement </param>
/// <param name="post.StartZ"> Starting Z coordinate of the movement </param>
/// <param name="post.EndX"> Target X coordinate of the movement </param>
/// <param name="post.EndY"> Target Y coordinate of the movement </param>
/// <param name="post.EndZ"> Target Z coordinate of the movement </param>
/// <param name="post.ArcCenterX"> X coordinate of the circle center </param>
/// <param name="post.ArcCenterY"> Y coordinate of the circle center </param>
/// <param name="post.ArcRadius"> Radius of the circle </param>
/// <param name="post.ArcDir"> Direction of the circle rotation false:CCW,true:CW
/// </summary>
private void Arc()
{
// Frame lines are skipped
if (post.Label == "Border") return;
// Rapid move to the starting point of the motion if necessary
RapidMoveToStartPos();
// Move to the Z starting coordinate
if (curZ != post.StartZ)
{
// Slow move to the Z axis cutting start coordinate
post.AddLine("G01" + post.Cmd("Z", post.StartZ, precision) +
post.Cmd("F", post.ZFeed, precision));
curZ = post.StartZ;
}
// Pouring start has been completed
if (g50_1 == true)
{
post.Add("G50.1 ");
g50_1 = false;
}
// Arc cutting command
if (post.ArcDir == false) post.Add("G03 ");
else post.Add("G02 ");
// X target
if (post.StartX != post.EndX)
post.Add(post.Cmd("X", post.EndX, precision));
// Y target
if (post.StartY != post.EndY)
post.Add(post.Cmd("Y", post.EndY, precision));
// Z target
if (post.StartZ != post.EndZ)
post.Add(post.Cmd("Z", post.EndZ, precision));
// Arc center X (I command)
post.Add(post.Cmd("I", post.ArcCenterX - post.StartX, precision));
// Arc center Y (J command)
post.Add(post.Cmd("J", post.ArcCenterY - post.StartY, precision));
// F target
if (curF != post.Feed)
post.Add(post.Cmd("F", post.Feed, 1));
// End of The Line
post.AddLine("");
// Store current position
curX = post.EndX;
curY = post.EndY;
curZ = post.EndZ;
curF = post.Feed;
}
/// <summary>
/// Rapid move to the start of the motion - if necessary
/// </summary>
private void RapidMoveToStartPos()
{
/*
if(post.Tool != tool){
post.AddLine( "M06 T" + post.Tool.ToString());
post.AddLine( "G43 H" + post.Tool.ToString());
tool = post.Tool;
}
*/
if ((post.SpindleDir != spdDir) || (post.SpindleSpeed != spdSpeed))
{
switch (post.SpindleDir)
{
case 1:
post.AddLine("M03 S" + post.SpindleSpeed.ToString());
break;
case 2:
post.AddLine("M04 S" + post.SpindleSpeed.ToString());
break;
default:
post.AddLine("M05");
break;
}
spdDir = post.SpindleDir;
spdSpeed = post.SpindleSpeed;
}
if ((Math.Abs(post.StartX - curX) > post.Tolerance)
|| (Math.Abs(post.StartY - curY) > post.Tolerance))
{
// Disable tool radius compensation (if enabled) before rapid move
if (compSide != 0) post.AddLine("G40");
// Rapid move to Z retract position
post.AddLine("G00" + post.Cmd("Z", post.ZRetract, precision));
// Add N numbers
post.AddLine("N" + NCounter++.ToString());
// Enable tool radius compensation before starting cutting
if (post.CompSide == 1) post.AddLine("G41 D" + post.Tool.ToString());
else if (post.CompSide == 2) post.AddLine("G42 D" + post.Tool.ToString());
compSide = post.CompSide;
// Rapid move to the XY cutting start point
post.AddLine("G00" + post.Cmd("X", post.StartX, precision) +
post.Cmd("Y", post.StartY, precision));
// Z Rapid move to the axis safety point
post.AddLine("G50.2 G00" + post.Cmd("Z", post.ZSafePos, precision));
// Slow move to the Z-axis cutting coordinate
post.AddLine("G01" + post.Cmd("Z", post.StartZ, precision) +
post.Cmd("F", post.ZFeed, precision));
// Store current position
curX = post.StartX;
curY = post.StartY;
curZ = post.StartZ;
curF = post.ZFeed;
}
}
