public static void Insert2D(string ccId, string tempfileName, string guid) { string module = $"{Product}.{Class}.{MethodBase.GetCurrentMethod().Name}()"; Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"Inserting 2D structure in ContentControl {ccId} Tag {guid}"); Word.Application app = Globals.Chem4WordV3.Application; Word.Document doc = app.ActiveDocument; var wordSettings = new WordSettings(app); var cc = GetContentControl(ccId); string bookmarkName = Constants.OoXmlBookmarkPrefix + guid; cc.Range.InsertFile(tempfileName, bookmarkName); if (doc.Bookmarks.Exists(bookmarkName)) { doc.Bookmarks[bookmarkName].Delete(); } wordSettings.RestoreSettings(app); cc.Tag = guid; cc.Title = Constants.ContentControlTitle; cc.LockContents = true; }
public static Word.ContentControl Insert1DChemistry(Word.Document doc, string text, bool isFormula, string tag) { string module = $"{Product}.{Class}.{MethodBase.GetCurrentMethod().Name}()"; if (Globals.Chem4WordV3.SystemOptions == null) { Globals.Chem4WordV3.LoadOptions(); } Word.Application app = Globals.Chem4WordV3.Application; var wordSettings = new WordSettings(app); Word.ContentControl cc = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref _missing); SetRichText(cc.ID, text, isFormula); wordSettings.RestoreSettings(app); cc.Tag = tag; cc.Title = Constants.ContentControlTitle; cc.LockContents = true; return(cc); }
private static void Update1D(string ccId, string text, bool isFormula, string tag) { string module = $"{Product}.{Class}.{MethodBase.GetCurrentMethod().Name}()"; Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"Updating 1D label in ContentControl {ccId} Tag {tag}"); Word.Application app = Globals.Chem4WordV3.Application; Word.Document doc = app.ActiveDocument; var wordSettings = new WordSettings(app); var cc = GetContentControl(ccId); cc.LockContents = false; cc.Range.Delete(); SetRichText(cc.ID, text, isFormula); Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"ContentControl updated at position {cc.Range.Start}"); wordSettings.RestoreSettings(app); cc.Tag = tag; cc.Title = Constants.ContentControlTitle; cc.LockContents = true; }
private static void SetRichText(string ccId, string text, bool isFormula) { string module = $"{Product}.{Class}.{MethodBase.GetCurrentMethod().Name}()"; Word.Application app = Globals.Chem4WordV3.Application; Word.Document doc = app.ActiveDocument; var wordSettings = new WordSettings(app); var cc = GetContentControl(ccId); if (isFormula) { Word.Range r = cc.Range; List <FormulaPart> parts = FormulaHelper.Parts(text); foreach (var part in parts) { switch (part.Count) { case 0: // Separator or multiplier case 1: // No Subscript if (!string.IsNullOrEmpty(part.Atom)) { r.InsertAfter(part.Atom); r.Font.Subscript = 0; r.Start = cc.Range.End; } break; default: // With Subscript if (!string.IsNullOrEmpty(part.Atom)) { r.InsertAfter(part.Atom); r.Font.Subscript = 0; r.Start = cc.Range.End; } if (part.Count > 0) { r.InsertAfter($"{part.Count}"); r.Font.Subscript = 1; r.Start = cc.Range.End; } break; } } } else { cc.Range.Text = text; } }
public static void Insert1D(Word.ContentControl cc, string text, bool isFormula, string tag) { string module = $"{Product}.{Class}.{MethodBase.GetCurrentMethod().Name}()"; Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"Inserting 1D label in ContentControl {cc.ID} Tag {tag}"); Word.Application app = cc.Application; var wordSettings = new WordSettings(app); SetRichText(cc, text, isFormula); wordSettings.RestoreSettings(app); cc.Tag = tag; cc.Title = Constants.ContentControlTitle; cc.LockContents = true; }
private static void Update2D(string ccId, string tempfileName, string guid) { string module = $"{Product}.{Class}.{MethodBase.GetCurrentMethod().Name}()"; Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"Updating 2D structure in ContentControl {ccId} Tag {guid}"); Word.Application app = Globals.Chem4WordV3.Application; Word.Document doc = app.ActiveDocument; var wordSettings = new WordSettings(app); var cc = GetContentControl(ccId); cc.LockContents = false; if (cc.Type == Word.WdContentControlType.wdContentControlPicture) { // Handle old Word 2007 style Word.Range range = cc.Range; cc.Delete(); cc = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, range); cc.Tag = guid; cc.Title = Constants.ContentControlTitle; cc.Range.Delete(); } else { cc.Range.Delete(); } string bookmarkName = Constants.OoXmlBookmarkPrefix + guid; cc.Range.InsertFile(tempfileName, bookmarkName); if (doc.Bookmarks.Exists(bookmarkName)) { doc.Bookmarks[bookmarkName].Delete(); } Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"ContentControl updated at position {cc.Range.Start}"); wordSettings.RestoreSettings(app); cc.Tag = guid; cc.Title = Constants.ContentControlTitle; cc.LockContents = true; }
public static Word.ContentControl Insert2DChemistry(Word.Document doc, string cml, bool isCopy) { string module = $"{Product}.{Class}.{MethodBase.GetCurrentMethod().Name}()"; if (Globals.Chem4WordV3.SystemOptions == null) { Globals.Chem4WordV3.LoadOptions(); } // Calling routine should check that Globals.Chem4WordV3.ChemistryAllowed = true Word.ContentControl cc = null; Word.Application app = doc.Application; var wordSettings = new WordSettings(app); IChem4WordRenderer renderer = Globals.Chem4WordV3.GetRendererPlugIn( Globals.Chem4WordV3.SystemOptions.SelectedRendererPlugIn); if (renderer != null) { try { app.ScreenUpdating = false; Globals.Chem4WordV3.DisableContentControlEvents(); var converter = new CMLConverter(); var model = converter.Import(cml); var modified = false; double before = model.MeanBondLength; if (before < Constants.MinimumBondLength - Constants.BondLengthTolerance || before > Constants.MaximumBondLength + Constants.BondLengthTolerance) { model.ScaleToAverageBondLength(Constants.StandardBondLength); modified = true; double after = model.MeanBondLength; Globals.Chem4WordV3.Telemetry.Write(module, "Information", $"Structure rescaled from {before.ToString("#0.00")} to {after.ToString("#0.00")}"); } if (isCopy) { // Always generate new Guid on Import model.CustomXmlPartGuid = Guid.NewGuid().ToString("N"); modified = true; } // Ensure each molecule has a Concise Formula set foreach (var molecule in model.Molecules) { if (string.IsNullOrEmpty(molecule.ConciseFormula)) { molecule.ConciseFormula = molecule.CalculatedFormula(); modified = true; } } if (modified) { // Re-export as the CustomXmlPartGuid or Bond Length has been changed cml = converter.Export(model); } string guid = model.CustomXmlPartGuid; renderer.Properties = new Dictionary <string, string>(); renderer.Properties.Add("Guid", guid); renderer.Cml = cml; // Generate temp file which can be inserted into a content control string tempfileName = renderer.Render(); if (File.Exists(tempfileName)) { cc = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref _missing); Insert2D(cc.ID, tempfileName, guid); if (isCopy) { doc.CustomXMLParts.Add(cml); } try { // Delete the temporary file now we are finished with it #if DEBUG #else File.Delete(tempfileName); #endif } catch { // Not much we can do here } } } catch (Exception ex) { throw new Exception("Error in Insert2DChemistry; See InnerException for details", ex); } finally { app.ScreenUpdating = true; Globals.Chem4WordV3.EnableContentControlEvents(); } } wordSettings.RestoreSettings(app); return(cc); }
public static Word.ContentControl Insert2DChemistry(Word.Document doc, string cml, bool isCopy) { string module = $"{Product}.{Class}.{MethodBase.GetCurrentMethod().Name}()"; if (Globals.Chem4WordV3.SystemOptions == null) { Globals.Chem4WordV3.LoadOptions(); } // Calling routine should check that Globals.Chem4WordV3.ChemistryAllowed = true Word.ContentControl cc = null; Word.Application app = doc.Application; var wordSettings = new WordSettings(app); IChem4WordRenderer renderer = Globals.Chem4WordV3.GetRendererPlugIn( Globals.Chem4WordV3.SystemOptions.SelectedRendererPlugIn); if (renderer != null) { try { app.ScreenUpdating = false; Globals.Chem4WordV3.DisableContentControlEvents(); var converter = new CMLConverter(); var model = converter.Import(cml); var modified = false; if (isCopy) { // Always generate new Guid on Import model.CustomXmlPartGuid = Guid.NewGuid().ToString("N"); modified = true; } if (modified) { // Re-export as the CustomXmlPartGuid or Bond Length has been changed cml = converter.Export(model); } string guid = model.CustomXmlPartGuid; renderer.Properties = new Dictionary <string, string>(); renderer.Properties.Add("Guid", guid); renderer.Cml = cml; // Generate temp file which can be inserted into a content control string tempfileName = renderer.Render(); if (File.Exists(tempfileName)) { cc = doc.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref _missing); Insert2D(cc.ID, tempfileName, guid); if (isCopy) { doc.CustomXMLParts.Add(cml); } try { // Delete the temporary file now we are finished with it #if DEBUG #else File.Delete(tempfileName); #endif } catch { // Not much we can do here } } } catch (Exception ex) { throw new Exception("Error in Insert2DChemistry; See InnerException for details", ex); } finally { app.ScreenUpdating = true; Globals.Chem4WordV3.EnableContentControlEvents(); } } wordSettings.RestoreSettings(app); return(cc); }