Example #1
0
File: IO.cs Project: i-noah/noah-n
        // Convert.FromBase64String(x)
        public static byte[] SerializeGrasshopperData(GH_Structure <IGH_Goo> tree, string name = "default", bool isEmpty = false)
        {
            GH_LooseChunk ghLooseChunk = new GH_LooseChunk("Noah Data");

            ghLooseChunk.SetGuid("OriginId", Guid.NewGuid());

            GH_IWriter chunk = ghLooseChunk.CreateChunk("Block", 0);

            chunk.SetString("Name", name);
            chunk.SetBoolean("Empty", isEmpty);
            if (!isEmpty)
            {
                if (!tree.Write(chunk.CreateChunk("Data")))
                {
                    throw new Exception(string.Format("There was a problem writing the {0} data.", name));
                }
            }

            return(ghLooseChunk.Serialize_Binary());
        }
Example #2
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            UpdateMessage();
            PythonScript script = PythonScript.Create();
            string       outDir = "";

            try
            {
                script.ExecuteScript("import scriptcontext as sc\nV=sc.sticky['NOAH_PROJECT']\nT=sc.sticky['TASK_TICKET']\nG=sc.sticky['NOAH_GENERATOR']\nID=sc.sticky['UUID']");
                NOAH_PROJECT   = (string)script.GetVariable("V");
                TASK_TICKET    = (string)script.GetVariable("T");
                NOAH_GENERATOR = (string)script.GetVariable("G");
                UUID           = (string)script.GetVariable("ID");
                if (File.Exists(NOAH_PROJECT))
                {
                    outDir      = Path.Combine(Path.GetDirectoryName(NOAH_PROJECT), ".noah", "tasks", UUID, TASK_TICKET, "out");
                    ProjectInfo = JObject.Parse(File.ReadAllText(NOAH_PROJECT));
                    JArray generators = JArray.Parse(ProjectInfo["generators"].ToString());
                    FindJobjectFromJArray(generators, NOAH_GENERATOR, out Generator, out GeneratorIndex);
                }
            }
            catch (Exception ex)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, ex.Message);
            }
            int outIndex = 0;

            DA.GetData(1, ref outIndex);

            JArray output = JArray.Parse(Generator["output"].ToString());

            if (outIndex >= output.Count)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "定义时未指定此输出端口");
            }
            switch (m_mode)
            {
            case ExportMode.None:
                Message = null;
                break;

            case ExportMode.Rhino:
                string fileName = Convert.ToString(outIndex) + ".3dm";
                string filePath = Path.Combine(outDir, fileName);

                File3dmWriter          writer     = new File3dmWriter(filePath);
                List <int>             ll         = new List <int>();
                List <ObjectLayerInfo> layeredObj = new List <ObjectLayerInfo>();
                DA.GetDataList(0, layeredObj);
                layeredObj.ForEach(x =>
                {
                    writer.ChildLayerSolution(x.Name);
                    ll.Add(writer.CreateLayer(x.Name, x.Color));
                });
                if (layeredObj.Count > 0)
                {
                    writer.Write(layeredObj, ll);
                    if (!exported)
                    {
                        ProjectInfo["generators"][GeneratorIndex]["output"][outIndex]["value"] = filePath;
                        File.WriteAllText(NOAH_PROJECT, JsonConvert.SerializeObject(ProjectInfo, Formatting.Indented));
                        exported = true;
                    }
                }

                break;

            case ExportMode.Text:
                if (!exported)
                {
                    string outputData = "";
                    DA.GetData(0, ref outputData);
                    ProjectInfo["generators"][GeneratorIndex]["output"][outIndex]["value"] = outputData;
                    File.WriteAllText(NOAH_PROJECT, JsonConvert.SerializeObject(ProjectInfo, Formatting.Indented));
                    exported = true;
                }
                break;

            case ExportMode.Data:
                fileName = Convert.ToString(outIndex) + ".noahdata";
                filePath = Path.Combine(outDir, fileName);
                if (string.IsNullOrWhiteSpace(filePath))
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "未指定文件.");
                    return;
                }

                GH_LooseChunk val = new GH_LooseChunk("Grasshopper Data");
                val.SetGuid("OriginId", base.InstanceGuid);
                val.SetInt32("Count", base.Params.Input.Count);
                IGH_Param     iGH_Param    = base.Params.Input[0];
                IGH_Structure volatileData = iGH_Param.VolatileData;
                GH_IWriter    val2         = val.CreateChunk("Block", 0);
                val2.SetString("Name", iGH_Param.NickName);
                val2.SetBoolean("Empty", volatileData.IsEmpty);
                if (!volatileData.IsEmpty)
                {
                    GH_Structure <IGH_Goo> tree = null;
                    DA.GetDataTree(0, out tree);
                    if (!tree.Write(val2.CreateChunk("Data")))
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"There was a problem writing the {iGH_Param.NickName} data.");
                    }
                }
                byte[] bytes = val.Serialize_Binary();
                try
                {
                    File.WriteAllBytes(filePath, bytes);
                }
                catch (Exception ex)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, ex.Message);
                }
                if (!exported)
                {
                    ProjectInfo["generators"][GeneratorIndex]["output"][outIndex]["value"] = filePath;
                    File.WriteAllText(NOAH_PROJECT, JsonConvert.SerializeObject(ProjectInfo, Formatting.Indented));
                    exported = true;
                }
                break;

            case ExportMode.CSV:
                fileName = Convert.ToString(outIndex) + ".csv";
                filePath = Path.Combine(outDir, fileName);
                List <object> oList = new List <object>();
                List <string> sList = new List <string>();
                DA.GetDataList(0, oList);
                oList.ForEach(el =>
                {
                    string tmp = "";
                    GH_Convert.ToString(el, out tmp, GH_Conversion.Both);
                    sList.Add(tmp);
                });
                File.WriteAllText(filePath, string.Join(Environment.NewLine, sList));
                if (!exported)
                {
                    ProjectInfo["generators"][GeneratorIndex]["output"][outIndex]["value"] = filePath;
                    File.WriteAllText(NOAH_PROJECT, JsonConvert.SerializeObject(ProjectInfo, Formatting.Indented));
                    exported = true;
                }
                break;
            }
        }