Problems Exporting .SLDDRW Files from PDM as PDF

Hello everyone. I’m creating a Solidworks PDM task add-in that needs to export many drawing files as PDFs. I struggled at first with getting the files to open correctly, since there is often a complicated tree of references that either takes a while to load, or fails entirely.

I solved this problem by using DetailingMode when opening the drawing, but this has led to a new issue where some model views will not appear at all in the exported PDFs, likely because they are not resolved or cached. I’m looking for a way to solve this issue as efficiently as possible, whether that be a way to detect missing views and warn the user to fix it manually, fix missing views in the task itself, or use another method of opening files which avoids the issue entirely.

Here is my current PDF export code:

private bool ExportToPdf(string sourcePath, string outputPath, ref int openErrors, ref int openWarnings, ref int saveErrors, ref int saveWarnings)
{
    ModelDoc2 swDoc = null;
    try
    {
        Log($"Opening doc at {sourcePath}");
        // open file as sw doc
        swDoc = (ModelDoc2)swApp.OpenDoc6(
            sourcePath,
            (int)swDocumentTypes_e.swDocDRAWING,
            (int)swOpenDocOptions_e.swOpenDocOptions_Silent | 
            (int) swOpenDocOptions_e.swOpenDocOptions_OpenDetailingMode,
            "",
            ref openErrors,
            ref openWarnings);

        if (swDoc == null)
            return false;

        ModelDocExtension ext = swDoc.Extension;

        Log($"Saving doc to {outputPath}");
        // save doc as pdf to output path
        bool result = ext.SaveAs(
            outputPath,
            (int)swSaveAsVersion_e.swSaveAsCurrentVersion,
            (int)swSaveAsOptions_e.swSaveAsOptions_Silent,
            null,
            ref saveErrors,
            ref saveWarnings);

        if (openWarnings == 2) // disregard read only warnings
            openWarnings = 0;
        
        return result;
    }
    catch (Exception ex)
    {
        Log($"Error exporting to {outputPath}: {ex.Message}", true, false);
        return false;
    }
    finally
    {
        swApp.CloseDoc(sourcePath);
        if (swDoc != null)
            Marshal.ReleaseComObject(swDoc);
    }
}

@AmenJlili I guess the primary thing I’m looking for is a way to access the reference tree that is both efficient and tolerant of errors. Efficiency is important, but the main issue is a file failing to open altogether because of a minor issue, when I can open it just fine in the Solidworks GUI (albeit while dismissing an error or two).

I do appreciate the link you sent, but unfortunately it seems like writing it myself is still my best option.