'convert printers in all layouts for a drawing
Public Shared Function ConvertLayoutPrinters(ByVal db As Database, ByVal sDwgName As String, ByRef dictPrinters As Dictionary(Of String, String), ByRef txtwriLog As System.IO.StreamWriter) As Boolean
Dim doc As Document = appAcad.DocumentManager.MdiActiveDocument
Dim ed As Editor = appAcad.DocumentManager.MdiActiveDocument.Editor
Dim sPlotConfigurationName As String = ""
Dim sLCasePlotConfigurationName As String = ""
Dim sNewPlotConfigurationName As String = ""
Dim sCanonicalMediaName As String = ""
Dim sNewCanonicalMediaName As String = ""
Dim sPageSetup As String = "" 'named page setup associated with layout
Dim bModel As Boolean 'is model space?
Dim bChanged As Boolean 'whether we changed anything in this dwg
Dim bError As Boolean = False 'was there an error somewhere
Using tr1 As Transaction = db.TransactionManager.StartTransaction()
Try
Dim layoutDic As DBDictionary
layoutDic = TryCast(tr1.GetObject(db.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
If layoutDic Is Nothing Then
MessageBox.Show(sDwgName & " layoutDic is nothing in ConvertLayoutPrinters")
End If
'assume no changes are made and no errors found at start of each drawing
bChanged = False
bError = False
For Each entry As DBDictionaryEntry In layoutDic
Dim layout As Layout = Nothing
Dim layoutId As ObjectId = ObjectId.Null
layoutId = entry.Value
layout = TryCast(tr1.GetObject(layoutId, OpenMode.ForRead), Layout)
If layout IsNot Nothing Then
sPlotConfigurationName = layout.PlotConfigurationName
'We have stored lower case printer name in the dictionary
sLCasePlotConfigurationName
= LCase(sPlotConfigurationName
) sCanonicalMediaName = layout.CanonicalMediaName
' Get any page setup associated with this layout
sPageSetup = layout.PlotSettingsName
Else
sPlotConfigurationName = ""
sCanonicalMediaName = ""
sLCasePlotConfigurationName = ""
sPageSetup = ""
End If
If dictPrinters.ContainsKey(sLCasePlotConfigurationName) And sPlotConfigurationName <> "" And sCanonicalMediaName <> "" Then
'Lookup new printer name from the conversion dictionary
sNewPlotConfigurationName = dictPrinters.Item(sLCasePlotConfigurationName)
bModel = layout.ModelType
sCanonicalMediaName = layout.CanonicalMediaName
'get a copy of the current plot settings for this layout
'we can make changes and then save back
Using currPlotSet As PlotSettings = New PlotSettings(layout.ModelType)
currPlotSet.CopyFrom(layout)
Dim plotSetVal As PlotSettingsValidator
plotSetVal = PlotSettingsValidator.Current
'AutoCAD recommends getting the latest list of devices and layouts
' before we do anything. Only need to do once.
layout.UpgradeOpen()
plotSetVal.RefreshLists(layout)
layout.DowngradeOpen()
'validate layout media / paper size to be safe
'we can't validate plot settings that are not applied to a layout
Using plotinf As PlotInfo = New PlotInfo
'To validate plot settings
Using plotinfval As PlotInfoValidator = New PlotInfoValidator
' This allows AutoCAD to match equivalent media on different plotting devices,
' e.g., map a RICOH plotter "Tabloid" page to the AutoCAD PDF "ANSI_expand_B_(11.00x17.00_inches)".
plotinfval.MediaMatchingPolicy = MatchingPolicy.MatchEnabled
' Make sure the PlotInfo has a Layout object it can latch onto.
' Looking at the layout of the drawing we are processing
plotinf.Layout = layout.ObjectId
' validating paper size / media settings
plotinf.OverrideSettings = currPlotSet
' Person running this must have new printer installed already or will have an error
plotinf.DeviceOverride = PlotConfigManager.SetCurrentConfig(sNewPlotConfigurationName)
' Validate the combined settings with the new device override.
' Layout we are testing against must be the current layout
LayoutManager.Current.CurrentLayout = layout.LayoutName
plotinfval.Validate(plotinf)
End Using 'plotinfval
' Set the plot configuration and media names for pushing to the new plot settings.
sNewPlotConfigurationName = plotinf.ValidatedSettings.PlotConfigurationName
sNewCanonicalMediaName = plotinf.ValidatedSettings.CanonicalMediaName
End Using 'plotinf
'update current plot settings
plotSetVal.SetPlotConfigurationName(currPlotSet, sNewPlotConfigurationName, sNewCanonicalMediaName)
' txtwriLog.WriteLine(sDwgName & "," & layout.LayoutName & "," & sPlotConfigurationName & "," & sNewPlotConfigurationName)
' Update the layout with the validated plot settings
layout.UpgradeOpen()
layout.CopyFrom(currPlotSet)
bChanged = True
End Using 'currPlotset
Else
'ed.WriteMessage(sDwgName & vbTab & layout.LayoutName & " printer " & sPlotConfigurationName & " not found in conversion file dictionary." & vbNewLine)
'txtwriLog.WriteLine(sDwgName & "," & layout.LayoutName & "," & sPlotConfigurationName & "," & "not found in conversion file.")
End If 'dictPrinters.ContainsKey
Next 'layout
'catch per tr1
Catch ex As System.AccessViolationException
'txtwriLog.WriteLine(sDwgName & "," & Layout.LayoutName & "," & "Error" & "," & ex.Message & "," & "Access violation / unhandled error in ConvertLayouts/For each layout.")
MessageBox.Show("convert Layout TR1 " & ex.Message & vbNewLine & vbNewLine & ex.StackTrace, sDwgName)
bError = True
Catch ex As System.Exception
MessageBox.Show("convert Layout TR1 " & sDwgName & " Unhandled exception in ConvertLayoutPrinters")
'txtwriLog.WriteLine(sDwgName & "," & Layout.LayoutName & "," & ex.Message & "," & " system error " & sNewPlotConfigurationName)
bError = True
Finally
If bError Then
tr1.Abort()
bChanged = False
Else
tr1.Commit()
End If
End Try 'tr1
End Using 'tr1
'return whether we changed anything so we know whether to save the dwg
Return bChanged
End Function