Create a Section View

This Sharp Macro code will create a section view around an element when the element is selected in a plan view or a elevation view. (Revit 2021).

// Create a Section View around an element;

using System;

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

using Autodesk.Revit.UI.Selection;

using System.Collections.Generic;

using System.Linq;

namespace CreateDetailView

{

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

[Autodesk.Revit.DB.Macros.AddInId("D560A7DD-E15F-47F4-8220-C3961C41336F")]

public partial class ThisApplication

{

private void Module_Startup(object sender, EventArgs e)

{


}


private void Module_Shutdown(object sender, EventArgs e)

{


}


#region Revit Macros generated code

private void InternalStartup()

{

this.Startup += new System.EventHandler(Module_Startup);

this.Shutdown += new System.EventHandler(Module_Shutdown);

}

#endregion

public void createdetail()

{


UIDocument uidoc = this.ActiveUIDocument; //dont need this

Document document = this.ActiveUIDocument.Document;

View view = uidoc.ActiveView;

// Get the element selection of current document

Selection selection = uidoc.Selection;

//store element id's

ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();

BoundingBoxXYZ box = null;

foreach (ElementId id in selectedIds)

{

Element e = document.GetElement(id);

box = e.get_BoundingBox(view);

}

// Create a new view section.

ElementId DetailViewId = ElementId.InvalidElementId;

IList<Element> elems = new FilteredElementCollector(document).OfClass(typeof(ViewFamilyType)).ToElements();

foreach (Element e in elems)

{

ViewFamilyType v = e as ViewFamilyType;

if (v != null && v.ViewFamily == ViewFamily.Detail)

{

DetailViewId = e.Id;

break;

}

}

using (Transaction trans = new Transaction(document, "viewsection"))

{

trans.Start();

ViewSection viewSection = ViewSection.CreateDetail(document, DetailViewId, box);

trans.Commit ();

ICollection<ElementId> refCallouts = viewSection.GetReferenceCallouts();

TaskDialog.Show("Revit", refCallouts.Count.ToString());

foreach (ElementId eleid in refCallouts)

{

TaskDialog.Show("One"," Text");

}

}

}