How to get current view [Top, Front, Left,Right...] orientation using C#

How to get current view [Top, Front, Left,Right...] orientation using C#

kite15
Advocate Advocate
2,340 Views
5 Replies
Message 1 of 6

How to get current view [Top, Front, Left,Right...] orientation using C#

kite15
Advocate
Advocate

Hi,

Can help any one how to get the orientation [Top, Front, Left, Right...] from Model space only ?.

Please, note that the model space divided/split grater than two view [snap attached].

Need to get current view orientation as respective of cursor position...

Thanks in advance..

 

0 Likes
2,341 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

You can check the ViewDirection property of the active model space viewport view (ViewTableRecord).

 

        [CommandMethod("CMD", CommandFlags.NoPaperSpace)]
        public void Cmd()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            using (var view = ed.GetCurrentView())
            {
                ed.WriteMessage($"\nCurrent View: {GetViewName(view.ViewDirection)}");
            }
        }

        private string GetViewName(Vector3d viewDirection)
        {
            double sqrt033 = Math.Sqrt(1.0 / 3.0);
            switch (viewDirection.GetNormal())
            {
                case Vector3d v when v.IsEqualTo( Vector3d.ZAxis): return "Top";
                case Vector3d v when v.IsEqualTo(Vector3d.ZAxis.Negate()): return "Bottom";
                case Vector3d v when v.IsEqualTo(Vector3d.XAxis): return "Right";
                case Vector3d v when v.IsEqualTo(Vector3d.XAxis.Negate()): return "Left";
                case Vector3d v when v.IsEqualTo(Vector3d.YAxis): return "Back";
                case Vector3d v when v.IsEqualTo(Vector3d.YAxis.Negate()): return "Front";
                case Vector3d v when v.IsEqualTo(new Vector3d(sqrt033, sqrt033, sqrt033)): return "NE Isometric";
                case Vector3d v when v.IsEqualTo(new Vector3d(-sqrt033, sqrt033, sqrt033)): return "NW Isometric";
                case Vector3d v when v.IsEqualTo(new Vector3d(-sqrt033, -sqrt033, sqrt033)): return "SW Isometric";
                case Vector3d v when v.IsEqualTo(new Vector3d(sqrt033, -sqrt033, sqrt033)): return "SE Isometric";
                default: return $"Custom View";
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 6

kite15
Advocate
Advocate

Hi _gile,

The code shown some error (attached snap).

Whereas, I have modified the code from switch, case to if, else.. and get the asking result.

Although, the switch, case way is great apart from performance. Please, comment on this

Code:

        [CommandMethod("CMD", CommandFlags.NoPaperSpace)]
        public void GetCurrentViewName()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            using (var view = ed.GetCurrentView())
            {               
                Vector3d viewDirection = ed.GetCurrentView().ViewDirection;
                if (viewDirection.IsEqualTo(Vector3d.ZAxis))
                {
                    ed.WriteMessage("\nTop");
                }
                else if (viewDirection.IsEqualTo(Vector3d.XAxis))
                {
                    ed.WriteMessage("\nRight");
                }
                else if (viewDirection.IsEqualTo(Vector3d.XAxis.Negate()))
                {
                    ed.WriteMessage("\nLeft");
                }
                else if (viewDirection.IsEqualTo(Vector3d.YAxis.Negate()))
                {
                    ed.WriteMessage("\nFront");
                }
                else
                {
                    ed.WriteMessage("\nCustom View");
                }
            }
        }
Message 4 of 6

_gile
Consultant
Consultant

Hi,

 

The code I posted uses pattern matching, one of the C# 7 (Visual Studio 2017) new features.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 6

kite15
Advocate
Advocate

Actually, I am using Visual Studio 2013. So, this is the reason.

Thank you...Smiley Happy

0 Likes
Message 6 of 6

ditran7577
Advocate
Advocate

Could you explain more about  sqrt033? How is about value for edge views or other angles view? Thanks for your helps