public void Add(BodyTessellation other) { int vertexOffset = VertexPositions.Count; int faceColorOffset = FaceColors.Count; VertexPositions.AddRange(other.VertexPositions); VertexNormals.AddRange(other.VertexNormals); FaceColors.AddRange(other.FaceColors); Faces.AddRange(other.Faces.Select(f => new FaceStruct { Vertex1 = f.Vertex1 + vertexOffset, Vertex2 = f.Vertex2 + vertexOffset, Vertex3 = f.Vertex3 + vertexOffset, Color = f.Color + faceColorOffset })); }
private static BodyTessellation GetBodyMesh(IBody2 body, IPartDoc partDoc, ITessellation tessellation, long?overrideColor) { var bodyTessellation = new BodyTessellation(); if (overrideColor.HasValue) { bodyTessellation.FaceColors.Add(overrideColor.Value); } else { double[] bodyColorArray = null; var features = (object[])body.GetFeatures(); if (features != null) { foreach (IFeature feature in features.Reverse()) { bodyColorArray = (double[])feature.GetMaterialPropertyValues2( (int)swInConfigurationOpts_e.swThisConfiguration, null); if (bodyColorArray[0] == -1 ) // All -1s are returned by features that don't assign color. { bodyColorArray = null; } if (bodyColorArray != null) { break; } } } if (bodyColorArray == null) { bodyColorArray = (double[])body.MaterialPropertyValues2; } if (bodyColorArray == null) { bodyColorArray = (double[])partDoc.MaterialPropertyValues; } var bodyColor = ColorArrayToColor(bodyColorArray); bodyTessellation.FaceColors.Add(bodyColor); } var coloredFaces = new Dictionary <IFace2, long>(); var faceCount = 0; foreach (IFace2 face in (object[])body.GetFaces()) { faceCount++; var colorArray = (double[])face.MaterialPropertyValues; if (colorArray != null) { coloredFaces[face] = ColorArrayToColor(colorArray); } } if (coloredFaces.Count < faceCount) { for (var i = 0; i < tessellation.GetVertexCount(); i++) { bodyTessellation.VertexPositions.Add((double[])tessellation.GetVertexPoint(i)); bodyTessellation.VertexNormals.Add((double[])tessellation.GetVertexNormal(i)); } foreach (IFace2 face in (object[])body.GetFaces()) { if (coloredFaces.ContainsKey(face)) { continue; } foreach (var facet in (int[])tessellation.GetFaceFacets(face)) { var vertexIndices = new List <int>(); foreach (var fin in (int[])tessellation.GetFacetFins(facet)) { vertexIndices.Add(((int[])tessellation.GetFinVertices(fin))[0]); } bodyTessellation.Faces.Add(new FaceStruct { Color = 0, Vertex1 = vertexIndices[0], Vertex2 = vertexIndices[1], Vertex3 = vertexIndices[2], }); } } } foreach (var pair in coloredFaces) { var colorIndex = bodyTessellation.FaceColors.IndexOf(pair.Value); if (colorIndex == -1) { bodyTessellation.FaceColors.Add(pair.Value); colorIndex = bodyTessellation.FaceColors.Count - 1; } foreach (var facet in (int[])tessellation.GetFaceFacets(pair.Key)) { var vertexIndices = new List <int>(); foreach (var fin in (int[])tessellation.GetFacetFins(facet)) { vertexIndices.Add(((int[])tessellation.GetFinVertices(fin))[0]); } bodyTessellation.Faces.Add( new FaceStruct { Color = colorIndex, Vertex1 = bodyTessellation.VertexPositions.Count, Vertex2 = bodyTessellation.VertexPositions.Count + 1, Vertex3 = bodyTessellation.VertexPositions.Count + 2 }); bodyTessellation.VertexPositions.Add( (double[])tessellation.GetVertexPoint(vertexIndices[0])); bodyTessellation.VertexPositions.Add( (double[])tessellation.GetVertexPoint(vertexIndices[1])); bodyTessellation.VertexPositions.Add( (double[])tessellation.GetVertexPoint(vertexIndices[2])); bodyTessellation.VertexNormals.Add( (double[])tessellation.GetVertexNormal(vertexIndices[0])); bodyTessellation.VertexNormals.Add( (double[])tessellation.GetVertexNormal(vertexIndices[1])); bodyTessellation.VertexNormals.Add( (double[])tessellation.GetVertexNormal(vertexIndices[2])); } } return(bodyTessellation); }
static PartTessellation GetBodyTessellation(Body body, Func<Face, Color> faceColor, double surfaceDeviation, double angleDeviation) { var tessellationOptions = new TessellationOptions(surfaceDeviation, angleDeviation); var tessellation = body.GetTessellation(null, tessellationOptions); var vertices = new Dictionary<PositionNormalTextured, int>(); var vertexList = new List<Point>(); var normalList = new List<Direction>(); var colors = new Dictionary<Color, int>(); var colorList = new List<Color>(); var faces = new List<FaceStruct>(); foreach (var pair in tessellation) { var color = faceColor(pair.Key); int colorIndex; if (!colors.TryGetValue(color, out colorIndex)) { colorList.Add(color); colorIndex = colorList.Count - 1; colors[color] = colorIndex; } var vertexIndices = new Dictionary<int, int>(); var i = 0; foreach (var vertex in pair.Value.Vertices) { int index; if (!vertices.TryGetValue(vertex, out index)) { vertexList.Add(vertex.Position); normalList.Add(vertex.Normal); index = vertexList.Count - 1; vertices[vertex] = index; } vertexIndices[i] = index; i++; } foreach (var facet in pair.Value.Facets) { faces.Add(new FaceStruct { Vertex1 = vertexIndices[facet.Vertex0], Vertex2 = vertexIndices[facet.Vertex1], Vertex3 = vertexIndices[facet.Vertex2], Color = colorIndex }); } } var faceTessellation = new BodyTessellation { VertexPositions = vertexList, VertexNormals = normalList, FaceColors = colorList, Faces = faces }; List<BodyTessellation> edges = new List<BodyTessellation>(); foreach (var edge in body.Edges) { edges.Add(new BodyTessellation { VertexPositions = new List<Point>(edge.GetPolyline()) }); } return new PartTessellation { Lines = edges, Meshes = { faceTessellation } }; }