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:
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!
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
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.
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?
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.
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
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.
Can't find what you're looking for? Ask the community or share your knowledge.