コード例 #1
0
ファイル: ExportJSON.cs プロジェクト: susannnwu/Triceratops
        /// <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
            string       path         = null;
            string       name         = null;
            SceneWrapper exportObject = null;
            bool         write        = false;

            // Reference the inputs
            DA.GetData(0, ref path);
            DA.GetData(1, ref name);
            DA.GetData(2, ref exportObject);
            DA.GetData(3, ref write);

            // Create filepath
            string filePath = path + "\\" + name + ".json";

            // Serialize the object tree
            string JSON = JsonConvert.SerializeObject(exportObject.ExportObject);

            // Write the JSON only when the run input is true
            if (write)
            {
                File.WriteAllText(filePath, JSON);
            }

            // If the write input is not true, trigger a warning
            else
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Toggle to true to export JSON");
            }

            // Set the outputs
            DA.SetData(0, filePath);
        }
コード例 #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
            List <GeometryWrapper> geometries = new List <GeometryWrapper>();

            // Reference the inputs
            DA.GetDataList(0, geometries);

            /// Metadata
            dynamic metadata = new ExpandoObject();

            metadata.version   = 4.3;
            metadata.type      = "Object";
            metadata.generator = "TriceratopsExporter";

            /// Scene
            dynamic scene = new ExpandoObject();

            scene.uuid   = Guid.NewGuid();
            scene.type   = "Scene";
            scene.name   = "";
            scene.matrix = new List <double> {
                1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
            };
            scene.children = new List <dynamic>();

            // Create the export object
            dynamic exportObject = new ExpandoObject();

            exportObject.metadata   = metadata;
            exportObject.geometries = new List <dynamic>();

            // Create a list for materials, and track ids to ensure no duplicates
            List <dynamic> materials   = new List <dynamic>();
            List <string>  materialIds = new List <string>();

            /// Loop over all of the imported geometries to add to object
            foreach (GeometryWrapper geometry in geometries)
            {
                // Add the child objects
                scene.children.Add(geometry.Child);
                exportObject.geometries.Add(geometry.Geometry);

                if (geometry.Material != null)
                {
                    /// If a material exists and it hasn't been added to the scene yet, add it to the scene
                    if (!materialIds.Contains(geometry.Material.uuid.ToString()))
                    {
                        materials.Add(geometry.Material);
                        materialIds.Add(geometry.Material.uuid.ToString());
                    }
                }
            }

            // Add materials and object to the export object
            exportObject.materials = materials;
            exportObject.@object   = scene;

            // Create a JSON string
            string JSON = JsonConvert.SerializeObject(exportObject);

            /// Wrap the bufferGeometries and children to the wrapper
            SceneWrapper wrapper = new SceneWrapper(exportObject);

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