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".