コード例 #1
0
            public void Save()
            {
                string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

                try
                {
                    var lib = new Database.Library();
                    lib.UpdateChemistry(ID, Name, XML, Formula);
                    Dirty = false;
                }
                catch (Exception ex)
                {
                    new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
                }
            }
コード例 #2
0
        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();

                    // Close the existing Library Pane
#if DEBUG
                    Debugger.Break();
#endif
                    //var app = Globals.Chem4WordV3.Application;
                    //foreach (CustomTaskPane taskPane in Globals.Chem4WordV3.CustomTaskPanes)
                    //{
                    //    if (app.ActiveWindow == taskPane.Window && taskPane.Title == Constants.LibraryTaskPaneTitle)
                    //    {
                    //        var custTaskPane = taskPane;
                    //        (custTaskPane.Control as LibraryHost)?.Refresh();
                    //    }
                    //}
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, TopLeft, module, ex).ShowDialog();
            }
        }
コード例 #3
0
        private void DeleteButton_OnClick(object sender, RoutedEventArgs e)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                if (AskUserYesNo("Do you want to delete this structure from the Library?") == DialogResult.Yes)
                {
                    var lib = new Database.Library();
                    lib.DeleteChemistry(((LibraryViewModel.Chemistry) this.DataContext).ID);
                    Globals.Chem4WordV3.LoadNamesFromLibrary();
                    ParentControl.MainGrid.DataContext = new LibraryViewModel();
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
            }
        }
コード例 #4
0
        private void AddNewChemistry(IList eNewItems)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                if (!_initializing)
                {
                    var lib = new Database.Library();
                    foreach (Chemistry chemistry in eNewItems)
                    {
                        chemistry.ID = lib.AddChemistry(chemistry.XML, chemistry.Name, chemistry.Formula);
                    }
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
            }
        }
コード例 #5
0
        public void LoadUserTagItems()
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                UserTagItems.Clear();

                var lib = new Database.Library();
                List <UserTagDTO> allTags = lib.GetAllUserTags();
                foreach (var obj in allTags)
                {
                    var tag = new UserTag();
                    tag.ID   = obj.Id;
                    tag.Text = obj.Text;
                    UserTagItems.Add(tag);
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
            }
        }
コード例 #6
0
        private void DeleteChemistry(IList eOldItems)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                if (!_initializing)
                {
                    var lib = new Database.Library();
                    foreach (Chemistry chemistry in eOldItems)
                    {
                        lib.DeleteChemistry(chemistry.ID);
                    }
                }
            }
            catch (Exception ex)
            {
                using (var form = new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex))
                {
                    form.ShowDialog();
                }
            }
        }
コード例 #7
0
        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.CloseLibrary();
                    Globals.Chem4WordV3.LoadNamesFromLibrary();

                    UserInteractions.InformUser("Your library has been cleared.");
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, TopLeft, module, ex).ShowDialog();
            }
        }
コード例 #8
0
        public ObservableCollection <UserTag> LoadUserTagItems(int ChemistryID)
        {
            var    results = new ObservableCollection <UserTag>();
            string module  = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                var lib = new Database.Library();
                List <UserTagDTO> allTags = lib.GetAllUserTags(ChemistryID);

                foreach (var dto in allTags)
                {
                    var tag = new UserTag();
                    tag.ID   = dto.Id;
                    tag.Text = dto.Text;
                    results.Add(tag);
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
            }
            return(results);
        }
コード例 #9
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)
            {
                using (var form = new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex))
                {
                    form.ShowDialog();
                }
            }
        }
コード例 #10
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();
            }
        }
コード例 #11
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();
            }
        }