Exemplo n.º 1
0
        private void DelButton_Click(object sender, RoutedEventArgs e)
        {
            ChemItem selectedItem = MoleculeTreeView.SelectedItem as ChemItem;

            if (selectedItem?.Chemistry is Bond bond)
            {
                if (bond.Parent != null)
                {
                    bond.Parent.RemoveBond(bond);
                }
            }
        }
Exemplo n.º 2
0
        private void LoadModel(string fileName)
        {
            void LoadTreeNode(Molecule modelMolecule, ChemItem root = null)
            {
                ChemItem parentNode;

                parentNode           = new ChemItem();
                parentNode.Header    = modelMolecule.ToString();
                parentNode.Tag       = "Molecule";
                parentNode.Chemistry = modelMolecule;
                if (root == null)
                {
                    //FileInfo fi = new FileInfo(fileName);
                    //parentNode = treeView1.Nodes.Add(modelMolecule.Path, fi.Name + ": " + modelMolecule.ToString());

                    MoleculeTreeView.Items.Add(parentNode);
                }
                else
                {
                    root.Items.Add(parentNode);
                }
                foreach (Atom atom in modelMolecule.Atoms.Values)
                {
                    var atomItem = new ChemItem();
                    atomItem.Header    = atom.ToString();
                    atomItem.Chemistry = atom;
                    atomItem.Tag       = "Atom";
                    parentNode.Items.Add(atomItem);
                }

                foreach (Bond bond in modelMolecule.Bonds)
                {
                    var bondItem = new ChemItem();
                    bondItem.Header    = bond.ToString();
                    bondItem.Tag       = "Bond";
                    bondItem.Chemistry = bond;
                    parentNode.Items.Add(bondItem);
                }

                foreach (Ring r in modelMolecule.Rings)
                {
                    var ringNode = new ChemItem();
                    ringNode.Header = r.ToString();
                    parentNode.Items.Add(ringNode);
                    ringNode.Tag = "Ring";
                    foreach (Atom a in r.Atoms)
                    {
                        var atomItem = new ChemItem();
                        atomItem.Header = a.Id;
                        atomItem.Tag    = "Atom";
                        ringNode.Items.Add(atomItem);
                    }
                }
                foreach (var childMol in modelMolecule.Molecules.Values)
                {
                    LoadTreeNode(childMol, parentNode);
                }
            }

            MoleculeTreeView.Items.Clear();

            var converter = new CMLConverter();

            using (StreamReader sr = new StreamReader(fileName))
            {
                Stopwatch sw = new Stopwatch();
                _myModel = converter.Import(sr.ReadToEnd());
                ConnectModelEvents();
                sw.Stop();
                //MessageBox.Show($"Converting took {sw.ElapsedMilliseconds}");

                foreach (var modelMolecule in _myModel.Molecules.Values)
                {
                    sw.Reset();

                    //MessageBox.Show($"Rebuilding rings took {sw.ElapsedMilliseconds}");
                    //MessageBox.Show($"Ring count= {modelMolecule.Rings.Count}");
                    LoadTreeNode(modelMolecule);
                }
            }
        }