public void DeletePreset(MachineModel model, MachinePreset preset)
 {
     if (preset.SourcePath.Length > 0)
     {
         File.Delete(preset.SourcePath);
     }
     model.Presets.Remove(preset);
 }
        public void StorePreset(MachinePreset preset)
        {
            if (preset.SourcePath.Length == 0)
            {
                DebugUtil.Log(2, "MachineDatabase.StorePreset: preset with id " + preset.Settings.Identifier + " has no path!");
                return;
            }
            SettingsSerializer serializer = new SettingsSerializer();

            serializer.StoreSettings(this, preset, false);
        }
        public MachineModel CreateDerivedMachine(Manufacturer mfg, MachineModel derivedFrom, MachinePreset initialPreset, string name)
        {
            string existingMachineDir = Path.GetDirectoryName(initialPreset.SourcePath);
            string useMfgDir          = Directory.GetParent(existingMachineDir).FullName;

            string newMachineDir = Path.Combine(useMfgDir, name);

            if (Directory.Exists(newMachineDir))
            {
                throw new Exception("ALREADY_EXISTS");
            }
            try {
                Directory.CreateDirectory(newMachineDir);
            } catch (Exception) {
                throw new Exception("CANNOT_CREATE");
            }
            if (!Directory.Exists(newMachineDir))
            {
                throw new Exception("ALREADY_EXISTS");
            }

            string newPresetPath = Path.Combine(newMachineDir, "Default.txt");

            MachineModel model = new MachineModel()
            {
                Name = name,
                UUID = System.Guid.NewGuid().ToString()
            };

            add_machine(mfg, model, false);

            MachinePreset newPreset = new MachinePreset(initialPreset.Settings.CloneAs <PlanarAdditiveSettings>(), newPresetPath);

            newPreset.Settings.Identifier = "Defaults";

            newPreset.Settings.BaseMachine.ManufacturerName = mfg.Name;
            newPreset.Settings.BaseMachine.ManufacturerUUID = mfg.UUID;
            newPreset.Settings.BaseMachine.ModelIdentifier  = model.Name;
            newPreset.Settings.BaseMachine.ModelUUID        = model.UUID;

            SettingsSerializer serializer = new SettingsSerializer();

            serializer.StoreSettings(this, newPreset, true);

            model.Presets.Add(newPreset);
            model.DefaultPreset = newPreset;

            return(model);
        }
        public MachinePreset CreateDerivedPreset(MachineModel model, MachinePreset derivedFrom, string name)
        {
            string machineDir = Path.GetDirectoryName(derivedFrom.SourcePath);
            string newPath    = Path.Combine(machineDir, name + ".txt");

            MachinePreset newPreset = new MachinePreset(derivedFrom.Settings.CloneAs <PlanarAdditiveSettings>(), newPath);

            newPreset.Settings.Identifier = name;
            SettingsSerializer serializer = new SettingsSerializer();

            serializer.StoreSettings(this, newPreset, true);

            //model.Presets.Add(newPreset);
            model.Presets.Insert(1, newPreset);
            return(newPreset);
        }
Exemple #5
0
        public void UpdateSettingsFromJson(MachineDatabase db, MachinePreset preset, string newJson, bool bCreate)
        {
            JsonSerializerSettings jsonSettings = makeWriteSerializer();

            if (bCreate == false && File.Exists(preset.SourcePath) == false)
            {
                throw new Exception("SettingsSerializer.StoreSettings: path " + preset.SourcePath + " does not exist!");
            }

            var save_culture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            System.IO.File.WriteAllText(preset.SourcePath, newJson);
            Thread.CurrentThread.CurrentCulture = save_culture;

            RefreshSettingsFromDisk(db, preset);
        }
        /// <summary>
        /// choose a default for this MachineModel.
        /// </summary>
        void set_default_preset(MachineModel model)
        {
            if (model.Presets.Count == 0)
            {
                throw new Exception("MachinDatabase.set_default_settings: no presets to choose from!");
            }

            MachinePreset defaultPreset =
                model.Presets.Find((s) => { return(s.Settings.Identifier.Equals("Defaults", StringComparison.OrdinalIgnoreCase)); });

            if (defaultPreset != null)
            {
                model.DefaultPreset = defaultPreset;
                return;
            }

            model.DefaultPreset = model.Presets[0];
        }
