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

Rotating within viewport

8 REPLIES 8
Reply
Message 1 of 9
greg
1830 Views, 8 Replies

Rotating within viewport

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!

8 REPLIES 8
Message 2 of 9
architectOfIdeas
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

Message 3 of 9
greg
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. 🙂

 

Thanks!

Message 4 of 9
architectOfIdeas
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

Message 5 of 9
greg
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. 🙂

Message 6 of 9
greg
in reply to: greg

Udpate:

 

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

 

Message 7 of 9
norman.yuan
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".

Message 8 of 9
greg
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!

Message 9 of 9
architectOfIdeas
in reply to: greg

Ah! I was totally wrong and learned something new!

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