Skip to content

Instantly share code, notes, and snippets.

@ricaun
Created June 7, 2023 20:43
Show Gist options
  • Save ricaun/35baa2ed9f33de3487e46e4217b5e8bd to your computer and use it in GitHub Desktop.
Save ricaun/35baa2ed9f33de3487e46e4217b5e8bd to your computer and use it in GitHub Desktop.
TessellatedShapeCreatorUtils Revit sample code with Command to create a D4
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
namespace RevitAddin.Commands
{
[Transaction(TransactionMode.Manual)]
public class CommandTessellatedD4 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
{
UIApplication uiapp = commandData.Application;
Document document = uiapp.ActiveUIDocument.Document;
var shapeBuilder = TessellatedShapeCreatorUtils.Create(builder =>
{
var points = new List<XYZ>(4);
points.Add(new XYZ(0, 0, 0)); // 0 origin
points.Add(new XYZ(1, 0, 0)); // 1 right
points.Add(new XYZ(0, 1, 0)); // 2 back
points.Add(new XYZ(0, 0, 1)); // 3 top
var materialId = ElementId.InvalidElementId;
builder.AddFace(new TessellatedFace(new[] { points[2], points[1], points[0] }, materialId)); // bottom face
builder.AddFace(new TessellatedFace(new[] { points[0], points[1], points[3] }, materialId)); // front face
builder.AddFace(new TessellatedFace(new[] { points[1], points[2], points[3] }, materialId)); // diagonal face
builder.AddFace(new TessellatedFace(new[] { points[2], points[0], points[3] }, materialId)); // left face
});
using (Transaction transaction = new Transaction(document))
{
transaction.Start("Create");
var ds = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_GenericModel));
ds.SetName(ds.Category.Name);
ds.SetShape(shapeBuilder.GetGeometricalObjects());
transaction.Commit();
}
return Result.Succeeded;
}
}
public static class TessellatedShapeCreatorUtils
{
public static TessellatedShapeBuilderResult Create(Action<TessellatedShapeBuilder> actionBuilder)
{
TessellatedShapeBuilder builder = new TessellatedShapeBuilder();
builder.Target = TessellatedShapeBuilderTarget.AnyGeometry;
builder.Fallback = TessellatedShapeBuilderFallback.Mesh;
builder.OpenConnectedFaceSet(true);
actionBuilder?.Invoke(builder);
builder.CloseConnectedFaceSet();
builder.Build();
TessellatedShapeBuilderResult result = builder.GetBuildResult();
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment