/// <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); }
/// <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.getAtomCount() == 0) { return(null); } //NativeMol.setProperty(CDKConstants.TITLE, "caffeine"); // title already set from input! sun.awt.Win32FontManager w32Font = new sun.awt.Win32FontManager(); // need this for class to be found Environment.SetEnvironmentVariable("CLASSPATH", ".", EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("JAVA_HOME", @"C:\Mobius_OpenSource\MobiusCommon\CdkMx\bin\Debug", EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("Path", @"%JAVA_HOME%", EnvironmentVariableTarget.Process); /* Need following? * CLASSPATH=.; / JAVA_HOME=C:\Program Files\Java\jdk1.6.0_21 / Path=%JAVA_HOME%\bin; */ //DecimalFormatSymbols.getInstance(Local.ENGLISH); //Locale.forLanguageTag("en-US") DepictionGenerator dptgen = new DepictionGenerator(); dptgen.withSize(bitmapWidth, bitmapHeight); // px (raster) or mm (vector) //dptgen.withMolTitle(); //dptgen.withTitleColor(Color.DarkGray); // annotations are red by default Depiction d = dptgen.depict(NativeMol); /* * * public const string SVG_FMT = "svg"; * public const string PS_FMT = "ps"; * public const string EPS_FMT = "eps"; * public const string PDF_FMT = "pdf"; * public const string JPG_FMT = "jpg"; * public const string PNG_FMT = "png"; * public const string GIF_FMT = "gif"; * * public const string UNITS_MM = "mm"; * public const string UNITS_PX = "px"; */ java.util.List formats = d.listFormats(); java.io.File file = new java.io.File(@"c:\downloads\test.jpg"); bool b = file.canWrite(); string s = d.toSvgStr(); d.writeTo("jpg", file); //d.writeTo("svg", @"c:\downloads\test.svg"); //string svg = d.toSvgStr(); //bm = SvgUtil.GetBitmapFromSvgXml(svg, bitmapWidth); bm = new Bitmap(@"c:\downloads\test.jpg"); return(bm); }
public ChemObjectBox() { Generator = new DepictionGenerator(); }