.NET

Reply
Valued Mentor
Mike.Wohletz
Posts: 376
Registered: ‎07-29-2008
Message 1 of 18 (2,336 Views)
Accepted Solution

Import Page Setup from Template

2336 Views, 17 Replies
04-15-2010 08:33 PM
I am lost as to how to go about importing a page setup from a template file and set it current, I am using vb.net and working with AutoCAD 2010. I have searched this thread and from what I can see several others have asked this question and it has not been answered. any help in the correct direction would be great.
thanks
Mike

Since I have not done this before, I decided to put togeter some quick code. It copies a layout from a drawing file into the current drawing in Acad. Here is the code:

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(CopyPageSetup.MyCommand))]
namespace CopyPageSetup
{
public class MyCommand
{
[CommandMethod("CopyLayout")]
public void RunThisMethod()
{
//The template drawing containing a layout named as
//"MyLayout" with desired page setup
string sourceDwg = @"E:\Temp\Test.dwg";
string layoutName = "MyLayout";
Document dwg = Autodesk.AutoCAD.ApplicationServices
.Application.DocumentManager.MdiActiveDocument;
//Get source database
Database db = null;
try
{
db = GetSourceDatabase(sourceDwg);
}
catch (System.Exception ex)
{
dwg.Editor.WriteMessage(
"\nReading source drawing error:\n {0}\n.",
ex.Message);
return;
}
//If a layout with the same name as in the source drawing
//exists. If yes, the layout's page setup will be overwritten
LayoutManager mgr = LayoutManager.Current;
try
{
//Set the eixsting layout active
mgr.CurrentLayout = layoutName;
}
catch
{
//create the alyout and set it active
mgr.CreateLayout(layoutName);
mgr.CurrentLayout = layoutName;
}
//Do layout page setup copying
using (db)
{
try
{
CopyLayout(db, layoutName, dwg.Database, mgr.GetLayoutId(layoutName));
}
catch (System.Exception ex)
{
dwg.Editor.WriteMessage(
"\nCopying layout error:\n {0}\n.",
ex.Message);
return;
}
}
dwg.Editor.Regen();
dwg.Editor.WriteMessage("\nLayout copy completed.");
}
#region private methods
private Database GetSourceDatabase(string sourceFile)
{
Database db = new Database();
db.ReadDwgFile(sourceFile, FileOpenMode.OpenForReadAndReadShare, false, null);
return db;
}
private void CopyLayout(Database sourceDB,
string layoutName, Database thisDB, ObjectId thisLayoutID)
{
using (Transaction tran = sourceDB.TransactionManager.StartTransaction())
{
DBDictionary lay1Dic = (DBDictionary)
tran.GetObject(sourceDB.LayoutDictionaryId, OpenMode.ForRead);
try
{
//Get source layout object in the source database
ObjectId lay1ID = lay1Dic.GetAt(layoutName);
Layout lay1 = (Layout)tran.GetObject(lay1ID, OpenMode.ForRead);
using (Transaction t = thisDB.TransactionManager.StartTransaction())
{
//Get the destination layout in current database
Layout lay2 = (Layout)
t.GetObject(thisLayoutID, OpenMode.ForWrite);
try
{
//Copy layout
lay2.CopyFrom(lay1);
t.Commit();
}
catch
{
t.Abort();
throw;
}
}
}
catch
{
tran.Abort();
throw;
}
}
}
#endregion
}
}

 

 HTH.

  

 

Valued Mentor
Mike.Wohletz
Posts: 376
Registered: ‎07-29-2008
Message 2 of 18 (2,333 Views)

Re: Import Page Setup from Template

04-19-2010 06:37 PM in reply to: Mike.Wohletz
A sample or a point in the right direction would be a great help! anyone?
Active Member
KLGalloway
Posts: 10
Registered: ‎02-24-2010
Message 3 of 18 (2,329 Views)

Re: Import Page Setup from Template

06-17-2010 10:34 AM in reply to: Mike.Wohletz

Have you managed to find out anything on this issue of setting a pagesetup (plotsettings object) as current?

*Expert Elite*
norman.yuan
Posts: 1,002
Registered: ‎04-27-2009
Message 4 of 18 (2,306 Views)

