From b321a7270d391d188a4d553e38019622354c3a72 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Fri, 14 Jun 2024 16:12:10 -0300 Subject: [PATCH] Add `CreateLines` in `ShapeCreator` --- CHANGELOG.md | 1 + Directory.Build.props | 2 +- ricaun.Revit.DB.Shape/ShapeCreator.cs | 58 ++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e3823..0967f6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add `ColorExtension` with `ColorEquals`, `Lerp`, `ToColor` and `ToColorWithTransparency`. - Add `ColorExtension` with `ToHex` for `Color` and `ColorWithTransparency` - Update `CreateBoxLines` with `graphicsStyleId` +- Add `CreateLines` in `ShapeCreator` ### Tests - Test all color names in `System.Windows.Media.Colors` with `Colors`. - Test all 9 colors in `MaterialUtils` and `GraphicsStyleUtils`. diff --git a/Directory.Build.props b/Directory.Build.props index e576d89..35b57a8 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 0.3.2-rc.3 + 0.3.2-rc.4 2024 2025 true diff --git a/ricaun.Revit.DB.Shape/ShapeCreator.cs b/ricaun.Revit.DB.Shape/ShapeCreator.cs index d7b2cf3..261387e 100644 --- a/ricaun.Revit.DB.Shape/ShapeCreator.cs +++ b/ricaun.Revit.DB.Shape/ShapeCreator.cs @@ -15,7 +15,59 @@ public static class ShapeCreator /// Default Sides for Prisms and Pyramids. /// internal const int Sides = 3; - #region Box + + #region Lines + /// + /// Creates an array of objects from a collection of points. + /// + /// A collection of points to create lines from. + /// + /// A boolean value indicating whether the lines should form a closed loop. + /// If true, an additional line will be created from the last point back to the first point. + /// + /// + /// An array of objects representing the lines created from the specified points. + /// + public static Line[] CreateLines(IEnumerable points, bool closed) + { + return CreateLines(points, null, closed); + } + /// + /// Creates an array of objects from a collection of points. + /// + /// A collection of points to create lines from. + /// + /// An optional representing the graphics style to apply to the lines. + /// If null, the default graphics style will be used. + /// + /// + /// A boolean value indicating whether the lines should form a closed loop. + /// If true, an additional line will be created from the last point back to the first point. + /// + /// + /// An array of objects representing the lines created from the specified points. + /// + public static Line[] CreateLines(IEnumerable points, ElementId graphicsStyleId = null, bool closed = false) + { + var lines = new List(); + var pointsArray = points.ToArray(); + + for (int i = 0; i < pointsArray.Length - 1; i++) + { + var line = CreateBound(pointsArray[i], pointsArray[i + 1], graphicsStyleId); + if (line is not null) + lines.Add(line); + } + + if (closed) + { + var line = CreateBound(pointsArray[0], pointsArray[pointsArray.Length - 1], graphicsStyleId); + if (line is not null) + lines.Add(line); + } + + return lines.ToArray(); + } /// /// Creates an array of lines representing the edges of a 3D box with the specified minimum and maximum points. /// @@ -64,7 +116,7 @@ public static Line[] CreateBoxLines(XYZ min, XYZ max, ElementId graphicsStyleId return lines.OfType().ToArray(); } - private static Line CreateBound(XYZ point1, XYZ point2, ElementId graphicsStyleId = null) + internal static Line CreateBound(XYZ point1, XYZ point2, ElementId graphicsStyleId = null) { try { @@ -78,6 +130,8 @@ private static Line CreateBound(XYZ point1, XYZ point2, ElementId graphicsStyleI catch { } return null; } + #endregion + #region Box /// /// Creates a 3D box with the specified center and size. ///