public bool SetSetting(string keyToSet, string valueToSetTo) { valueToSetTo = valueToSetTo.Replace("\"", "").Trim(); List <string> lines = new List <string>(); FieldInfo[] fields; fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo field in fields) { System.Attribute[] attributes = System.Attribute.GetCustomAttributes(field); List <string> possibleNames = new List <string>(); possibleNames.Add(field.Name); foreach (Attribute attribute in attributes) { LegacyName legacyName = attribute as LegacyName; if (legacyName != null) { possibleNames.Add(legacyName.Name); } } if (possibleNames.Contains(keyToSet)) { string name = field.Name; object value = field.GetValue(this); switch (field.FieldType.Name) { case "Int32": field.SetValue(this, (int)double.Parse(valueToSetTo)); break; case "Double": field.SetValue(this, double.Parse(valueToSetTo)); break; case "Boolean": field.SetValue(this, bool.Parse(valueToSetTo)); break; case "FMatrix3x3": { field.SetValue(this, new FMatrix3x3(valueToSetTo)); } break; case "DoublePoint": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); string[] xyValues = bracketContents.Split(','); field.SetValue(this, new DoublePoint(double.Parse(xyValues[0]), double.Parse(xyValues[1]))); } break; case "IntPoint": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); string[] xyValues = bracketContents.Split(','); field.SetValue(this, new IntPoint(double.Parse(xyValues[0]), double.Parse(xyValues[1]))); } break; case "IntPoint[]": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); List <IntPoint> points = new List <IntPoint>(); string intPointString; int nextIndex = GetInsides(out intPointString, bracketContents, '[', ']', 0); do { string[] xyValues = intPointString.Split(','); points.Add(new IntPoint(double.Parse(xyValues[0]) * 1000, double.Parse(xyValues[1]) * 1000)); nextIndex = GetInsides(out intPointString, bracketContents, '[', ']', nextIndex); } while (nextIndex != -1); field.SetValue(this, points.ToArray()); } break; case "String": field.SetValue(this, valueToSetTo.Replace("\\n", "\n")); break; case "REPAIR_OVERLAPS": case "REPAIR_OUTLINES": case "SUPPORT_TYPE": case "INFILL_TYPE": case "OUTPUT_TYPE": try { valueToSetTo = valueToSetTo.Replace('|', ','); field.SetValue(this, Enum.Parse(field.FieldType, valueToSetTo)); } catch (Exception) { } break; default: throw new NotImplementedException("unknown type"); } return(true); } } return(false); }
// .35 mm for .4 mm nozzle public bool SetSetting(string keyToSet, string valueToSetTo) { valueToSetTo = valueToSetTo.Replace("\"", "").Trim(); List <string> lines = new List <string>(); foreach (PropertyInfo property in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { // List of case insensitive names that will import as this property HashSet <string> possibleNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase); possibleNames.Add(property.Name); // Including any mapped LegacyName attributes foreach (Attribute attribute in Attribute.GetCustomAttributes(property)) { LegacyName legacyName = attribute as LegacyName; if (legacyName != null) { possibleNames.Add(legacyName.Name); } } if (possibleNames.Contains(keyToSet)) { switch (property.PropertyType.Name) { case "Int32": property.SetValue(this, (int)double.Parse(valueToSetTo)); break; case "Double": property.SetValue(this, double.Parse(valueToSetTo)); break; case "Boolean": property.SetValue(this, bool.Parse(valueToSetTo)); break; case "FMatrix3x3": { property.SetValue(this, new FMatrix3x3(valueToSetTo)); } break; case "DoublePoint": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); string[] xyValues = bracketContents.Split(','); property.SetValue(this, new DoublePoint(double.Parse(xyValues[0]), double.Parse(xyValues[1]))); } break; case "IntPoint": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); string[] xyValues = bracketContents.Split(','); property.SetValue(this, new IntPoint(double.Parse(xyValues[0]), double.Parse(xyValues[1]))); } break; case "IntPoint[]": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); List <IntPoint> points = new List <IntPoint>(); string intPointString; int nextIndex = GetInsides(out intPointString, bracketContents, '[', ']', 0); do { string[] xyValues = intPointString.Split(','); points.Add(new IntPoint(double.Parse(xyValues[0]) * 1000, double.Parse(xyValues[1]) * 1000)); nextIndex = GetInsides(out intPointString, bracketContents, '[', ']', nextIndex); } while (nextIndex != -1); property.SetValue(this, points.ToArray()); } break; case "String": property.SetValue(this, valueToSetTo.Replace("\\n", "\n")); break; case "REPAIR_OVERLAPS": case "REPAIR_OUTLINES": case "SUPPORT_TYPE": case "INFILL_TYPE": case "OUTPUT_TYPE": try { valueToSetTo = valueToSetTo.Replace('|', ','); property.SetValue(this, Enum.Parse(property.PropertyType, valueToSetTo)); } catch (Exception) { } break; default: throw new NotImplementedException("unknown type"); } return(true); } } return(false); }
public void DumpSettings(string fileName) { List <string> lines = new List <string>(); FieldInfo[] fields; fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo field in fields) { string fieldDescription = ""; System.Attribute[] attributes = System.Attribute.GetCustomAttributes(field); foreach (Attribute attribute in attributes) { LegacyName legacyName = attribute as LegacyName; if (legacyName != null) { string Name = legacyName.Name; } SettingDescription description = attribute as SettingDescription; if (description != null) { fieldDescription = " # {0}".FormatWith(description.Description); } } string name = field.Name; object value = field.GetValue(this); switch (field.FieldType.Name) { case "Int32": case "Double": case "Boolean": case "FMatrix3x3": // all these setting just output correctly with ToString() so we don't have to do anything special. lines.Add("{0}={1}{2}".FormatWith(name, value, fieldDescription)); break; case "IntPoint": lines.Add("{0}={1}{2}".FormatWith(name, ((IntPoint)value).OutputInMm(), fieldDescription)); break; case "DoublePoint": lines.Add("{0}=[{1},{2}]{3}".FormatWith(name, ((DoublePoint)value).X, ((DoublePoint)value).Y, fieldDescription)); break; case "IntPoint[]": { IntPoint[] valueIntArray = value as IntPoint[]; string values = "["; bool first = true; foreach (IntPoint intPoint in valueIntArray) { if (!first) { values += ","; } values = values + intPoint.OutputInMm(); first = false; } lines.Add("{0}={1}]{2}".FormatWith(name, values, fieldDescription)); } break; case "String": if (fieldDescription != "") { throw new Exception("We can't output a description on a string as we need to write whatever the string says."); } // change the cariage returns to '\n's in the file lines.Add("{0}={1}".FormatWith(name, value).Replace("\n", "\\n")); break; case "REPAIR_OUTLINES": case "REPAIR_OVERLAPS": case "SUPPORT_TYPE": case "OUTPUT_TYPE": case "INFILL_TYPE": // all the enums can be output by this function lines.Add("{0}={1} # {2}{3}".FormatWith(name, value, GetEnumHelpText(field.FieldType, field.FieldType.Name), fieldDescription)); break; default: throw new NotImplementedException("unknown type '{0}'".FormatWith(field.FieldType.Name)); } } lines.Sort(); using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName)) { foreach (string line in lines) { file.WriteLine(line); } } }
// .35 mm for .4 mm nozzle public bool SetSetting(string keyToSet, string valueToSetTo) { // Leave quoted string in place for additionalArgsToProcess - quotes required for file paths if (keyToSet != nameof(AdditionalArgsToProcess) && keyToSet != "additionalArgsToProcess") { // Drop quotes from all other settings valueToSetTo = valueToSetTo.Replace("\"", "").Trim(); } foreach (PropertyInfo property in allProperties) { // List of case insensitive names that will import as this property HashSet <string> possibleNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase); possibleNames.Add(property.Name); // TODO: No one makes use of the LegacyName attribute thus the possibleNames HashSet and the LegacyName class could be removed as part of a code cleanup pass // // Including any mapped LegacyName attributes foreach (Attribute attribute in Attribute.GetCustomAttributes(property)) { LegacyName legacyName = attribute as LegacyName; if (legacyName != null) { possibleNames.Add(legacyName.Name); } } if (possibleNames.Contains(keyToSet)) { switch (property.PropertyType.Name) { case "Int32": property.SetValue(this, (int)double.Parse(valueToSetTo)); break; case "Double": property.SetValue(this, double.Parse(valueToSetTo)); break; case "Boolean": property.SetValue(this, bool.Parse(valueToSetTo)); break; case "FMatrix3x3": { property.SetValue(this, new DMatrix3x3(valueToSetTo)); } break; case "DoublePoint": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); string[] xyValues = bracketContents.Split(','); property.SetValue(this, new DoublePoint(double.Parse(xyValues[0]), double.Parse(xyValues[1]))); } break; case "IntPoint": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); string[] xyValues = bracketContents.Split(','); property.SetValue(this, new IntPoint(double.Parse(xyValues[0]), double.Parse(xyValues[1]))); } break; case "IntPoint[]": { string bracketContents = GetInsides(valueToSetTo, '[', ']'); List <IntPoint> points = new List <IntPoint>(); string intPointString; int nextIndex = GetInsides(out intPointString, bracketContents, '[', ']', 0); do { string[] xyValues = intPointString.Split(','); points.Add(new IntPoint(double.Parse(xyValues[0]) * 1000, double.Parse(xyValues[1]) * 1000)); nextIndex = GetInsides(out intPointString, bracketContents, '[', ']', nextIndex); }while (nextIndex != -1); property.SetValue(this, points.ToArray()); } break; case "String": if (keyToSet == "additionalArgsToProcess") { property.SetValue(this, valueToSetTo); } else { property.SetValue(this, valueToSetTo.Replace("\\n", "\n")); } break; case "REPAIR_OVERLAPS": case "REPAIR_OUTLINES": case "SUPPORT_TYPE": case "INFILL_TYPE": case "OUTPUT_TYPE": try { valueToSetTo = valueToSetTo.Replace('|', ','); property.SetValue(this, Enum.Parse(property.PropertyType, valueToSetTo)); } catch (Exception) { } break; default: throw new NotImplementedException("unknown type"); } return(true); } } return(false); }