AnsweredAssumed Answered

Solidworks Crash After Saving Drawing to PDF

Question asked by Tim Clift on Dec 21, 2018
Latest reply on Dec 21, 2018 by Artem Taturevych

I have written a Solidworks addin in C# using Microsoft Visual Studio 2017.  I am running Solidworks 2015 SP4.0.  In the code shown below when stepping through the code in the Visual Studio debugger Solidworks crashes as soon as I step past the last line of code in the OnClose member in the handler.  The basic function of the addin is to display a property manager with a list of all open drawings.  The property manager allows the user to select drawings from the list to be saved as a .pdf file or sent to a printer.  The code executes with no errors and saves the .pdf files.  It's only when the OnClose member ends does Solidworks crash.  Solidworks does not crash if the print option is selected.  If I comment out the SaveAs line in the save .pdf option portion of the code Solidworks does not crash.  This seems like a bug in the API.  Has anyone else seen this problem?

 

public void OnClose(int reason)
        {
            string exportFilename = "";
            string drawingName;
            DrawingDoc drawingDoc;
            double sheetHeight = 0;
            double sheetWidth = 0;
            int paperSize;
            Sheet currentSheet;

            if (!Directory.Exists(TARGETPATH))
            {
                Directory.CreateDirectory(TARGETPATH);
            }

            swExportPDFData = (ExportPdfData)iSwApp.GetExportFileData((int)swExportDataFileType_e.swExportPdfData);
            swExportPDFData.ViewPdfAfterSaving = true;
            swExportPDFData.SetSheets((int)swExportDataSheetsToExport_e.swExportData_ExportAllSheets, null);

            if (reason == (int)swPropertyManagerPageCloseReasons_e.swPropertyManagerPageClose_Okay)
            {
                short[] selectedDrawings = printOpenDrawingsPage.partsList.GetSelectedItems();
              
                for (short i = 0; i <= selectedDrawings.Length - 1; i++)
                {
                    drawingName = printOpenDrawingsPage.partsList.get_ItemText(selectedDrawings[i]);

                    foreach (ModelDoc2 p in printOpenDrawingsPage.ModelList)
                    {
                        if (string.Equals(Path.GetFileName(p.GetPathName()), drawingName, StringComparison.OrdinalIgnoreCase))
                        {
                            if (printOpenDrawingsPage.exportOption.Checked)
                            {
                                exportFilename = TARGETPATH + Path.GetFileNameWithoutExtension(p.GetPathName()) + ".pdf";
                                savestatus = p.Extension.SaveAs(exportFilename, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_AvoidRebuildOnSave, swExportPDFData, ref errors, ref warnings);
                            }

                            if (printOpenDrawingsPage.printOption.Checked)
                            {
                                drawingDoc = (DrawingDoc)p;
                                currentSheet = drawingDoc.GetCurrentSheet();
                                paperSize = currentSheet.GetSize(sheetWidth, sheetHeight);
                                pageSetup = p.PageSetup;

                                if (paperSize == 0)
                                {
                                    pageSetup.PrinterPaperSize = 1;
                                }

                                if (paperSize == 2)
                                {
                                    pageSetup.PrinterPaperSize = 3;
                                }

                                pageSetup.Orientation = 2;
                                p.Extension.PrintOut3(sheets, 1, true, PRINTER, "", true);
                            }
                        }
                    }
                }
            }
        }

Outcomes