Create 2D Section

I am working on this code, and I can make it work with the Create Section method. However, what I need is for this code to create a 2D section from the left or bottom of the conduit, depending on orientation. When i try the Create Detail method, I must not be passing the right arguements because I get an error. I am new to pyrevit and the revit api in general. Any help would be greatly appreciated!

import clr
import Autodesk.Revit.DB as DB
from pyrevit import revit, forms

# Prompt user to select a conduit
selection = revit.pick_elements()

if selection:
    conduit = selection[0]  # Select the first element if multiple are picked

    # Acquire the bounding box of the conduit
    bbox = conduit.get_BoundingBox(None)
    min_point = bbox.Min
    max_point = bbox.Max

    # Create a 2D section view
    view_family_type = revit.doc.GetDefaultElementTypeId(DB.ElementTypeGroup.ViewTypeSection)
    with revit.Transaction("Create 2D Section"):
        section_view = DB.ViewSection.CreateSection(revit.doc, view_family_type, bbox)
        section_view.Name = "Conduit Section"

    print("2D Section view created successfully.")
else:
    print("No conduit selected.")

Your code actually works

import pyrevit
from pyrevit import DB, revit

selection = revit.pick_element()
if selection:
    conduit = selection

    bbox = conduit.get_BoundingBox(None)
    min_point = bbox.Min
    max_point = bbox.Max

    view_family_type = \
        revit.doc.GetDefaultElementTypeId(DB.ElementTypeGroup.ViewTypeSection)
    with revit.Transaction("Create 2D Section"):
        section_view = DB.ViewSection.CreateSection(revit.doc, view_family_type, bbox)
        section_view.Name = "Conduit Section"
else:
    pass

This is what I have tested and all seems ok.

It does but it only makes a plan view section. I’m not sure how exactly, but I need to make a section from the side. I have looked into transforming the bounding box so it looks at the element from the side but haven’t managed to make that work.

Hi @codyjh16 ,

if you look at the remarks section of the CreateSection documentation, it states (BoundingBoxXYZ is your bbox):

Create a section whose view volume corresponds geometrically with the specified sectionBox.
The view direction of the resulting section will be sectionBox.Transform.BasisZ and the up direction will be sectionBox.Transform.BasisY.
The right hand direction will be computed so that (right, up, view direction) form a left handed coordinate system.
The resulting view will be cropped, and far clipping will be active.
The crop region will correspond to the projections of BoundingBoxXYZ.Min and BoundingBoxXYZ.Max onto the view’s cut plane.
The far clip distance will be equal to the difference of the z-coordinates of BoundingBoxXYZ.Min and BoundingBoxXYZ.Max.
The new section ViewSection will receive a unique view name.

So you have to pass in a bounding box that is already oriented in the direction you need.
if you already have a view aligned to the item, you can pass it to the get_BoundingBox method instead of None.
From Conduit.BoundingBox remarks:

Pass in a view to query view-specific (e.g., cut) geometry or a null reference (None) for model geometry.

@codyjh16
Maybe this can help

Also in pyrevit there is tool to create section for walls, maybe reading thecode will also beneficial.

Hello, I am currently in the same situation except that the cut that is created does not sit on the side of the element, but underneath it (see screenshot taken from the side of the element).