Skip to content

Instantly share code, notes, and snippets.

@ricaun
Last active March 10, 2023 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricaun/5145ce37c3bbf0613930c8d167d4ab73 to your computer and use it in GitHub Desktop.
Save ricaun/5145ce37c3bbf0613930c8d167d4ab73 to your computer and use it in GitHub Desktop.
RevitAddin Command to edit a selected DirectShape, if DirectShape has a DirectShapeType the type is edited instead.
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Linq;
namespace RevitAddin.Commands
{
[Transaction(TransactionMode.Manual)]
public class CommandShapeEdit : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document document = uidoc.Document;
View view = uidoc.ActiveView;
Selection selection = uidoc.Selection;
var elements = selection.GetElementIds().Select(id => document.GetElement(id));
var shapes = elements.OfType<DirectShape>();
using (Transaction transaction = new Transaction(document))
{
transaction.Start("Shape Edit");
var lines = new[] {
Line.CreateBound(-XYZ.BasisX, XYZ.BasisX),
Line.CreateBound(-XYZ.BasisY, XYZ.BasisY),
Line.CreateBound(-XYZ.BasisZ, XYZ.BasisZ)
};
foreach (var shape in shapes)
{
if (document.GetElement(shape.TypeId) is DirectShapeType shapeType)
{
shapeType.SetShape(lines);
continue;
}
shape.SetShape(lines);
}
transaction.Commit();
}
return Result.Succeeded;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment