Plt file is described by HPGL, which is the primary printer control language used by Hewlett-Packard plotters. Sometimes we want to get the printable area of a PLT file, how to do it?
My solution is to extract the coordinates in PLT file and find out the boundary rectangle.
Following is the sample code by C#:
string filename = @"C:/Documents/test.PLT" ;//make sure you have this file
StreamReader sr = new StreamReader(filename);
string input = sr.ReadToEnd();
sr.Close();
//use regular expression to extract the coordinates boundary
Regex reg = new Regex("PDPA[//d+,//d+]+;");
MatchCollection matches = reg.Matches(input);
// if following coordinates are divided by 400, the unit will be mm
int xMax = 0;
int xMin = 0;
int yMax = 0;
int yMin = 0;
//find out the coordinates boundary
foreach (Match m in matches)
{
//remove 'PDPA'
string v = m.Value.Substring(4,m.Value.Length-5);
string[] coords = v.Split(',');
IEnumerable<string> xCoords = coords.Where((s, i) => i%2 == 0);
IEnumerable<string> yCoords = coords.Where((s, i) => i % 2 != 0);
int LocalXMax = xCoords.Max((s)=>Int32.Parse(s));
int LocalXMin = xCoords.Min((s) => Int32.Parse(s));
int LocalYMax = yCoords.Max((s) => Int32.Parse(s));
int LocalYMin = yCoords.Min((s) => Int32.Parse(s));
xMax = xMax >= LocalXMax ? xMax : LocalXMax;
xMin = xMin <= LocalXMin ? xMin : LocalXMin;
yMax = yMax >= LocalYMax ? yMax : LocalYMax;
yMin = yMin <= LocalYMax ? yMin : LocalYMin;
}
//draw the boundary
string outFileName = filename + "out.plt";
string pt1 = "PA" + xMin +"," + yMin + ";";
string pt2 = "PA" + xMin + "," + yMax + ";";
string pt3 = "PA" + xMax + "," + yMax + ";";
string pt4 = "PA" + xMax + "," + yMin + ";";
string appendingText = pt1 + "PD;" + pt2 + pt3 + pt4 + pt1 + "PU;SP;PG1";
string text = input.Replace("PG1",appendingText);
StreamWriter sw = new StreamWriter(outFileName);
sw.Write(text);
sw.Flush();
sw.Close();