What is the correct way of copying an existing viewport, What I am actually doing is that to clone a layout by copying what is inside it, everything works smoothly except for the viewports.
I tried to use the clone method, but it didnt work, I also tried to use the copyfrom method but also to no avail.
In both cases the copied viewport is a dumb black rectangle
Dim exkeyvport As Autodesk.AutoCAD.DatabaseServices.Viewport = DirectCast(acTrans.GetObject(objId, OpenMode.ForRead), Autodesk.AutoCAD.DatabaseServices.Viewport) Dim keyvport As New Autodesk.AutoCAD.DatabaseServices.Viewport keyvport.CopyFrom(exkeyvport) keyvport.UpdateDisplay() NewblkTableRec.AppendEntity(keyvport) acTrans.AddNewlyCreatedDBObject(keyvport, True)
The only way that partially worked for me was to create a viewport and copy its attributes one by one, but this is exhaustive and not reliable, so could anyone tell us of the proper way to copy a viewport?
Solved! Go to Solution.
Add this :
keyvport.CenterPoint = New Point3d(keyvportCenterX, keyvportCenterY, 0)
keyvport.Width = keyvportWidth
keyvport.Height = keyvportHeight
keyvport.Layer = "0"
keyvport.StandardScale = StandardScaleType.Scale1To1
keyvport.ViewDirection = Vector3d.ZAxis
keyvport.ViewTarget = New Point3d(0, 0, 0)
keyvport.ViewCenter = Point2d.Origin
keyvport.Visible = True
keyvport.On = True
The easiest way to do that is to create a new viewport and set its view parameters based on the source viewport.
BlockTableRecord btrDest = (BlockTableRecord)Tx.GetObject(LayoutDest.BlockTab
leRecordId, OpenMode.ForWrite); Viewport vp = Tx.GetObject(per.ObjectId, OpenMode.ForRead) as Viewport; Viewport vpNew = new Viewport(); vpNew.SetDatabaseDefaults(); Point2d viewCenter = vp.ViewCenter; vpNew.ViewCenter = viewCenter; Point3d viewTarget = vp.ViewTarget; vpNew.ViewTarget = viewTarget; Vector3d viewDir = vp.ViewDirection; vpNew.ViewDirection = viewDir; double viewHeight = vp.ViewHeight; vpNew.ViewHeight = viewHeight; double viewportHeight = vp.Height; vpNew.Height = viewportHeight; double viewportWidth = vp.Width; vpNew.Width = viewportWidth; Point3d centerPoint = vp.CenterPoint; vpNew.CenterPoint = centerPoint; double custScale = vp.CustomScale; vpNew.CustomScale = custScale; double elevation = vp.Elevation; vpNew.Elevation = elevation; double lensLength = vp.LensLength; vpNew.LensLength = lensLength; bool isVisible = vp.Visible; vpNew.Visible = isVisible; oid = btrDest.AppendEntity(vpNew); Tx.AddNewlyCreatedDBObject(vpNew, true);
I was finally able to clone the viewport, without having the need to copy each parameter alone, the way to copy a viewport is by using the deepcloneobjects method
'objid refers to the viewport object id If (objId.ObjectClass.DxfName.ToUpper = "VIEWPORT") Then Dim id As ObjectId = newLayout.BlockTableRecordId Dim idcol As New ObjectIdCollection idcol.Add(objId) Dim idmap As New IdMapping acCurdb.DeepCloneObjects(idcol, acCurdb.CurrentSpaceId, idmap, False) End if
The only bad thing in using this method is that I do not get the objectid of the newly created viewport, so that I can not modify it after cloning it (I wonder if there is a method to do so).
The viewport object also has a method that is deepclone(), I tried to use it but I didnt find enough documentation on how to use it, and I didn't reach a notable result.
Thanks for your answers guys
Glad to know that you have another solution in hand.
To get the ObjectId, look for the idmap. That should have that info.
I have checked your post here:
and your note: This solution does not change the UCS name that is displayed by AutoCAD using the UCSNAME system variable.
Is there a solution that does the same but shows the ucs name when using the ucsname variable?
Thanks for help
My kind request would be to create a separate forum post for this topic.
It makes it easier for someone searching for answers later and also it is likely that you will get more than one way to solve it from the experts in this forum.