/ (C) CodeHimBelonga kdub 2012.06.15
// Revised 2012.09.09
// http://www.theswamp.org/index.php?topic=41937.0
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
//using MgdAcadApp = Autodesk.AutoCAD.ApplicationServices.Application; // <= ac2012
using MgdAcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application; // == ac2013
using Exception = System.Exception;
[assembly
: ExtensionApplication
(typeof (ViewTest
.MyViewCommandsStartup))] [assembly
: CommandClass
(typeof (ViewTest
.MyViewCommands))]
namespace ViewTest
{
public class MyViewCommands
{
private Database db;
private Document doc;
private Editor ed;
public MyViewCommands()
{
ActiveDoc = MgdAcadApp.DocumentManager.MdiActiveDocument;
}
private Document ActiveDoc
{
get { return doc; }
set
{
doc = value;
if (doc == null) {
db = null;
ed = null;
}
else {
db = doc.Database;
ed = doc.Editor;
}
}
}
//------------------------------------------------------------------
[CommandMethod("iso5030")]
public void iso5030()
{
BuildNewView(viewName: "iso5030", angleInXyPlane: 50, angleFromXyPlane: 30, zoomE: true);
}
//------------------------------------------------------------------
[CommandMethod("iso13030")]
public void iso13030()
{
BuildNewView(viewName: "iso13030", angleInXyPlane: 130, angleFromXyPlane: 30, zoomE: true);
}
//------------------------------------------------------------------
[CommandMethod("iso23030")]
public void iso23030()
{
BuildNewView(viewName: "iso23030", angleInXyPlane: 230, angleFromXyPlane: 30, zoomE: true);
}
//------------------------------------------------------------------
[CommandMethod("iso27030")]
public void iso27030()
{
BuildNewView(viewName: "iso27030", angleInXyPlane: 270, angleFromXyPlane: 30, zoomE: true);
}
//------------------------------------------------------------------
[CommandMethod("iso31030")]
public void iso31030()
{
BuildNewView(viewName: "iso31030", angleInXyPlane: 310, angleFromXyPlane: 30, zoomE: true);
}
//------------------------------------------------------------------
[CommandMethod("isoPrompt")]
public void isoPrompt()
{
PromptStringOptions pso
= new PromptStringOptions
("\nEnter VIEW name"); pso.AllowSpaces = true;
PromptResult psr = ed.GetString(pso);
if ((psr.Status != PromptStatus.OK) || (psr.StringResult == string.Empty)){
return;
}
PromptDoubleOptions pdo1
= new PromptDoubleOptions
("\nEnter angle in XY plane from X axis (deg): "); PromptDoubleResult pdr1 = ed.GetDouble(pdo1);
if (pdr1.Status != PromptStatus.OK) {
return;
}
PromptDoubleOptions pdo2
= new PromptDoubleOptions
("\nEnter angle from XY plane (deg): "); PromptDoubleResult pdr2 = ed.GetDouble(pdo2);
if (pdr2.Status != PromptStatus.OK) {
return;
}
PromptKeywordOptions pko
= new PromptKeywordOptions
("\nZoom Extents?"); pko.AllowNone = false;
pko.Keywords.Add("Yes");
pko.Keywords.Add("No");
pko.Keywords.Default = "Yes";
PromptResult pkr = ed.GetKeywords(pko);
if (pkr.Status != PromptStatus.OK) {
return;
}
BuildNewView(viewName: (psr.StringResult),
angleInXyPlane: (pdr1.Value),
angleFromXyPlane: (pdr2.Value),
zoomE: (pkr.StringResult == "Yes") );
}
//------------------------------------------------------------------
private void BuildNewView(string viewName, double angleInXyPlane, double angleFromXyPlane, bool zoomE)
{
using (Transaction tr = db.TransactionManager.StartTransaction()) {
ViewTable vt = tr.GetObject(db.ViewTableId, OpenMode.ForRead) as ViewTable;
ViewTableRecord newView = ed.GetCurrentView();
newView.ViewDirection = ComputeViewDirection(DegToRad(angleInXyPlane),
DegToRad(angleFromXyPlane));
newView.Name = viewName;
if (zoomE) {
db.UpdateExt(true);
Extents3d extents
= new Extents3d
(db
.Extmin, db
.Extmax); SetViewToExtents(extents, newView);
}
if (!vt.Has(viewName)) {
vt.UpgradeOpen();
vt.Add(newView);
tr.AddNewlyCreatedDBObject(newView, true);
}
using (ViewTableRecord vtr
= new ViewTableRecord
()) { vtr.CopyFrom(newView);
ed.SetCurrentView(vtr);
}
tr.Commit();
}
}
//------------------------------------------------------------------
private Vector3d ComputeViewDirection(double angleInXyPlane, double angleFromXyPlane)
{
Vector3d v1 = Vector3d.XAxis.RotateBy(angleInXyPlane, Vector3d.ZAxis);
Vector3d v2 = v1.CrossProduct(Vector3d.ZAxis);
Vector3d viewDir = v1.RotateBy(angleFromXyPlane, v2);
return viewDir;
}
//------------------------------------------------------------------
private void SetViewToExtents(Extents3d extents, AbstractViewTableRecord view)
{
// Get the screen aspect ratio to calculate the height and width
double scrRatio = (view.Width/view.Height);
// Prepare Matrix for DCS to WCS transformation
// For DCS target point is the origin
// WCS Xaxis is twisted by twist angle
// Tranform the extents to the DCS defined by the viewdir
Matrix3d matWCS2DCS = Matrix3d.PlaneToWorld(view.ViewDirection);
matWCS2DCS = Matrix3d.Displacement(view.Target - Point3d.Origin)*matWCS2DCS;
matWCS2DCS = Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target)*matWCS2DCS;
matWCS2DCS = matWCS2DCS.Inverse();
extents.TransformBy(matWCS2DCS);
// Width of the extents in current view
// Height of the extents in current view
// Get the view center point
double width = (extents.MaxPoint.X - extents.MinPoint.X);
double height = (extents.MaxPoint.Y - extents.MinPoint.Y);
Point2d center
= new Point2d
((extents
.MaxPoint.X + extents
.MinPoint.X)*0.5,
(extents.MaxPoint.Y + extents.MinPoint.Y)*0.5);
// Check if the width 'fits' in current window
// if not then get the new height as per
// the viewports aspect ratio
if (width > (height*scrRatio)) {
height = width/scrRatio;
}
view.Height = height;
view.Width = height*scrRatio;
view.CenterPoint = center;
}
//------------------------------------------------------------------
private double DegToRad(double deg)
{
return deg*Math.PI/180.0;
}
//------------------------------------------------------------------
}
//=======================================================================
//=======================================================================
public class MyViewCommandsStartup : IExtensionApplication
{
#region IExtensionApplication Members
public void Initialize()
{
try {
MgdAcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"\nMyViewCommands: \n{0}, {1}, {2}, {3}, {4}, {5}",
"iso5030", "iso13030", "iso23030", "iso27030", "iso31030", "isoPrompt"
);
}
catch (Exception e) {
MgdAcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"\n\nError in {0}.Initialize():\n{1}", GetType().FullName, e.ToString());
}
}
public void Terminate()
{
//TODO: add code to clean up things when the ExtApp terminates.
}
#endregion
}
}