private static void AnalyzeCode(GCodeCollection col, GCode code) { List <GCodeParameterType> pars = new List <GCodeParameterType>(); foreach (GCodeParameter par in code.Parameters) { col.CommandCount++; string[] parBits = par.Value.Split('.'); string sDigits = (parBits[0].Substring(0, 1) == "-" ? parBits[0].Substring(1) : parBits[0]); int digits = sDigits.Length; int precision = 0; string sPrecision = ""; if (parBits.Length > 1) { sPrecision = parBits[1].TrimEnd('0'); precision = parBits[1].Length; } if (precision > col.MaxPrecision) { col.MaxPrecision = precision; } if (digits > col.MaxUnit) { col.MaxUnit = parBits[0].Length; } if (precision + digits > col.MaxSigDig) { col.MaxSigDig = precision + digits; } if (Convert.ToInt32(sDigits + sPrecision) > col.MaxInt) { col.MaxInt = Convert.ToInt32(sDigits + sPrecision); } pars.Add(par.Type); } col.ParamCount += code.Parameters.Count; pars.Sort(); StringBuilder sb = new StringBuilder(code.Command.ToString()); foreach (GCodeParameterType par in pars) { sb.Append(par.ToString()); } if (col.Commands.ContainsKey(sb.ToString())) { col.Commands[sb.ToString()] += 1; } else { col.Commands.Add(sb.ToString(), 1); } }
static GCodeCollection parse(TextReader reader, bool inMemory) { string line; Match match; GCodeCollection gcCol = new GCodeCollection(); gcCol.Delta = new Delta(); gcCol.Commands = new Dictionary <string, int>(); gcCol.ParamCount = 0; gcCol.MaxPrecision = 0; gcCol.MaxUnit = 0; gcCol.MaxSigDig = 0; gcCol.CommandCount = 0; Regex commandRegexObj = new Regex(@"(?:([GM;][\d]*)((?:[\t ]+[XYZSEF][\d.-]+)*)|(?:.*))", RegexOptions.Multiline); Regex parameterRegexObj = new Regex(@"[XYZSEF][\d.-]+", RegexOptions.Multiline); GCode current; gcCol.AsciiSize = 0; while ((line = reader.ReadLine()) != null) { gcCol.AsciiSize += line.Length + 2; if ((match = commandRegexObj.Match(line)).Success && match.Value != "") { if (match.Groups[1].Value == ";") { continue; } current = new GCode(); current.Command = (GCodeCommand)Enum.Parse(typeof(GCodeCommand), match.Groups[1].Value); MatchCollection allMatchResults = null; if (match.Groups.Count == 3) { allMatchResults = parameterRegexObj.Matches(match.Groups[2].Value); if (allMatchResults.Count > 0) { foreach (Match m in allMatchResults) { current.Parameters.Add(new GCodeParameter() { Type = (GCodeParameterType)Enum.Parse(typeof(GCodeParameterType), m.Value.Substring(0, 1)), Value = m.Value.Substring(1) }); } } } AnalyzeCode(gcCol, current); gcCol.Delta.Translate(current); if (inMemory) { gcCol.Add(current); } } } return(gcCol); }
static void Main(string[] args) { if (args.Length != 1 || !File.Exists(args[0])) { Console.WriteLine("File not found"); return; } GCodeCollection gcc = GCodeCollection.fromFile(args[0], false); HuffmanTree tree = HuffmanTree.Build(gcc.Commands); Console.WriteLine("Command occurance:"); int compressedCommandBits = 0; foreach (KeyValuePair <string, int> kvp in gcc.Commands) { Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value); compressedCommandBits += kvp.Value * tree.Bits(kvp.Key); } Console.WriteLine("Total commands: {0}", gcc.CommandCount); Console.WriteLine("Possible Huffmann bits: {0}", tree.maxdepth()); Console.WriteLine("Float max units: {0} precision: {1} sig digits {2} max int {3}", gcc.MaxUnit, gcc.MaxPrecision, gcc.MaxSigDig, gcc.MaxInt); int paramBits = (int)(1 + Math.Floor(Math.Log(gcc.MaxPrecision + 1, 2)) + Math.Floor(Math.Log(gcc.MaxInt + 1, 2))); Console.WriteLine("Bits to represent compressed float: {0}", paramBits); Console.WriteLine("Original gCode File size: {0}", gcc.AsciiSize); int compressed_size = (gcc.CommandCount + (gcc.ParamCount * 4)); Console.WriteLine("Compressed size (byte instruction {0}kb, w/o Huffmann, 4 byte floats {1}kb): {2}kb ({3:N2}% compression)", gcc.CommandCount / 1024, gcc.ParamCount * 4 / 1024, compressed_size / 1024, 100 - compressed_size * 100 / gcc.AsciiSize); Console.WriteLine("Compressed size (Huffmann instructions {0}kb, compressed floats {1}kb): {2}kb ({3:N2}% compression)", compressedCommandBits / (8 * 1024), (gcc.ParamCount * paramBits) / (8 * 1024), (compressedCommandBits + (gcc.ParamCount * paramBits)) / (8 * 1024), 100 - ((compressedCommandBits + (gcc.ParamCount * paramBits)) / 8) * 100 / gcc.AsciiSize); Console.WriteLine("CError: {0} JError: {1}", gcc.Delta.MaxCandidateError, gcc.Delta.MaxJohannError); Console.WriteLine("CSteps: {0} JSteps: {1} CDeltaSurp: {2}", gcc.Delta.CandidateTotalSteps, gcc.Delta.JohanTotalSteps, gcc.Delta.CandidateDeltaSurplus); Console.WriteLine("CDistance: {0} CMaxCallDepth: {1}", gcc.Delta.DistanceCalculations, gcc.Delta.MaxDepth); Console.ReadLine(); }