Re: Import Page Setup from Template

06-18-2010 11:13 AM in reply to: Mike.Wohletz

Since I have not done this before, I decided to put togeter some quick code. It copies a layout from a drawing file into the current drawing in Acad. Here is the code:

 

using System;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(CopyPageSetup.MyCommand))]

namespace CopyPageSetup
{
    public class MyCommand
    {
        [CommandMethod("CopyLayout")]
        public void RunThisMethod()
        {
            //The template drawing containing a layout named as 
            //"MyLayout" with desired page setup
            string sourceDwg = @"E:\Temp\Test.dwg";
            string layoutName = "MyLayout";

            Document dwg = Autodesk.AutoCAD.ApplicationServices
                        .Application.DocumentManager.MdiActiveDocument;

            //Get source database
            Database db = null;
            try
            {
                db = GetSourceDatabase(sourceDwg);
            }
            catch (System.Exception ex)
            {
                dwg.Editor.WriteMessage(
                    "\nReading source drawing error:\n {0}\n.",
                    ex.Message);
                return;
            }

            //If a layout with the same name as in the source drawing
            //exists. If yes, the layout's page setup will be overwritten
            LayoutManager mgr = LayoutManager.Current;
            try
            {
                //Set the eixsting layout active
                mgr.CurrentLayout = layoutName;
            }
            catch
            {
                //create the alyout and set it active
                mgr.CreateLayout(layoutName);
                mgr.CurrentLayout = layoutName;
            }

            //Do layout page setup copying
            using (db)
            {
                try
                {
                    CopyLayout(db, layoutName, dwg.Database, mgr.GetLayoutId(layoutName));
                }
                catch (System.Exception ex)
                {
                    dwg.Editor.WriteMessage(
                        "\nCopying layout error:\n {0}\n.", 
                        ex.Message);
                    return;
                }
            }

            dwg.Editor.Regen();
            dwg.Editor.WriteMessage("\nLayout copy completed.");
        }

        #region private methods

        private Database GetSourceDatabase(string sourceFile)
        {
            Database db = new Database();
            db.ReadDwgFile(sourceFile, FileOpenMode.OpenForReadAndReadShare, false, null);
            return db;
        }

        private void CopyLayout(Database sourceDB, 
            string layoutName, Database thisDB, ObjectId thisLayoutID)
        {
            using (Transaction tran = sourceDB.TransactionManager.StartTransaction())
            {
                DBDictionary lay1Dic = (DBDictionary)
                    tran.GetObject(sourceDB.LayoutDictionaryId, OpenMode.ForRead);
                try
                {
                    //Get source layout object in the source database
                    ObjectId lay1ID = lay1Dic.GetAt(layoutName);
                    Layout lay1 = (Layout)tran.GetObject(lay1ID, OpenMode.ForRead);

                    using (Transaction t = thisDB.TransactionManager.StartTransaction())
                    {
                        //Get the destination layout in current database
                        Layout lay2 = (Layout)
                            t.GetObject(thisLayoutID, OpenMode.ForWrite);
 
                        try
                        {
                            //Copy layout
                            lay2.CopyFrom(lay1);
                            t.Commit();
                        }
                        catch
                        {
                            t.Abort();
                            throw;
                        }
                    }
                }
                catch
                {
                    tran.Abort();
                    throw;
                }
            }
        }

        #endregion
    }
}

 

 HTH.

  

 

Active Member
KLGalloway
Posts: 10
Registered: ‎02-24-2010
Message 5 of 18 (2,281 Views)

Re: Import Page Setup from Template

06-21-2010 06:42 AM in reply to: norman.yuan

Norman,

At the risk of sounding like the one particular smartaleck that posts on here and other AutoCAD sites simply to ridicule and poke fun at perfectly serious questions and suggestions, what does your post have to do with the original question or my query? Your code is first of all in C# and the question requests a VB.Net solution and the requested solution deals with page setup/plot settings objects while your code is for copying layouts. Sure, one or more plot settings objects would be part of the drawing the copied layout comes from and layouts are derived from plotsettings objects, but which one is current for the drawing?

I don't want to discourage anyone from answering posts, but you really need to make your reply have something to do with the posted question.

