I’ve taken a few days off to visit some very old friends who are back in the UK from New Zealand for the festive season, but thought I’d go ahead and share some code I’ve been playing with, even if I can’t actually tell you what it’s for.
I can tell you what it does, of course, but I’m using it in conjunction with some other capabilities that I’m probably not allowed to talk about explicitly. Feel free to speculate at your leisure, I may or may not be able to confirm or deny your suspicions (I need to check on that before being more clear).
The implementation encapsulates the ability to zoom, pan or orbit the current view in AutoCAD. This code is part of a larger proof of concept – and is therefore likely to go through some updates in the very near future – but it seemed likely to be at least of some use as it currently stands.
Here’s the C# code – I won’t show the commands in action, but it should be simple enough to try them out yourself:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
namespace CameraTest
{
public class Camera
{
// Members
private Document _doc = null;
private ViewTableRecord _vtr = null;
private ViewTableRecord _initial = null;
public Camera(Document doc)
{
_doc = doc;
_initial = doc.Editor.GetCurrentView();
_vtr = (ViewTableRecord)_initial.Clone();
}
// Reset to the initial view
public void Reset()
{
_doc.Editor.SetCurrentView(_initial);
_doc.Editor.Regen();
}
// Zoom in or out
public void Zoom(double factor)
{
// Adjust the ViewTableRecord
_vtr.Height *= factor;
_vtr.Width *= factor;
// Set it as the current view
_doc.Editor.SetCurrentView(_vtr);
// Zoom requires a regen for the gizmos to update
_doc.Editor.Regen();
}
// Pan in the specified direction
public void Pan(double leftRight, double upDown)
{
// Adjust the ViewTableRecord
_vtr.CenterPoint =
_vtr.CenterPoint + new Vector2d(leftRight, upDown);
// Set it as the current view
_doc.Editor.SetCurrentView(_vtr);
}
// Orbit by angle around axis
public void Orbit(Vector3d axis, double angle)
{
// Adjust the ViewTableRecord
_vtr.ViewDirection =
_vtr.ViewDirection.TransformBy(
Matrix3d.Rotation(angle, axis, Point3d.Origin)
);
// Set it as the current view
_doc.Editor.SetCurrentView(_vtr);
}
}
public class Commands
{
[CommandMethod("ZIN")]
public void ZoomIn()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Zoom(0.9);
}
[CommandMethod("ZOUT")]
public void ZoomOut()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Zoom(1.1);
}
[CommandMethod("PLEFT")]
public void PanLeft()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Pan(-1, 0);
}
[CommandMethod("PRIGHT")]
public void PanRight()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Pan(1, 0);
}
[CommandMethod("PUP")]
public void PanUp()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Pan(0, 1);
}
[CommandMethod("PDOWN")]
public void PanDown()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Pan(0, -1);
}
[CommandMethod("OLEFT")]
public void OrbitLeft()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Orbit(Vector3d.ZAxis, Math.PI / 12);
}
[CommandMethod("ORIGHT")]
public void OrbitRight()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Orbit(Vector3d.ZAxis, -Math.PI / 12);
}
[CommandMethod("OUP")]
public void OrbitUp()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Orbit(Vector3d.XAxis, Math.PI / 12);
}
[CommandMethod("ODOWN")]
public void OrbitDown()
{
var cam =
new Camera(Application.DocumentManager.MdiActiveDocument);
cam.Orbit(Vector3d.XAxis, -Math.PI / 12);
}
}
}
With any luck I’ll get the chance to post at least once more before our annual “Week of Rest”, once I'm back home in Switzerland.