public stdole.IPictureDisp GetPreview2(string FileName, bool Deep = false) { // This method is only supported for in-process execution. We need this the execute in a multi-threaded environment. // I tried starting a new instance of the SWDocMgr class for each thread, and // that seemed to work once, then didn't work, no explanation. // The error, when in a thread, is "Catastrophic Failure." // Use GetPreview() instead // get doc type SwDmDocumentType swDocType = GetTypeFromString(FileName); if (swDocType == SwDmDocumentType.swDmDocumentUnknown) { return(null); } // get the document SwDMDocument19 swDoc; SwDmDocumentOpenError nRetVal = 0; swDoc = (SwDMDocument19)swDocMgr.GetDocument(FileName, swDocType, true, out nRetVal); if (SwDmDocumentOpenError.swDmDocumentOpenErrorNone != nRetVal) { DialogResult dr = MessageBox.Show("Failed to open solidworks file: " + FileName, "Loading SW File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return(null); } SwDmPreviewError ePrevError = SwDmPreviewError.swDmPreviewErrorNone; stdole.IPictureDisp ipicPreview; try { ipicPreview = swDoc.GetPreviewPNGBitmap(out ePrevError); swDoc.CloseDoc(); return(ipicPreview); } catch { //DialogResult dr = MessageBox.Show("Failed to get solidworks preview image: " + FileName + ": " + ePrevError.ToString(), // "Loading SW Preview", // MessageBoxButtons.OK, // MessageBoxIcon.Exclamation, // MessageBoxDefaultButton.Button1); swDoc.CloseDoc(); return(null); } }
public Bitmap GetPreview(string FileName, bool Deep = false) { // external references for assembly files (GetAllExternalReferences4) // external references for part files (GetExternalFeatureReferences) SwDMDocument11 swDoc = default(SwDMDocument11); // get doc type SwDmDocumentType swDocType = GetTypeFromString(FileName); if (swDocType == SwDmDocumentType.swDmDocumentUnknown) { return(null); } // get the document SwDmDocumentOpenError nRetVal = 0; swDoc = (SwDMDocument11)swDocMgr.GetDocument(FileName, swDocType, true, out nRetVal); if (SwDmDocumentOpenError.swDmDocumentOpenErrorNone != nRetVal) { DialogResult dr = MessageBox.Show("Failed to open solidworks file: " + FileName, "Loading SW File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return(null); } SwDmPreviewError ePrevError = SwDmPreviewError.swDmPreviewErrorNone; try { byte[] bPreview = swDoc.GetPreviewBitmapBytes(out ePrevError); MemoryStream ms = new MemoryStream(bPreview); Bitmap bmp = (Bitmap)Bitmap.FromStream(ms); // crop and pad the image to 640x480 Bitmap bmp2 = resizeImage(bmp); return(bmp2); } catch { //DialogResult dr = MessageBox.Show("Failed to get solidworks preview image: " + FileName + ": " + ePrevError.ToString(), // "Loading SW Preview", // MessageBoxButtons.OK, // MessageBoxIcon.Exclamation, // MessageBoxDefaultButton.Button1); return(null); } }