From 8d2794eb991c6230ad7e97763a3c88d16e792e3d Mon Sep 17 00:00:00 2001 From: Bernard Grosperrin Date: Fri, 17 Apr 2015 23:54:39 +0200 Subject: [PATCH] Added CultureInfo.InvariantCulture to all parse functions for float and double, as it is triggering an exception now if the current culture use commas to separate units and decimals. --- src/CNCInfusion/CustomPanel/CustomPanel.cs | 9 +-- src/CNCInfusion/Settings.cs | 21 ++++--- src/CNCInfusion/clsProcessor.cs | 71 +++++++++++----------- src/CNCInfusion/clsSettings.cs | 21 ++++--- src/CNCInfusion/frmViewer.cs | 17 +++--- src/CNCInfusion/grblPreprocessor.cs | 9 ++- 6 files changed, 78 insertions(+), 70 deletions(-) diff --git a/src/CNCInfusion/CustomPanel/CustomPanel.cs b/src/CNCInfusion/CustomPanel/CustomPanel.cs index 0f71295..eb4ff1e 100644 --- a/src/CNCInfusion/CustomPanel/CustomPanel.cs +++ b/src/CNCInfusion/CustomPanel/CustomPanel.cs @@ -1,5 +1,6 @@ -using System; - +using System; +using System.Globalization; + namespace Utility.Panel { @@ -349,8 +350,8 @@ public static void DrawBorder3D(System.Drawing.Graphics graphics, System.Drawing } public static int DoubleToInt(double value) - { - return System.Decimal.ToInt32(System.Decimal.Floor(System.Decimal.Parse((value).ToString()))); + { + return System.Decimal.ToInt32(System.Decimal.Floor(System.Decimal.Parse((value).ToString(), CultureInfo.InvariantCulture))); } } } \ No newline at end of file diff --git a/src/CNCInfusion/Settings.cs b/src/CNCInfusion/Settings.cs index 11ba0ff..a4398dd 100644 --- a/src/CNCInfusion/Settings.cs +++ b/src/CNCInfusion/Settings.cs @@ -7,7 +7,8 @@ * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; -using System.Drawing; +using System.Drawing; +using System.Globalization; using System.Windows.Forms; using System.Collections.Generic; @@ -113,15 +114,15 @@ string convertUnits(string readvalue, string setting) try { if(setting.Contains("steps/mm")) { - double fval = double.Parse(readvalue)*(1.0/TOINCHES); - convert = fval.ToString(); + double fval = double.Parse(readvalue, CultureInfo.InvariantCulture)*(1.0/TOINCHES); + convert = fval.ToString(CultureInfo.InvariantCulture); } - else if(setting.Contains("mm")) { - double fval = double.Parse(readvalue)*TOINCHES; - convert = fval.ToString(); + else if(setting.Contains("mm")) { + double fval = double.Parse(readvalue, CultureInfo.InvariantCulture) * TOINCHES; + convert = fval.ToString(CultureInfo.InvariantCulture); } - else if(setting.Contains("binary")) { - int fval = int.Parse(readvalue); + else if(setting.Contains("binary")) { + int fval = int.Parse(readvalue, CultureInfo.InvariantCulture); convert = GetIntBinaryString(fval); } else { @@ -181,8 +182,8 @@ void DataGridView1CellValidating(object sender, DataGridViewCellValidatingEventA // int.Parse(sVal); // TODO error checking is weak - try { - enteredValue = float.Parse(e.FormattedValue.ToString()); + try { + enteredValue = float.Parse(e.FormattedValue.ToString(), CultureInfo.InvariantCulture); } catch { MessageBox.Show("The value must be a non-negative number. You entered '" + diff --git a/src/CNCInfusion/clsProcessor.cs b/src/CNCInfusion/clsProcessor.cs index 7dc0f56..e4bf2b8 100644 --- a/src/CNCInfusion/clsProcessor.cs +++ b/src/CNCInfusion/clsProcessor.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections.Generic; +using System.Globalization; using System.Text.RegularExpressions; using System.Text; using MacGen; @@ -440,7 +441,7 @@ private float FormatAxis(string sVal, int precision) { //decimal place if (sVal.Contains(".")) { - return float.Parse(sVal); + return float.Parse(sVal, CultureInfo.InvariantCulture); } else { return (float)(float.Parse(sVal) * (Math.Pow(10, - precision)));//convert a number from a 4 place @@ -453,27 +454,27 @@ public void ProcessFile(string ncFile, List gfxRecs) mGfxRecs = gfxRecs; mCodefile = ncFile; { - mMotion.SubCall.Label = mCurMachine.Subcall[0]; - mMotion.SubCall.Value = int.Parse(mCurMachine.Subcall.Substring(1)); + mMotion.SubCall.Label = mCurMachine.Subcall[0]; + mMotion.SubCall.Value = int.Parse(mCurMachine.Subcall.Substring(1), CultureInfo.InvariantCulture); - mMotion.SubReturn.Label = mCurMachine.SubReturn[0]; - mMotion.SubReturn.Value = int.Parse(mCurMachine.SubReturn.Substring(1)); + mMotion.SubReturn.Label = mCurMachine.SubReturn[0]; + mMotion.SubReturn.Value = int.Parse(mCurMachine.SubReturn.Substring(1), CultureInfo.InvariantCulture); - mMotion.Abs.Label = mCurMachine.Absolute[0]; - mMotion.Abs.Value = int.Parse(mCurMachine.Absolute.Substring(1)); - mMotion.CCArc.Label = mCurMachine.CCArc[0]; - mMotion.CCArc.Value = int.Parse(mCurMachine.CCArc.Substring(1)); - mMotion.CWArc.Label = mCurMachine.CWArc[0]; - mMotion.CWArc.Value = int.Parse(mCurMachine.CWArc.Substring(1)); + mMotion.Abs.Label = mCurMachine.Absolute[0]; + mMotion.Abs.Value = int.Parse(mCurMachine.Absolute.Substring(1), CultureInfo.InvariantCulture); + mMotion.CCArc.Label = mCurMachine.CCArc[0]; + mMotion.CCArc.Value = int.Parse(mCurMachine.CCArc.Substring(1), CultureInfo.InvariantCulture); + mMotion.CWArc.Label = mCurMachine.CWArc[0]; + mMotion.CWArc.Value = int.Parse(mCurMachine.CWArc.Substring(1), CultureInfo.InvariantCulture); - mMotion.Inc.Label = mCurMachine.Incremental[0]; - mMotion.Inc.Value = int.Parse(mCurMachine.Incremental.Substring(1)); + mMotion.Inc.Label = mCurMachine.Incremental[0]; + mMotion.Inc.Value = int.Parse(mCurMachine.Incremental.Substring(1), CultureInfo.InvariantCulture); - mMotion.Linear.Label = mCurMachine.Linear[0]; - mMotion.Linear.Value = int.Parse(mCurMachine.Linear.Substring(1)); + mMotion.Linear.Label = mCurMachine.Linear[0]; + mMotion.Linear.Value = int.Parse(mCurMachine.Linear.Substring(1), CultureInfo.InvariantCulture); - mMotion.Rapid.Label = mCurMachine.Rapid[0]; - mMotion.Rapid.Value = int.Parse(mCurMachine.Rapid.Substring(1)); + mMotion.Rapid.Label = mCurMachine.Rapid[0]; + mMotion.Rapid.Value = int.Parse(mCurMachine.Rapid.Substring(1), CultureInfo.InvariantCulture); mMotion.Rotary.Label = mCurMachine.Rotary[0]; mMotion.Rotary.Value = 0; @@ -481,24 +482,24 @@ public void ProcessFile(string ncFile, List gfxRecs) mMotion.DrillRapid.Label = mCurMachine.DrillRapid[0]; mMotion.DrillRapid.Value = 0; - mMotion.Plane[0].Label = mCurMachine.XYplane[0]; - mMotion.Plane[0].Value = int.Parse(mCurMachine.XYplane.Substring(1)); - mMotion.Plane[1].Label = mCurMachine.XZplane[0]; - mMotion.Plane[1].Value = int.Parse(mCurMachine.XZplane.Substring(1)); - mMotion.Plane[2].Label = mCurMachine.YZplane[0]; - mMotion.Plane[2].Value = int.Parse(mCurMachine.YZplane.Substring(1)); + mMotion.Plane[0].Label = mCurMachine.XYplane[0]; + mMotion.Plane[0].Value = int.Parse(mCurMachine.XYplane.Substring(1), CultureInfo.InvariantCulture); + mMotion.Plane[1].Label = mCurMachine.XZplane[0]; + mMotion.Plane[1].Value = int.Parse(mCurMachine.XZplane.Substring(1), CultureInfo.InvariantCulture); + mMotion.Plane[2].Label = mCurMachine.YZplane[0]; + mMotion.Plane[2].Value = int.Parse(mCurMachine.YZplane.Substring(1), CultureInfo.InvariantCulture); - mMotion.ReturnLevel[0].Label = mCurMachine.ReturnLevel[0][0]; - mMotion.ReturnLevel[0].Value = int.Parse(mCurMachine.ReturnLevel[0].Substring(1)); - mMotion.ReturnLevel[1].Label = mCurMachine.ReturnLevel[1][0]; - mMotion.ReturnLevel[1].Value = int.Parse(mCurMachine.ReturnLevel[1].Substring(1)); + mMotion.ReturnLevel[0].Label = mCurMachine.ReturnLevel[0][0]; + mMotion.ReturnLevel[0].Value = int.Parse(mCurMachine.ReturnLevel[0].Substring(1), CultureInfo.InvariantCulture); + mMotion.ReturnLevel[1].Label = mCurMachine.ReturnLevel[1][0]; + mMotion.ReturnLevel[1].Value = int.Parse(mCurMachine.ReturnLevel[1].Substring(1), CultureInfo.InvariantCulture); for (int r = 0; r <= mMotion.Drills.Length - 1; r++) { if (mCurMachine.Drills[r].Length > 2) { - mMotion.Drills[r].Label = mCurMachine.Drills[r][0]; - mMotion.Drills[r].Value = int.Parse(mCurMachine.Drills[r].Substring(1)); + mMotion.Drills[r].Label = mCurMachine.Drills[r][0]; + mMotion.Drills[r].Value = int.Parse(mCurMachine.Drills[r].Substring(1), CultureInfo.InvariantCulture); } } } @@ -563,8 +564,8 @@ public void ProcessFile(string ncFile, List gfxRecs) p = new clsProg(); p.Main = false; p.Index = thisIndex; - p.Label = Char.ToUpper(m.Value[0]).ToString(); - p.Value = int.Parse(m.Groups[1].Value); + p.Label = Char.ToUpper(m.Value[0]).ToString(); + p.Value = int.Parse(m.Groups[1].Value, CultureInfo.InvariantCulture); mNcProgs.Add(p); lastIndex = m.Index; } @@ -643,11 +644,11 @@ private void ProcessSubWords(clsProg p) //Word mCurAddress.Label = Char.ToUpper(ncWord.Value[0]); mCurAddress.StringValue = ncWord.Groups[1].Value; - mCurAddress.Value = float.Parse(ncWord.Groups[1].Value); + mCurAddress.Value = float.Parse(ncWord.Groups[1].Value, CultureInfo.InvariantCulture); if (mCurAddress.Matches(mMotion.SubCall)) { - //M98 P. Use the next word value as the sub name - clsProg retProg = FindSubByValue(int.Parse(ncWord.NextMatch().Groups[1].Value)); + //M98 P. Use the next word value as the sub name + clsProg retProg = FindSubByValue(int.Parse(ncWord.NextMatch().Groups[1].Value, CultureInfo.InvariantCulture)); if ((retProg != null)) { if (retProg.TimesCalled > 100) return;//Prevent infinite loop diff --git a/src/CNCInfusion/clsSettings.cs b/src/CNCInfusion/clsSettings.cs index 7487010..ce5abe4 100644 --- a/src/CNCInfusion/clsSettings.cs +++ b/src/CNCInfusion/clsSettings.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Xml; using MacGen; @@ -101,7 +102,7 @@ public void LoadMachine(string sName) xReader.MoveToContent(); xReader.ReadToDescendant("Name"); Machine.Name = xReader.ReadElementContentAsString(); - Machine.Description = xReader.ReadElementContentAsString(); + Machine.Description = xReader.ReadElementContentAsString(); Machine.AbsArcCenter = bool.Parse(xReader.ReadElementContentAsString()); Machine.LatheMinus = bool.Parse(xReader.ReadElementContentAsString()); Machine.HelixPitch = bool.Parse(xReader.ReadElementContentAsString()); @@ -110,18 +111,18 @@ public void LoadMachine(string sName) Machine.Endmain = xReader.ReadElementContentAsString(); Machine.MachineType = (MachineType)Enum.Parse(typeof(MachineType), xReader.ReadElementContentAsString()); Machine.RotaryAxis = (Axis)Enum.Parse(typeof(Axis), xReader.ReadElementContentAsString()); - Machine.RotaryDir = (RotaryDirection)Enum.Parse(typeof(RotaryDirection), xReader.ReadElementContentAsString()); - Machine.Precision = int.Parse(xReader.ReadElementContentAsString()); + Machine.RotaryDir = (RotaryDirection)Enum.Parse(typeof(RotaryDirection), xReader.ReadElementContentAsString()); + Machine.Precision = int.Parse(xReader.ReadElementContentAsString(), CultureInfo.InvariantCulture); Machine.ProgramId = xReader.ReadElementContentAsString(); - Machine.SubReturn = xReader.ReadElementContentAsString(); - Machine.RotPrecision = int.Parse(xReader.ReadElementContentAsString()); + Machine.SubReturn = xReader.ReadElementContentAsString(); + Machine.RotPrecision = int.Parse(xReader.ReadElementContentAsString(), CultureInfo.InvariantCulture); Machine.RotaryType = (RotaryMotionType)Enum.Parse(typeof(RotaryMotionType), xReader.ReadElementContentAsString()); Machine.Searchstring = xReader.ReadElementContentAsString(); - for (r = 0; r <= Machine.ViewAngles.Length - 1; r++) { - Machine.ViewAngles[r] = float.Parse(xReader.ReadElementContentAsString()); + for (r = 0; r <= Machine.ViewAngles.Length - 1; r++) { + Machine.ViewAngles[r] = float.Parse(xReader.ReadElementContentAsString(), CultureInfo.InvariantCulture); } - for (r = 0; r <= Machine.ViewShift.Length - 1; r++) { - Machine.ViewShift[r] = float.Parse(xReader.ReadElementContentAsString()); + for (r = 0; r <= Machine.ViewShift.Length - 1; r++) { + Machine.ViewShift[r] = float.Parse(xReader.ReadElementContentAsString(), CultureInfo.InvariantCulture); } Machine.Absolute = xReader.ReadElementContentAsString(); Machine.Incremental = xReader.ReadElementContentAsString(); diff --git a/src/CNCInfusion/frmViewer.cs b/src/CNCInfusion/frmViewer.cs index 319bea7..8259450 100644 --- a/src/CNCInfusion/frmViewer.cs +++ b/src/CNCInfusion/frmViewer.cs @@ -1,5 +1,6 @@ using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Globalization; using System.Threading; using System.Windows.Forms; using System.Diagnostics; @@ -737,13 +738,13 @@ private void UpdatePositionLEDS(string str) //Debug.WriteLine(str + "\r\n"); - try { - mx = double.Parse(groups[1].Value.ToString()); - my = double.Parse(groups[2].Value.ToString()); - mz = double.Parse(groups[3].Value.ToString()); - wx = double.Parse(groups[4].Value.ToString()); - wy = double.Parse(groups[5].Value.ToString()); - wz = double.Parse(groups[6].Value.ToString()); + try { + mx = double.Parse(groups[1].Value, CultureInfo.InvariantCulture); + my = double.Parse(groups[2].Value, CultureInfo.InvariantCulture); + mz = double.Parse(groups[3].Value, CultureInfo.InvariantCulture); + wx = double.Parse(groups[4].Value, CultureInfo.InvariantCulture); + wy = double.Parse(groups[5].Value, CultureInfo.InvariantCulture); + wz = double.Parse(groups[6].Value, CultureInfo.InvariantCulture); if(GrblReportsInches) { mx = mx * TOINCHES; diff --git a/src/CNCInfusion/grblPreprocessor.cs b/src/CNCInfusion/grblPreprocessor.cs index dd7ebfd..096d463 100644 --- a/src/CNCInfusion/grblPreprocessor.cs +++ b/src/CNCInfusion/grblPreprocessor.cs @@ -7,7 +7,8 @@ * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Globalization; using System.Threading; using System.Windows.Forms; using System.Diagnostics; @@ -51,8 +52,10 @@ private bool GrblPreprocess(string line) MatchCollection matches = rgx.Matches(line); if (matches.Count > 0) { - foreach (Match match in matches) { - arg = double.Parse(match.Value.Substring(1)); + foreach (Match match in matches) + { + string valueStr = match.Value.Substring(1, match.Value.Length - 1); + arg = double.Parse(valueStr,CultureInfo.InvariantCulture); switch(match.Value[0]) { case 'G': // explicitly supported G codes