AnsweredAssumed Answered

Save as PDF API

Question asked by Kirill Ishin on Oct 24, 2019

Hello everyone. We used that code for batch save in SW2018. Now in v.2020 it doesn't work. Please, help to solve this problem.

Macro code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.Top = 955;
            this.Left = 5;
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string catalog = textBox1.Text;
                Directory.CreateDirectory(catalog + "\\pdf");
                List<string> filesname = Directory.GetFiles(catalog, "*.slddrw").ToList<string>();
                string[] filesname1 = new string[filesname.Count()];
                SldWorks swApp = new SldWorks();
                ExportPdfData swExportPDFData = default(ExportPdfData);
                ModelDoc2 swModel = default(ModelDoc2);
                ModelDocExtension swModExt = default(ModelDocExtension);
                DrawingDoc swDrawDoc = default(DrawingDoc);
                Sheet swSheet = default(Sheet);
                bool boolstatus = false;
                string filesname2 = null;
                int errors = 0;
                int warnings = 0;
                string[] obj = null;
                for (int i = 0; i < filesname.Count(); i++)
                {
                    filesname1[i] = Path.GetFileNameWithoutExtension(filesname[i]);
                    boolstatus = false;
                    filesname2 = catalog + "\\pdf\\" + filesname1[i] + ".pdf";
                    errors = 0;
                    warnings = 0;
                    obj = null;
                    label2.Text = filesname[i] + "    -    сохраняется в pdf";
                    swModel = swApp.OpenDoc6(filesname[i], (int)swDocumentTypes_e.swDocDRAWING, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings);
                    swModExt = (ModelDocExtension)swModel.Extension;
                    swExportPDFData = (ExportPdfData)swApp.GetExportFileData((int)swExportDataFileType_e.swExportPdfData);
                    swDrawDoc = (DrawingDoc)swModel;
                    obj = (string[])swDrawDoc.GetSheetNames();
                    int count = obj.Length;
                    object[] objs = new object[count];
                    DispatchWrapper[] arrObjIn = new DispatchWrapper[count];
                    for (int j = 0; j < count; j++)
                    {
                        boolstatus = swDrawDoc.ActivateSheet((obj[j]));
                        swSheet = (Sheet)swDrawDoc.GetCurrentSheet();
                        objs[j] = swSheet;
                        arrObjIn[j] = new DispatchWrapper(objs[j]);
                    }
                    boolstatus = swExportPDFData.SetSheets((int)swExportDataSheetsToExport_e.swExportData_ExportSpecifiedSheets, (arrObjIn));
                    boolstatus = swModExt.SaveAs(filesname2, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, swExportPDFData, ref errors, ref warnings);
                }
                swApp.ExitApp();
                swApp = null;
                label2.Text = "Все файлы сохранены";
            }
            catch
            {
                label2.Text = "Что-то пошло не так";
            }           
        }
    }
}

Attachments

Outcomes