Ejemplo n.º 1
0
        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);
        }