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
Solved! Go to Solution.
Solved by mgorecki. Go to Solution.
Why not call it with 0 and then reorder using Layout.TabOrder?
Hi Fenton,
I'm not sure what you mean by "call it with 0". Can you please clarify?
Thanks,
Mark
Sorry, I meant call cloneLayout with newindex = 0
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
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
Can't find what you're looking for? Ask the community or share your knowledge.