Example #1
0
        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);
                }
            }
        }
Example #2
0
        // .4 mm for .4 mm nozzle
        public void DumpSettings(string fileName)
        {
            List <string> lines = new List <string>();

            foreach (PropertyInfo property in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                string fieldDescription = "";
                foreach (Attribute attribute in Attribute.GetCustomAttributes(property))
                {
                    SettingDescription description = attribute as SettingDescription;
                    if (description != null)
                    {
                        fieldDescription = " # {0}".FormatWith(description.Description);
                    }
                }

                string name  = property.Name;
                object value = property.GetValue(this);

                switch (property.PropertyType.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.");
                    }

                    // Replace newline characters with escaped newline characters
                    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(property.PropertyType, property.PropertyType.Name), fieldDescription));
                    break;

                default:
                    throw new NotImplementedException("unknown type '{0}'".FormatWith(property.PropertyType.Name));
                }
            }

            lines.Sort();

            File.WriteAllLines(fileName, lines.ToArray());
        }