2023 Method
Changes 3
M

SketchEditScope.StartWithNewSketch

Description:
Starts a sketch edit mode for an element which, at this moment, doesn't have a sketch.
Remarks:
Some surface Revit elements (like some Walls or some Analytical Elements) does not have a valid sketch all the time so in order to edit them, we have to create a valid sketch first. The application will need to start a transaction to actually make changes to the element. SketchEditScope can only be started when there is no transaction active, thus it does not work for commands running in automatic transaction mode.
public void StartWithNewSketch(
	ElementId elementId
)
Document document = commandData.Application.ActiveUIDocument.Document;

//create analytical panel
AnalyticalPanel analyticalPanel = CreateAnalyticalPanel.CreateAMPanel(document);
if (analyticalPanel != null)
{
   // Start a sketch edit scope
   SketchEditScope sketchEditScope = new SketchEditScope(document, "Replace line with an arc");
   sketchEditScope.StartWithNewSketch(analyticalPanel.Id);

   using (Transaction transaction = new Transaction(document, "Modify sketch"))
   {
      transaction.Start();

      //replace a boundary line with an arc
      Line line = null;
      Sketch sketch = document.GetElement(analyticalPanel.SketchId) as Sketch;
      if (sketch != null)
      {
         foreach (CurveArray curveArray in sketch.Profile)
         {
            foreach (Curve curve in curveArray)
            {
               line = curve as Line;
               if (line != null)
               {
                  break;
               }
            }
            if (line != null)
            {
               break;
            }
         }
      }

      // 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 FailurePreproccessor());

}

return Result.Succeeded;