Example #1
0
        private void SwitchToMulti_OnClick(object sender, RoutedEventArgs e)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                if (!_loading)
                {
                    object obj = ExecuteJavaScript("GetJSON");
                    if (obj != null)
                    {
                        string mol = obj.ToString();
                        if (!string.IsNullOrEmpty(mol))
                        {
                            JSONConverter jc    = new JSONConverter();
                            Model.Model   model = jc.Import(mol);
                            StructureJson = jc.Export(model);
                            SwapMode();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                new ReportError(Telemetry, TopLeft, module, ex).ShowDialog();
            }
        }
Example #2
0
        private void EditorHost_Load(object sender, EventArgs e)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            Cursor.Current = Cursors.WaitCursor;

            if (TopLeft.X != 0 && TopLeft.Y != 0)
            {
                Left = (int)TopLeft.X;
                Top  = (int)TopLeft.Y;
            }

            CMLConverter  cc = new CMLConverter();
            JSONConverter jc = new JSONConverter();

            Model.Model model = cc.Import(_cml);

            WpfChemDoodle editor = new WpfChemDoodle();

            editor.Telemetry          = Telemetry;
            editor.ProductAppDataPath = ProductAppDataPath;
            editor.UserOptions        = UserOptions;
            editor.TopLeft            = TopLeft;

            editor.StructureJson     = jc.Export(model);
            editor.IsSingleMolecule  = model.Molecules.Count == 1;
            editor.AverageBondLength = model.MeanBondLength;

            editor.InitializeComponent();
            elementHost1.Child    = editor;
            editor.OnButtonClick += OnWpfButtonClick;

            this.Show();
            Application.DoEvents();
        }
Example #3
0
        private void MainWindow_OnClosing(object sender, CancelEventArgs e)
        {
            if (Editor.IsDirty)
            {
                var jsonConvertor = new JSONConverter();
                var temp          = Editor.ActiveViewModel.Model.Copy();
                temp.RescaleForCml();
                string jsonString = jsonConvertor.Export(temp);
                var    jc         = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
                var    fg         = _functionalGroups.FirstOrDefault(f => f.Name.Equals(_lastFunctionalGroup));
                if (fg != null)
                {
                    fg.Expansion = jc;
                }
            }

            List <FunctionalGroup> listOfGroups = new List <FunctionalGroup>();

            foreach (var group in _functionalGroups)
            {
                listOfGroups.Add(group);
            }

            string json = JsonConvert.SerializeObject(listOfGroups,
                                                      Formatting.Indented,
                                                      new JsonSerializerSettings
            {
                DefaultValueHandling = DefaultValueHandling.Ignore
            });

            Clipboard.SetText(json);
            MessageBox.Show("Results on Clipboard !");
        }
Example #4
0
        private void Groups_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var jsonConvertor = new JSONConverter();

            if (Groups.SelectedItem is FunctionalGroup fg)
            {
                if (!string.IsNullOrEmpty(_lastFunctionalGroup))
                {
                    if (Editor.IsDirty)
                    {
                        var temp = Editor.ActiveViewModel.Model.Copy();
                        temp.RescaleForCml();
                        string expansion = jsonConvertor.Export(temp, true);
                        var    fge       = _functionalGroups.FirstOrDefault(f => f.Name.Equals(_lastFunctionalGroup));
                        fge.Expansion = expansion;
                    }
                }

                _lastFunctionalGroup = fg.ToString();

                if (fg.Expansion == null)
                {
                    var model = jsonConvertor.Import("{'a':[{'l':'" + fg.Name + "','x':0,'y':0}]}");
                    Editor.SetModel(model);
                }
                else
                {
                    string groupJson = JsonConvert.SerializeObject(fg.Expansion);
                    var    model     = jsonConvertor.Import(groupJson);
                    Editor.SetModel(model);
                }
            }
        }
Example #5
0
        private void ExportAs_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lastModel != null)
            {
                string result = string.Empty;

                switch (ExportAs.SelectedIndex)
                {
                case 1:
                    var cmlConverter = new CMLConverter();
                    result = cmlConverter.Export(lastModel);
                    break;

                case 2:
                    var sdFileConverter = new SdFileConverter();
                    result = sdFileConverter.Export(lastModel);
                    break;

                case 3:
                    var jsonConverter = new JSONConverter();
                    result = jsonConverter.Export(lastModel);
                    break;
                }

                if (!string.IsNullOrEmpty(result))
                {
                    //Clipboard.SetText(result);
                    //MessageBox.Show("Last loaded model exported to clipboard as CML");
                    textBox1.Text = result + Environment.NewLine;
                }
            }
            ExportAs.SelectedIndex = 0;
            LoadStructure.Focus();
        }
