VB| Saveas dialog between preview and print
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I am writing code that when you click a ribbon button, you then select two points (which become the plot window area). Now I put that window to an A size sheet and print preview so the user can determine if it looks right. If so then a save as dialog pops up for the user to pick where to save and what to call the drawing. Then Plot. The code breaks (System.AccessViolationException) when the saveas dialog pops up. I'm guessing its interrupting the plot process but I'm not clear on why. If I put the saveas dialog at the beginning, the saveas dialog and print preview and plot works fine. I have two questions: 1. Is there a way to get the pdf name and location other than a saveas dialog? 2. If not is there a way to pause the plot procedure to allow the saveas dialog to complete before moving on?
Imports CadApp = Autodesk.AutoCAD.ApplicationServices
'Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports CadEdIn = Autodesk.AutoCAD.EditorInput
Imports CadDBS = Autodesk.AutoCAD.DatabaseServices
Imports CadGeo = Autodesk.AutoCAD.Geometry
Imports CadPrint = Autodesk.AutoCAD.PlottingServices
Imports CadPubl = Autodesk.AutoCAD.Publishing
Imports CadCui = Autodesk.AutoCAD.Customization '<-- used for developing CUIX files
Imports CadWin = Autodesk.Windows '<--(aduimdg.dll & adwindows.dll) used for classes and enum for ribbon
Imports System.Reflection
Imports DB = System.Diagnostics.Debug ' Shortens code to print to the output tab below
#Region " Emp_PDF_Window_FitToA"
<CadRun.CommandMethod("Emp_PDF_Window_FitToA")>
Public Sub Emp_PDF_Window_FitToA()
Initiate()
' Exit if printer is busy
If CadPrint.PlotFactory.ProcessPlotState <> CadPrint.ProcessPlotState.NotPlotting Then : g_AcDoc.Editor.WriteMessage(vbLf & "Another plot is in progress.") : DB.WriteLine(vbNewLine & "Class1 | Emp_Window_FitToA | Another plot is in progress." & vbNewLine) : Exit Sub : End If
' Get and set the BackgroundPlot system variable value
Dim backPlot As Object = CadApp.Application.GetSystemVariable("BACKGROUNDPLOT") : CadApp.Application.SetSystemVariable("BACKGROUNDPLOT", 0)
Dim SnapSet As Object = CadApp.Application.GetSystemVariable("OSMODE") : CadApp.Application.SetSystemVariable("OSMODE", 0)
Dim Trans As CadDBS.Transaction = g_AcDatBas.TransactionManager.StartTransaction()
Dim BlTblRec As CadDBS.BlockTableRecord = CType(Trans.GetObject(g_AcDatBas.CurrentSpaceId, CadDBS.OpenMode.ForRead), CadDBS.BlockTableRecord)
Dim PlotInfo As CadPrint.PlotInfo = New CadPrint.PlotInfo
Dim PlSet As CadDBS.PlotSettings = New CadDBS.PlotSettings(g_ActiveLayout.Layout.ModelType)
Dim PlSetVal As CadDBS.PlotSettingsValidator = CadDBS.PlotSettingsValidator.Current
Dim PlStyle As New PlotStyle
Dim PrPl As New PrinterPlotter
'Try
Dim WinCoord As CadDBS.Extents2d = GetWindow()
PlotInfo.Layout = BlTblRec.LayoutId
' API makes sure that the new "plotsettings" is initialized correctly.
Dim plSets As CadDBS.DBDictionary = Trans.GetObject(g_AcDatBas.PlotSettingsDictionaryId, CadDBS.OpenMode.ForRead)
' Check to see if the page setup exists
If plSets.Contains("EZ-Print-PDF-Window-A-Scale_To_Fit") = False Then
PrintObj(ShtSizeEnum.A, FitOrScaleEnum.Fit, g_ActiveLayout.Layout, g_ActiveLayout.Standard, True, WinCoord)
PlSet = plSets.GetAt("EZ-Print-PDF-Window-A-Scale_To_Fit").GetObject(CadDBS.OpenMode.ForWrite)
Else
PlSet = plSets.GetAt("EZ-Print-PDF-Window-A-Scale_To_Fit").GetObject(CadDBS.OpenMode.ForWrite)
End If
'Add these plot settings to the plot settings dictionary
PlSet.AddToPlotSettingsDictionary(g_AcDatBas)
' Refresh the plot settings with plot settings validator
PlSetVal.RefreshLists(PlSet)
' Update the plot info with the plot settings
PlotInfo.OverrideSettings = PlSet
Dim PlotInfoVal As CadPrint.PlotInfoValidator = New CadPrint.PlotInfoValidator
PlotInfoVal.MediaMatchingPolicy = CadPrint.MatchingPolicy.MatchEnabled
PlotInfoVal.Validate(PlotInfo)
' Plot with preview
Dim PrevStatus As CadPrint.PreviewEndPlotStatus
PrevStatus = PreviewOrPlotWithDialog(PlotInfo, True, True, g_DefaultPdfFileName)
' if plot is selected from the preview dialog, then plot
If PrevStatus = CadPrint.PreviewEndPlotStatus.Plot Then
Stop
Dim SetFileName As String = SaveAsDialog()
PrevStatus = PreviewOrPlotWithDialog(PlotInfo, False, True, SetFileName)
PlSet.Dispose()
Trans.Commit()
' Regenerate the document
g_AcDoc.Editor.Regen()
' Restore the previous value for the system variables
CadApp.Application.SetSystemVariable("BACKGROUNDPLOT", backPlot)
CadApp.Application.SetSystemVariable("OSMODE", SnapSet)
Else
PlSet.Dispose()
Trans.Dispose()
' Restore the previous value for the system variables
CadApp.Application.SetSystemVariable("BACKGROUNDPLOT", backPlot)
CadApp.Application.SetSystemVariable("OSMODE", SnapSet)
End If
'Catch ex As Exception
' Return
'End Try
End Sub
#End Region
#Region " SaveAsDialog | Public Function"
''' <summary>
''' Open a SaveAs dialog and return its full filename
''' </summary>
''' <returns>Full filename as string</returns>
Public Function SaveAsDialog() As String
DB.WriteLine(vbNewLine & "Globals | SaveAsDialog | Started" & vbNewLine)
Dim FileName As String = ""
Dim SaveDlog As New System.Windows.Forms.SaveFileDialog
SaveDlog.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*"
SaveDlog.FilterIndex = 1
SaveDlog.DefaultExt = "pdf"
SaveDlog.FileName = System.IO.Path.GetFileNameWithoutExtension(g_AcDoc.Name) & " - " & g_ActiveLayout.Layout.LayoutName
SaveDlog.InitialDirectory = "Z:\vault_pdf\"
SaveDlog.OverwritePrompt = True
Dim DlogRes As Windows.Forms.DialogResult = SaveDlog.ShowDialog
' Print PDF as Document name - Layout Name to the vault directory
If DlogRes = Windows.Forms.DialogResult.OK Then
DB.WriteLine(vbNewLine & "Globals | SaveAsDialog | Returning: " & SaveDlog.FileName & vbNewLine)
Return SaveDlog.FileName
Else
DB.WriteLine(vbNewLine & "Globals | SaveAsDialog | NOT returning anything." & vbNewLine)
Return Nothing
Exit Function
End If
DB.WriteLine(vbNewLine & "Globals | SaveAsDialog | End of Function" & vbNewLine)
End Function
#End Region
#Region " PreviewPlotWithDialog | Public Sub"
Public Function PreviewOrPlotWithDialog(ByRef PlotInfo As CadPrint.PlotInfo, ByVal IsPreview As Boolean, Optional ByVal PlotPDF As Boolean = False, Optional ByVal PdfName As String = "") As CadPrint.PreviewEndPlotStatus
Dim PrEng As CadPrint.PlotEngine = CadPrint.PlotFactory.CreatePreviewEngine(CInt(CadPrint.PreviewEngineFlags.Plot))
Dim PreviewStat As CadPrint.PreviewEndPlotStatus = Nothing
'Dim PlEng As CadPrint.PlotEngine = CadPrint.PlotFactory.CreatePublishEngine
Dim PlProgDlog As CadPrint.PlotProgressDialog = New CadPrint.PlotProgressDialog(IsPreview, 1, True)
Dim Trans As CadDBS.Transaction = g_AcDatBas.TransactionManager.StartTransaction
Dim DwgName As String = System.IO.Path.GetFileNameWithoutExtension(g_AcDoc.Name)
Dim LaytName As String = g_ActiveLayout.Layout.LayoutName
Dim PrintDesc As String = DwgName & " - " & LaytName
Dim EndPltStat As CadPrint.PreviewEndPlotStatus = CadPrint.PreviewEndPlotStatus.Cancel
' Setup the plot progress dialog
'ppd.PlotMsgString(CadPrint.PlotMessageIndex.DialogTitle, "Custom Plot Progress")
PlProgDlog.PlotMsgString(CadPrint.PlotMessageIndex.DialogTitle) = "Custom Preivew Progress"
PlProgDlog.PlotMsgString(CadPrint.PlotMessageIndex.SheetName) = PrintDesc
PlProgDlog.PlotMsgString(CadPrint.PlotMessageIndex.CancelJobButtonMessage) = "Cancel Job"
PlProgDlog.PlotMsgString(CadPrint.PlotMessageIndex.CancelSheetButtonMessage) = "Cancel Sheet"
PlProgDlog.PlotMsgString(CadPrint.PlotMessageIndex.SheetSetProgressCaption) = "Sheet Set Progress"
PlProgDlog.PlotMsgString(CadPrint.PlotMessageIndex.SheetProgressCaption) = "Sheet Progress"
' Set the progress bar limits and position
PlProgDlog.LowerPlotProgressRange = 0
PlProgDlog.UpperPlotProgressRange = 100
PlProgDlog.PlotProgressPos = 0
' Establish start point
PlProgDlog.OnBeginPlot()
' Show dialog is visible
PlProgDlog.IsVisible = True
' Start plotting
PrEng.BeginPlot(PlProgDlog, Nothing)
' Begin with this document (plot settings, Document name, parameters, # of copies, plot to file, filename for plot to file)
If PlotPDF = False Then
PrEng.BeginDocument(PlotInfo, g_AcDoc.Name, Nothing, 1, Not IsPreview, "")
Else
#Region " *Commented Test to desktop"
'' Print PDF as Document name - Layout Name to the desktop
'Dim MyDesktop As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\"
'PlotEng.BeginDocument(PlotInfo, g_AcDoc.Name, Nothing, 1, True, MyDesktop & g_PrintDesc)
'DB.WriteLine(vbNewLine & $"Globals | PlotWithDialog | Printing PDF: {MyDesktop.ToString & DwgName & " - " & LaytName} to the desktop." & vbNewLine)
#End Region
PrEng.BeginDocument(PlotInfo, g_AcDoc.Name, Nothing, 1, IsPreview, PdfName)
End If
' Dialog start with this sheet(Show Progress bar)
PlProgDlog.OnBeginSheet()
' Set sheet progress bar limits and position
PlProgDlog.LowerSheetProgressRange = 0
PlProgDlog.UpperPlotProgressRange = 100
PlProgDlog.SheetProgressPos = 0
' Show dialog is visible
'PlProgDlog.IsVisible = True
' Create page info
Dim SheetPlPgInfo As CadPrint.PlotPageInfo = New CadPrint.PlotPageInfo()
' Plot engine begin with this page
PrEng.BeginPage(SheetPlPgInfo, PlotInfo, True, Nothing)
' Handle progress bar graphic
PrEng.BeginGenerateGraphics(Nothing)
PlProgDlog.SheetProgressPos = 50
PrEng.EndGenerateGraphics(Nothing)
Dim PreviewEPI As CadPrint.PreviewEndPlotInfo = New CadPrint.PreviewEndPlotInfo
' Establish this page as the last page
PrEng.EndPage(PreviewEPI)
EndPltStat = PreviewEPI.Status
' On last page set the sheet progress bar to complete
PlProgDlog.SheetProgressPos = 100
' Dialog end plot of sheet
PlProgDlog.OnEndSheet()
' Plot engine end plot of document
PrEng.EndDocument(Nothing)
' Set dialog progress bar to complete
PlProgDlog.PlotProgressPos = 100
' Dialog end
PlProgDlog.OnEndPlot()
' Plot engine end
PrEng.EndPlot(Nothing)
' Close dialog
'PlProgDlog.Dispose()
Trans.Commit()
PrEng.Destroy()
Return EndPltStat
End Function
#End Region
**Moderator edit: Changed code window format from General to Visual Basic