• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Contributor
    Posts: 31
    Registered: ‎05-09-2007
    Accepted Solution

    Creating a new layout from a template

    170 Views, 6 Replies
    06-28-2012 08:19 PM

    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.

     

    thanks

    btm

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Creating a new layout from a template

    06-29-2012 03:59 AM in reply to: btmcad

    Try the code from this article:

    http://adndevblog.typepad.com/autocad/2012/06/copy-a-layout-with-its-entities-from-one-drawing-to-an...

     

    ~'J'~

     

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 31
    Registered: ‎05-09-2007

    Re: Creating a new layout from a template

    06-29-2012 05:11 AM in reply to: Hallex

    Thanks Hallex,

     

    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.

     

    Thanks

    btm

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Creating a new layout from a template

    06-29-2012 06:00 AM in reply to: btmcad

    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,

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Creating a new layout from a template

    06-29-2012 06:49 AM in reply to: Hallex

    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.DocumentManager.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.BlockTableRecordId, 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(OpenMode.ForRead), DBDictionary)
    
                                    Dim layout As Layout = DirectCast(layoutdict.GetAt(layoutname).GetObject(OpenMode.ForRead), Layout)
    
                                    Dim exbtr As BlockTableRecord = DirectCast(extr.GetObject(layout.BlockTableRecordId, 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.ShowAlertDialog(ex.ToString & vbCr & ex.Message)
                        End Try
    
                    End Using
    
                End Using
    
            End Sub

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 31
    Registered: ‎05-09-2007

    Re: Creating a new layout from a template

    07-13-2012 08:37 AM in reply to: Hallex

    Hallex,

     

    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.

     

     

    btm

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Creating a new layout from a template

    07-13-2012 08:43 AM in reply to: btmcad

    Glad I could help,

    Cheers :smileyhappy:

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.