|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Autodesk.DesignScript.Runtime; |
| 5 | +using Autodesk.Revit.DB; |
| 6 | +using BoundingBox = Autodesk.DesignScript.Geometry.BoundingBox; |
| 7 | +using PolyCurve = Autodesk.DesignScript.Geometry.PolyCurve; |
| 8 | + |
| 9 | +namespace Revit.GeometryConversion |
| 10 | +{ |
| 11 | + |
| 12 | + [SupressImportIntoVM] |
| 13 | + internal static class CurveLoopPartition |
| 14 | + { |
| 15 | + public static List<CurvePartition> ByCurveLoops(List<List<Curve>> curveLoops) |
| 16 | + { |
| 17 | + if (curveLoops.Count == 1) |
| 18 | + return new List<CurvePartition>() |
| 19 | + { |
| 20 | + new CurvePartition(){ OuterCurves = curveLoops[0] } |
| 21 | + }; |
| 22 | + |
| 23 | + var tesselatedCurveLoops = new List<Tuple<BoundingBox, List<XYZ>, List<Curve>>>(); |
| 24 | + foreach (var curveLoop in curveLoops) |
| 25 | + { |
| 26 | + var verts = TesselateCurveLoop(curveLoop).ToList(); |
| 27 | + List<Autodesk.DesignScript.Geometry.Curve> curves = |
| 28 | + new List<Autodesk.DesignScript.Geometry.Curve>(); |
| 29 | + foreach (var curve in curveLoop) |
| 30 | + { |
| 31 | + curves.Add(curve.ToProtoType(false)); |
| 32 | + } |
| 33 | + var geom = new List<Autodesk.DesignScript.Geometry.Geometry>(); |
| 34 | + geom.Add(PolyCurve.ByJoinedCurves(curves)); |
| 35 | + tesselatedCurveLoops.Add(new Tuple<BoundingBox, List<XYZ>, List<Curve>>(BoundingBox.ByGeometry(geom), verts, curveLoop)); |
| 36 | + curves.ForEach(x => x.Dispose()); |
| 37 | + curves.Clear(); |
| 38 | + geom.ForEach(x => x.Dispose()); |
| 39 | + geom.Clear(); |
| 40 | + } |
| 41 | + |
| 42 | + var outerCurveLoops = new List<Tuple<BoundingBox, List<XYZ>, List<Curve>>>(); |
| 43 | + var innerCurveLoops = new List<Tuple<BoundingBox, List<XYZ>, List<Curve>>>(); |
| 44 | + |
| 45 | + // collect the outer loops, which are not contained in any other loop |
| 46 | + for (int i = 0, j = tesselatedCurveLoops.Count - 1; i < tesselatedCurveLoops.Count; j = i++) |
| 47 | + { |
| 48 | + var curveLoop = tesselatedCurveLoops[i]; |
| 49 | + var isOuter = tesselatedCurveLoops.Where((_, l) => i != l) |
| 50 | + .All(bound => !curveLoop.Item2.IsContainedIn(bound.Item1)); |
| 51 | + |
| 52 | + if (isOuter) |
| 53 | + { |
| 54 | + outerCurveLoops.Add(curveLoop); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + innerCurveLoops.Add(curveLoop); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + var curvesPartitions = new List<CurvePartition>(); |
| 63 | + |
| 64 | + // for each outer loop, collect the loops that are inside of it |
| 65 | + // forming the partitions of the original curve loop set |
| 66 | + foreach (var outerLoop in outerCurveLoops) |
| 67 | + { |
| 68 | + var comp = new List<List<Curve>> { outerLoop.Item3 }; |
| 69 | + var mask = new List<bool>(); |
| 70 | + var curvePatition = new CurvePartition(); |
| 71 | + curvePatition.OuterCurves.AddRange(outerLoop.Item3); |
| 72 | + foreach (var innerLoop in innerCurveLoops) |
| 73 | + { |
| 74 | + if (innerLoop.Item2.IsContainedIn(outerLoop.Item1)) |
| 75 | + { |
| 76 | + mask.Add(false); |
| 77 | + comp.Add(innerLoop.Item3); |
| 78 | + curvePatition.InnerCurves.Add(innerLoop.Item3); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + mask.Add(true); |
| 83 | + } |
| 84 | + } |
| 85 | + // remove innerEdge loops that have already been added to a partition |
| 86 | + innerCurveLoops = innerCurveLoops.Where((_, j) => mask[j]).ToList(); |
| 87 | + // add the new partition |
| 88 | + curvesPartitions.Add(curvePatition); |
| 89 | + } |
| 90 | + |
| 91 | + return curvesPartitions; |
| 92 | + } |
| 93 | + |
| 94 | + public static List<List<Autodesk.Revit.DB.Curve>> GetAllCurveloopsFromRevitFace(Face face) |
| 95 | + { |
| 96 | + var curveLoops = face.GetEdgesAsCurveLoops(); |
| 97 | + List<List<Autodesk.Revit.DB.Curve>> curves = new List<List<Curve>>(); |
| 98 | + foreach (var curveloop in curveLoops) |
| 99 | + { |
| 100 | + var a = curveloop.GetCurveLoopIterator(); |
| 101 | + bool hasItems = false; |
| 102 | + List<Curve> curve = new List<Curve>(); |
| 103 | + do |
| 104 | + { |
| 105 | + hasItems = a.MoveNext(); |
| 106 | + if (hasItems) |
| 107 | + { |
| 108 | + var item = a.Current; |
| 109 | + curve.Add(item); |
| 110 | + } |
| 111 | + } while (hasItems); |
| 112 | + curves.Add(curve); |
| 113 | + } |
| 114 | + return curves; |
| 115 | + } |
| 116 | + |
| 117 | + private static IEnumerable<Autodesk.Revit.DB.XYZ> TesselateCurveLoop(IEnumerable<Autodesk.Revit.DB.Curve> curveLoop) |
| 118 | + { |
| 119 | + return curveLoop.SelectMany(x => x.Tessellate().TakeAllButLast()); |
| 120 | + } |
| 121 | + |
| 122 | + private static bool IsContainedIn(this IEnumerable<XYZ> curveLoop, BoundingBox boundingBox) |
| 123 | + { |
| 124 | + return curveLoop.All(x => boundingBox.Contains(x.ToPoint(false))); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public class CurvePartition |
| 129 | + { |
| 130 | + public List<Curve> OuterCurves; |
| 131 | + public List<List<Curve>> InnerCurves; |
| 132 | + |
| 133 | + public CurvePartition() |
| 134 | + { |
| 135 | + OuterCurves = new List<Curve>(); |
| 136 | + InnerCurves = new List<List<Curve>>(); |
| 137 | + } |
| 138 | + |
| 139 | + public List<List<Curve>> GetAllCurves() |
| 140 | + { |
| 141 | + var allCurves = new List<List<Curve>>(); |
| 142 | + allCurves.Add(OuterCurves); |
| 143 | + allCurves.AddRange(InnerCurves); |
| 144 | + return allCurves; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments