4

I have an object which I draw in AutoCAD from my program. After the object is drawn I have the camera set so that it zooms to the wall looking down from the top view. What I then want it to do is to orbit 45 degrees down toward front view and 45 degrees left toward left view. This essentially mimics selecting the object in AutoCAD and then clicking the view cube.

enter image description here

Here is my orbit method.

/// <summary>
/// Orbit the angle around a passed axis
/// </summary>
public static void Orbit(Vector3d axis, Point3d pivotPoint, Angle angle)
{
    Editor activeEditor = AcadApp.DocumentManager.MdiActiveDocument.Editor; //Get editor for current document
    ViewTableRecord activeView = activeEditor.GetCurrentView(); //Get current view table
    activeView.ViewDirection = activeView.ViewDirection.TransformBy(Matrix3d.Rotation(angle.Radians, axis, pivotPoint)); //Adjust the ViewTableRecord
    activeEditor.SetCurrentView(activeView); //Set it as the current view
}

And here is how I call the orbit method

CameraMethods.Orbit(Vector3d.XAxis, GeometryAdapter.ClearspanPointToAcadPoint(wallToZoomTo.FrontLine.MidPoint), new Angle(AngleType.Radian, Math.PI / 4));
CameraMethods.Orbit(Vector3d.ZAxis, GeometryAdapter.ClearspanPointToAcadPoint(wallToZoomTo.FrontLine.MidPoint), new Angle(AngleType.Radian, Math.PI / 4));

The problem is that when I pass in the midpoint of the wall object it orbits the camera so that it is very far away (way up in the upper left part of the view somewhere)

Does anyone have a way to easily orbit around a chosen object in AutoCAD via C#? Thanks in advance!

Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90

1 Answers1

0

Try this:

[CommandMethod("MYORBIT")]
public void MyOrbit()
{
  Document doc = Application.DocumentManager.MdiActiveDocument;
  Database db = doc.Database;
  Editor ed = doc.Editor;

  PromptPointResult ppr = ed.GetPoint("\nSelect orbit point: ");
  if (ppr.Status == PromptStatus.Cancel) return;

  using (Transaction tr = db.TransactionManager.StartTransaction())
  {
    short cvport = (short)Application.GetSystemVariable("CVPORT");
    using (Manager gm = doc.GraphicsManager)
    using (var kd = new KernelDescriptor())
    {
      kd.addRequirement(Autodesk.AutoCAD.UniqueString.Intern("3D Drawing"));
      using (View view = gm.ObtainAcGsView(cvport, kd))
      {
        double d = view.Position.DistanceTo(view.Target);
        view.SetView(ppr.Value + new Vector3d(-1, -1, 1).GetNormal() * d,
          ppr.Value, Vector3d.ZAxis, view.FieldWidth, view.FieldHeight);
        gm.SetViewportFromView(cvport, view, true, true, true);
      }
    }

    // Needed if wireframe 2D
    ed.Regen();

    tr.Commit();
  }
}
Maxence
  • 12,868
  • 5
  • 57
  • 69