2

I'm developing C# VSTA macro for our manufacturing department. I'm using SldWorks.GetPreviewBitMapFile to save a bitmap preview of the selected model and show it in the picturebox as shown below:

enter image description here

The code looks fine and execute fine except for the strange colors:

bool status = swApp.GetPreviewBitmapFile(filepath, configuration, "D:\\preview.bmp");
            pictureBox1.ImageLocation = "D:\\Preview.bmp";
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Refresh();

Has anyone had a similar problem and what's the remedy?

Amen Jlili
  • 1,884
  • 4
  • 28
  • 51
  • Did you try to save image with GetPreviewBitmap? var h = swApp.GetPreviewBitmap("Part11.SLDPRT", "Default"); Bitmap iconBitmap = Bitmap.FromHicon(h.handle); iconBitmap.Save("Part11.bmp"); – Vladyslav Litunovsky Oct 19 '15 at 01:56
  • Cannot use var. I'm using C# 2.0 – Amen Jlili Oct 19 '15 at 07:57
  • It is strange, GetPreviewBitmap works fine in VBA macro, but doesn't work in .net macros. – Vladyslav Litunovsky Oct 19 '15 at 13:35
  • After digging a bit, it turns out the var keyword is only available in C# 3.0 and higher. I'm using VSTA 2005, so I can't get the handle/pointer using var. Perhaps, there's another way to get the pointer using GC (Garbage collector)? I will be very grateful if you can help me. – Amen Jlili Oct 19 '15 at 14:53
  • Do you use SolidWorks x64? In api help noted that GetPreviewBitmap is not supported in macros in SolidWorks x64. In this case convertging macros to addin might be solution. – Vladyslav Litunovsky Oct 19 '15 at 19:30

2 Answers2

3

Here is an example of working code:

//to compare output
iSwApp.GetPreviewBitmapFile(@"c:\Path\Part1.SLDPRT", "Default", @"c:\Path\Part1_0.bmp");

object com = iSwApp.GetPreviewBitmap(@"c:\Path\Part1.SLDPRT", "Default");
stdole.StdPicture pic = com as stdole.StdPicture;
Bitmap bmp = Bitmap.FromHbitmap((IntPtr)pic.Handle);
bmp.Save(@"c:\Path\Part1_1.bmp");

There are few notes about GetPreviewBitmap from SW API:

  • Currently only in-process applications (that is, macros or add-ins) can use this method; out-of-process applications (that is, executables) will get an automation error because the IPictureDisp interface cannot be marshalled across process boundaries. This is a Microsoft behavior by design. See the Microsoft Knowledge Base for details.
  • This method is not supported in macros or out-of-process applications in SolidWorks x64.

I was able to make it work in SolidWorks x64 only in .NET add-in and in VBA macros. Let me know if you need add-in example.

  • Code's not working in .NET macro. The `GetPreviewBitmap` is returning a null. I'm running SW15 on a Win X64. – Amen Jlili Oct 19 '15 at 21:31
  • Yes, it is mentioned in my reply "This method is not supported in macros or out-of-process applications in SolidWorks x64". I made it work in add-in. Did you consider to update your macros to SW add-in? I didn't find another way to get preview except SWDocumetManager, but it's different story and I'm not sure if you have serial key for it. – Vladyslav Litunovsky Oct 20 '15 at 13:18
  • I've emailed the API support. They said it's a high priority bug and that they're working on it. They've suggested I use the ModelDoc2.SaveBMP instead. Thank you for your @Vladyslav. I'm going to tick your answer. – Amen Jlili Oct 20 '15 at 20:24
2

An update for SW2016. The GetPreviewBitmapFile function works for an out-of-process Csharp console program. However, the colors of the generated bitmap are very strange. It turns out the only valid solution is to use the SaveBmp function.

Jeppe S.
  • 21
  • 2