Ejemplo n.º 1
0
        public static void InsertChemistry(bool isCopy, Application app, Display display)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            Document       doc = app.ActiveDocument;
            Selection      sel = app.Selection;
            ContentControl cc  = null;

            if (Globals.Chem4WordV3.SystemOptions == null)
            {
                Globals.Chem4WordV3.LoadOptions();
            }

            bool   allowed = true;
            string reason  = "";

            if (Globals.Chem4WordV3.ChemistryAllowed)
            {
                if (sel.ContentControls.Count > 0)
                {
                    cc = sel.ContentControls[1];
                    if (cc.Title != null && cc.Title.Equals(Constants.ContentControlTitle))
                    {
                        reason  = "a chemistry object is selected";
                        allowed = false;
                    }
                }
            }
            else
            {
                reason  = Globals.Chem4WordV3.ChemistryProhibitedReason;
                allowed = false;
            }

            if (allowed)
            {
                try
                {
                    cc = ChemistryHelper.Insert2DChemistry(doc, display.Chemistry.ToString(), isCopy);
                }
                catch (Exception ex)
                {
                    new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
                }
                finally
                {
                    if (cc != null)
                    {
                        // Move selection point into the Content Control which was just edited or added
                        app.Selection.SetRange(cc.Range.Start, cc.Range.End);
                    }
                }
            }
            else
            {
                UserInteractions.WarnUser($"You can't insert a chemistry object because {reason}");
            }
        }
Ejemplo n.º 2
0
        public static void DoUpgrade(Word.Document doc)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            int sel = doc.Application.Selection.Range.Start;

            Globals.Chem4WordV3.DisableDocumentEvents(doc);

            try
            {
                string extension   = doc.FullName.Split('.').Last();
                string guid        = Guid.NewGuid().ToString("N");
                string timestamp   = DateTime.Now.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture);
                string destination = Path.Combine(Globals.Chem4WordV3.AddInInfo.ProductAppDataPath, "Backups", $"Chem4Word-{timestamp}-{guid}.{extension}");
                File.Copy(doc.FullName, destination);
            }
            catch (Exception ex)
            {
                // Nothing much we can do here :-(
                Debug.WriteLine(ex.Message);
            }

            Dictionary <string, CustomXMLPart> customXmlParts = new Dictionary <string, CustomXMLPart>();
            List <UpgradeTarget> targets = CollectData(doc);
            int upgradedCCs = 0;
            int upgradedXml = 0;

            foreach (var target in targets)
            {
                if (target.ContentControls.Count > 0)
                {
                    upgradedXml++;
                    upgradedCCs += target.ContentControls.Count;
                }

                foreach (var cci in target.ContentControls)
                {
                    foreach (Word.ContentControl cc in doc.ContentControls)
                    {
                        if (cc.ID.Equals(cci.Id))
                        {
                            int    start;
                            bool   isFormula;
                            string source;
                            string text;

                            switch (cci.Type)
                            {
                            case "2D":
                                cc.LockContents = false;
                                cc.Title        = Constants.ContentControlTitle;
                                cc.Tag          = target.Model.CustomXmlPartGuid;
                                cc.LockContents = true;

                                // ToDo: Regenerate converted 2D structures
                                break;

                            case "new":
                                cc.LockContents = false;
                                cc.Range.Delete();
                                start = cc.Range.Start;
                                cc.Delete();

                                doc.Application.Selection.SetRange(start - 1, start - 1);
                                var model    = new Model.Model();
                                var molecule = new Molecule();
                                molecule.ChemicalNames.Add(new ChemicalName {
                                    Id = "m1.n1", Name = cci.Text, DictRef = Constants.Chem4WordUserSynonym
                                });
                                model.Molecules.Add(molecule);
                                model.CustomXmlPartGuid = Guid.NewGuid().ToString("N");

                                var cmlConvertor = new CMLConverter();
                                doc.CustomXMLParts.Add(cmlConvertor.Export(model));

                                Word.ContentControl ccn = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref _missing);
                                ChemistryHelper.Insert1D(ccn, cci.Text, false, $"m1.n1:{model.CustomXmlPartGuid}");
                                ccn.LockContents = true;
                                break;

                            default:
                                cc.LockContents = false;
                                cc.Range.Delete();
                                start = cc.Range.Start;
                                cc.Delete();

                                doc.Application.Selection.SetRange(start - 1, start - 1);
                                isFormula = false;
                                text      = ChemistryHelper.GetInlineText(target.Model, cci.Type, ref isFormula, out source);
                                Word.ContentControl ccr = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref _missing);
                                ChemistryHelper.Insert1D(ccr, text, isFormula, $"{cci.Type}:{target.Model.CustomXmlPartGuid}");
                                ccr.LockContents = true;
                                break;
                            }
                        }
                    }
                }

                CMLConverter  converter = new CMLConverter();
                CustomXMLPart cxml      = doc.CustomXMLParts.SelectByID(target.CxmlPartId);
                if (customXmlParts.ContainsKey(cxml.Id))
                {
                    customXmlParts.Add(cxml.Id, cxml);
                }
                doc.CustomXMLParts.Add(converter.Export(target.Model));
            }

            EraseChemistryZones(doc);

            foreach (var kvp in customXmlParts.ToList())
            {
                kvp.Value.Delete();
            }

            Globals.Chem4WordV3.EnableDocumentEvents(doc);
            doc.Application.Selection.SetRange(sel, sel);
            if (upgradedCCs + upgradedXml > 0)
            {
                Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"Upgraded {upgradedCCs} Chemistry Objects for {upgradedXml} Structures");
                UserInteractions.AlertUser($"Upgrade Completed{Environment.NewLine}{Environment.NewLine}Upgraded {upgradedCCs} Chemistry Objects for {upgradedXml} Structures");
            }
        }
