Hello,
My ultimate goal is to switch to World coordinate system, then back to the user coordinate system.
With that said, I have not been able to get past populating the ViewportTableRecord in my code below. I keep getting the same error:
{Unable to cast object of type 'Autodesk.AutoCAD.DatabaseServices.Viewport' to type 'Autodesk.AutoCAD.DatabaseServices.ViewportTableRecord}
Any suggestions? or an easier way? -- thanks PT
Here is my sample code:
Sub World() Dim lDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim Db As Database = lDrawing.Database Dim ed As Editor = lDrawing.Editor 'Set to WORLD Coordinate System and back. Using LockDoc As DocumentLock = lDrawing.LockDocument Using acTrans As Transaction = Db.TransactionManager.StartTransaction() Dim acVpT As ViewportTable acVPT = acTrans.GetObject(Db.ViewportTableId, OpenMode.ForWrite) 'Dim acVpTRec As ViewportTableRecord 'I have tried the following lines. All give the same error. Dim acVpTRec As ViewportTableRecord = acTrans.GetObject(ed.ActiveViewportId, OpenMode.ForWrite) 'acVpTRec = acTrans.GetObject(ed.CurrentViewportObjectId, OpenMode.ForWrite) 'acVpTRec = CType(acTrans.GetObject(ed.ActiveViewportId, OpenMode.ForWrite), ViewportTableRecord) 'acVpTRec = CType(acTrans.GetObject(ed.CurrentViewportObjectId, OpenMode.ForWrite), ViewportTableRecord) 'acVpTRec = CType(acTrans.GetObject(ed.ActiveViewportId, OpenMode.ForWrite), Autodesk.AutoCAD.DatabaseServices.ViewportTableRecord) Dim VpTRecID As Autodesk.AutoCAD.DatabaseServices.ObjectId 'grab current User Coordinate System. VpTRecID = ed.ActiveViewportId 'Set to World acVpTRec.SetUcsToWorld() 'return to User acVpTRec.SetUcs(VpTRecID) End Using End Using End Sub
Could you, please, check that it is the cause of the issue:
[from ObjectARX\docs\arxref.chm]
AcDbObjectId acedActiveViewportId();
Description
Returns the object ID of the viewport that is currently active in the editor. In tilemode, it returns the ID is of a viewport table record. In layout mode, returns the object ID of a viewport entity .
Have you tried to cast to Viewport? I guess you can see the actual type of the object in the debugger's Watch window.