.NET

Reply
Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 1 of 7 (270 Views)
Accepted Solution

Cloning a Layout Using the Same Taborder

270 Views, 6 Replies
07-12-2013 02:34 PM

Hi, I have a program that lets the user create a new page, by cloning a selected page and placing it at the desired layout order (taborder).  For example, the user could copy layout 4 as layout 3 and then there are options for changing the layers that are visible.

The problem I'm running into is when a user enters new page = 3, by copying the current layout 3.  What I want is to have the new layout placed at taborder 3, and the rest moved to the right.

It gives me an error, though, saying I'm using the same taborder.

' This will copy the selected layout and call it the new page.
    Public Sub addPage(ByVal pageNumber As Integer, ByVal pageToCopy As Integer)
        Dim myDWG As ApplicationServices.Document
        Dim myDB As DatabaseServices.Database
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction

        myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
        myDB = myDWG.Database
        myTransMan = myDWG.TransactionManager
        myTrans = myTransMan.StartTransaction

        Dim myBT As DatabaseServices.BlockTable
        Dim myBTR As DatabaseServices.BlockTableRecord
        Dim myBTRE As DatabaseServices.SymbolTableEnumerator
        Dim myLM As DatabaseServices.LayoutManager
        Dim myLayout As DatabaseServices.Layout
        Dim layoutToCopy As String

        myBT = myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForWrite)
        myBTRE = myBT.GetEnumerator
        myLM = DatabaseServices.LayoutManager.Current

        ' Copies the selected layout to the new layout
        While myBTRE.MoveNext
            myBTR = myBTRE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
            If myBTR.IsLayout Then
                myLayout = myBTR.LayoutId.GetObject(DatabaseServices.OpenMode.ForRead)
                ' If the layout is the selected layout to copy
                If myLayout.TabOrder = pageToCopy Then
                    layoutToCopy = (myLayout.LayoutName)
                    ' Copys the selected layout and puts it in the desired layout location
                    myLM.CloneLayout(layoutToCopy, "newpage", pageNumber)
                End If
            End If
        End While
        myTrans.Commit()
        myDWG.Editor.Regen()
        myTrans.Dispose()
    End Sub

 I thought, maybe I could put the new layout at the end, and then just change its taborder, but all that did was overwrite the current layout 3 with the new layout.

 

Any help would be greatly appreciated.

Mark

Hi Felton,

I tried googling "newindex" in this regard, even looked in all the regular places like the Autodesk .Net Developers Guide, but couldn't find anything.

