.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Changing 3D View to Top View

17 REPLIES 17
SOLVED
Reply
Message 1 of 18
dynamicscope
5636 Views, 17 Replies

Changing 3D View to Top View

doc.SendStringToExecute("-view\ntop\n", true, false, false);

 

Well, I know this would change the view.

But I need to do this in the middle of some process. (I tried it, but the next step gets executed before view change is done.)

I am changing the view to use SelectWindowPolygon().

So I need to do this by lines of codes. (So that the view change gets done before the next step.)

 

I found a couple solutions but the codes contain some methods that is not supported in ARX 2007.

 

If anyone can give me something I can start with, it would be very appreciated.

 

FYI, I am using ARX 2007 with C#

 

 

17 REPLIES 17
Message 2 of 18
fenton.webb
in reply to: dynamicscope

Message 3 of 18
dynamicscope
in reply to: fenton.webb

Yes, I looked at it. It looks little complicated.
I just hoped if there is any easier way to do it. (Since there is a command to do it.)
But I guess it is worth trying...
🙂
Message 4 of 18
fenton.webb
in reply to: dynamicscope

Yes, there is a command to do it however the smooth view scrolling is disabled in that command if invoked from a script, so it's not so nice 😞




Fenton Webb
AutoCAD Engineering
Autodesk

Message 5 of 18
dynamicscope
in reply to: fenton.webb

Actually, I do not quite need the animation effect.

I need to change the view to use Editor.SelectWindowPolygon()

 

Well, I came up with this~~

 

Document doc = Application.DocumentManager.MdiActiveDocument;            
Editor ed = doc.Editor;
Manager gsm = doc.GraphicsManager;
int vp = Convert.ToInt32(Application.GetSystemVariable("CVPORT"));
using (View view = gsm.GetGsView(vp, true))
{
	ViewTableRecord v = new ViewTableRecord();	
	v.SetUcs(view.Position, Vector3d.XAxis, Vector3d.YAxis);
	ed.SetCurrentView(v);
}

 

This quite "LOOKS" acting right, but the result is weird.

If I use Editor.SelectWindowPolygon() (with the same polygon), it catches different objects (compared to the result I get by entering "-view top ", then run the Editor.SelectWindowsPolygon() ).

 

I need to check why this is happening.

 

If anyone knows the reason, any comment would be appreciated.

Message 6 of 18
_gile
in reply to: dynamicscope

Hi,

 

You can synchrously call  the VIEW command P/Invoking acedCmd or using the Tony Tanzillo's wrapper for non-public Editor.RunCommand() method.

 

You can also use the following method.

 

        private void SetViewTop()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            Extents3d extents = db.TileMode ?
                new Extents3d(db.Extmin, db.Extmax) :
                (short)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 WCS2DCS =
                    Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
                    Matrix3d.Displacement(view.Target - Point3d.Origin);

                extents.TransformBy(WCS2DCS.Inverse());

                view.ViewDirection = Vector3d.ZAxis;
                view.Width = extents.MaxPoint.X - extents.MinPoint.X;
                view.Height = extents.MaxPoint.Y - extents.MinPoint.Y;
                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();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 18
_gile
in reply to: _gile

I'am glad you solve your probem.

 

Here's a more generic method to set any orthogonal and isometric view and some commands to set these views from the numpad.

 

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.2; view.Height = (extents.MaxPoint.Y - extents.MinPoint.Y) * 1.2; 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); } } }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 18
jaboone
in reply to: _gile

There is one command I use all the time to out of 3d and back to my current usc, or back to original world.  This always puts me back on track.

PLAN

Learning as I go
Message 9 of 18
Skarafaz
in reply to: jaboone

Hi! I found the code posted by _gile very interesting; is there a way to set these fixed views on an offscreen device? Device.Add() accepts GraphicsSystem.View objects that are quite different from ViewTableRecord objects so I can't understand how to do the same calculations.

Message 10 of 18
kpablo80
in reply to: _gile

i was wondering if you could show me where this scrip is save to as i am not to familiar with the location.

thank you 

kevin 

Message 11 of 18
jaboone
in reply to: kpablo80

Use UCS to setup any type of view save it to current view then use PLAN to set 2d to that coordinate.  One of the UCS's is 3 point.  I mostly use that to switch between a front or side view.  Not sure what you are asking.

Learning as I go
Message 12 of 18
kpablo80
in reply to: jaboone

what i am asking is how do i get the above script, to use numbers to choose the view in autocad, 

eg 5 = PLan view, 2 = front view etc... 

 

