By Joe Ye
A sketch and a sketch plane are created when creating a floor, roof, and filled region et.al. I want to retrieve the sketch and sketch plane. However there is no API to do that . Would you please tell me how to find the two objects?
Solution
When creating a floor, slab and filled region et.al, users need to draw a profile to define the element's shape. Revit automatically creates a sketch and sketch plane for the profile. If the floor, slab or filled region is deleted, the corresponding sketch and sketch plane will be deleted accordingly.
Document.Delete() method returns all deleted elements' Id. Using this mechanism, we can figure out this solution.
Start a transaction, and delete the target element. The sketch and sketch plane Id should be in the returned element id list. Filter out the target elements by class name. Then rollback the transaction to keep the target element unchanged.
Here is the code showing the usage.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit .DB;
using Autodesk.Revit.UI;
using Autodesk.Revit .ApplicationServices;
using Autodesk.Revit.Attributes ;
using Autodesk.Revit.UI.Selection;
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string messages,
ElementSet elements)
{
UIApplication app = commandData.Application;
UIDocument uiDoc = app.ActiveUIDocument;
Document doc = uiDoc.Document;
Selection sel = uiDoc.Selection;
Reference ref1 = sel.PickObject(
ObjectType.Element, "Please pick an element");
Element elem = ref1.Element;
Transaction trans = new Transaction(doc);
trans.Start("test");
ICollection<ElementId> ids = doc.Delete(elem);
trans.RollBack();
string strName = "Host Id = " +
elem.Id.IntegerValue.ToString() + "\r\n";
foreach (ElementId id in ids)
{
Element elemIter = doc.get_Element(id);
if (elemIter is Sketch || elemIter is SketchPlane)
{
strName += "Id = " + id.IntegerValue.ToString()
+ ";" + elemIter.Name
+ " ;" + elemIter.GetType().ToString() + "\r\n";
}
}
TaskDialog.Show("test", strName);
return Result.Succeeded ;
}
}
Using this approach, elements that are closing related with a specified element can be retrieved too. For example, the detailed lines that forms the profile can also be found.