/// <summary> /// Get the SVG for a molecule within a specified size rectangle (units important, eg. px, mm /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <param name="units"></param> /// <param name="dp"></param> /// <returns></returns> public string GetMoleculeSvg( int width = -1, int height = -1, string units = Depiction.UnitsPixel, DisplayPreferences dp = null) { string svg = null; UpdateNativeMolecule(); if (NativeMol == null || NativeMol.Atoms.Count == 0) { return(null); } //width = height = -1; // debug //units = null; //NativeMol.setProperty(CDKConstants.TITLE, "caffeine"); // title already set from input! // Use size and units if specified. If not defined then a mm size box just containing the structure will be defined in generated svg DepictionGenerator dptgen = new DepictionGenerator(); if (width > 0 && height > 0) { dptgen.Size = new System.Windows.Size(width, height); dptgen.FillToFit = true; } Depiction d = dptgen.Depict(NativeMol); if (Lex.IsDefined(units)) { if (units != Depiction.UnitsPixel && units != Depiction.UnitsMM) { throw new Exception("Invalid Depiction units: " + units); } svg = d.ToSvgString(units); // get SVG setting desired units in the XML } else { svg = d.ToSvgString(); } return(svg); }
/// <summary> /// Render molecule into bitmap of specified size. /// </summary> /// <param name="bitmapWidth"></param> /// <param name="bitmapHeight"></param> /// <param name="dp"></param> /// <returns></returns> public Bitmap GetMoleculeBitmap( int bitmapWidth, int bitmapHeight, DisplayPreferences dp = null) { byte[] ba; FileStream fs; float top, bottom, left, right, height, width, strBottom, hCenter, drop, stdBndLen, scale, fontSize, bondThickness; int txtLen; Bitmap bm; UpdateNativeMolecule(); if (NativeMol == null || NativeMol.Atoms.Count == 0) { return(null); } //NativeMol.setProperty(CDKConstants.TITLE, "caffeine"); // title already set from input! DepictionGenerator dptgen = new DepictionGenerator(); dptgen.Size = new System.Windows.Size(bitmapWidth, bitmapHeight); Depiction d = dptgen.Depict(NativeMol); dptgen.Size = new System.Windows.Size(bitmapWidth, bitmapHeight); dptgen.FillToFit = true; //string svg = d.ToSvgString(); //bm = SvgUtil.GetBitmapFromSvgXml(svg, bitmapWidth); //System.Windows.Media.Imaging.RenderTargetBitmap rtBm = d.ToBitmap(); string path = TempFile.GetTempFileName(ClientDirs.TempDir, "jpg", true); d.WriteTo("jpg", path); bm = new Bitmap(path); return(bm); }
/// <summary> /// https://github.com/cdk/cdk/wiki/Toolkit-Rosetta /// </summary> /// <returns>Full path to created .png file</returns> public static string DepictMolecule(this NCDK.IAtomContainer mol, string folder = null, int width = 300, int height = 350) { folder = folder ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"NCDK\Images"); width = width < 100 ? 300 : width; height = height < 100 ? 350 : height; if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } var dptgen = new DepictionGenerator { Size = new Size(width, height), ShowMoleculeTitle = true }; dptgen.SymbolVisibility = SymbolVisibility.All; var depiction = dptgen.Depict(mol); var canonicalSmiles = mol.GetProperty <string>(NCDK.CDKPropertyName.Title) ?? Path.GetRandomFileName(); var filterName = new StringBuilder(); foreach (var c in canonicalSmiles.ToCharArray()) { if (Path.GetInvalidPathChars().Contains(c) || Path.GetInvalidFileNameChars().Contains(c)) { continue; } filterName.Append(c); } filterName.Append(".png"); var filename = Path.Combine(folder, filterName.ToString()); depiction.WriteTo(filename); return(filename); }