.NET

.NET

Reply
Contributor
cwelkusl
Posts: 19
Registered: ‎01-18-2011
Message 1 of 7 (1,005 Views)

Creating a Paperspace Layout and Viewport Issues

1005 Views, 6 Replies
06-01-2012 04:17 PM

I've done a check on similar topics here and found som possible suggestions to my problem, but either they aren't working or I'm not approaching it correctly, so I'm hoping someone can point me in the right direction or point out my error.

 

What I'm trying to accomplish:

  1. Have the user select a number of objects that contain attribute data that they wish to create information slips from (a paper space layout for each)
  2. Create a new "temp" paperspace layout based on a layout the user selects from another drawing
  3. Switch to the new layout and using a rectanlge jig have the user draw a viewport location.
  4. Create a viewport at the location specified by the user
  5. Clone the "temp" layout for each object the user originally selected, and zoom the viewport to the location of the object that sheet is displaying information for.
  6. Delete the "temp" layout

So far all of this works except, when I click on one of the layouts created and perform a zoom extents to see the sheet, the viewport is empty.  I then have to switch to another layout tab, switch back and do another zoom extents before the viewport shows anything.

 

So that's the portion I'm trying to figure out.  I've seen some information about initializing the layout before hand, but so far in playing with that have been unsuccessful in fixing the issue.  The code I'm using for the steps important to layout and viewport creation are as follows:

 

Step 2: Creating a "temp" layout based on a layout from another drawing.

Using acTrans As Transaction = acDoc.TransactionManager.StartTransaction()
                            Dim acObjIDs As New ObjectIdCollection
                            Dim acIDMaps As New IdMapping

                            acObjIDs.Add(acLayoutID)

                            Dim acDestLayouts As DBDictionary = acTrans.GetObject(acDB.LayoutDictionaryId, OpenMode.ForWrite)
                            acTemplateDB.WblockCloneObjects(acObjIDs, acDestLayouts.ObjectId, acIDMaps, DuplicateRecordCloning.Ignore, False)
                            Dim ip As IdPair = acIDMaps.Lookup(acLayoutID)
                            acNewLayoutID = ip.Value
                            acTrans.Commit()
                            acEd.Regen() 'Show new temp layout tab
                        End Using
                        

 Step 4: Create the viewport on the "temp" layout

'Create the viewport
            Using acTrans As Transaction = acDoc.TransactionManager.StartTransaction()
                Dim acCurSpace As BlockTableRecord = acTrans.GetObject(acDB.CurrentSpaceId, OpenMode.ForWrite)
                Dim acVP As Viewport = New Viewport()
                Dim acVPCenterX As Double
                Dim acVPCenterY As Double
                Dim acVPWidth As Double
                Dim acVPHeight As Double


                'acVP.SetDatabaseDefaults()

                acCurSpace.AppendEntity(acVP)
                acTrans.AddNewlyCreatedDBObject(acVP, True)

                If acVPBounds.GetPoint2dAt(0).X > acVPBounds.GetPoint2dAt(2).X Then
                    acVPCenterX = acVPBounds.GetPoint2dAt(0).X - ((acVPBounds.GetPoint2dAt(0).X - acVPBounds.GetPoint2dAt(2).X) / 2)
                    acVPWidth = (acVPBounds.GetPoint2dAt(0).X - acVPBounds.GetPoint2dAt(2).X)
                Else
                    acVPCenterX = acVPBounds.GetPoint2dAt(2).X - ((acVPBounds.GetPoint2dAt(2).X - acVPBounds.GetPoint2dAt(0).X) / 2)
                    acVPWidth = (acVPBounds.GetPoint2dAt(2).X - acVPBounds.GetPoint2dAt(0).X)
                End If

                If acVPBounds.GetPoint2dAt(0).Y > acVPBounds.GetPoint2dAt(2).Y Then
                    acVPCenterY = acVPBounds.GetPoint2dAt(0).Y - ((acVPBounds.GetPoint2dAt(0).Y - acVPBounds.GetPoint2dAt(2).Y) / 2)
                    acVPHeight = (acVPBounds.GetPoint2dAt(0).Y - acVPBounds.GetPoint2dAt(2).Y)
                Else
                    acVPCenterY = acVPBounds.GetPoint2dAt(2).Y - ((acVPBounds.GetPoint2dAt(2).Y - acVPBounds.GetPoint2dAt(0).Y) / 2)
                    acVPHeight = (acVPBounds.GetPoint2dAt(2).Y - acVPBounds.GetPoint2dAt(0).Y)
                End If
                acVP.CenterPoint = New Point3d(acVPCenterX, acVPCenterY, 0)
                
                
                acVP.Width = acVPWidth
                acVP.Height = acVPHeight

                acVP.Layer = strVPLayer

                'Set scale as 1:1000
                acVP.CustomScale = 1

                'Center Viewport on block
                acVP.ViewDirection = Vector3d.ZAxis
                acVP.ViewTarget = New Point3d(0, 0, 0)
                acVP.ViewCenter = Point2d.Origin

                acVP.Visible = True
                acVP.On = True
                acVP.UpdateDisplay()
                acTrans.Commit()
            End Using

 Step 5: Clone the "temp" layout

