I have a small project that I'm working on that inserts a new layout based on a user selected template size (A, B, C or D sizes). I need the program to insert the layout along with the titleblock geometry from the existing A, B, C, or D dwt files.
I know how to add a new blank layout. However, I'm having some trouble determing how to insert the titleblock geomtry. I've read a number of posts on here related to layouts, and while they were informative, none of them covered exactly what I need to do. I'm having some trouble following the code for some of the solutions that I have seen.
I would greatly appreciate it if someone could provide some direction, or an explanation of the steps that I need to follow in order to write the proper code.
Solved! Go to Solution.
Try the code from this article:
That code works, but I'm more interested in learning to code the actual ".net" process of copying layouts from a .dwt to a current drawing. I'm fairly new to .net programming and I'm trying to learn as much as possible, as quickly as possible.
I've seen a few examples of similar programs, but I'm not following some of the code.
I know how to create new blank layout, and the code makes sense to me. I just don't know how to get the drawing objects, and other format related items into the new layout.
To get objects from external drawing use WBlockCloneObjects method of Database,
this will allow you to copy what you need, just gather all of the objects from particular layout
you want to copy,
In addition try this code, limited tested
you have to add check if file exist, if layout name
is not from your layout list etc:
<CommandMethod("clayout", CommandFlags.Session)> _ Public Sub testCopyLayoutFromTemplate() CopyLayoutFromTemplate("C:\Test\Temp2.dwt", "A") End Sub Public Sub CopyLayoutFromTemplate(ByVal filepath As String, ByVal layoutname As String) Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.D
ocumentManager.MdiActiveDocument Dim curdb As Database = doc.Database Dim ed As Editor = doc.Editor Dim loc As DocumentLock = doc.LockDocument() Using loc Dim tr As Transaction = doc.TransactionManager.StartTransaction Using tr Try Dim mLayoutmgr As LayoutManager = LayoutManager.Current Dim newlayoutname As String = layoutname Dim layoutId As ObjectId = ObjectId.Null Dim btrId As ObjectId = ObjectId.Null layoutId = mLayoutmgr.CreateLayout(newlayoutname) Dim newlayout As Layout = DirectCast(tr.GetObject(layoutId, OpenMode.ForWrite), Layout) Dim layoutbtr As BlockTableRecord = DirectCast(tr.GetObject(newlayout.BlockTableRecord Id, OpenMode.ForWrite), BlockTableRecord) btrId = layoutbtr.ObjectId Dim btr As BlockTableRecord = CType(tr.GetObject(curdb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord) Dim exdb As Database = New Database(False, True) Using exdb Using extr As Transaction = exdb.TransactionManager.StartTransaction exdb.ReadDwgFile(filepath, System.IO.FileShare.Read, False, "") Dim layoutdict As DBDictionary = DirectCast(exdb.LayoutDictionaryId.GetObject(OpenM ode.ForRead), DBDictionary) Dim layout As Layout = DirectCast(layoutdict.GetAt(layoutname).GetObject( OpenMode.ForRead), Layout) Dim exbtr As BlockTableRecord = DirectCast(extr.GetObject(layout.BlockTableRecordI d, OpenMode.ForRead), BlockTableRecord) Dim objcoll As ObjectIdCollection = New ObjectIdCollection For Each id As ObjectId In exbtr objcoll.Add(id) Next exdb.WblockCloneObjects(objcoll, btrId, New IdMapping, DuplicateRecordCloning.Ignore, False) End Using End Using mLayoutmgr.CurrentLayout = newlayoutname tr.Commit() ed.Regen() Catch ex As Autodesk.AutoCAD.Runtime.Exception Autodesk.AutoCAD.ApplicationServices.Application.S howAlertDialog(ex.ToString & vbCr & ex.Message) End Try End Using End Using End Sub
I finally got back to working on this project. I tested the code that you posted above and it works great. I had to make a couple small adjustments for 2013, but otherwise, it worked as-is.
Thanks for the help. Your code is very clear and concise.