Dear All,



I can not figure out how to print my autocad drawings - all located in a same folder - to pdf from a Visual Studio App.

So far, I can open a Autocad Drawing, but after that, can for activated the particular PaperSpace (called "A3 - ENGLISH").

The PC3 file is Herve.pc3 and Herve.pmp.


Here below the code I have but not working. I look on google but still can not figure out how to di it.
Thank you
Herve

Code:
Imports System
Imports System.IO
Imports System.Text
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel


Module Mdl_Print
    Dim AcadApp As AcadApplication
    Dim AcadDoc As AcadDocument

    Public Sub PDF_Print()

        Dim dirInfo As New DirectoryInfo(Frm_Main.LblFolder1.Text)
        Dim fileInfo As FileInfo

        Dim AcadApp As AcadApplication = New AcadApplication
        AcadApp.Visible = True
        AcadApp.WindowState = AcWindowState.acMax
        Dim success As Boolean = False


        For Each fileInfo In dirInfo.GetFiles("*.dwg") 'dwg for Autocad
            AcadDoc = AcadApp.Documents.Open(fileInfo.FullName)


            Dim oSS As AcadSelectionSet
            oSS = AcadApp.ActiveDocument.SelectionSets.Add("oSS")
            oSS.Clear()


            Try
                Dim ftype(0) As Int16
                Dim fdata(0) As Object
                ftype(0) = 410  'layout
                fdata(0) = "A3 - ENGLISH"

                oSS.Select(AcSelect.acSelectionSetAll, , , ftype, fdata)

                Dim objPrefFiles As AcadPreferencesFiles
                Dim PC3PathOld As String
                Dim PC3PathNew As String
                Dim PMPPathOld As String
                Dim PMPPathNew As String

                Dim PtConfigs As AcadPlotConfigurations
                Dim PlotConfig As AcadPlotConfiguration
                Dim PtObj As AcadPlot
                Dim BackPlot As Object

                'Set the preferences object
                objPrefFiles = AcadDoc.Application.Preferences.Files

                'Get the current Printer Config Path (pc3)
                PC3PathOld = objPrefFiles.PrinterConfigPath
                MessageBox.Show(PC3PathOld, "old Printer Config path (pc3)")
                objPrefFiles.PrinterConfigPath = "C:\Users\ROBERT\Documents\Deb + Herve + Will\B - Herve\Technip\Autocad\1st project\OpenExcel\PC3 Files"
                PC3PathNew = objPrefFiles.PrinterConfigPath
                MessageBox.Show(PC3PathNew, "new Printer Config path (pc3)")

                'Get the current Printer Desc Path (pmp)
                PMPPathOld = objPrefFiles.PrinterDescPath
                MessageBox.Show(PMPPathOld, "Printer Desc Path (pmp)")
                objPrefFiles.PrinterDescPath = "C:\Users\ROBERT\Documents\Deb + Herve + Will\B - Herve\Technip\Autocad\1st project\OpenExcel\PMP Files"
                PMPPathNew = objPrefFiles.PrinterDescPath
                MessageBox.Show(PMPPathNew, "Printer Desc Path (pmp)")



                PtObj = AcadDoc.Plot
                AcadApp.ActiveDocument.AcPlotType.acExtents()

                PtConfigs = AcadDoc.PlotConfigurations
                ''Add a new plot configuration
                PtConfigs.Add("PDF", False)
                '
                ''The plot config you created become active
                PlotConfig = PtConfigs.Item("PDF")
                
                ''Use this method to set the scale
                PlotConfig.StandardScale = AcPlotScale.acScaleToFit
                ''Updates the plot
                PlotConfig.RefreshPlotDeviceInfo()

                'Here you specify the pc3 file you want to use
                PlotConfig.ConfigName = "Herve.pc3"

                'You can select the plot style table here
                PlotConfig.StyleSheet = "monochrome.ctb"

                'You can select the Scale Line Weight
                PlotConfig.ScaleLineweights = True

                'Specifies Paper Size
                PlotConfig.CanonicalMediaName = "ISO_A3_(420.00_x_297.00_MM)"

                'Specifies whether or not to plot using the plot styles
                PlotConfig.PlotWithPlotStyles = True

                BackPlot = AcadDoc.GetVariable("BACKGROUNDPLOT")
                AcadDoc.SetVariable("BACKGROUNDPLOT", 0)

                'Updates the plot
                PlotConfig.RefreshPlotDeviceInfo()
                MessageBox.Show("scale" & PlotConfig.StandardScale)
                MessageBox.Show("canonical media name" & PlotConfig.CanonicalMediaName)
                MessageBox.Show("config name" & PlotConfig.ConfigName)
                MessageBox.Show("plot type" & PlotConfig.PlotType)
                MessageBox.Show("scale line weight" & PlotConfig.ScaleLineweights)

                'Now you can use the PlotTofile method
                If PtObj.PlotToFile(Replace(AcadDoc.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then
                    AcadDoc.Utility.Prompt(vbLf + "PDF Was Created")
                Else
                    AcadDoc.Utility.Prompt(vbLf + "PDF Creation Unsuccessful")
                End If

                'If you wish you can delete the plot configuration you created programmatically, and set the 'BACKGROUNDPLOT' system variable to its original status.
                PtConfigs.Item("PDF").Delete()
                PlotConfig = Nothing
                AcadDoc.SetVariable("BACKGROUNDPLOT", BackPlot)

                objPrefFiles.PrinterConfigPath = "C:\Users\ROBERT\AppData\Roaming\Autodesk\AutoCAD 2014\R19.1\enu\Plotters"
                objPrefFiles.PrinterDescPath = "C:\Users\ROBERT\AppData\Roaming\Autodesk\AutoCAD 2014\R19.1\enu\Plotters\PMP File

                oSS.Clear()

                If AcadDoc.Saved Then
                    AcadDoc.Close(False)
                End If
                AcadDoc = Nothing
                AcadApp.Quit()

                AcadApp = Nothing
                success = True
            Catch ex As Exception
                MsgBox(ex.Message + vbLf + ex.StackTrace)
                success = False
            End Try
            '            AcadDoc.Close()
            AcadDoc = Nothing
            '            AcadApp.Quit()
        Next
        AcadApp.Quit()
    End Sub