0 Replies Latest reply on Nov 18, 2016 3:11 AM by Bratitsa Vlaids

    Convert Drawing document (slddrw) to pdf using virtual printer (C#)

    Bratitsa Vlaids

      Hi guys, I use this code

      1. Process[] processes = Process.GetProcessesByName("SLDWORKS");?
      2.                 foreach (var process in processes)?
      3.                 {?
      4.                     process.Kill();?
      5.                 }?
      6.                 sld = new SldWorks() { Visible = true };?
      7.                 int errors = 0, warnings = 0;?
      8.                 ModelDoc2 swModel = sld.OpenDoc6(@"D:\test drawings\ВНС-900.56.003+.SLDDRW", (int)swDocumentTypes_e.swDocDRAWING, (int)swOpenDocOptions_e.swOpenDocOptions_Silent +(int)swOpenDocOptions_e.swOpenDocOptions_ViewOnly, "", errors, warnings);              ?
      9.                 IDrawingDoc drawingDoc = swModel as IDrawingDoc;?
      10.                 string[] sheetNames = drawingDoc.GetSheetNames();?
      11.                 int sheetsCount = sheetNames.Length;?
      12.                       double[] propertis = null;?
      13. ?
      14.                 Random rnd = new Random();?
      15.                 sld.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swPageSetupPrinterDrawingScaleToFit, true);?
      16.                 for (int i = 0; i < sheetsCount; i++)?
      17.                 {?
      18.                     drawingDoc.ActivateSheet(sheetNames[i]);?
      19.                     Sheet sheet = drawingDoc.GetCurrentSheet();?
      20.                     PageSetup pagesetup = sheet.PageSetup;?
      21. ?
      22.                    ?
      23.                     pagesetup.Orientation =rnd.Next(1,2);?
      24.                     pagesetup.PrinterPaperSize = rnd.Next(8, 9); ;?
      25. ?
      26.                     (drawingDoc as ModelDoc).PrintOut(i + 1, i + 1, 1, true, "PDFCreator", 1, true);?
      27. ?
      28.                 }

      and virtual printer "Bullzip PDF Printer", to convert slddrw assembly into pdf. The file is converted successfully, but the test parameters aren’t applied (It doesn’t change orientation and paper size), despite the fact that a similar script on VBA worked

      1. Set swApp = Application.SldWorks?
      2. Set swDrDoc = swApp.ActiveDoc?
      3. If swDrDoc.GetType <> swDocDRAWING Or swDrDoc Is Nothing Then?
      4.   Exit Sub?
      5. End If?
      6. ?
      7. currSheet = swDrDoc.GetCurrentSheet.GetName?
      8. sheetNames = swDrDoc.GetSheetNames?
      9. ?
      10. For i = 0 To swDrDoc.GetSheetCount - 1?
      11. swDrDoc.ActivateSheet sheetNames(i)?
      12. With swDrDoc.PageSetup?
      13.   .DrawingColor = swPageSetup_BlackAndWhite?
      14.   .Orientation = swPageSetupOrient_Landscape?
      15.   .ScaleToFit = True?
      16.   .HighQuality = True?
      17.   .PrinterPaperSize = 8?
      18.    printer = printerCDE?
      19. End With?
      20. 'Debug.Print "Sheet: " & sheetNames(i) & "    Sheet Size: " & sheetSize & "    Printer: " & printer?
      21. swDrDoc.PrintOut i + 1, i + 1, 1, False, printer, 1, False?
      22. Next i?
      23. swDrDoc.ActivateSheet currSheet

       

      Help me solve this problem please. tnx

       

      ps I know about swExportPDFData.SetSheets (..) => modelDocExtension.SaveAs (..), but in large assemblies, this approach takes a lot of RAM