Slow Batch Creation of Layouts

Slow Batch Creation of Layouts

tristan.jonas8XAAW
Advocate Advocate
355 Views
2 Replies
Message 1 of 3

Slow Batch Creation of Layouts

tristan.jonas8XAAW
Advocate
Advocate

Hi all,

So I've ran into a problem. When I try to make layouts in bulk it takes too long, because when it creates new layouts  it automatically generates viewports one at a time and then zooms to extents of the objects in model space within the VP before adjusting the viewport to where it actually needs to go before moving onto the next layout.

The problem is that the file is rather large, so every time it generates a viewport and automatically zooms out to extents that makes AutoCAD render the entire drawing after each viewport is generated. This makes every layout takes 5 seconds or so to prepare before I can move onto the next layout in the iteration, and if you have 200 layouts or so obviously this gets to be an impractical problem.

My question is, is there a way to do this more quickly? I feel like there should be a way to add these viewports pre-zoomed, or not have to make the layout being edited current before moving on, truly anything would work.

Here's what I have so far (note I made some arbitrary changes like how many layouts get created so that this is more testable/readable):

 <CommandMethod("ASGOT")>
        Public Sub CreateLayoutsWithViewport()
            Dim acAppComObj As Object = Application.AcadApplication
            Dim acPrefComObj As Object = acAppComObj.Preferences

            ' Store the current setting
            Dim originalSetting As Boolean = acPrefComObj.Display.LayoutCreateViewport

            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database

            Try
                ' Change the setting to prevent automatic viewport creation
                acPrefComObj.Display.LayoutCreateViewport = False

                For i As Integer = 1 To 3
                    Dim layoutName As String = "NewLayout" & i
                    CreateLayout(db, layoutName)
                    CreateViewport(db, layoutName)
                Next

            Catch ex As Exception
                doc.Editor.WriteMessage("Error in creating layouts with viewports: " & ex.Message)
            Finally
                ' Restore the original setting
                acPrefComObj.Display.LayoutCreateViewport = originalSetting
            End Try
        End Sub

        Private Sub CreateLayout(db As Database, layoutName As String)
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim lm As LayoutManager = LayoutManager.Current

                If Not lm.LayoutExists(layoutName) Then
                    lm.CreateLayout(layoutName)
                End If

                lm.CurrentLayout = layoutName
                tr.Commit()
            End Using
        End Sub

        Private Sub CreateViewport(db As Database, layoutName As String)
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim lm As LayoutManager = LayoutManager.Current
                Dim layoutId As ObjectId = lm.GetLayoutId(layoutName)
                Dim layout As Layout = tr.GetObject(layoutId, OpenMode.ForWrite)
                Dim btr As BlockTableRecord = tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite)

                Using vp As New Viewport()
                    btr.AppendEntity(vp)
                    tr.AddNewlyCreatedDBObject(vp, True)

                    vp.CenterPoint = New Point3d(5.25, 4.0, 0.0)
                    vp.Width = 8.4
                    vp.Height = 6.4

                    Dim minPoint As Point2d = New Point2d(1448444.1609, 624876.8246)
                    Dim maxPoint As Point2d = New Point2d(1449418.8916, 625525.3315)
                    vp.ViewCenter = New Point2d((minPoint.X + maxPoint.X) / 2, (minPoint.Y + maxPoint.Y) / 2)
                    vp.ViewHeight = maxPoint.Y - minPoint.Y

                    vp.On = True
                End Using

                tr.Commit()
            End Using
        End Sub
0 Likes
Accepted solutions (1)
356 Views
2 Replies
Replies (2)
Message 2 of 3

bdean123456
Contributor
Contributor
Accepted solution

You could try to freeze everything but the essential layers during batch creation to speed up the process? If you have not already unloaded any references, that would speed it up as well.

Message 3 of 3

tristan.jonas8XAAW
Advocate
Advocate

That's clever, and it seemed to work! Thank you!

0 Likes