Drafting View Work Plane Toggle & Spacing Setting

Hi, all.

We are trying to write a script for a tool that runs in drafting views, which basically toggles the work plane on and off (the same as going to Systems tab > Work Plane panel > Show button), and also sets the spacing of the workplane to 0.2". We got the script below, which is simple enough but somehow won’t work:

# Import necessary libraries
from pyrevit import revit, DB

# Get the current document
doc = revit.doc

# Get the active view
active_view = doc.ActiveView

# Check if the work plane is visible
if active_view.ShowActiveWorkPlane:
    # If the work plane is visible, hide it
    active_view.HideActiveWorkPlane()
else:
    # If the work plane is hidden, show it and set the spacing to 0.2"
    active_view.ShowActiveWorkPlane()
    # TODO: set spacing to 0.2"

We get errors saying there’s no “scketchPlane” in the view, and other similar errors, depending on how we change the script. We’ve been going to the API docs to review methods, and it seems we are in the right track…

Could we get a hand on this script? If we get the toggling to work, could we also have it set the spacing to 0.2"? I looked around the API docs for something like that but couldn’t find methods that seem to do that…

Thanks in advance,

Edgar

Hi,
My guess is that drafting view do not have a workplane, even though they inherit Show and Hide Active Work Plane methods.

ShowActiveWorkPlane() method

The general code below would work in most cases, not in drafting views.

from pyrevit import revit, DB

doc = revit.doc

active_view = doc.ActiveView

with revit.Transaction('set active workplane'):
    # active_view.HideActiveWorkPlane()
    active_view.ShowActiveWorkPlane()
    # get workplane:
    wp = DB.FilteredElementCollector(doc,active_view.Id).OfCategory(DB.BuiltInCategory.OST_IOSSketchGrid).ToElements()[0]
    wp.get_Parameter(DB.BuiltInParameter.SKETCH_GRID_SPACING_PARAM).Set(1) # 1 is in decimal feet, like revit internal unit system

As a general rule, if the functionality is not available in the UI, it is not available in the API.

To further expand the discussion, the sketch grid is available in sheets where you place drafting view.
You would need to

  1. create or use the existing sketch grid of the sheet,
  2. activate it,
  3. set its spacing

Hi, Jean-Marc. Thanks first for always being available to help us here in this forum :+1:

I agree with the general rule - we probably wouldn’t pursue something like this unless it was part of the UI or user experience - which is just the thing - work planes are indeed available for the users on Drafting Views through the user interface:

It is very useful when you want to generate schematic drawings and have the content neatly spaced and more easily readable (and it looks much more professional anyway). Once we turn it on, it comes on with a generic spacing and so we have to then select the work plane (it’s selectable by clicking on its boundary/edges) and change its “Spacing” property to 0.2 inches. It would be ideal if we could have a button that does all that on the same tab with all of our schematic tools and symbols.

I tried employing some AI yesterday and got this, which also does not work but might help in tuning down on something that might (not sure). Btw, AI might have promises, but I much prefer human-generated code, or at least human-fine-tuned code, which is why I come to you here in the forums:

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')

from Autodesk.Revit.DB import Document, Transaction
from Autodesk.Revit.UI import UIApplication

# Function to check if the work plane is visible
def is_work_plane_visible(uiapp):
    return uiapp.ActiveUIDocument.GetOpenUIViews()[0].WorkPlaneVisibility

# Function to toggle the work plane visibility
def toggle_work_plane(uiapp, visible):
    for view in uiapp.ActiveUIDocument.GetOpenUIViews():
        view.WorkPlaneVisibility = visible

# Function to set work plane grid spacing
def set_work_plane_grid_spacing(doc, spacing):
    with Transaction(doc, "Set Work Plane Grid Spacing") as trans:
        trans.Start()
        doc.ActiveView.SketchPlane.GetPlane().GridSpacing = spacing
        trans.Commit()

# Main function
def main():
    # Get the active UIApplication
    uiapp = UIApplication(__revit__.Application)
    doc = __revit__.ActiveUIDocument.Document

    # Check if the work plane is visible
    if is_work_plane_visible(uiapp):
        # Work plane is on, toggle it off
        toggle_work_plane(uiapp, False)
    else:
        # Work plane is off, toggle it on and set grid spacing
        toggle_work_plane(uiapp, True)
        set_work_plane_grid_spacing(doc, 0.2)  # Grid spacing set to 0.2 inches

# Execute the main function
main()

Again, thanks in advance,

Edgar