diff --git a/LaserGRBL/GrblCore.cs b/LaserGRBL/GrblCore.cs index eb9a30ad8..0484b6d92 100644 --- a/LaserGRBL/GrblCore.cs +++ b/LaserGRBL/GrblCore.cs @@ -143,6 +143,11 @@ public void OpenFile() } } + public void SaveProgram(string filename) + { + if (HasProgram) + file.SaveProgram(filename); + } public void ExportConfig(string filename) { diff --git a/LaserGRBL/GrblFile.cs b/LaserGRBL/GrblFile.cs index 4cbcbd90e..657ad0dc4 100644 --- a/LaserGRBL/GrblFile.cs +++ b/LaserGRBL/GrblFile.cs @@ -19,6 +19,19 @@ public class GrblFile : IEnumerable private TimeSpan mEstimatedTimeOn; private TimeSpan mEstimatedTimeOff; + public void SaveProgram(string filename) + { + try + { + using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename)) + { + foreach (GrblCommand cmd in list) + sw.WriteLine(cmd.Command); + sw.Close(); + } + } catch{} + } + public void LoadFile(string filename) { long start = Tools.HiResTimer.TotalMilliseconds; @@ -65,7 +78,7 @@ public void LoadImage(Bitmap image, string filename, int resolution, int oX, int bool mustG1 = false; bool rtl = true; //right to left - SetHeader(travelSpeed, ref mustG1); + SetHeader(minPower, travelSpeed, ref mustG1); int curSpeed = travelSpeed; for (int Y = H-1; Y >= 0; Y--) //per ogni linea dell'immagine @@ -99,7 +112,7 @@ public void LoadImage(Bitmap image, string filename, int resolution, int oX, int { int newSpeed = (power == 0 ? travelSpeed : markSpeed); if (curSpeed != newSpeed) - CreateSegment(power, rtl ? X : X+1, oX, resolution, ref mustG1, String.Format("F{0}", curSpeed = newSpeed )); + CreateSegment(power, rtl ? X : X+1, oX, resolution, ref mustG1, curSpeed = newSpeed); else CreateSegment(power, rtl ? X : X+1, oX, resolution, ref mustG1); } @@ -115,7 +128,7 @@ public void LoadImage(Bitmap image, string filename, int resolution, int oX, int rtl = !rtl; list.Add(new GrblCommand(lOff)); //spegni il laser - IncrementY(H-Y, oY, resolution, ref mustG1, String.Format("F{0}", curSpeed = travelSpeed )); + IncrementY(H-Y, oY, resolution, ref mustG1, curSpeed = travelSpeed); } Analyze(); @@ -132,29 +145,31 @@ private int GetColor(Bitmap I, int X, int Y, int min, int max) return rv * (max - min) / 255 + min; //scale to range } - private void SetHeader(int speed, ref bool mustG1) + private void SetHeader(int power, int speed, ref bool mustG1) { list.Add(new GrblCommand("G90")); list.Add(new GrblCommand(String.Format("F{0}", speed))); - list.Add(new GrblCommand("G0X0Y0")); - list.Add(new GrblCommand("M5S0")); + list.Add(new GrblCommand("G0 X0 Y0")); + list.Add(new GrblCommand(String.Format("M5 S{0}", power))); mustG1 = true; } private void LaserSpeed(int speed) {list.Add(new GrblCommand(String.Format("F{0}", speed))); } - private void IncrementY(int Y, int oY, int res, ref bool mustG1, string f = null) + private void IncrementY(int Y, int oY, int res, ref bool mustG1, int speed) { - list.Add(new GrblCommand(string.Format("G0Y{0}S0{1}", formatnumber(oY + (double)Y / (double)res), f))); //muovi a posizione Y + list.Add(new GrblCommand(string.Format("G0 Y{0} F{1}", formatnumber(oY + (double)Y / (double)res), speed))); //muovi a posizione Y mustG1 = true; } - private void CreateSegment(int power, int X, int oX, int res, ref bool mustG1, string f = null) + private void CreateSegment(int power, int X, int oX, int res, ref bool mustG1, int speed = 0) { + string fcommand = speed > 0 ? string.Format(" F{0}", speed) : ""; + if (mustG1) - list.Add(new GrblCommand(string.Format("G1X{0}S{1}{2}", formatnumber(oX + (double)X / (double)res) , power, f))); //il laser non si spegne durante tutto il movimento X + list.Add(new GrblCommand(string.Format("G1 X{0} S{1}{2}", formatnumber(oX + (double)X / (double)res) , power, fcommand))); //il laser non si spegne durante tutto il movimento X else - list.Add(new GrblCommand(string.Format("X{0}S{1}{2}", formatnumber(oX + (double)X / (double)res) , power, f))); //il laser non si spegne durante tutto il movimento X + list.Add(new GrblCommand(string.Format("X{0} S{1}{2}", formatnumber(oX + (double)X / (double)res) , power, fcommand))); //il laser non si spegne durante tutto il movimento X mustG1 = false; } @@ -197,7 +212,7 @@ private void Process(Graphics g, Size s) if (analyze) { mRange.ResetRange(); - mRange.UpdateSRange(0); + //mRange.UpdateSRange(0); mRange.UpdateXYRange(0, 0, false); mTotalTravelOn = 0; mTotalTravelOff = 0; @@ -224,7 +239,12 @@ private void Process(Graphics g, Size s) speed = cmd.F.Number; if (cmd.S != null) - curAlpha = mRange.SpindleRange.ValidRange ? (int)(cmd.S.Number * 255 / mRange.SpindleRange.S.Max) : 255; + { + if (mRange.SpindleRange.ValidRange) + curAlpha = (int)((cmd.S.Number - mRange.SpindleRange.S.Min) * 255 / (mRange.SpindleRange.S.Max - mRange.SpindleRange.S.Min)); + else + curAlpha = 255; + } if (analyze && cmd.S != null) mRange.UpdateSRange(cmd.S.Number); @@ -474,7 +494,7 @@ public void ResetRange() } public bool ValidRange - { get { return Min != decimal.MaxValue && Max != decimal.MinValue && Max > 0; } } + { get { return Min != Max && Min != decimal.MaxValue && Max != decimal.MinValue && Max > 0; } } } public Range S = new Range(); diff --git a/LaserGRBL/MainForm.Designer.cs b/LaserGRBL/MainForm.Designer.cs index 409a1a11e..1315cf729 100644 --- a/LaserGRBL/MainForm.Designer.cs +++ b/LaserGRBL/MainForm.Designer.cs @@ -72,6 +72,8 @@ private void InitializeComponent() this.MnExit = new System.Windows.Forms.ToolStripMenuItem(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.MnFileOpen = new System.Windows.Forms.ToolStripMenuItem(); + this.MnSaveProgram = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator(); this.MnFileSend = new System.Windows.Forms.ToolStripMenuItem(); this.windowsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.joggingToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -289,6 +291,8 @@ private void InitializeComponent() // this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.MnFileOpen, + this.MnSaveProgram, + this.toolStripMenuItem1, this.MnFileSend}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20); @@ -297,15 +301,27 @@ private void InitializeComponent() // MnFileOpen // this.MnFileOpen.Name = "MnFileOpen"; - this.MnFileOpen.Size = new System.Drawing.Size(103, 22); - this.MnFileOpen.Text = "&Open"; + this.MnFileOpen.Size = new System.Drawing.Size(165, 22); + this.MnFileOpen.Text = "&Open File"; this.MnFileOpen.Click += new System.EventHandler(this.MnFileOpen_Click); // + // MnSaveProgram + // + this.MnSaveProgram.Name = "MnSaveProgram"; + this.MnSaveProgram.Size = new System.Drawing.Size(165, 22); + this.MnSaveProgram.Text = "&Save Program"; + this.MnSaveProgram.Click += new System.EventHandler(this.MnSaveProgramClick); + // + // toolStripMenuItem1 + // + this.toolStripMenuItem1.Name = "toolStripMenuItem1"; + this.toolStripMenuItem1.Size = new System.Drawing.Size(162, 6); + // // MnFileSend // this.MnFileSend.Name = "MnFileSend"; - this.MnFileSend.Size = new System.Drawing.Size(103, 22); - this.MnFileSend.Text = "&Send"; + this.MnFileSend.Size = new System.Drawing.Size(165, 22); + this.MnFileSend.Text = "Send To &Machine"; this.MnFileSend.Click += new System.EventHandler(this.MnFileSend_Click); // // windowsToolStripMenuItem @@ -436,6 +452,8 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem MnUnlock; private System.Windows.Forms.ToolStripSeparator toolStripMenuItem3; private System.Windows.Forms.ToolStripMenuItem MnExit; + private System.Windows.Forms.ToolStripMenuItem MnSaveProgram; + private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1; } } diff --git a/LaserGRBL/MainForm.cs b/LaserGRBL/MainForm.cs index 4a9502df8..1291ece2e 100644 --- a/LaserGRBL/MainForm.cs +++ b/LaserGRBL/MainForm.cs @@ -109,6 +109,7 @@ private void TimerUpdate() TTLEstimated.Text = "Estimated Time:"; MnFileOpen.Enabled = Core.CanLoadNewFile; + MnSaveProgram.Enabled = Core.HasProgram; MnFileSend.Enabled = Core.CanSendFile; MnExportConfig.Enabled = Core.CanImportExport; MnImportConfig.Enabled = Core.CanImportExport; @@ -274,6 +275,21 @@ private void MnDisconnect_Click(object sender, EventArgs e) { Core.CloseCom(); } + void MnSaveProgramClick(object sender, EventArgs e) + { + string filename = null; + using (System.Windows.Forms.SaveFileDialog ofd = new SaveFileDialog()) + { + ofd.Filter = "GCODE Files|*.nc"; + ofd.AddExtension = true; + ofd.RestoreDirectory = true; + if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) + filename = ofd.FileName; + } + + if (filename != null) + {Core.SaveProgram(filename);} + } }