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

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

            try
            {
                if (Globals.Chem4WordV3.LibraryNames == null)
                {
                    Globals.Chem4WordV3.LoadNamesFromLibrary();
                }

                StringBuilder sb;

                // Start with V2 Add-In data path
                string importFolder = Path.Combine(Globals.Chem4WordV3.AddInInfo.AppDataPath, @"Chemistry Add-In for Word");
                if (Directory.Exists(importFolder))
                {
                    if (Directory.Exists(Path.Combine(importFolder, "Chemistry Gallery")))
                    {
                        importFolder = Path.Combine(importFolder, "Chemistry Gallery");
                    }
                }
                else
                {
                    importFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                }

                if (Directory.Exists(importFolder))
                {
                    // Fix scrolling to selected item by using code from https://social.msdn.microsoft.com/Forums/expression/en-US/1257aebc-22a6-44f6-975b-74f5067728bc/autoposition-showfolder-dialog?forum=vbgeneral

                    VistaFolderBrowserDialog browser = new VistaFolderBrowserDialog();

                    browser.Description            = "Select a folder to import cml files from";
                    browser.UseDescriptionForTitle = true;
                    browser.RootFolder             = Environment.SpecialFolder.Desktop;
                    browser.ShowNewFolderButton    = false;
                    browser.SelectedPath           = importFolder;
                    Forms.DialogResult dr = browser.ShowDialog();

                    if (dr == Forms.DialogResult.OK)
                    {
                        string selectedFolder = browser.SelectedPath;
                        string doneFile       = Path.Combine(selectedFolder, "library-import-done.txt");

                        sb = new StringBuilder();
                        sb.AppendLine("Do you want to import the Gallery structures into the Library?");
                        sb.AppendLine("(This cannot be undone.)");
                        dr = UserInteractions.AskUserYesNo(sb.ToString());
                        if (dr == Forms.DialogResult.Yes)
                        {
                            if (File.Exists(doneFile))
                            {
                                sb = new StringBuilder();
                                sb.AppendLine($"All files have been imported already from '{selectedFolder}'");
                                sb.AppendLine("Do you want to rerun the import?");
                                dr = UserInteractions.AskUserYesNo(sb.ToString());
                                if (dr == Forms.DialogResult.Yes)
                                {
                                    File.Delete(doneFile);
                                }
                            }
                        }

                        if (dr == Forms.DialogResult.Yes)
                        {
                            int fileCount = 0;

                            var lib         = new Database.Library();
                            var transaction = lib.StartTransaction();

                            try
                            {
                                var xmlFiles = Directory.GetFiles(selectedFolder, "*.cml").ToList();
                                xmlFiles.AddRange(Directory.GetFiles(selectedFolder, "*.mol"));
                                if (xmlFiles.Count > 0)
                                {
                                    ProgressBar.Maximum          = xmlFiles.Count;
                                    ProgressBar.Minimum          = 0;
                                    ProgressBarHolder.Visibility = Visibility.Visible;
                                    SetButtonState(false);

                                    int progress = 0;
                                    int total    = xmlFiles.Count;

                                    foreach (string cmlFile in xmlFiles)
                                    {
                                        progress++;
                                        ShowProgress(progress, cmlFile.Replace($"{selectedFolder}\\", "") + $" [{progress}/{total}]");

                                        var cml = File.ReadAllText(cmlFile);

                                        // Set second parameter to true if properties are to be calculated on import
                                        if (lib.ImportCml(cml, transaction, false))
                                        {
                                            fileCount++;
                                        }
                                    }
                                    lib.EndTransaction(transaction, false);
                                }

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

                                File.WriteAllText(doneFile, $"{fileCount} cml files imported into library");
                                FileInfo fi = new FileInfo(doneFile);
                                fi.Attributes = FileAttributes.Hidden;

                                Globals.Chem4WordV3.LoadNamesFromLibrary();

                                UserInteractions.InformUser($"Successfully imported {fileCount} structures from '{selectedFolder}'.");
                            }
                            catch (Exception ex)
                            {
                                lib.EndTransaction(transaction, true);
                                ProgressBarHolder.Visibility = Visibility.Collapsed;
                                SetButtonState(true);

                                new ReportError(Globals.Chem4WordV3.Telemetry, TopLeft, module, ex).ShowDialog();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ProgressBarHolder.Visibility = Visibility.Collapsed;
                SetButtonState(true);

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