private static void GenerateChemicalStructurePictureOnCell(Excel.Range cell, IPictureGegerator pictureGenerator, ICollection <string> shapeNames) { try { var text = (string)cell.Text; if (!string.IsNullOrEmpty(text)) { var structGen = pictureGenerator.GenerateTemporary(text, (double)cell.Width, (double)cell.Height); if (structGen != null) { try { var tempPng = structGen.FileName; dynamic sheet = Globals.ThisAddIn.Application.ActiveSheet; string pictureName = CreateUniqueString(shapeNames, prefix: PicturePrefix); var shapeToDelete = FindChemShape(cell); if (shapeToDelete != null) { shapeToDelete.Delete(); } AddPicture(cell, tempPng, pictureName); } finally { structGen.Dispose(); } } } } catch (Exception e) { Trace.TraceWarning(e.Message); } }
/// <summary> /// Add pictures of chemical strucure indicatied by text in each cell in range. /// </summary> /// <param name="range">The range to sweep.</param> /// <param name="callback">Callback function before visiting cell.</param> public static void AddChemicalStructures(dynamic range, IPictureGegerator pictureGenerator, Action callback = null) { switch (range) { case Excel.Range cells: dynamic sheet = Globals.ThisAddIn.Application.ActiveSheet; Excel.Shapes shapes = sheet.Shapes; var saveScreenUpdating = Globals.ThisAddIn.Application.ScreenUpdating; var saveCalculation = Globals.ThisAddIn.Application.Calculation; try { Globals.ThisAddIn.Application.ScreenUpdating = false; Globals.ThisAddIn.Application.Calculation = Excel.XlCalculation.xlCalculationManual; var shapeNames = shapes.Cast <Excel.Shape>().Select(n => n.Name).ToList(); ExcelTool.EnumerateCells(cells, cell => GenerateChemicalStructurePictureOnCell(cell, pictureGenerator, shapeNames), callback); } finally { Globals.ThisAddIn.Application.Calculation = saveCalculation; Globals.ThisAddIn.Application.ScreenUpdating = saveScreenUpdating; } break; default: break; } }
public static void UpdatePictures(IPictureGegerator pictureGenerator) { dynamic sheet = Globals.ThisAddIn.Application.ActiveSheet; Excel.Shapes shapes = sheet.Shapes; var shapeList = new List <Tuple <Excel.Shape, int, int> >(); foreach (var shape in shapes.Cast <Excel.Shape>().Where(n => IsChemicalStructure(n))) { var cell = shape.TopLeftCell; shapeList.Add(new Tuple <Excel.Shape, int, int>(shape, cell.Row, cell.Column)); } var saveScreenUpdating = Globals.ThisAddIn.Application.ScreenUpdating; var saveCalculation = Globals.ThisAddIn.Application.Calculation; try { Globals.ThisAddIn.Application.ScreenUpdating = false; Globals.ThisAddIn.Application.Calculation = Excel.XlCalculation.xlCalculationManual; foreach (var shapeInfo in shapeList) { var shape = shapeInfo.Item1; Excel.Range cell = sheet.Cells[shapeInfo.Item2, shapeInfo.Item3]; var pictureName = shape.Name; shape.Delete(); var sg = pictureGenerator.GenerateTemporary((string)cell.Text, (double)cell.Width, (double)cell.Height); if (sg != null) { try { try { var filename = sg.FileName; AddPicture(cell, filename, pictureName); } catch (Exception ex) { Trace.TraceInformation(ex.Message); } } finally { sg.Dispose(); } } } } finally { Globals.ThisAddIn.Application.Calculation = saveCalculation; Globals.ThisAddIn.Application.ScreenUpdating = saveScreenUpdating; } }
public static void AddChemicalStructures(dynamic range, IPictureGegerator pictureGenerator) { var executer = new AddChemicalStructuresExecuter(range, pictureGenerator); if (EnableProgressBar) { var result = ProgressDialog.Execute( null, "Converting to image", (Action)executer.Run, ProgressDialogSettings.WithSubLabelAndCancel); } else { executer.Run(); } }
private void AddChemicalStructures(IPictureGegerator pictureGenerator) { dynamic keep = Globals.ThisAddIn.Application.Selection; try { Stuff.AddChemicalStructures(keep, pictureGenerator); } catch (Exception) { } finally { if (keep != null) { keep.Select(); } } }
public AddChemicalStructuresExecuter(dynamic range, IPictureGegerator pictureGenerator) { this.range = range; this.pictureGenerator = pictureGenerator; }