Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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;
            }
        }