AcCoreConsole Will Publish Only the Model Layout

AcCoreConsole Will Publish Only the Model Layout

larry.daubenspeck
Advocate Advocate
509 Views
0 Replies
Message 1 of 1

AcCoreConsole Will Publish Only the Model Layout

larry.daubenspeck
Advocate
Advocate

I have an AutoCAD addin which draws a simplistic view of my company’s product (given a few variable inputs).  When finished, it saves as both .dwg and .pdf, then emails the two as attachments back to the sales person who made the request.  It works great, producing a scale drawing that the sales people include with their quotations. 

 

Now, I am re-writing this to work with the AcCoreConsole.  All was going well, until I got to the pdf creation.  After reading post after post on this subject, I cobbled together a function which does the job – sort of – and returns True if successful.  The program then creates an email attachment out of the pdf file.  The problem is, the only layout that seems to work is the Model tab.  If I pass in the name of the tab I really want (“Elevation” in my case), the function still returns True, but the main program which called the function just quits when it gets to the line of code that tries to create the attachment.  I put it in a Try-Catch statement, and it doesn’t even make it to the Catch phrase; it just quits, with no indication of an error whatsoever.  I know the Elevation tab exists, because it’s there in the dwg file.  If I change “Elevation” to “Model” it works fine (except there is no border and title block).  Any ideas?

 

Here is my code:

 

   Friend Function SaveAsPDF(ByRef PdfDatabase As Database, FilePath As String, FileName As String, LayoutName As String, ByRef ErrorMsg As String) As Boolean

 

        Try

            'Put the plot in foreground.

            Dim bgPlot As Short = Core.Application.GetSystemVariable("BACKGROUNDPLOT")

            Core.Application.SetSystemVariable("BACKGROUNDPLOT", 0)

 

            'Get the layout ObjectId List.

            Dim layoutIds As System.Collections.Generic.List(Of ObjectId) = GetLayoutIds(PdfDatabase)

 

            Using Trans As Transaction = PdfDatabase.TransactionManager.StartTransaction

 

                Dim collection As DsdEntryCollection = New DsdEntryCollection

 

                For Each layoutId As ObjectId In layoutIds

                    Dim layout As Layout = Trans.GetObject(layoutId, OpenMode.ForRead)

                    If layout.LayoutName.Contains(LayoutName) Then

                        Dim entry As DsdEntry = New DsdEntry

                        With entry

                            .DwgName = FilePath & FileName & ".dwg"

                            .Layout = layout.LayoutName

                            .Title = FileName

                            .NpsSourceDwg = entry.DwgName

                            .Nps = "Setup1"

                        End With

                        collection.Add(entry)

                    End If

                Next

 

 

                Dim dsdData As DsdData = New DsdData

                With dsdData

                    .SheetType = SheetType.SinglePdf

                    .ProjectPath = FilePath

                    .DestinationName = FilePath & FileName & ".pdf"

                End With

 

 

                If System.IO.File.Exists(dsdData.DestinationName) Then System.IO.File.Delete(dsdData.DestinationName)

 

                dsdData.SetDsdEntryCollection(collection)

 

                Dim dsdFile As String = dsdData.ProjectPath & FileName & ".dsd"

 

                'Workaround to avoid promp for dwf file name.

                'Set PromptForDwfName=FALSE in dsdData using StreamReader/StreamWriter

 

                dsdData.WriteDsd(dsdFile)

 

                Dim sr As StreamReader = New StreamReader(dsdFile)

 

                Dim str As String = sr.ReadToEnd

                sr.Close()

 

                str = str.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE")

 

                Dim sw As StreamWriter = New StreamWriter(dsdFile)

 

                sw.Write(str)

                sw.Close()

 

                dsdData.ReadDsd(dsdFile)

                System.IO.File.Delete(dsdFile)

 

                Dim plotConfig As PlotConfig = PlotConfigManager.SetCurrentConfig("DWG To PDF.pc3")

                Dim publisher As Autodesk.AutoCAD.Publishing.Publisher = Autodesk.AutoCAD.ApplicationServices.Core.Application.Publisher

                publisher.PublishExecute(dsdData, plotConfig)

 

                Trans.Commit()

            End Using

 

            'Reset the background plot value.

            Core.Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot)

        Catch ex As Exception

            ErrorMsg = "Error @ AcadDrawing.SaveAsPDF()." & vbCrLf & ex.Message

            Return False

        End Try

 

        Return True

 

    End Function

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
510 Views
0 Replies
Replies (0)