Example #6
0
        private void SwitchToSingle_OnClick(object sender, RoutedEventArgs e)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                if (!_loading)
                {
                    object obj1 = ExecuteJavaScript("GetJSON");
                    object obj2 = ExecuteJavaScript("GetFirstMolJSON");
                    if (obj1 != null && obj2 != null)
                    {
                        string mol1 = obj1.ToString();
                        string mol2 = obj2.ToString();
                        if (!string.IsNullOrEmpty(mol1) && !string.IsNullOrEmpty(mol2))
                        {
                            JSONConverter jc     = new JSONConverter();
                            Model.Model   model1 = jc.Import(mol1);
                            Model.Model   model2 = jc.Import(mol2);

                            if (model1.Molecules.Count == 1)
                            {
                                StructureJson = jc.Export(model1);
                                SwapMode();
                            }
                            else
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine($"Warning your structure '{model1.ConciseFormula}' contains more than one molecule.");
                                sb.AppendLine($"Only the first molecule '{model2.ConciseFormula}' will be retained.");
                                sb.AppendLine("    Do you wish to continue?");
                                DialogResult dr = UserInteractions.AskUserYesNo(sb.ToString());
                                if (dr == DialogResult.Yes)
                                {
                                    StructureJson = jc.Export(model2);
                                    SwapMode();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                new ReportError(Telemetry, TopLeft, module, ex).ShowDialog();
            }
        }
Example #7
0
        public DialogResult Edit()
        {
            string       module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";
            DialogResult result = DialogResult.Cancel;

            try
            {
                Telemetry.Write(module, "Verbose", "Called");
                if (HasSettings)
                {
                    LoadSettings();
                }

                CMLConverter  cmlConverter  = new CMLConverter();
                Model.Model   model         = cmlConverter.Import(Cml);
                JSONConverter jsonConverter = new JSONConverter();
                string        json          = jsonConverter.Export(model);

                ChemDoodleWeb cdwEditor = new ChemDoodleWeb();
                cdwEditor.TopLeft            = TopLeft;
                cdwEditor.Telemetry          = Telemetry;
                cdwEditor.ProductAppDataPath = ProductAppDataPath;
                cdwEditor.UserOptions        = _editorOptions;
                cdwEditor.Before_JSON        = json;
                cdwEditor.IsSingleMolecule   = model.Molecules.Count == 1;
                cdwEditor.AverageBondLength  = model.MeanBondLength;

                result = cdwEditor.ShowDialog();
                if (result == DialogResult.OK)
                {
                    Properties = new Dictionary <string, string>();
                    Properties.Add("Before_Formula", cdwEditor.Before_Formula);
                    Properties.Add("After_Formula", cdwEditor.After_Formula);
                    json = cdwEditor.After_JSON;
                    cdwEditor.Close();
                    model = jsonConverter.Import(json);
                    Cml   = cmlConverter.Export(model);
                }
            }
            catch (Exception ex)
            {
                new ReportError(Telemetry, TopLeft, module, ex).ShowDialog();
            }

            return(result);
        }
Example #8
0
        private void btnConvertModel_Click(object sender, EventArgs e)
        {
            TextBoxFormat format = GetTextBoxFormat(txtStructure);

            switch (format)
            {
            case TextBoxFormat.Json:
                JSONConverter converter1 = new JSONConverter();
                Model         model1     = converter1.Import((object)txtStructure.Text);
                model1.RebuildMolecules();
                model1.Relabel();
                model1.CustomXmlPartGuid = Guid.NewGuid().ToString("N");
                CMLConverter converter2 = new CMLConverter();
                txtStructure.Text = converter2.Export(model1);
                break;

            case TextBoxFormat.Cml:
                CMLConverter converter3 = new CMLConverter();
                Model        model2     = converter3.Import((object)txtStructure.Text);
                model2.RebuildMolecules();
                model2.Relabel();
                JSONConverter converter4 = new JSONConverter();
                txtStructure.Text = converter4.Export(model2);
                break;

            case TextBoxFormat.MolFile:
                SdFileConverter converter5 = new SdFileConverter();
                Model           model3     = converter5.Import((object)txtStructure.Text);
                model3.RebuildMolecules();
                model3.Relabel();
                CMLConverter converter6 = new CMLConverter();
                txtStructure.Text = converter6.Export(model3);
                break;
            }

            EnableButtons();
        }