Ejemplo n.º 3
0
        public static void InsertChemistry(bool isCopy, Application app, Display display, bool fromLibrary)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            Document       doc = app.ActiveDocument;
            Selection      sel = app.Selection;
            ContentControl cc  = null;

            if (Globals.Chem4WordV3.SystemOptions == null)
            {
                Globals.Chem4WordV3.LoadOptions();
            }

            bool   allowed = true;
            string reason  = "";

            if (Globals.Chem4WordV3.ChemistryAllowed)
            {
                if (sel.ContentControls.Count > 0)
                {
                    cc = sel.ContentControls[1];
                    if (cc.Title != null && cc.Title.Equals(Constants.ContentControlTitle))
                    {
                        reason  = "a chemistry object is selected";
                        allowed = false;
                    }
                }
            }
            else
            {
                reason  = Globals.Chem4WordV3.ChemistryProhibitedReason;
                allowed = false;
            }

            if (allowed)
            {
                try
                {
                    CMLConverter cmlConverter = new CMLConverter();
                    var          model        = cmlConverter.Import(display.Chemistry.ToString());

                    if (fromLibrary)
                    {
                        if (Globals.Chem4WordV3.SystemOptions.RemoveExplicitHydrogensOnImportFromLibrary)
                        {
                            var targets = model.GetHydrogenTargets();

                            if (targets.Atoms.Any())
                            {
                                foreach (var bond in targets.Bonds)
                                {
                                    bond.Parent.RemoveBond(bond);
                                }
                                foreach (var atom in targets.Atoms)
                                {
                                    atom.Parent.RemoveAtom(atom);
                                }
                            }
                        }

                        var outcome = model.EnsureBondLength(Globals.Chem4WordV3.SystemOptions.BondLength,
                                                             Globals.Chem4WordV3.SystemOptions.SetBondLengthOnImportFromLibrary);
                        if (!string.IsNullOrEmpty(outcome))
                        {
                            Globals.Chem4WordV3.Telemetry.Write(module, "Information", outcome);
                        }
                    }

                    cc = ChemistryHelper.Insert2DChemistry(doc, cmlConverter.Export(model), isCopy);
                }
                catch (Exception ex)
                {
                    new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
                }
                finally
                {
                    if (cc != null)
                    {
                        // Move selection point into the Content Control which was just edited or added
                        app.Selection.SetRange(cc.Range.Start, cc.Range.End);
                    }
                }
            }
            else
            {
                UserInteractions.WarnUser($"You can't insert a chemistry object because {reason}");
            }
        }