On a good note, I did solve my problem by adding a "dummy" layout, and then, since the layout numbers have changed I was able to use the pagetocopy + 1 (now it wasn't picking the same spot which was the viloation).  After it copied the layout, it then deletes the dummy layout.  Here's the code.

 ' This will copy the selected layout and call it the new page.
Public Sub addPage(ByVal pageNumber As Integer, ByVal pageToCopy As Integer)
Dim myDWG As ApplicationServices.Document
Dim myDB As DatabaseServices.Database
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDB = myDWG.Database
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction
Dim myBT As DatabaseServices.BlockTable
Dim myBTR As DatabaseServices.BlockTableRecord
Dim myBTRE As DatabaseServices.SymbolTableEnumerator
Dim myLM As DatabaseServices.LayoutManager
Dim myLayout As DatabaseServices.Layout
Dim layoutToCopy As String
myBT = myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForWrite)
myBTRE = myBT.GetEnumerator
myLM = DatabaseServices.LayoutManager.Current
Dim numberOfLayouts As Integer = myLM.LayoutCount
If pageNumber <> pageToCopy Then
' Copies the selected layout to the new layout
While myBTRE.MoveNext
myBTR = myBTRE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If myBTR.IsLayout Then
myLayout = myBTR.LayoutId.GetObject(DatabaseServices.OpenMode.ForRead)
' If the new page number is not the same as the page to copy
' If the layout is the selected layout to copy
If myLayout.TabOrder = pageToCopy Then
layoutToCopy = (myLayout.LayoutName)
' Copys the selected layout and puts it in the desired layout location
myLM.CloneLayout(layoutToCopy, "newpage", pageNumber)
End If
End If
End While
End If
' If the new page number is the same as the page to copy
If pageNumber = pageToCopy Then
Dim newpageToCopy As Integer = pageToCopy + 1
Dim dummyLayout As DatabaseServices.Layout
' Since you cannot copy a page and put it in the same taborder as the page to copy,
' add a dummy layout and still copy the correct layout
dummyLayout = myLM.CreateLayout("dummy").GetObject(DatabaseServices.OpenMode.ForWrite)
dummyLayout.TabOrder = pageNumber
myDWG.Editor.Regen()
While myBTRE.MoveNext
myBTR = myBTRE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If myBTR.IsLayout Then
myLayout = myBTR.LayoutId.GetObject(DatabaseServices.OpenMode.ForRead)
' Since we added the dummy layout, the page to copy has been increased by 1 (newpageToCopy)
If myLayout.TabOrder = newpageToCopy Then
layoutToCopy = (myLayout.LayoutName)
' Copys the layout and put it in the desired layout location as a dummy placeholder
myLM.CloneLayout(layoutToCopy, "newpage", pageNumber)
End If
End If
End While
' Deletes the dummy layout
myLM.DeleteLayout("dummy")
End If
myTrans.Commit()
myDWG.Editor.Regen()
myTrans.Dispose()
End Sub

 Best regards,

Mark

ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 2 of 7 (224 Views)

Re: Cloning a Layout Using the Same Taborder

07-15-2013 12:26 PM in reply to: mgorecki

Why not call it with 0 and then reorder using Layout.TabOrder?





Fenton Webb

Developer Technical Services

Autodesk Developer Network


Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 3 of 7 (219 Views)

Re: Cloning a Layout Using the Same Taborder

07-15-2013 01:14 PM in reply to: fenton.webb

Hi Fenton,

I'm not sure what you mean by "call it with 0".  Can you please clarify?

 

Thanks,
Mark

ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 4 of 7 (213 Views)

Re: Cloning a Layout Using the Same Taborder

07-15-2013 02:13 PM in reply to: mgorecki

Sorry, I meant call cloneLayout with newindex = 0





Fenton Webb

Developer Technical Services

Autodesk Developer Network


Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 5 of 7 (195 Views)

Re: Cloning a Layout Using the Same Taborder

07-16-2013 10:14 AM in reply to: fenton.webb

Hi Felton,

I tried googling "newindex" in this regard, even looked in all the regular places like the Autodesk .Net Developers Guide, but couldn't find anything.