thanks 

kevin 

Message 13 of 18
_gile
in reply to: kpablo80

Hi,

 

The above code is not a script, it's a .NET code (C#) which have to be compiled into a DLL and load in AutoCAD with the NETLOAD command (or any other loading mechanism).

 

Probably easier to use, the following AutoLISP code does the same thing.

 

;;; Vue prédéfinies depuis le pavé numérique (gile)
;;; 0 = Bas
;;; 1 = Isométrie sud Ouest
;;; 2 = Avant
;;; 3 = Isométrie sud Est
;;; 4 = Gauche
;;; 5 = Haut
;;; 6 = Droite
;;; 7 = Isométrie Nord Ouest
;;; 8 = Arrière
;;; 9 = Isométrie Nord Est

(mapcar
  '(lambda (f v)
     (eval (list 'defun
		 f
		 nil
		 (list 'command "_.view" v)
		 '(princ)
	   )
     )
   )
  '(c:0 c:1 c:2 c:3 c:4 c:5 c:6 c:7 c:8 c:9)
  '("_bottom"	"_swiso"    "_front"	"_seiso"    "_left"
    "_top"	"_right"    "_nwiso"	"_back"	    "_neiso"
   )
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 14 of 18
olivier.lebescond
in reply to: _gile

Hi Gilles, (Salut)

This is an old topic but I don't manage to set the ViewDirection of the view with two points : the observation point (3D), and the target point (3D). The view's width is not a problem, I just want this fck view be right compared to my two points. Have you ever developed something like this ?

Best regards (en te remerciant),

Olivier

Message 15 of 18
_gile
in reply to: olivier.lebescond

@olivier.lebescond 

Something like this ?

        [CommandMethod("SETVIEW")]
        public static void SetViewCmd()
        {
            var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            var ppo = new PromptPointOptions("\nSource point: ");
            var ppr = ed.GetPoint(ppo);
            if (ppr.Status == PromptStatus.OK)
            {
                var source = ppr.Value;
                ppo.Message = "\nTarget point: ";
                ppo.BasePoint = source;
                ppo.UseBasePoint = true;
                ppr = ed.GetPoint(ppo);
                if (ppr.Status == PromptStatus.OK)
                {
                    var target = ppr.Value;
                    var viewDir = source.GetVectorTo(target).TransformBy(ed.CurrentUserCoordinateSystem);
                    using (var view = ed.GetCurrentView())
                    {
                        view.ViewDirection = viewDir;
                        ed.SetCurrentView(view);
                    }
                }
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 16 of 18
olivier.lebescond
in reply to: _gile

Hi Gilles,

Thanks a lot for your help.

In fact I get the same result that this I have computed (and to be honnest more complicated than yours, so thanks). And due to the fact your code gives me the same bad result, that helped me to find that my view had a ViewTwist different of 0, that created the issue.

Now all is fine, that's good.

Thanks again,

Olivier

Message 17 of 18
jhdempsey
in reply to: _gile

@_gile , your script above has been very helpful, but its doing one thing that I cant understand.

I have a DWG file which is a block that has been exported using he WBLOCK command (see attached file I_Furn_Misc_Pingpong.dwg)

Im using your code within my script to switch between each of the different views, and then take a screenshot

 

From the screenshots below, which were taken by my code, you can see that they are all coming out as expected, except for the "North-East" view, which is too zoomed in

block-top.png

block-ne.png

block-nw.png

block-se.png

block-sw.png

 

Ive managed to work out that its always the first isometric view that has this problem. If i tell it to take a different isometric view first, that one has the same problem.

If i tell it to take the north east first, then a different one, then the north east one again, the second attempt at the north east one comes out correctly.

 

Any idea what might be causing this behaviour?

Message 18 of 18
_gile
in reply to: jhdempsey

@jhdempsey, thanks to report.

There was a mistake in the operations order.

This one seems to work as expected.

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
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)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            Vector3d viewDir;
            switch (vDir)
            {
                case ViewDirection.Top:
                default:
                    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;
            }

            using (var view = ed.GetCurrentView())
            {
                view.ViewDirection = viewDir;
                ed.SetCurrentView(view);
                db.UpdateExt(true);
                
                var viewTransform =
                    Matrix3d.WorldToPlane(viewDir) *
                    Matrix3d.Displacement(view.Target.GetAsVector().Negate()) *
                    Matrix3d.Rotation(view.ViewTwist, view.ViewDirection, view.Target);

                var 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);
                extents.TransformBy(viewTransform);

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

        [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);
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost