.NET

Reply
Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 1 of 9 (267 Views)

Rotating within viewport

267 Views, 8 Replies
02-13-2014 07:45 AM

Hi, I have been searching for code examples of how to rotate a viewport.  Or rather rotate the model space within a view port.  I have a block that my code is automatically retrieving the rotation angle of and would like to use that to establish the new horizontal..or north...whichever.

 

I have currently been experimenting with .TwistAngle, .ViewDirection but the twist angle appears to give unexpected results?  Viewdirection, my undestanding, is actually the 'camera' angle?

 

Any direction on where to go would be greatly appreciated.  Like I said, i have found examples but nothing seems to work for some reason?

 

Thanks a bunch!

Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 2 of 9 (249 Views)

Re: Rotating within viewport

02-13-2014 09:09 AM in reply to: greg

Hey Greg,

 

You're right. "Rotating the viewport" is common misconception; you can't rotate the viewport, but you can set a UCS within a viewport that is rotated!

 

A good way to remember this is to consider how you would "rotate your view" in modelspace, because AutoCAD actually perceives your modelspace as a viewport. 

 

If you're unfamiliar with defining a UCS programmatically, this will get you started.

 

Drew

Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 3 of 9 (234 Views)

Re: Rotating within viewport

02-13-2014 12:47 PM in reply to: architectOfIdeas

Hi Drew,

 

Thanks for the message!  Greatly appreciated.

 

I am not familiar with programmatically defining a UCS.  However, it looks like I will need to familiarize myself...thanks for the link.

 

So the only way to 'rotate' the model space of the viewport programmatically is with a UCS?  In my drafting days I believe we made use of MVSetup which I guess essentially creates a UCS?  

 

I will let you know if I run into issues...most likely will but we shall see. :smileyhappy:

 

Thanks!

Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 4 of 9 (230 Views)

Re: Rotating within viewport

02-13-2014 12:57 PM in reply to: greg

mvsetup.lsp! Nice!

 

I don't exactly know how that command works, but I assume it would create a UCS at the defined rotation in the process...

 

Let me know how it goes. Also, keep in mind that the sample in the above link is setting the UCS to the active viewport. Your code will be a little different, as you'll need to identify the specific viewport to set the UCS to.

 

Good luck!

Drew

Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 5 of 9 (220 Views)

Re: Rotating within viewport

02-13-2014 01:14 PM in reply to: architectOfIdeas

It is interesting, when I copy that code verbatim with no edits I get an error at:

 

 '' Open the active viewport
            Dim acVportTblRec As ViewportTableRecord
            acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, _
            OpenMode.ForWrite)

 with the error stating:

unable to cast object of type...viewport to type.....viewportTableRecord

 

odd since this is coming directly from Autodesk?

 

anyways, i guess i will need to solve this before i even attempt to implement this into my code. :smileyhappy:

Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 6 of 9 (219 Views)

Re: Rotating within viewport

02-13-2014 01:17 PM in reply to: greg

Udpate:

 

Works in Model space but not through a viewport...okay..i think i can work with that....maybe

 

*Expert Elite*
norman.yuan
Posts: 1,062
Registered: ‎04-27-2009
Message 7 of 9 (206 Views)

Re: Rotating within viewport

02-13-2014 03:00 PM in reply to: greg

I am not sure I understand your question correctly: you have something in model space in certain angle. You want to see these entities in a rotated angle in viewport. For example, there is a line in 45 degree. You want to see it in a viewport as a horizantal line. That is, the viewport shows the modelspace in a twisted angle. If this is want you want, yes, you change Viewport.TwistAngle property (along with other properties, if you want to make sure the target entities being displayed in the viewport.

 

Let's say, there is a line in model space drawn in angle between 0 - 90d. On the layout "Layout1" there are 2 viewports showing the line. You now want one of the viewport twisted so that the line displayed horizontally. The following code does just that:

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(TwistViewport.MyCommands))]

namespace TwistViewport
{
    public class MyCommands
    {
        [CommandMethod("TwistVp")]
        public static void RunMyCommand()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            //Make sure current layout is paperspace
            LayoutManager.Current.CurrentLayout = "MODEL";

            //Pick line to get its angle
            Line line = GetLine(ed);
            if (line!=null)
            {
                LayoutManager.Current.CurrentLayout = "Layout1";

                using (line)
                {
                    //Pick a viewport
                    ObjectId vpId = PickViewport(ed);
                    if (!vpId.IsNull)
                    {
                        //Twist the viewport
                        TwistViewport(vpId, line);
                    }
                }
            }
            
            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }

        private static ObjectId PickViewport(Editor ed)
        {
            ObjectId id = ObjectId.Null;

            PromptEntityOptions opt = 
                new PromptEntityOptions("\nPlease pick an viewport:");
            opt.SetRejectMessage("\nInvalid pick: not a viewport.");
            opt.AddAllowedClass(typeof(Viewport), true);
            PromptEntityResult res = ed.GetEntity(opt);
            if (res.Status==PromptStatus.OK)
            {
                id = res.ObjectId;
            }
            return id;
        }

        private static Line GetLine(Editor ed)
        {
            Line line = null;

            PromptEntityOptions opt = new PromptEntityOptions("\nPick a line:");
            opt.SetRejectMessage("\nInvalid: must be a line.");
            opt.AddAllowedClass(typeof(Line), true);
            PromptEntityResult res = ed.GetEntity(opt);
            if (res.Status==PromptStatus.OK)
            {
                using (Transaction tran=
                    res.ObjectId.Database.TransactionManager.StartTransaction())
                {
                    line = (Line)tran.GetObject(res.ObjectId, OpenMode.ForRead);
                    tran.Commit();
                }
            }

            return line;
        }

        private static void TwistViewport(ObjectId vpId, Line line)
        {
            Point3d midPoint = line.GetPointAtDist(line.Length / 2.0);

            using (Transaction tran=vpId.Database.TransactionManager.StartTransaction())
            {
                Viewport vport = (Viewport)tran.GetObject(vpId, OpenMode.ForWrite);
                vport.Locked = false;
                vport.ViewDirection = Vector3d.ZAxis;
                vport.ViewTarget = midPoint;
                vport.ViewCenter = Point2d.Origin;
                vport.TwistAngle = Math.PI * 2 - line.Angle;
                //vport.Locked = true;

                tran.Commit();
            }
        }
    }
}

 The attached picture shows the effect of running the command "TwistVp".

Active Contributor
greg
Posts: 33
Registered: ‎02-13-2013
Message 8 of 9 (184 Views)

Re: Rotating within viewport

02-13-2014 07:26 PM in reply to: norman.yuan

Hi Norman,

 

Thanks a bunch!  I couldn't quite get the UCS figured out but was still working on it when I saw your post.  So I switched gears and although I was trying your method earlier, there must have been something there.  I believe it was actually the proper angle for the TwistAngle.  I didnt think to take 360 minus my angle...not sure why I didnt think of that...anyways...thank you!  Works like a charm now.  I actually had most of the code, just that angle.

 

And thank you to Drew as well for your assistance.  

Greatly appreciative of both of your ideas!

Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 9 of 9 (179 Views)

Re: Rotating within viewport

02-13-2014 07:45 PM in reply to: greg

Ah! I was totally wrong and learned something new!

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.