private void EraseLibrary_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 = new StringBuilder();
                sb.AppendLine("This will delete all the structures from the Library");
                sb.AppendLine("It will not delete any tags.");
                sb.AppendLine("");
                sb.AppendLine("Do you want to proceed?");
                sb.AppendLine("This cannot be undone.");
                Forms.DialogResult dr =
                    UserInteractions.AskUserYesNo(sb.ToString(), Forms.MessageBoxDefaultButton.Button2);
                if (dr == Forms.DialogResult.Yes)
                {
                    var lib = new Database.Library();
                    lib.DeleteAllChemistry();
                    Globals.Chem4WordV3.LoadNamesFromLibrary();
                    UserInteractions.InformUser("Your library has been cleared.");
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, TopLeft, module, ex).ShowDialog();
            }
        }
        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();
            }
        }
示例#3
0
        private void OnSaveClick(object sender, EventArgs e)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

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

            try
            {
                if (CanSave())
                {
                    Cml = _cmlConvertor.Export(_model);

                    DialogResult = DialogResult.OK;
                }
                else
                {
                    UserInteractions.InformUser("Can't save because at least one user defined label contains an error");
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
            }
        }
        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();
            }
        }
        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();
            }
        }
示例#6
0
        /// <summary>
        /// Add this to the OnMouseLeftButtonDown attribute
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UIElementOnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            dynamic clobberedElement = sender;

            UserInteractions.InformUser(clobberedElement.ID);
        }