// Insipred by // https://github.com/DynamoDS/DynamoRevit/blob/master/src/Libraries/RevitNodes/GeometryConversion/ProtoToRevitMesh.cs public IList <GeometryObject> MeshToNative(Mesh mesh, TessellatedShapeBuilderTarget target = TessellatedShapeBuilderTarget.Mesh, TessellatedShapeBuilderFallback fallback = TessellatedShapeBuilderFallback.Salvage) { var tsb = new TessellatedShapeBuilder() { Fallback = fallback, Target = target, GraphicsStyleId = ElementId.InvalidElementId }; var valid = tsb.AreTargetAndFallbackCompatible(target, fallback); tsb.OpenConnectedFaceSet(target == TessellatedShapeBuilderTarget.Solid); var vertices = ArrayToPoints(mesh.vertices, mesh.units); int i = 0; while (i < mesh.faces.Count) { int n = mesh.faces[i]; if (n < 3) { n += 3; // 0 -> 3, 1 -> 4 to preserve backwards compatibility } var points = mesh.faces.GetRange(i + 1, n).Select(x => vertices[x]).ToArray(); if (IsNonPlanarQuad(points)) { //Non-planar quads will be triangulated as it's more desirable than `TessellatedShapeBuilder.Build`'s attempt to make them planar. //TODO consider triangulating all n > 3 polygons var triPoints = new List <XYZ> { points[0], points[1], points[3] }; var face1 = new TessellatedFace(triPoints, ElementId.InvalidElementId); tsb.AddFace(face1); triPoints = new List <XYZ> { points[1], points[2], points[3] };; var face2 = new TessellatedFace(triPoints, ElementId.InvalidElementId); tsb.AddFace(face2); } else { var face = new TessellatedFace(points, ElementId.InvalidElementId); tsb.AddFace(face); } i += n + 1; } tsb.CloseConnectedFaceSet(); try { tsb.Build(); } catch (Exception e) { Report.LogConversionError(e); return(null); } var result = tsb.GetBuildResult(); return(result.GetGeometricalObjects());
// Insipred by // https://github.com/DynamoDS/DynamoRevit/blob/master/src/Libraries/RevitNodes/GeometryConversion/ProtoToRevitMesh.cs public IList <GeometryObject> MeshToNative(Mesh mesh, TessellatedShapeBuilderTarget target = TessellatedShapeBuilderTarget.Mesh, TessellatedShapeBuilderFallback fallback = TessellatedShapeBuilderFallback.Salvage) { var tsb = new TessellatedShapeBuilder() { Fallback = fallback, Target = target, GraphicsStyleId = ElementId.InvalidElementId }; var valid = tsb.AreTargetAndFallbackCompatible(target, fallback); tsb.OpenConnectedFaceSet(target == TessellatedShapeBuilderTarget.Solid); var vertices = ArrayToPoints(mesh.vertices, mesh.units); int i = 0; while (i < mesh.faces.Count) { var points = new List <XYZ>(); if (mesh.faces[i] == 0) { // triangle points = new List <XYZ> { vertices[mesh.faces[i + 1]], vertices[mesh.faces[i + 2]], vertices[mesh.faces[i + 3]] }; var face = new TessellatedFace(points, ElementId.InvalidElementId); var check = !tsb.DoesFaceHaveEnoughLoopsAndVertices(face); tsb.AddFace(face); i += 4; } else { // quad points = new List <XYZ> { vertices[mesh.faces[i + 1]], vertices[mesh.faces[i + 2]], vertices[mesh.faces[i + 4]] }; var face1 = new TessellatedFace(points, ElementId.InvalidElementId); var check1 = tsb.DoesFaceHaveEnoughLoopsAndVertices(face1); tsb.AddFace(face1); points = new List <XYZ> { vertices[mesh.faces[i + 2]], vertices[mesh.faces[i + 3]], vertices[mesh.faces[i + 4]] }; var face2 = new TessellatedFace(points, ElementId.InvalidElementId); var check2 = tsb.DoesFaceHaveEnoughLoopsAndVertices(face2); tsb.AddFace(face2); i += 5; } } tsb.CloseConnectedFaceSet(); try { tsb.Build(); } catch (Exception e) { ConversionErrors.Add(e); return(null); } var result = tsb.GetBuildResult(); return(result.GetGeometricalObjects()); }