Пример #1
0
        private void RemoveOldAtomsAndBond(NotifyCollectionChangedEventArgs e)
        {
            foreach (Molecule child in e.OldItems)
            {
                child.Parent = null;

                foreach (Atom atom in child.Atoms.ToList())
                {
                    AllAtoms.Remove(atom);
                }

                foreach (Bond bond in child.Bonds.ToList())
                {
                    AllBonds.Remove(bond);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Adding molecules to  or removing from the model also adds the Atoms and Bonds
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Molecules_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:

                foreach (Molecule m in e.NewItems)
                {
                    foreach (Atom atom in m.Atoms)
                    {
                        if (!AllAtoms.Contains(atom))
                        {
                            AllAtoms.Add(atom);
                        }
                    }

                    foreach (Bond bond in m.Bonds)
                    {
                        if (!AllBonds.Contains(bond))
                        {
                            AllBonds.Add(bond);
                        }
                    }
                }
                break;

            case NotifyCollectionChangedAction.Remove:
                foreach (Molecule m in e.OldItems)
                {
                    foreach (Atom atom in m.Atoms.ToList())
                    {
                        AllAtoms.Remove(atom);
                    }

                    foreach (Bond bond in m.Bonds.ToList())
                    {
                        AllBonds.Remove(bond);
                    }
                }
                break;
            }
        }
Пример #3
0
        private void BondsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                foreach (Bond bond in e.NewItems)
                {
                    bond.Parent = this;
                    AllBonds.Add(bond);
                }
                break;

            case NotifyCollectionChangedAction.Move:
                break;

            case NotifyCollectionChangedAction.Remove:
                foreach (Bond bond in e.OldItems)
                {
                    AllBonds.Remove(bond);
                    bond.Parent = null;
                }
                break;

            case NotifyCollectionChangedAction.Replace:
                foreach (Bond bond in e.NewItems)
                {
                    bond.Parent = this;
                    AllBonds.Add(bond);
                }
                foreach (Bond bond in e.OldItems)
                {
                    AllBonds.Remove(bond);
                    bond.Parent = null;
                }
                break;

            case NotifyCollectionChangedAction.Reset:
                break;
            }
        }
Пример #4
0
        private void AddNewAtomsAndBonds(NotifyCollectionChangedEventArgs e)
        {
            foreach (Molecule child in e.NewItems)
            {
                child.Parent = this;

                foreach (Atom atom in child.Atoms)
                {
                    if (!AllAtoms.Contains(atom))
                    {
                        AllAtoms.Add(atom);
                    }
                }

                foreach (Bond bond in child.Bonds)
                {
                    if (!AllBonds.Contains(bond))
                    {
                        AllBonds.Add(bond);
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Rebuilds the molecule without trashing it totally
        /// </summary>
        /// <param name="seed">start Atom for refresh operation</param>
        private void Refresh(Atom seed = null)
        {
            //keep a list of the atoms to refer to later when rebuilding
            List <Atom> checklist = new List <Atom>();

            //set the parent to null but keep a list of all atoms
            foreach (Atom atom in Atoms)
            {
                atom.Parent = null;
                checklist.Add(atom);
            }

            //clear the associated collections
            Atoms.RemoveAll();
            AllAtoms.RemoveAll();
            foreach (Bond bond in Bonds)
            {
                bond.Parent = null;
            }
            Bonds.RemoveAll();
            AllBonds.RemoveAll();
            Rings.RemoveAll();

            //if we've been provided with a seed atom, use that
            //else use the first atom in the checklist

            if (seed == null)
            {
                seed = checklist[0];
            }
            //now traverse the tree as far as it will go

            DepthFirstTraversal(seed,
                                operation: atom =>
            {
                Atoms.Add(atom);

                foreach (Bond b in atom.Bonds.Where(b => b.Parent == null))
                {
                    Bonds.Add(b);
                }
                if (checklist.Contains(atom))
                {
                    checklist.Remove(atom);
                }
            },
                                isntProcessed: atom => atom.Parent == null);
            //only if the molecule has rings do we rebuild it
            RebuildRings();

            //now we check to see whether there are any more unconnected regions lurking around

            while (checklist.Count > 0)//means we haven't yet accounted for all the original atoms
            {
                seed = checklist[0];
                Molecule addnlMol = new Molecule();
                DepthFirstTraversal(seed,
                                    operation: atom =>
                {
                    addnlMol.Atoms.Add(atom);

                    foreach (Bond b in atom.Bonds.Where(b => b.Parent == null))
                    {
                        addnlMol.Bonds.Add(b);
                    }
                    if (checklist.Contains(atom))
                    {
                        checklist.Remove(atom);
                    }
                },
                                    isntProcessed: atom => atom.Parent == null);
                //only if the molecule has rings do we rebuild it
                addnlMol.RebuildRings();
                this.Parent.Molecules.Add(addnlMol);
            }
        }