Exemple #7
0
        public void StoreSettings(MachineDatabase db, MachinePreset preset, bool bCreate = false)
        {
            JsonSerializerSettings jsonSettings = makeWriteSerializer();

            if (bCreate == false && File.Exists(preset.SourcePath) == false)
            {
                throw new Exception("SettingsSerializer.StoreSettings: path " + preset.SourcePath + " does not exist!");
            }

            var save_culture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            string json = JsonConvert.SerializeObject(preset.Settings, jsonSettings);

            System.IO.File.WriteAllText(preset.SourcePath, json);

            Thread.CurrentThread.CurrentCulture = save_culture;
        }
        public MachineModel CreateManufacturer(string mfgName, string mfgUUID, string defaultMachineUUID)
        {
            Manufacturer new_mfg = new Manufacturer()
            {
                Name = mfgName, UUID = mfgUUID
            };

            add_manufacturer(new_mfg);

            MachinePreset preset = new MachinePreset(new GenericPrinterSettings(mfgName, mfgUUID, defaultMachineUUID));

            MachineModel model = new MachineModel()
            {
                Name    = "Unknown", UUID = defaultMachineUUID,
                Presets = new List <MachinePreset>()
                {
                    preset
                }
            };

            add_machine(new_mfg, model);

            SettingsSerializer serializer  = new SettingsSerializer();
            string             machinePath = serializer.CreateNewSettingsFolder(this, new_mfg, model, cotangent.CCOptions.SettingsDBPath);

            preset.Settings.Identifier = "Defaults";
            preset.Settings.BaseMachine.ManufacturerName = mfgName;
            preset.Settings.BaseMachine.ManufacturerUUID = mfgUUID;
            preset.Settings.BaseMachine.ModelIdentifier  = "(Unknown)";
            preset.Settings.BaseMachine.ModelUUID        = defaultMachineUUID;

            preset.SourcePath = Path.Combine(machinePath, "Default.txt");
            serializer.StoreSettings(this, preset, true);

            model.Presets.Add(preset);
            model.DefaultPreset = preset;

            return(model);
        }
Exemple #9
0
        public void RefreshSettingsFromDisk(MachineDatabase db, MachinePreset preset)
        {
            if (File.Exists(preset.SourcePath) == false)
            {
                throw new Exception("SettingsSerializer.RefreshSettingsFromDisk: path " + preset.SourcePath + " does not exist!");
            }

            var save_culture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            string data = File.ReadAllText(preset.SourcePath);

            Thread.CurrentThread.CurrentCulture = save_culture;

            SingleMaterialFFFSettings settings = JsonToSettings(data);

            if (settings == null)
            {
                throw new Exception("SettingsSerializer.RefreshSettingsFromDisk: json data could not be parsed!");
            }

            preset.Settings = settings;
        }
Exemple #10
0
        /// <summary>
        /// Scan through a top-level 'manufacturer' folder for a list of machine folders,
        /// and then restore any settings files from each of those.
        /// </summary>
        bool read_manufacturer(string mfgPath, ref List <MachinePreset> presetsForModels)
        {
            SettingsSerializer serializer = new SettingsSerializer();

            string[] folders = Directory.GetDirectories(mfgPath);
            foreach (string machineFolder in folders)
            {
                List <PlanarAdditiveSettings> settings;
                List <string> sourceFilePaths;
                if (serializer.RestoreFromFolder(machineFolder, out settings, out sourceFilePaths) == false)
                {
                    DebugUtil.Log(2, "MachineDatabase: did not find any valid settings files in folder " + machineFolder);
                    continue;
                }

                for (int i = 0; i < settings.Count; ++i)
                {
                    MachinePreset preset = new MachinePreset(settings[i], sourceFilePaths[i]);
                    presetsForModels.Add(preset);
                }
            }

            return(presetsForModels.Count > 0);
        }