I hope if you can help me with this problem, I am still stuck at it. My objective is to find whether a layout viewport has a rotated ICS or not.
I have attached a visual studio 2012 project along a testdrawing, please check my code and tell me why it is not working.
In fact the code works but not always, for example if you run the code and select the viewport in the layout of the attached drawing, it will falsely give you (1,0,0) which is the vector or the world ucs x-axis, but this is not true. Now activate the model space in the viewport in autocad manually, then run the code again, it would work and give you the right coordinates of the ucs x-axis.
I don't understand how and why it works this way, so could you help me a little please.
I think the real problem is in the part of the code that makes the viewport active, I cannot believe that there is no code in vb.net to activate a viewport, and using the com code may be the problem.
Great thanks for you, I hope this problem gets resolved.
Public Class Test
<CommandMethod("GetUCS", CommandFlags.NoTileMode)> _
Public Sub GetUCS()
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurdb As Database = acDoc.Database
Dim ed As Editor = acDoc.Editor()
Dim peo As New PromptEntityOptions("Select a viewport : ")
peo.SetRejectMessage("Select a viewport.")
Dim per As PromptEntityResult = ed.GetEntity(peo)
Dim vpId As ObjectId = per.ObjectId
Dim acadDoc As AcadDocument = Autodesk.AutoCAD.ApplicationServices.DocumentExtension.GetAcadDocument(acDoc)
Using tr As Transaction = acCurdb.TransactionManager.StartTransaction()
Dim vp As Autodesk.AutoCAD.DatabaseServices.Viewport = TryCast(tr.GetObject(vpId, OpenMode.ForRead), Autodesk.AutoCAD.DatabaseServices.Viewport)
'Setting the chosen viewport active
acadDoc.ActivePViewport = vp.AcadObject
'I just want to read the x-axis vector to know whether the viewport has been rotated or not
Dim curXdir As Vector3d = acCurdb.Ucsxdir
ed.WriteMessage(vbLf & curXdir.ToString)
Here is some C# code that set a viewport UCS, I would assume the same code works as well to retrieve the current UCS:
public void SetViewportUcs()
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
AcadPViewport acadViewport = null;
AcadDocument acadDoc = doc.GetAcadDocument() as AcadDocument;
acadDoc.ActiveLayout = acadDoc.Layouts.Item("Layout1");
//Reference the ViewPort object on Layout (the ActiveLayout)
if (acadDoc.PaperSpace.Item(1).ObjectName == "AcDbViewport")
acadViewport = acadDoc.PaperSpace.Item(1) as AcadPViewport;
//Make sure the Viewport is displayed and on
acadViewport.ViewportOn = true;
//Before making a pViewport active the mspace property needs
//to be True for a floating viewport
acadDoc.MSpace = true;
acadDoc.ActivePViewport = acadViewport;
Matrix3d newUcsMat = Matrix3d.AlignCoordinateSystem(
new Point3d(0, 0, 0), new Vector3d(1, 0, 0), new Vector3d(0, 1, 0), new Vector3d(0, 0, 1),
new Point3d(0, 0, 0), new Vector3d(0, 1, 0), new Vector3d(-1, 0, 0), new Vector3d(0, 0, 1));
ed.CurrentUserCoordinateSystem = newUcsMat;
Philippe Leefsma Developer Technical Services Autodesk Developer Network