Creating Active Work Plane of the View using Revit API

By Saikat Bhattacharya

In some workflows, where you wish to use to the PickPoint() method to prompt users to pick a point, you might get an error stating “No work plane set in current view”.

To be able to use the PickObject() method, as the exception mentioned, we might need to create a workplane using the API. For that, we can use the active view’s origin and view’s right direction to create a plane which is then used to create a new sketch plane. This sketch plane is then assigned to the current view’s sketch plane property and now we can safely pick a point on the active workplane. The following complete code illustrates the steps.

using System;

using System.Collections.Generic;

using System.Text;

 

using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;

using Autodesk.Revit.UI;

 

namespace Revit.SDK.Samples.HelloRevit.CS

{

  [Transaction(TransactionMode.Manual)]

  public class Command : IExternalCommand

  {       

    public Result Execute(ExternalCommandData commandData,

      ref string message,

      ElementSet elements)

    {

      UIApplication uiApp = commandData.Application;

      UIDocument uiDoc = uiApp.ActiveUIDocument;

      Transaction transaction =

        new Transaction(uiDoc.Document, "WorkPlane");

      transaction.Start();

 

      Plane plane =

        new Plane(

          uiDoc.Document.ActiveView.ViewDirection,

          uiDoc.Document.ActiveView.Origin);

      SketchPlane sp = uiDoc.Document.Create.NewSketchPlane(plane);

 

      uiDoc.Document.ActiveView.SketchPlane = sp;

      uiDoc.Document.ActiveView.ShowActiveWorkPlane();

      transaction.Commit();

      return Result.Succeeded;

    } 

  }

}

<p>The above provided code can also help in general, whenever you have the need to create a work plane for the active view. 

Comments

7 responses to “Creating Active Work Plane of the View using Revit API”

  1. Hi,
    I would like to pick a point in a sheet view, but when I attempt to set the view’s SketchPlane property an exception is thrown with the following message:
    “Model lines can not be drawn in current view.”

  2. Hi Trevor
    Sorry for not getting a chance as yet to respond to this comment on the post. I have just responded to your case logged with the same query. Hope you would have received the email by now.
    cheers

  3. Hi,
    I’m also getting the same message: “Model lines can not be drawn in current view.” when trying to run the command in 3D Ortho view. Is there any work around?

  4. Hi Raul
    Sorry I dont know of any workaround that I can suggest. Regarding the exception with Sheet view, we have discussed this internally and the Development team is aware of this.

  5. Hi Saiket,
    I am also getting the same exception on Sheet view. Did you found any solution for the above. Please let me know.
    Thanks…

  6. Hey everyone,
    I was having this same problem with picking a point in a sheet and I think I figured out a workaround. By adding a line and then deleting it in a transaction before picking the point, then the workplane will be set.
    Transaction t1 = new Transaction(doc, “WorkPlane”);
    t1.Start();
    Line line1 = app.Create.NewLineBound(XYZ.Zero, XYZ.BasisX);
    DetailCurve dc1 = doc.Create.NewDetailCurve(doc.ActiveView, line1);
    ElementId id = dc1.Id;
    doc.Delete(id);
    t1.Commit();
    XYZ point = uidoc.Selection.PickPoint(“Pick a Point”);
    I tried doing a rollback but didn’t work. Seems as though it needs to be committed.

  7. Hi Mike,
    The problem is solved with your code suggestion. It’s working fine.Now I can pick a point on Sheet view.

Leave a Reply to vidyaCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading