xarial / xcad

Framework for developing CAD applications for SOLIDWORKS, including add-ins, stand-alone applications, macro features, property manager pages, etc.
https://xcad.net
MIT License
126 stars 25 forks source link

3D PDF creation #92

Closed popfra closed 1 year ago

popfra commented 1 year ago

Hello,

I would like to know, if it is possible to use xarial/xcad for 3D PDF creation and if yes do you have a small example.

Best regards

popfra

popfra commented 1 year ago

Hello,

sorry disturbing. I found a solution

    private const string ThreeD = "_3D";

    private static void SaveAs(string directoryName, string fileName, string extension, SwAddInEx source)
    {
        if (string.IsNullOrWhiteSpace(fileName))
            source.Application.ShowMessageBox(ExceptionHelper.CreateFileNotFoundException(fileName).Message);

        int error = 0;
        int warning = 0;
        var exportPDFAs3D = extension == $"{FileExtensions.Pdf}{ThreeD}";

        if (exportPDFAs3D)
        {
            extension = extension.Replace(ThreeD, string.Empty);
            fileName = ThreeD.Create(fileName, extension);
        }
        else
        {
            fileName = string.Empty.Create(fileName, extension);
        }

        var model = source.Application.Documents.Active.Model;
        var path = Path.Combine(directoryName, fileName);

        if (exportPDFAs3D)
        {
            var swApplication = source.Application.Sw;

            var exportData = swApplication.GetExportFileData((int)swExportDataFileType_e.swExportPdfData).To<IExportPdfData>();
            exportData.ExportAs3D = exportPDFAs3D;

            var saveAsOptions = model.Extension.GetAdvancedSaveAsOptions((int)swSaveWithReferencesOptions_e.swSaveWithReferencesOptions_IncludeToolBoxParts);

            if (!model.Extension.SaveAs3(path, 0, 2, exportData, saveAsOptions, error, warning))
                source.Application.ShowMessageBox(CreateExportFailedMessage(path, error, warning));

            return;
        }

        if (!model.SaveAs4(path, 0, 2, ref error, ref warning))
            source.Application.ShowMessageBox(CreateExportFailedMessage(path, error, warning));
    }

usage for 3D PDF:

        var model = Source.Application.Documents.Active.Model;

        SaveAs(@this.DisplayedData.ExportFolder.ItemName!, model.GetPathName().ToFileName(), $"{FileExtensions.Pdf}{ThreeD}", Source);

usage for normal PDF:

        var model = Source.Application.Documents.Active.Model;

        SaveAs(@this.DisplayedData.ExportFolder.ItemName!, model.GetPathName().ToFileName(), FileExtensions.Pdf, Source);