Dim lm As LayoutManager = LayoutManager.Current
                Dim acLayout As Layout = acTrans.GetObject(acNewLayoutID, OpenMode.ForRead)
                Dim strNewLayoutName As String = "Lot " & acLotInfo.LotNumber.ToString
                While lm.GetLayoutId(strNewLayoutName) <> Nothing
                    strNewLayoutName = "Lot " & acLotInfo.LotNumber.ToString & "(Copy " & GenerateRandomString(2) & ")"
                End While
                lm.CloneLayout(acLayout.LayoutName, strNewLayoutName, lm.LayoutCount)

                Dim acClonedLayout As Layout = acTrans.GetObject(lm.GetLayoutId("Lot " & acLotInfo.LotNumber.ToString), OpenMode.ForWrite)
                'acClonedLayout.Initialize()

                Dim acClonedLayoutBTR As BlockTableRecord = acTrans.GetObject(acClonedLayout.BlockTableRecordId, OpenMode.ForRead)

 Step 5: Set the VP on the cloned layout to view the object



Dim acVP As Viewport = CType(acEnt, Viewport)
                        acVP.UpgradeOpen()

                        'Set scale as 1:1000
                        acVP.CustomScale = 1

                        'Center Viewport on block
                        acVP.ViewDirection = Vector3d.ZAxis
                        acVP.ViewTarget = acLotInfo.InsertionPointforBlock
                        acVP.ViewCenter = Point2d.Origin

                        'Set viewport rotation to  360 - block rotation angle
                        acVP.TwistAngle = (2 * PI) - acLotInfo.BlockRotation

                        acVP.UpdateDisplay()
                        acVP.DowngradeOpen()

 I've attached the full .vb file here if anyone would like to see the full code for this procedure.  The Procedure name is "CreateBGSlips()".  I appreciate any help or light anyone can shed on this!





Chris Welk
Urban Systems Ltd.
Engineering Technologist (Civil)/CADD Applications Developer
Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 2 of 7 (952 Views)

Re: Creating a Paperspace Layout and Viewport Issues

06-07-2012 05:24 AM in reply to: cwelkusl

Hi Chris,

 

Had a read of your post and opened your code file. I'd like to try to help, but to be honest I can't go through your entire project.

 

Are you able to track down the specific part of your code that is causing you problems? Are you able to create just a small code sample that reproduces the core issue, so others can try it and give advice?

 

Without the whole visual studio solution I can't easily run it and see what happens, especially if I also need custom dwg or excel files to do so.

 

Art

Contributor
cwelkusl
Posts: 19
Registered: ‎01-18-2011
Message 3 of 7 (943 Views)

Re: Creating a Paperspace Layout and Viewport Issues

06-07-2012 07:34 AM in reply to: cwelkusl

Hey Art,

 

Thanks for the offer of help, I appreciate it.  I unfortunately can't narrow it down to a specific section of code because I'm not sure what the culprit is or what I'm doing wrong.  Essentially my issue is that once I've created a layout with a viewport on it, when I switch to that layout I have to do a zoom extents, switch to another layout, then go back and do another zoom extents before the zoom extents actually sticks and the floating viewport actually shows anything.

 

I know I have a lot of code there pertaining to many things that don't contribute to the problem, so what I'll do is write up a small section of code that recreates the essence, and the problem of what I am doing and repost it here a little later.

 

Thanks again.

Chris Welk
Urban Systems Ltd.
Engineering Technologist (Civil)/CADD Applications Developer
Valued Contributor
FFlix
Posts: 95
Registered: ‎11-15-2011
Message 4 of 7 (925 Views)

