Help exporting a preview image from a model.


Hello all,

  I am looking to export preview images from SolidEdge models, during a process when we run through entire assembly structures.

  I have use the method of opening components in windows of their own, saving as an image and then closing the window, but this causes further problems with changes to the models, etc...

  So, I would like the investigate using the sepreview.ocx component to extract them after the models have been saved.

  Does anyone have any example code they could share for using this component? - I am working in a C++ addin environment.

  Or, if anyone has any alternate, better suggestions for how to retrieve the preview...




Posted by: chris mann
Post date: 1/24/2008 4:19:35 AM


RE: Help exporting a preview image from a model.


Hi Chris,

Here's some C# code to extract the thumbnail preview directly from the file (the interface definitions etc are missing, but you'll only need to include the right header..) [DllImport("gdi32", CharSet=CharSet.Auto)] private extern static int DeleteObject(IntPtr hObject); [DllImport("shell32")] private extern static int SHGetDesktopFolder([Out] out IShellFolder ppshf); private static Guid IIDISHELLFOLDER = new Guid("000214E6-0000-0000-C000-000000000046"); private static Guid IIDIEXTRACTIMAGE = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); public static Bitmap GetThumbnail(string path) { return GetThumbnail(path, new Size(100, 100)); } public static Bitmap GetThumbnail(string path, Size imageSize) { Bitmap thumbnail = null; IShellFolder folder = null; IShellFolder item = null; IntPtr pidlFolder = IntPtr.Zero; IntPtr hBmp = IntPtr.Zero; IExtractImage extractImage = null; IntPtr pidl = IntPtr.Zero; FileInfo file = new FileInfo(path); if (file.Exists) { try { SHGetDesktopFolder(out folder); if (folder != null) { int cParsed; int pdwAttrib; int HR = folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, file.Directory.FullName, out cParsed, out pidlFolder, out pdwAttrib); if (HR != S_OK) return null; if (pidlFolder != IntPtr.Zero) { HR = folder.BindToObject(pidlFolder, IntPtr.Zero, ref IIDISHELLFOLDER, out item); if (HR != S_OK) return null; if (item != null) { int pchEaten; int pdwAttributes; HR = item.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, file.Name, out pchEaten, out pidl, out pdwAttributes); if (HR != S_OK) return null; int prgf = 0; object temp; HR = item.GetUIObjectOf(0, 1, new IntPtr[] {pidl}, ref IIDIEXTRACTIMAGE, out prgf, out temp); if (HR != S_OK) return null; extractImage = temp as IExtractImage; if (extractImage != null) { StringBuilder location = new StringBuilder(MAXPATH, MAXPATH); int priority = 0; int requestedColorDepth = 32; IEIFLAG uFlags = IEIFLAG.IEIFLAG_ASPECT | IEIFLAG.IEIFLAG_ORIGSIZE | IEIFLAG.IEIFLAG_QUALITY; HR = extractImage.GetLocation(location, location.Capacity, ref priority, ref imageSize, requestedColorDepth, ref uFlags); if (HR != S_OK) return null; HR = extractImage.Extract(out hBmp); if (HR != S_OK) return null; if (hBmp != IntPtr.Zero) thumbnail = Bitmap.FromHbitmap(hBmp); } } } } } finally { if (hBmp != IntPtr.Zero) DeleteObject(hBmp); if (pidlFolder != IntPtr.Zero) Marshal.FreeCoTaskMem(pidlFolder); if (extractImage != null) Marshal.ReleaseComObject(extractImage); if (item != null) Marshal.ReleaseComObject(item); if (folder != null) Marshal.ReleaseComObject(folder); } } return thumbnail; }




Posted by: Calum McLellan
Post date: 1/24/2008 4:28:40 AM

RE: Help exporting a preview image from a model.

Thanks, Calum - That was enough to get me to where I needed to be in C++!


Only problem is..... the previews are not quite as nice as the previews I was generating myself (fitting the model to window, switching off datums, etc... before saving the image...)

Maybe I can do that on a pre-save trigger to ensure that the preview automatically generated looks nice...





Posted by: chris mann
Post date: 1/25/2008 4:46:43 AM