Example #1
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declate variables
            Curve           curve    = null;
            string          name     = "";
            MaterialWrapper material = null;

            // Reference the inputs
            DA.GetData(0, ref curve);
            DA.GetData(1, ref name);
            DA.GetData(2, ref material);

            // Throw error message if curve cannot be converted to a polyline
            if (!curve.IsPolyline())
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input must be polyine, curves are not allowed.");
                return;
            }

            // Convert curve to polyline
            Polyline polyline = null;

            curve.TryGetPolyline(out polyline);

            // Fill a list with the polyline's point coordinates
            List <double> array = new List <double>();

            for (int i = 0; i < polyline.Count; i++)
            {
                // Get point values
                var px = polyline.X[i];
                var py = polyline.Y[i];
                var pz = polyline.Z[i];

                // Add point values to array
                array.Add(Math.Round(px, 3) * -1);
                array.Add(Math.Round(pz, 3));
                array.Add(Math.Round(py, 3));
            }

            //Build the position object
            dynamic position = new ExpandoObject();

            position.itemSize   = 3;
            position.type       = "Float32Array";
            position.array      = array;
            position.normalized = false;

            // Build the attribute object
            dynamic attributes = new ExpandoObject();

            attributes.position = position;

            // If LineDashedMaterial is used, add the lineDistance attribute
            if (material != null && string.Equals(material.Material.type, "LineDashedMaterial"))
            {
                // Create list to populate with lineDistances
                List <double> lineDistances = new List <double>();
                Point3d       previousPt    = new Point3d();

                // Loop over vertices and measure the distance from start to each vertex
                for (int i = 0; i < polyline.Count; i++)
                {
                    // Get point values
                    var px = polyline.X[i];
                    var py = polyline.Y[i];
                    var pz = polyline.Z[i];

                    // Distance to previous point
                    Point3d pt = new Point3d(px, py, pz);
                    if (i == 0)
                    {
                        lineDistances.Add(0);
                    }
                    else
                    {
                        lineDistances.Add(pt.DistanceTo(previousPt) + lineDistances[i - 1]);
                    }
                    previousPt = pt;
                }

                // Build the lineDistance object
                dynamic lineDistance = new ExpandoObject();
                lineDistance.type     = "Float32Array";
                lineDistance.array    = lineDistances;
                lineDistance.count    = polyline.Count;
                lineDistance.itemSize = 1;

                // Add the lineDistance object to the attributes object
                attributes.lineDistance = lineDistance;
            }

            // Build the data object
            dynamic data = new ExpandoObject();

            data.attributes = attributes;

            // Build the geometry object
            dynamic geometry = new ExpandoObject();

            geometry.uuid = Guid.NewGuid();
            geometry.type = "BufferGeometry";
            geometry.data = data;

            // Build the child object
            dynamic child = new ExpandoObject();

            child.uuid = Guid.NewGuid();
            if (name.Length > 0)
            {
                child.name = name;
            }
            child.type     = "Line";
            child.geometry = geometry.uuid;
            if (material != null)
            {
                child.material = material.Material.uuid;
            }
            child.matrix = new List <double> {
                1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
            };

            // Fill the children list
            List <dynamic> children = new List <dynamic>();

            children.Add(child);

            // Wrap the objects in a wrapper object
            GeometryWrapper wrapper = null;

            if (material != null)
            {
                wrapper = new GeometryWrapper(geometry, child, material.Material);
            }
            else
            {
                wrapper = new GeometryWrapper(geometry, child, null);
            }

            // Serialize wrapper object
            string JSON = JsonConvert.SerializeObject(wrapper);

            // Set outputs
            DA.SetData(0, JSON);
            DA.SetData(1, wrapper);
        }