On a good note, I did solve my problem by adding a "dummy" layout, and then, since the layout numbers have changed I was able to use the pagetocopy + 1 (now it wasn't picking the same spot which was the viloation).  After it copied the layout, it then deletes the dummy layout.  Here's the code.

    ' This will copy the selected layout and call it the new page.
    Public Sub addPage(ByVal pageNumber As Integer, ByVal pageToCopy As Integer)
        Dim myDWG As ApplicationServices.Document
        Dim myDB As DatabaseServices.Database
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction

        myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
        myDB = myDWG.Database
        myTransMan = myDWG.TransactionManager
        myTrans = myTransMan.StartTransaction

        Dim myBT As DatabaseServices.BlockTable
        Dim myBTR As DatabaseServices.BlockTableRecord
        Dim myBTRE As DatabaseServices.SymbolTableEnumerator
        Dim myLM As DatabaseServices.LayoutManager
        Dim myLayout As DatabaseServices.Layout
        Dim layoutToCopy As String

        myBT = myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForWrite)
        myBTRE = myBT.GetEnumerator
        myLM = DatabaseServices.LayoutManager.Current
        Dim numberOfLayouts As Integer = myLM.LayoutCount

        If pageNumber <> pageToCopy Then
            ' Copies the selected layout to the new layout
            While myBTRE.MoveNext
                myBTR = myBTRE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
                If myBTR.IsLayout Then
                    myLayout = myBTR.LayoutId.GetObject(DatabaseServices.OpenMode.ForRead)
                    ' If the new page number is not the same as the page to copy

                    ' If the layout is the selected layout to copy
                    If myLayout.TabOrder = pageToCopy Then
                        layoutToCopy = (myLayout.LayoutName)
                        ' Copys the selected layout and puts it in the desired layout location
                        myLM.CloneLayout(layoutToCopy, "newpage", pageNumber)
                    End If
                End If
            End While
        End If
        ' If the new page number is the same as the page to copy
        If pageNumber = pageToCopy Then
            Dim newpageToCopy As Integer = pageToCopy + 1
            Dim dummyLayout As DatabaseServices.Layout
            ' Since you cannot copy a page and put it in the same taborder as the page to copy,
            ' add a dummy layout and still copy the correct layout
            dummyLayout = myLM.CreateLayout("dummy").GetObject(DatabaseServices.OpenMode.ForWrite)
            dummyLayout.TabOrder = pageNumber
            myDWG.Editor.Regen()
            While myBTRE.MoveNext
                myBTR = myBTRE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
                If myBTR.IsLayout Then
                    myLayout = myBTR.LayoutId.GetObject(DatabaseServices.OpenMode.ForRead)
                    ' Since we added the dummy layout, the page to copy has been increased by 1 (newpageToCopy)
                    If myLayout.TabOrder = newpageToCopy Then
                        layoutToCopy = (myLayout.LayoutName)
                        ' Copys the layout and put it in the desired layout location as a dummy placeholder
                        myLM.CloneLayout(layoutToCopy, "newpage", pageNumber)
                    End If
                End If
            End While
            ' Deletes the dummy layout
            myLM.DeleteLayout("dummy")
        End If
        
        myTrans.Commit()
        myDWG.Editor.Regen()
        myTrans.Dispose()
    End Sub

 Best regards,

Mark

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 7 (152 Views)

Re: Cloning a Layout Using the Same Taborder

07-20-2013 05:14 AM in reply to: mgorecki

Try this code if you need to copy from existing layout

from current document

        <CommandMethod("cla")> _
        Public Sub testAddPage()

            Dim db As Database = HostApplicationServices.WorkingDatabase

            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Using tr As Transaction = db.TransactionManager.StartTransaction
                Dim ltdict As DBDictionary = CType(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
                LayoutManager.Current.CopyLayout("Electrical", "NewPageName") ' e.g, copy from existing layout named 'Electrical"
                LayoutManager.Current.CurrentLayout = "NewPageName"
                Dim ltId As ObjectId = ObjectId.Null
                For Each de As DBDictionaryEntry In ltdict
                    If de.Key = "NewPageName" Then
                        ltId = de.Value
                        Exit For
                    End If
                Next
                Dim lt As Layout = CType(tr.GetObject(ltId, OpenMode.ForWrite), Layout)
                ' set any tab position
                Dim pagenum As Integer = 10 'dummy number, put your exact value instead
                ' check if page number do not exceed number of tabs
                If pagenum > ltdict.Count Then
                    pagenum = ltdict.Count 'if exceed then move page  to be last one
                End If
                lt.TabOrder = pagenum
                ed.Regen()
                tr.Commit()
            End Using
        End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
mgorecki
Posts: 414
Registered: ‎09-23-2004
Message 7 of 7 (137 Views)

Re: Cloning a Layout Using the Same Taborder

07-22-2013 08:08 AM in reply to: Hallex

Hi Hallex,

Thanks for the code, I'll try it out.

 

Best regards,

Mark

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

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