public string Render() { string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()"; string result = null; try { Telemetry.Write(module, "Verbose", "Called"); if (HasSettings) { LoadSettings(); } string guid = Properties["Guid"]; result = OoXmlFile.CreateFromCml(Cml, guid, _rendererOptions, Telemetry, TopLeft); if (!File.Exists(result)) { Telemetry.Write(module, "Exception", "Structure could not be rendered."); Telemetry.Write(module, "Exception(Data)", Cml); UserInteractions.WarnUser("Sorry this structure could not be rendered."); } // Deliberate crash to test Error Reporting //int ii = 2; //int dd = 0; //int bang = ii / dd; } catch (Exception ex) { new ReportError(Telemetry, TopLeft, module, ex).ShowDialog(); } return(result); }
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}"); } }
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}"); } }
public static void InsertChemistry(bool isCopy, Application app, FlexDisplay flexDisplay) { 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) { app.ScreenUpdating = false; Globals.Chem4WordV3.DisableDocumentEvents(doc); try { CMLConverter cmlConverter = new CMLConverter(); Model.Model chem = cmlConverter.Import(flexDisplay.Chemistry); double before = chem.MeanBondLength; if (before < Constants.MinimumBondLength - Constants.BondLengthTolerance || before > Constants.MaximumBondLength + Constants.BondLengthTolerance) { chem.ScaleToAverageBondLength(Constants.StandardBondLength); double after = chem.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 chem.CustomXmlPartGuid = Guid.NewGuid().ToString("N"); } string guidString = chem.CustomXmlPartGuid; string bookmarkName = "C4W_" + guidString; if (Globals.Chem4WordV3.SystemOptions == null) { Globals.Chem4WordV3.LoadOptions(); } Globals.Chem4WordV3.SystemOptions.WordTopLeft = Globals.Chem4WordV3.WordTopLeft; IChem4WordRenderer renderer = Globals.Chem4WordV3.GetRendererPlugIn( Globals.Chem4WordV3.SystemOptions.SelectedRendererPlugIn); if (renderer == null) { UserInteractions.WarnUser("Unable to find a Renderer Plug-In"); } else { // Export just incase the CustomXmlPartGuid has been changed string cml = cmlConverter.Export(chem); renderer.Properties = new Dictionary <string, string>(); renderer.Properties.Add("Guid", guidString); renderer.Cml = cml; string tempfileName = renderer.Render(); if (File.Exists(tempfileName)) { cc = CustomRibbon.Insert2D(doc, tempfileName, bookmarkName, guidString); if (isCopy) { doc.CustomXMLParts.Add(cml); } try { // Delete the temporary file now we are finished with it File.Delete(tempfileName); } catch { // Not much we can do here } } } } catch (Exception ex) { new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex) .ShowDialog(); } finally { // Tidy Up - Resume Screen Updating and Enable Document Event Handlers app.ScreenUpdating = true; Globals.Chem4WordV3.EnableDocumentEvents(doc); 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}"); } }