Example #2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Declare variables
            Rhino.Geometry.Mesh mesh = null;
            string          name     = "";
            MaterialWrapper material = null;

            // Reference the inputs
            DA.GetData(0, ref mesh);
            DA.GetData(1, ref name);
            DA.GetData(2, ref material);

            // If the meshes have quads, triangulate them
            if (!mesh.Faces.ConvertQuadsToTriangles())
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Error triangulating quad meshes. Try triangulating quads before feeding into this component.");
            }
            ;

            // Fill the vertices and colors list
            List <double> vertices = new List <double>();
            List <double> colors   = new List <double>();

            for (int i = 0; i < mesh.Vertices.Count; i++)
            {
                vertices.Add(Math.Round(mesh.Vertices[i].X, 3) * -1);
                vertices.Add(Math.Round(mesh.Vertices[i].Z, 3));
                vertices.Add(Math.Round(mesh.Vertices[i].Y, 3));

                colors.Add((float)mesh.VertexColors[i].R / 255);
                colors.Add((float)mesh.VertexColors[i].G / 255);
                colors.Add((float)mesh.VertexColors[i].B / 255);
            }

            // Create position object
            dynamic position = new ExpandoObject();

            position.itemSize   = 3;
            position.type       = "Float32Array";
            position.array      = vertices;
            position.normalized = false;

            // Create color object
            dynamic color = new ExpandoObject();

            color.itemSize = 3;
            color.type     = "Float32Array";
            color.array    = colors;

            // Fill the normals list
            List <double> normals = new List <double>();

            for (int i = 0; i < mesh.Normals.Count; i++)
            {
                normals.Add(Math.Round(mesh.Normals[i].X, 3) * -1);
                normals.Add(Math.Round(mesh.Normals[i].Z, 3));
                normals.Add(Math.Round(mesh.Normals[i].Y, 3));
            }

            // Create normal object
            dynamic normal = new ExpandoObject();

            normal.itemSize   = 3;
            normal.type       = "Float32Array";
            normal.array      = normals;
            normal.normalized = false;

            // Fill the uvs list
            List <double> uvs = new List <double>();

            for (int i = 0; i < mesh.TextureCoordinates.Count; i++)
            {
                uvs.Add(Math.Round(mesh.TextureCoordinates[i].X, 3));
                uvs.Add(Math.Round(mesh.TextureCoordinates[i].Y, 3));
            }

            // Create uv object
            dynamic uv = new ExpandoObject();

            uv.itemSize   = 2;
            uv.type       = "Float32Array";
            uv.array      = uvs;
            uv.normalized = false;

            // Create attributes object
            dynamic attributes = new ExpandoObject();

            attributes.position = position;
            attributes.color    = color;
            attributes.normal   = normal;
            attributes.uv       = uv;

            // Fill faces list
            List <int> faces = new List <int>();

            for (int i = 0; i < mesh.Faces.Count; i++)
            {
                faces.Add(mesh.Faces.GetFace(i).A);
                faces.Add(mesh.Faces.GetFace(i).B);
                faces.Add(mesh.Faces.GetFace(i).C);
            }

            // Create the index object
            dynamic index = new ExpandoObject();

            index.type  = "Uint32Array";
            index.array = faces;

            // Create the data object
            dynamic data = new ExpandoObject();

            data.attributes = attributes;
            data.index      = index;

            /// Add the bufferGeometry
            dynamic bufferGeometry = new ExpandoObject();

            bufferGeometry.uuid = Guid.NewGuid();;
            bufferGeometry.type = "BufferGeometry";
            bufferGeometry.data = data;

            /// Add the child
            dynamic child = new ExpandoObject();

            child.uuid = Guid.NewGuid();
            if (name.Length > 0)
            {
                child.name = name;
            }
            child.type     = "Mesh";
            child.geometry = bufferGeometry.uuid;
            child.material = material.Material.uuid;
            child.matrix   = new List <double> {
                1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
            };
            child.castShadow    = true;
            child.receiveShadow = true;

            // Set the material to use vertex colors
            material.Material.vertexColors = 2;
            material.Material.color        = "0xffffff";

            /// Wrap the bufferGeometries and children to the wrapper
            GeometryWrapper wrapper = new GeometryWrapper(bufferGeometry, child, material.Material);

            // Create JSON string
            string JSON = JsonConvert.SerializeObject(wrapper);

            // Set outputs
            DA.SetData(0, JSON);
            DA.SetData(1, wrapper);
        }