.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Creating a new layout from a template

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
btmcad
1389 Views, 6 Replies

Creating a new layout from a template

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

6 REPLIES 6
Message 2 of 7
Hallex
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
Message 3 of 7
btmcad
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

Message 4 of 7
Hallex
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
Message 5 of 7
Hallex
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
Message 6 of 7
btmcad
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

Message 7 of 7
Hallex
in reply to: btmcad

Glad I could help,

Cheers 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost