Author Topic: predefined views  (Read 915 times)

0 Members and 1 Guest are viewing this topic.

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
predefined views
« on: August 14, 2013, 08:21:16 PM »
Hi,

Here's a method to set orthogonal or isometric views.
The zoom is equivalent to a zoom extents plus a zoom 0.8X.

The following code defines also 10 commands to set predefined views with the numpad.

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(ViewSample.CommandMethods))]

namespace ViewSample
{
    public class CommandMethods
    {
        enum ViewDirection { Top, Bottom, Front, Back, Left, Right, SeIso, SwIso, NeIso, NwIso }

        private void SetView(ViewDirection vDir)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            Vector3d viewDir = new Vector3d();
            switch (vDir)
            {
                case ViewDirection.Top:
                    viewDir = Vector3d.ZAxis; break;
                case ViewDirection.Bottom:
                    viewDir = Vector3d.ZAxis.Negate(); break;
                case ViewDirection.Front:
                    viewDir = Vector3d.YAxis.Negate(); break;
                case ViewDirection.Back:
                    viewDir = Vector3d.YAxis; break;
                case ViewDirection.Left:
                    viewDir = Vector3d.XAxis.Negate(); break;
                case ViewDirection.Right:
                    viewDir = Vector3d.XAxis; break;
                case ViewDirection.SeIso:
                    viewDir = new Vector3d(1.0, -1.0, 1.0); break;
                case ViewDirection.SwIso:
                    viewDir = new Vector3d(-1.0, -1.0, 1.0); break;
                case ViewDirection.NeIso:
                    viewDir = new Vector3d(1.0, 1.0, 1.0); break;
                case ViewDirection.NwIso:
                    viewDir = new Vector3d(-1.0, 1.0, 1.0); break;
            }

            db.UpdateExt(true);
            Extents3d extents = db.TileMode ?
                new Extents3d(db.Extmin, db.Extmax) :
                (int)Application.GetSystemVariable("CVPORT") == 1 ?
                    new Extents3d(db.Pextmin, db.Pextmax) :
                    new Extents3d(db.Extmin, db.Extmax);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            using (ViewTableRecord view = ed.GetCurrentView())
            {
                Matrix3d viewTransform =
                    Matrix3d.PlaneToWorld(viewDir)
                    .PreMultiplyBy(Matrix3d.Displacement(view.Target - Point3d.Origin))
                    .PreMultiplyBy(Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target))
                    .Inverse();

                extents.TransformBy(viewTransform);

                view.ViewDirection = viewDir;
                view.Width = (extents.MaxPoint.X - extents.MinPoint.X) * 1.25;
                view.Height = (extents.MaxPoint.Y - extents.MinPoint.Y) * 1.25;
                view.CenterPoint = new Point2d(
                    (extents.MinPoint.X + extents.MaxPoint.X) / 2.0,
                    (extents.MinPoint.Y + extents.MaxPoint.Y) / 2.0);
                ed.SetCurrentView(view);
                tr.Commit();
            }
        }

        [CommandMethod("0")]
        public void ViewBottom()
        {
            SetView(ViewDirection.Bottom);
        }

        [CommandMethod("1")]
        public void ViewSouthWestIso()
        {
            SetView(ViewDirection.SwIso);
        }

        [CommandMethod("2")]
        public void ViewFront()
        {
            SetView(ViewDirection.Front);
        }

        [CommandMethod("3")]
        public void ViewSeIso()
        {
            SetView(ViewDirection.SeIso);
        }

        [CommandMethod("4")]
        public void ViewLeft()
        {
            SetView(ViewDirection.Left);
        }

        [CommandMethod("5")]
        public void ViewTop()
        {
            SetView(ViewDirection.Top);
        }

        [CommandMethod("6")]
        public void ViewSouthEastIso()
        {
            SetView(ViewDirection.Right);
        }

        [CommandMethod("7")]
        public void ViewNorthWestIso()
        {
            SetView(ViewDirection.NwIso);
        }

        [CommandMethod("8")]
        public void ViewBack()
        {
            SetView(ViewDirection.Back);
        }

        [CommandMethod("9")]
        public void ViewNorthEastIso()
        {
            SetView(ViewDirection.NeIso);
        }
    }
}
« Last Edit: August 14, 2013, 10:02:33 PM by (gile) »