Re: Creating a Paperspace Layout and Viewport Issues

06-08-2012 11:50 AM in reply to: cwelkusl

hi chris, as an idea, what happens if you:

1. switch the vp on in step 5 (as opposed to step 4)?

2. after step 5, on the cloned layout, use the document editor to .switchtomodelspace and back .switchtopaperspace?

Contributor
cwelkusl
Posts: 19
Registered: ‎01-18-2011
Message 5 of 7 (868 Views)

Re: Creating a Paperspace Layout and Viewport Issues

06-26-2012 11:23 AM in reply to: Artvegas

So here is some simplified code.  There is still some routines not displayed here to do the ZoomExtents (standard code from Autodesk) and to do a RectangleJig, but neither pertain to the issues I am experiencing.  I have attached the full code file if needed.

 

Public Class CoreFile
    <CommandMethod("CLVP")> _
    Public Sub CLVP()
        Dim acDoc As Document = DocumentManager.MdiActiveDocument
        Dim acDB As Database = acDoc.Database
        Dim acEd As Editor = acDoc.Editor


        Dim strLayoutNameBase As String = "Temp"
        Dim strClonedLayoutNameBase As String = "Clone"
        Dim strLayoutNameFinal As String = ""
        Dim iIndex As Integer = 0
        Dim acNewLayoutID As New ObjectId
        Dim acVPBounds As New Polyline
        Dim acTempLayout As New Layout

        Using acTrans As Transaction = acDB.TransactionManager.StartTransaction

            '** Create new temp layout with a unique name ****************************************************************
            Dim acLM As LayoutManager = LayoutManager.Current
            While acLM.GetLayoutId(strLayoutNameBase & iIndex.ToString) <> Nothing
                iIndex += 1
            End While
            strLayoutNameFinal = strLayoutNameBase & iIndex.ToString
            'acNewLayoutID = acLM.CreateLayout(strLayoutNameFinal)
            acTempLayout.LayoutName = strLayoutNameFinal

            'acTempLayout = acTrans.GetObject(acNewLayoutID, OpenMode.ForRead)

            Dim acLayDict As DBDictionary = acTrans.GetObject(acDB.LayoutDictionaryId, OpenMode.ForRead)
            Dim acBT As BlockTable = acTrans.GetObject(acDB.BlockTableId, OpenMode.ForRead)
            Dim acBTR As New BlockTableRecord
            acBTR.Name = "*Paper_Space"
            acBT.UpgradeOpen()
            Dim acBTR_ObjID As ObjectId = acBT.Add(acBTR)
            acBT.DowngradeOpen()
            acTrans.AddNewlyCreatedDBObject(acBTR, True)
            acTempLayout.AddToLayoutDictionary(acDB, acBTR_ObjID)
            acTrans.AddNewlyCreatedDBObject(acTempLayout, True)
            acTempLayout.Initialize()

            acTrans.Commit()
            acEd.Regen()
        End Using


        Using acTrans As Transaction = acDB.TransactionManager.StartTransaction
            Dim acLM As LayoutManager = LayoutManager.Current
            '** Switch to the temp layout and zoom extents **************************************************************
            acLM.CurrentLayout = acTempLayout.LayoutName
            ZoomExtents()

            '** Have user draw floating viewport location using a rectangular jig ***************************************
            Dim acPointPrompt As PromptPointOptions = New PromptPointOptions(vbLf & "Select first viewport corner:")
            Dim acPointResult As PromptPointResult
            With acPointPrompt
                .AllowNone = False
            End With

            acPointResult = acEd.GetPoint(acPointPrompt)
            Select Case acPointResult.Status
                Case PromptStatus.OK
                    Dim RectJig As CoreFile.RectangleJig = New CoreFile.RectangleJig(acPointResult.Value)
                    If acEd.Drag(RectJig).Status = PromptStatus.Cancel Then
                        Exit Sub
                    End If
                    acVPBounds = RectJig.PolylineObject
                Case Else
                    Exit Sub
            End Select

            '** Create Viewport *****************************************************************************************
            Dim acCurSpace As BlockTableRecord = acTrans.GetObject(acDB.CurrentSpaceId, OpenMode.ForWrite)
            Dim acVP As Viewport = New Viewport()
            Dim acVPCenterX As Double
            Dim acVPCenterY As Double
            Dim acVPWidth As Double
            Dim acVPHeight As Double

            acCurSpace.AppendEntity(acVP)
            acTrans.AddNewlyCreatedDBObject(acVP, True)

            If acVPBounds.GetPoint2dAt(0).X > acVPBounds.GetPoint2dAt(2).X Then
                acVPCenterX = acVPBounds.GetPoint2dAt(0).X - ((acVPBounds.GetPoint2dAt(0).X - acVPBounds.GetPoint2dAt(2).X) / 2)
                acVPWidth = (acVPBounds.GetPoint2dAt(0).X - acVPBounds.GetPoint2dAt(2).X)
            Else
                acVPCenterX = acVPBounds.GetPoint2dAt(2).X - ((acVPBounds.GetPoint2dAt(2).X - acVPBounds.GetPoint2dAt(0).X) / 2)
                acVPWidth = (acVPBounds.GetPoint2dAt(2).X - acVPBounds.GetPoint2dAt(0).X)
            End If

            If acVPBounds.GetPoint2dAt(0).Y > acVPBounds.GetPoint2dAt(2).Y Then
                acVPCenterY = acVPBounds.GetPoint2dAt(0).Y - ((acVPBounds.GetPoint2dAt(0).Y - acVPBounds.GetPoint2dAt(2).Y) / 2)
                acVPHeight = (acVPBounds.GetPoint2dAt(0).Y - acVPBounds.GetPoint2dAt(2).Y)
            Else
                acVPCenterY = acVPBounds.GetPoint2dAt(2).Y - ((acVPBounds.GetPoint2dAt(2).Y - acVPBounds.GetPoint2dAt(0).Y) / 2)
                acVPHeight = (acVPBounds.GetPoint2dAt(2).Y - acVPBounds.GetPoint2dAt(0).Y)
            End If
            acVP.CenterPoint = New Point3d(acVPCenterX, acVPCenterY, 0)

            acVP.Width = acVPWidth
            acVP.Height = acVPHeight

            acVP.Layer = "0"

            acVP.StandardScale = StandardScaleType.Scale1To1

            'Center Viewport on 0,0,0
            acVP.ViewDirection = Vector3d.ZAxis
            acVP.ViewTarget = New Point3d(0, 0, 0)
            acVP.ViewCenter = Point2d.Origin

            acVP.Visible = True
            acVP.On = True
            acVP.UpdateDisplay()

            '** Clone layout a few times **********************************************************************
            For i = 1 To 3
                iIndex = 0
                While acLM.GetLayoutId(strClonedLayoutNameBase & iIndex.ToString) <> Nothing
                    iIndex += 1
                End While
                acLM.CloneLayout(acTempLayout.LayoutName, strClonedLayoutNameBase & iIndex, 0)
            Next i
            acTrans.Commit()
            acEd.Regen()
        End Using
    End Sub

 Essentially, the layout and VPs are getting created, but the VPs really do not act correctly.  I can change to a created layout, witch to model space in the created VP and do a zoom extents.  When I switch away from the layout and then back, the VP is at it's original scale factor.  So I think I'm doing something wrong with the VP creation method, or I'm missing a step I'm not aware of.

Chris Welk
Urban Systems Ltd.
Engineering Technologist (Civil)/CADD Applications Developer
Valued Contributor
FFlix
Posts: 95
Registered: ‎11-15-2011
Message 6 of 7 (862 Views)

Re: Creating a Paperspace Layout and Viewport Issues

06-26-2012 12:27 PM in reply to: cwelkusl

hi chris, in the 'create viewport' section

1. you specify width and height, immediately followed by a standardscale1to1 - i think you want either or, not both;

2. try add acVP.locked=True

Contributor
cwelkusl
Posts: 19
Registered: ‎01-18-2011
Message 7 of 7 (848 Views)

Re: Creating a Paperspace Layout and Viewport Issues

06-26-2012 04:22 PM in reply to: FFlix

Found the issue in my simplified code:

 

acVP.ViewCenter = Point2d.Origin

 

Is apparently the culprit.  Modified to:

 

acVp.ViewCenter = New Point2d(0,0)

 

and the VPs act as expected.  Now to solve the issues in my original code.

Chris Welk
Urban Systems Ltd.
Engineering Technologist (Civil)/CADD Applications Developer
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.