コード例 #1
0
        private void ExportFromLibrary_OnClick(object sender, RoutedEventArgs e)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            Globals.Chem4WordV3.Telemetry.Write(module, "Action", "Triggered");

            try
            {
                string exportFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                VistaFolderBrowserDialog browser = new VistaFolderBrowserDialog();

                browser.Description            = "Select a folder to export your Library's structures as cml files";
                browser.UseDescriptionForTitle = true;
                browser.RootFolder             = Environment.SpecialFolder.Desktop;
                browser.ShowNewFolderButton    = false;
                browser.SelectedPath           = exportFolder;
                Forms.DialogResult dr = browser.ShowDialog();

                if (dr == Forms.DialogResult.OK)
                {
                    exportFolder = browser.SelectedPath;

                    if (Directory.Exists(exportFolder))
                    {
                        Forms.DialogResult doExport         = Forms.DialogResult.Yes;
                        string[]           existingCmlFiles = Directory.GetFiles(exportFolder, "*.cml");
                        if (existingCmlFiles.Length > 0)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine($"This folder contains {existingCmlFiles.Length} cml files.");
                            sb.AppendLine($"Do you wish to continue?");
                            doExport = UserInteractions.AskUserYesNo(sb.ToString(), Forms.MessageBoxDefaultButton.Button2);
                        }
                        if (doExport == Forms.DialogResult.Yes)
                        {
                            Database.Library lib = new Database.Library();

                            int exported = 0;

                            List <ChemistryDTO> dto = lib.GetAllChemistry(null);
                            foreach (var obj in dto)
                            {
                                var filename = Path.Combine(browser.SelectedPath, $"Chem4Word-{obj.Id:000000000}.cml");
                                File.WriteAllText(filename, obj.Cml);
                                exported++;
                            }

                            if (exported > 0)
                            {
                                UserInteractions.InformUser($"Exported {exported} structures to {browser.SelectedPath}");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, TopLeft, module, ex).ShowDialog();
            }
        }
コード例 #2
0
        public void LoadChemistryItems(string filter)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                _initializing = true;
                ChemistryItems.Clear();
                var lib = new Database.Library();
                List <ChemistryDTO> dto = lib.GetAllChemistry(filter);
                foreach (var chemistry in dto)
                {
                    var mol = new Chemistry();
                    mol.Initializing = true;

                    mol.ID      = chemistry.Id;
                    mol.XML     = chemistry.Cml;
                    mol.Name    = chemistry.Name;
                    mol.Formula = chemistry.Formula;

                    ChemistryItems.Add(mol);
                    LoadOtherNames(mol);

                    mol.Initializing = false;
                }

                _initializing = false;
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
            }
        }
コード例 #3
0
        private void ExportFromLibrary_OnClick(object sender, RoutedEventArgs e)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            Globals.Chem4WordV3.Telemetry.Write(module, "Action", "Triggered");

            try
            {
                string exportFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                VistaFolderBrowserDialog browser = new VistaFolderBrowserDialog();

                browser.Description            = "Select a folder to export your Library's structures as cml files";
                browser.UseDescriptionForTitle = true;
                browser.RootFolder             = Environment.SpecialFolder.Desktop;
                browser.ShowNewFolderButton    = false;
                browser.SelectedPath           = exportFolder;
                Forms.DialogResult dr = browser.ShowDialog();

                if (dr == Forms.DialogResult.OK)
                {
                    exportFolder = browser.SelectedPath;

                    if (Directory.Exists(exportFolder))
                    {
                        Forms.DialogResult doExport         = Forms.DialogResult.Yes;
                        string[]           existingCmlFiles = Directory.GetFiles(exportFolder, "*.cml");
                        if (existingCmlFiles.Length > 0)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine($"This folder contains {existingCmlFiles.Length} cml files.");
                            sb.AppendLine("Do you wish to continue?");
                            doExport = UserInteractions.AskUserYesNo(sb.ToString(), Forms.MessageBoxDefaultButton.Button2);
                        }
                        if (doExport == Forms.DialogResult.Yes)
                        {
                            Database.Library lib = new Database.Library();

                            int exported = 0;
                            int progress = 0;

                            List <ChemistryDTO> dto = lib.GetAllChemistry(null);
                            int total = dto.Count;
                            if (total > 0)
                            {
                                ProgressBar.Maximum          = dto.Count;
                                ProgressBar.Minimum          = 0;
                                ProgressBarHolder.Visibility = Visibility.Visible;
                                SetButtonState(false);

                                var converter = new CMLConverter();

                                foreach (var obj in dto)
                                {
                                    progress++;
                                    ShowProgress(progress, $"Structure #{obj.Id} [{progress}/{total}]");

                                    var filename = Path.Combine(browser.SelectedPath, $"Chem4Word-{obj.Id:000000000}.cml");

                                    Model model = converter.Import(obj.Cml);

                                    var outcome = model.EnsureBondLength(Globals.Chem4WordV3.SystemOptions.BondLength, false);
                                    if (!string.IsNullOrEmpty(outcome))
                                    {
                                        Globals.Chem4WordV3.Telemetry.Write(module, "Information", outcome);
                                    }

                                    File.WriteAllText(filename, converter.Export(model));
                                    exported++;
                                }
                            }

                            ProgressBarHolder.Visibility = Visibility.Collapsed;
                            SetButtonState(true);

                            if (exported > 0)
                            {
                                UserInteractions.InformUser($"Exported {exported} structures to {browser.SelectedPath}");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ProgressBarHolder.Visibility = Visibility.Collapsed;
                SetButtonState(true);

                new ReportError(Globals.Chem4WordV3.Telemetry, TopLeft, module, ex).ShowDialog();
            }
        }