void BtnSaveClick(object sender, EventArgs e) { string newFileName = Path.GetFileNameWithoutExtension(QuickSettings.Get["LastOpenedFile"]) + "_optimized.gcode"; sfdSaveDialog.FileName = newFileName; var result = sfdSaveDialog.ShowDialog(); if (result == DialogResult.OK) { // save original //GCodeUtils.SaveGCode(_alg.BestPath, _points, sfdSaveDialog.FileName + "_orig.gcode"); // first sort by z-order var sortedBestPath = GCodeUtils.SortBlocksByZDepth(_alg.BestPath, _points); // then save GCodeUtils.SaveGCode(sortedBestPath, _points, sfdSaveDialog.FileName); } }