使用C#模拟AutoCAD视图的行为

时间:2015-08-08 19:11:53

标签: c# autocad autocad-plugin

我有一个从我的程序中用AutoCAD绘制的对象。绘制对象后,我将相机设置为从顶视图向下俯视墙。我当时想要它做的是向前视图向下45度,向左视图向左45度。这基本上模仿了在AutoCAD中选择对象,然后单击视图立方体。

enter image description here

这是我的轨道方法。

/// <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
}

以下是我如何调用轨道方法

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));

问题在于,当我通过墙体物体的中点时,它会绕着相机运行,使它离得很远(在某处的视图的左上部分向上)

有没有人可以通过C#轻松绕过AutoCAD中所选对象的轨道?提前谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

[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();
  }
}