Here is a sample macro that was uploaded to the Revit Preview forum:
/*
* Sample - Edit Floor Sketch.cs
* Created by SharpDevelop.
* User: t_matva
* Date: 11/17/2020
* Time: 11:18 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
namespace Sample
{
[Autodesk.Revit.Attributes.Transaction( Autodesk.Revit.Attributes.TransactionMode.Manual )]
[Autodesk.Revit.DB.Macros.AddInId( "994A64E6-839B-4C1F-B473-1E7C614A5455" )]
public partial class ThisDocument
{
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 CreateFloor()
{
Curve left = Line.CreateBound( new XYZ( 0, 0, 0 ), new XYZ( 0, 100, 0 ) );
Curve upper = Line.CreateBound( new XYZ( 0, 100, 0 ), new XYZ( 100, 100, 0 ) );
Curve right = Line.CreateBound( new XYZ( 100, 100, 0 ), new XYZ( 100, 0, 0 ) );
Curve lower = Line.CreateBound( new XYZ( 100, 0, 0 ), new XYZ( 0, 0, 0 ) );
CurveLoop floorProfile = new CurveLoop();
floorProfile.Append( left );
floorProfile.Append( upper );
floorProfile.Append( right );
floorProfile.Append( lower );
ElementId levelId = Level.GetNearestLevelId( Document, 0.0 );
using( Transaction transaction = new Transaction( Document ) )
{
transaction.Start( "Create floor" );
ElementId floorTypeId = Floor.GetDefaultFloorType( Document, false );
Floor floor = Floor.Create( Document, new List<CurveLoop>() { floorProfile }, floorTypeId, levelId );
transaction.Commit();
}
}
// Find a line in a sketch, delete it and create an arc in its place.
public void ReplaceBoundaryLine()
{
FilteredElementCollector floorCollector = new FilteredElementCollector( Document )
.WhereElementIsNotElementType()
.OfCategory( BuiltInCategory.OST_Floors ).OfClass( typeof( Floor ) );
Floor floor = floorCollector.FirstOrDefault() as Floor;
if( floor == null )
{
TaskDialog.Show( "Error", "Document does not contain a floor." );
return;
}
Sketch sketch = Document.GetElement( floor.SketchId ) as Sketch;
Line line = null;
foreach( CurveArray curveArray in sketch.Profile )
{
foreach( Curve curve in curveArray )
{
line = curve as Line;
if( line != null )
{
break;
}
}
if( line != null )
{
break;
}
}
if( line == null )
{
TaskDialog.Show( "Error", "Sketch does not contain a straight line." );
return;
}
// Start a sketch edit scope
SketchEditScope sketchEditScope = new SketchEditScope( Document, "Replace line with an arc" );
sketchEditScope.Start( sketch.Id );
using( Transaction transaction = new Transaction( Document, "Modify sketch" ) )
{
transaction.Start();
// Create arc
XYZ normal = line.Direction.CrossProduct( XYZ.BasisZ ).Normalize().Negate();
XYZ middle = line.GetEndPoint( 0 ).Add( line.Direction.Multiply( line.Length / 2 ) );
Curve arc = Arc.Create( line.GetEndPoint( 0 ), line.GetEndPoint( 1 ), middle.Add( normal.Multiply( 20 ) ) );
// Remove element referenced by the found line.
Document.Delete( line.Reference.ElementId );
// Model curve creation automatically puts the curve into the sketch, if sketch edit scope is running.
Document.Create.NewModelCurve( arc, sketch.SketchPlane );
transaction.Commit();
}
sketchEditScope.Commit( new FailuresPreprocessor() );
}
/// <summary>
/// Add new profile to the sketch.
/// </summary>
public void MakeHole()
{
FilteredElementCollector floorCollector = new FilteredElementCollector( Document )
.WhereElementIsNotElementType()
.OfCategory( BuiltInCategory.OST_Floors ).OfClass( typeof( Floor ) );
Floor floor = floorCollector.FirstOrDefault() as Floor;
if( floor == null )
{
TaskDialog.Show( "Error", "Document does not contain a floor." );
return;
}
Sketch sketch = Document.GetElement( floor.SketchId ) as Sketch;
// Create a circle inside the floor
// Start a sketch edit scope
SketchEditScope sketchEditScope = new SketchEditScope( Document, "Add profile to the sketch" );
sketchEditScope.Start( sketch.Id );
using( Transaction transaction = new Transaction( Document, "Make a hole" ) )
{
transaction.Start();
// Create and add an ellipse
Curve circle = Ellipse.CreateCurve( new XYZ( 50, 50, 0 ), 10, 10, XYZ.BasisX, XYZ.BasisY, 0, 2 * Math.PI );
// Model curve creation automatically puts the curve into the sketch, if sketch edit scope is running.
Document.Create.NewModelCurve( circle, sketch.SketchPlane );
transaction.Commit();
}
sketchEditScope.Commit( new FailuresPreprocessor() );
}
}
public class FailuresPreprocessor : IFailuresPreprocessor
{
public FailureProcessingResult PreprocessFailures(
FailuresAccessor failuresAccessor )
{
return FailureProcessingResult.Continue;
}
}
I hope this helps.