Mike,
Here is the code that I have so far. The one thing that it doesn't explicitly do is set the copied or created PlotSettings object to current as per the first dialog when working with PageSetups. Let me know if you have questions. The constants are pretty self-explanatory and I haven’t included their contents as many of them contain company proprietary information. The code insert function for the forum keeps the indentations, but is not wide enough to keep the lines from wrapping so you will probably want to paste the code into VS to read through it.
Kerry
These are the imports from the code module not all of which are used by the two procedures included here.
Imports System
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.PlottingServices
Imports Autodesk.AutoCAD.Runtime
Public Function CopyPageSetupFromFile(ByVal SourceName As String, ByVal CallerName As String) As Boolean
'***************************************************************************************************************
' Procedure Name: CopyPageSetupFromFile
'
' Purpose: Checks for the Passed PlotSettings Object in the Source Drawing File and Copies It to the Current Drawing's Layout1
'
' Return Values: True/False
'
' Assumptions: Any Called Procedures will Provide Error Handling and Messages
'
' Globals: N/A
'
'***************************************************************************************************************
Const CONST_PROCEDURE_NAME As String = "CopyPageSetupFromFile"
' Declare and Instanciate the Variables Needed for Handling the External Source Document.
Dim objSourceDatabase As New Database
Dim objSourceTransactionManager As DatabaseServices.TransactionManager
'**************** Source Transaction Starts Here ****************
objSourceTransactionManager = objSourceDatabase.TransactionManager
Using objSourceTransaction As DatabaseServices.Transaction = objSourceTransactionManager.StartTransaction()
' Set Up the Source File Name
Dim SourceFilePath As String = CONST_SPE_FILEPATH & "\" & SourceName.ToUpper & CONST_DWG_EXTENSION.ToUpper
' Try to Open the Source Drawing File
Try
objSourceDatabase.ReadDwgFile(SourceFilePath, FileOpenMode.OpenForReadAndReadShare, True, "")
Catch excAutoCAD As Autodesk.AutoCAD.Runtime.Exception
' Handle Specific AutoCAD Exceptions Then
' Generic AutoCAD Exceptions and Then Generic System Exceptions
Dim strMessage As String
If excAutoCAD.Message = "eFileNotFound" Then
strMessage = "An Error Occurred While Attempting to Insert the Layout from" & vbCrLf & _
SourceFilePath & " as a Source Drawing." & vbCrLf & vbCrLf & _
"The File must be Located in Your SPE Folder and Must Have the " & vbCrLf & _
"Values Set as per the SPE Installation Instructions." & vbCrLf & _
CONST_ERRMSG_INSERTMAT_CLOSING
MsgBox(strMessage, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, CallerName)
Return False
Else
strMessage = CONST_ACAD_ERRMSG_START & CONST_PROCEDURE_NAME & _
CONST_GEN_ERRMSG_MIDDLE & excAutoCAD.ToString & _
CONST_GEN_ERRMSG_END & _
CONST_ERRMSG_INSERTMAT_CLOSING
MsgBox(strMessage, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, CallerName)
Return False
End If
Catch excGeneric As System.Exception
Dim strMessage As String
strMessage = CONST_GEN_ERRMSG_START & CONST_PROCEDURE_NAME & _
CONST_GEN_ERRMSG_MIDDLE & excGeneric.ToString & _
CONST_GEN_ERRMSG_END & _
CONST_ERRMSG_INSERTMAT_CLOSING
MsgBox(strMessage, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, CallerName)
Return False
End Try
Dim objSourcePlotSettings As PlotSettings = New PlotSettings(False)
Dim objSourcePlotSettingsDictionary As DBDictionary = objSourceTransaction.GetObject _
(objSourceDatabase.PlotSettingsDictionaryId, OpenMode.ForRead)
' Get the Passed PlotSettings Object as the Source If It Exists in the Source Drawing
Dim PlotSettingsFound = False
If objSourcePlotSettingsDictionary.Count > 0 Then
For Each objDictionaryEntry As DictionaryEntry In objSourcePlotSettingsDictionary
If String.Compare(objDictionaryEntry.Key.ToString.ToUpper, SourceName.ToUpper, True) = 0 Then
objSourcePlotSettings = objSourceTransaction.GetObject(objDictionaryEntry.Value, OpenMode.ForRead)
PlotSettingsFound = True
Exit For
End If
Next
End If
' If It was Not Found, We Need to Send a Message and Shutdown the InsertMat Program
If Not PlotSettingsFound Then
Dim strMessage = "The File " & SourceFilePath & " Does Not Contain the " & SourceName & " Page Setup." & vbCrLf & _
"You Must Rebuild the " & SourceName.ToUpper & CONST_DWG_EXTENSION & " File." & vbCrLf & _
"The File must be Located in Your SPE Folder and Must Have the " & vbCrLf & _
"Values Set as per the SPE Installation Instructions." & vbCrLf & _
CONST_ERRMSG_INSERTMAT_CLOSING
MsgBox(strMessage, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, CallerName)
Return False
End If
'********************************************************************************************
' Set the Current Drawing Object to the Application's Active Document - Replaces ThisDrawing
'********************************************************************************************
Dim objCurrentDrawing As Document = DocumentManager.MdiActiveDocument
'******** The CurrentDrawing Object will be the Destination Drawing for This Routine ********
' Set The Other Variables Needed to Handle the CurrentDrawing and Its Objects
Dim objDatabase As Database = objCurrentDrawing.Database
Dim objTransactionManager As DatabaseServices.TransactionManager = objDatabase.TransactionManager
'**************** Destination Transaction Starts Here ****************
Using objTransaction As DatabaseServices.Transaction = objTransactionManager.StartTransaction()
If SetLayoutAsCurrent(CONST_LAYOUT1, CallerName) Then
' The Layout Exists and is Current So Get a Reference to It Using the Layout Manager
Dim objLayoutManager As LayoutManager = LayoutManager.Current
Dim objLayout As Layout = objTransaction.GetObject(objLayoutManager.GetLayoutId(objLayoutManager.CurrentLayout), _
OpenMode.ForWrite)
' Output the Name of the Current Layout and Its Device
objCurrentDrawing.Editor.WriteMessage(vbLf & "Current Layout: " & objLayout.LayoutName & vbLf)
objCurrentDrawing.Editor.WriteMessage("Current Plot Configuration Name: " & objLayout.PlotConfigurationName & vbLf)
' Reference the Destination PlotSettings Dictionary
Dim objPlotSettingsDictionary As DBDictionary = objTransaction.GetObject(objDatabase.PlotSettingsDictionaryId, _
OpenMode.ForRead)
' If the Passed PlotSettings Object Exists in the Current Drawing, Delete It
If objPlotSettingsDictionary.Count > 0 Then
For Each objDictionaryEntry As DictionaryEntry In objPlotSettingsDictionary
If String.Compare(objDictionaryEntry.Key.ToString.ToUpper, SourceName.ToUpper, True) = 0 Then
objPlotSettingsDictionary.UpgradeOpen()
objPlotSettingsDictionary.Remove(objDictionaryEntry.Value)
objPlotSettingsDictionary.DowngradeOpen()
Exit For
End If
Next
End If
' Create the Destination PlotSettings Object and Populate It from the Source PlotSettings
Dim objDestinationPlotSettings As PlotSettings = New PlotSettings(False)
objDestinationPlotSettings.CopyFrom(objSourcePlotSettings)
' Update the PlotSettings Object to Make It Handle the Mat Insertion
Dim objDestinationPlotSettingsValidator As PlotSettingsValidator = PlotSettingsValidator.Current
' Set to Use a Standard Scale Type 1:1
If Not objDestinationPlotSettings.UseStandardScale Then
objDestinationPlotSettingsValidator.SetUseStandardScale(objDestinationPlotSettings, True)
End If
If Not objDestinationPlotSettings.StdScaleType = StdScaleType.StdScale1To1 Then
objDestinationPlotSettingsValidator.SetStdScaleType(objDestinationPlotSettings, StdScaleType.StdScale1To1)
End If
' Set to Scale the Lineweights
If Not objDestinationPlotSettings.ScaleLineweights Then
objDestinationPlotSettings.ScaleLineweights = True
End If
' Make Sure that the Plot is in Landscape
If Not objDestinationPlotSettings.PlotRotation = PlotRotation.Degrees090 Then
objDestinationPlotSettingsValidator.SetPlotRotation(objDestinationPlotSettings, PlotRotation.Degrees090)
End If
' Set to Plot Extents
If Not objDestinationPlotSettings.PlotType = DatabaseServices.PlotType.Extents Then
objDestinationPlotSettingsValidator.SetPlotType(objDestinationPlotSettings, DatabaseServices.PlotType.Extents)
End If
' Add the Plot Settings Object to the Destination Plot Settings Dictionary
objDestinationPlotSettings.AddToPlotSettingsDictionary(objDatabase)
objLayout.CopyFrom(objDestinationPlotSettings)
' Confirm that the Copied PlotSettings Object Exists in the Current Drawing's Plot Settings Dictionary
PlotSettingsFound = False
If objPlotSettingsDictionary.Count > 0 Then
For Each objDictionaryEntry As DictionaryEntry In objPlotSettingsDictionary
If String.Compare(objDictionaryEntry.Key.ToString.ToUpper, SourceName.ToUpper, True) = 0 Then
PlotSettingsFound = True
' Output the Name of the Copied PlotSettings Object and Its Device Name
objCurrentDrawing.Editor.WriteMessage(vbLf & "Copied PlotSettings: " & objLayout.PlotSettingsName & vbLf)
objCurrentDrawing.Editor.WriteMessage("Copied Device Name: " & objLayout.PlotConfigurationName & vbLf)
Exit For
End If
Next
End If
objPlotSettingsDictionary = Nothing
' If It was Not Found, We Need to Send a Message and Shutdown the InsertMat Program
If Not PlotSettingsFound Then
Dim strMessage = "The " & SourceName & " Page Setup Did Not Copy Correctly!" & vbCrLf & vbCrLf & _
"You Must Rebuild the " & SourceName.ToUpper & CONST_DWG_EXTENSION & " File." & vbCrLf & _
"The File must be Located in Your SPE Folder and Must Have the " & vbCrLf & _
"Values Set as per the SPE Installation Instructions." & vbCrLf & _
CONST_ERRMSG_INSERTMAT_CLOSING
MsgBox(strMessage, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, CallerName)
Return False
End If
' Downgrade the Current Layout Mode Back to Read
objLayout.DowngradeOpen()
' Commit and Save the Changes to the Current Drawing
objTransaction.Commit()
' Clean Up the Destination Objects
objDestinationPlotSettingsValidator = Nothing
objDestinationPlotSettings = Nothing
objLayout = Nothing
objCurrentDrawing.Editor.Regen()
objCurrentDrawing.Editor.UpdateScreen()
Else
Dim strMessage = "Layout1 could Not be Set as the Current Layout!" & vbCrLf & vbCrLf & _
"Please be Sure That Layout1 Exists in the '" & objCurrentDrawing.Name & "' Drawing" & vbCrLf & _
"and Re-run the Insert Mat Utility." & vbCrLf & vbCrLf & _
"The Insert Mat Utility will Now be Terminated." & vbCrLf & _
"Use the AutoCAD 'Undo' Button to Undo Changes Made by This Utility."
MsgBox(strMessage, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, CallerName)
' Abort and Abandon the Changes to the Current Document
objTransaction.Abort()
End If
End Using ' Destination Transaction
' Clean Up the Current Drawing Transaction Objects
objTransactionManager.Dispose()
objTransactionManager = Nothing
objDatabase = Nothing
objCurrentDrawing = Nothing
' Abort Any Changes to the Source Drawing
objSourceTransaction.Abort()
' Clean Up the Source Drawing Transaction Objects
objSourcePlotSettings = Nothing
objSourcePlotSettingsDictionary = Nothing
End Using ' Source Transaction
objSourceTransactionManager.Dispose()
objSourceTransactionManager = Nothing
objSourceDatabase.Dispose()
objSourceDatabase = Nothing
Return True
End Function ' CopyPageSetupFromFile
Public Function SetLayoutAsCurrent(ByVal LayoutName As String, ByVal CallerName As String) As Boolean
'***************************************************************************************************************
' Procedure Name: SetLayoutAsCurrent
'
' Purpose: Checks for the Passed Layout in the Current Drawing, Sets It as Current, or Creates It.
'
' Return Values: N/A
'
' Assumptions: Any Called Procedures will Provide Error Handling and Messages
'
' Globals: N/A
'
'***************************************************************************************************************
Const CONST_PROCEDURE_NAME As String = "SetLayoutAsCurrent"
'********************************************************************************************
' Set the Current Drawing Object to the Application's Active Document - Replaces ThisDrawing
'********************************************************************************************
Dim objCurrentDrawing As Document = DocumentManager.MdiActiveDocument
Using objLock As DocumentLock = objCurrentDrawing.LockDocument
' Reference the Layout Manager
Dim objLayoutManager As LayoutManager
objLayoutManager = LayoutManager.Current
' If the Current Layout is Not the Passed Layout
If Not objLayoutManager.CurrentLayout = LayoutName Then
' Set The Other Variables Needed to Handle the Current Drawing's Layouts
Dim objDatabase As Database = objCurrentDrawing.Database
Dim objTransactionManager As DatabaseServices.TransactionManager = objDatabase.TransactionManager
Using objTransaction As Transaction = objTransactionManager.StartTransaction
' Try to Set the Current Layout to the Passed Layout Name
Try
objLayoutManager.CurrentLayout = LayoutName
Catch excAutoCAD As Autodesk.AutoCAD.Runtime.Exception
If excAutoCAD.Message = "eSetFailed" Then
' The Passed Layout Doesn't Exist So We Will Create It
Dim objLayout As Layout
objLayout = objTransaction.GetObject(objLayoutManager.CreateLayout(LayoutName), OpenMode.ForRead)
objLayoutManager.CurrentLayout = LayoutName
objLayout = Nothing
Else
Dim strMessage As String
strMessage = CONST_ACAD_ERRMSG_START & CONST_PROCEDURE_NAME & _
CONST_GEN_ERRMSG_MIDDLE & excAutoCAD.ToString & _
CONST_GEN_ERRMSG_END & _
CONST_ERRMSG_INSERTMAT_CLOSING
MsgBox(strMessage, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, CallerName)
objCurrentDrawing = Nothing
Return False
End If
Catch excGeneric As System.Exception
Dim strMessage As String
strMessage = CONST_GEN_ERRMSG_START & CONST_PROCEDURE_NAME & _
CONST_GEN_ERRMSG_MIDDLE & excGeneric.ToString & _
CONST_GEN_ERRMSG_END & _
CONST_ERRMSG_INSERTMAT_CLOSING
MsgBox(strMessage, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, CallerName)
objCurrentDrawing = Nothing
Return False
End Try
' Commit the Transaction and Dispose of the Transaction Objects
objTransaction.Commit()
objTransactionManager.Dispose()
objTransactionManager = Nothing
End Using
End If
objLayoutManager = Nothing
End Using
objCurrentDrawing = Nothing
Return True
End Function ' SetLayoutAsCurrent