For all posters, I would like to make a request that if you post a question and then find an answer on your own, please come back and post that solution. This thread is one of several that I have found asking the same questions I was researching, but with no updates or posted solutions. Even if the result was to abandon that line of pursuit because no  solution was found in the allotted time for the project that is all good information.

 

Kerry

*Expert Elite*
norman.yuan
Posts: 1,002
Registered: ‎04-27-2009
Message 6 of 18 (2,277 Views)

Re: Import Page Setup from Template

06-21-2010 07:09 AM in reply to: Mike.Wohletz

Are you the same person as "mike.wohletz"? If not, where is your ORIGINALPOST which specifically requires VB.NET solution?

The posts here is focusing on logic of Acad .NET solution, not on the language itself, especially when it is .NET code. If the code does what you want, are you going to ask some do the language conversion?

I am a bit sorry if you did not find the code is useful for you, after all it is not directly to your question. Given the not-so-detailed original post, if I assumed the OP wants to import page setup/plotting setup from other drawing's layout, at least the code works on that assumption.

If you bother to describe your requirement in details in YOUR OWN POST, someone might put forth a solution perfectly suitable to you, or there might be no solution at all because of very odd requirements, no one knows ahead of time until one can see your reasonably described post.

Valued Mentor
caddzone
Posts: 305
Registered: ‎01-26-2005
Message 7 of 18 (2,274 Views)

Re: Import Page Setup from Template

06-21-2010 08:06 AM in reply to: KLGalloway

The only thing you've achieved here is to ensure that future

requests for help from you will probably be ignored.

 


KLGalloway wrote:

Norman,

At the risk of sounding like the one particular smartaleck that posts on here and other AutoCAD sites simply to ridicule and poke fun at perfectly serious questions and suggestions, what does your post have to do with the original question or my query? Your code is first of all in C# and the question requests a VB.Net solution and the requested solution deals with page setup/plot settings objects while your code is for copying layouts. Sure, one or more plot settings objects would be part of the drawing the copied layout comes from and layouts are derived from plotsettings objects, but which one is current for the drawing?

I don't want to discourage anyone from answering posts, but you really need to make your reply have something to do with the posted question.

For all posters, I would like to make a request that if you post a question and then find an answer on your own, please come back and post that solution. This thread is one of several that I have found asking the same questions I was researching, but with no updates or posted solutions. Even if the result was to abandon that line of pursuit because no  solution was found in the allotted time for the project that is all good information.

 

Kerry


 



AcadXTabs for AutoCAD
Supporting AutoCAD 2000-2011


Active Member
KLGalloway
Posts: 10
Registered: ‎02-24-2010
Message 8 of 18 (2,255 Views)

Re: Import Page Setup from Template

06-21-2010 10:15 AM in reply to: norman.yuan

Norman,

I will just quote the original post with some added emphasis which actually was pretty detailed and very concise - "I am lost as to how to go about importing a page setup from a template file and set it current, I am using vb.net and working with AutoCAD 2010".

Never said the original post was mine only the later inquiry. You wrote "Given the not-so-detailed original post, if I assumed the OP wants to import page setup/plotting setup from other drawing's layout, at least the code works on that assumption". Actually your code doesn't import a page setup from another drawings layout. It imports the entire layout which is not the same at all; it doesn’t even look for page setups much less set one of them current and is actually not a good thing at all.

If I just want the page setup I can get that easily enough and there are lots of examples in VB.Net and C# around to do that without having to drag over and add an entire layout to my drawing. The important part is setting a particular plot settings object as current programmatically which still needs a solution.

Active Member
KLGalloway
Posts: 10
Registered: ‎02-24-2010
Message 9 of 18 (2,256 Views)

Re: Import Page Setup from Template

06-21-2010 10:19 AM in reply to: caddzone

Tell you what, Tony, I don't see a downside to having you ignore my posts. :smileyvery-happy: After all you were the contributor I referenced in my first reply to Norman.

Active Member
KLGalloway
Posts: 10
Registered: ‎02-24-2010
Message 10 of 18 (2,251 Views)

Re: Import Page Setup from Template

06-21-2010 11:04 AM in reply to: Mike.Wohletz

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

 

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.