public static GH_Structure <IGH_Goo> DeserializeGrasshopperData(byte[] array) { GH_LooseChunk val = new GH_LooseChunk("Noah Data"); val.Deserialize_Binary(array); if (val.ItemCount == 0) { return(null); } GH_Structure <IGH_Goo> gH_Structure = new GH_Structure <IGH_Goo>(); GH_IReader val2 = val.FindChunk("Block", 0); bool boolean = val2.GetBoolean("Empty"); if (boolean) { return(null); } GH_IReader val3 = val2.FindChunk("Data"); if (val3 == null) { return(null); } else if (!gH_Structure.Read(val3)) { return(null); } return(gH_Structure); }
protected override void SolveInstance(IGH_DataAccess DA) { DA.DisableGapLogic(); string SourceFile = ""; DA.GetData(0, ref SourceFile); if (string.IsNullOrWhiteSpace(SourceFile)) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No source file has been specified."); return; } if (!File.Exists(SourceFile)) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Source file location doesn't exist: " + SourceFile); return; } byte[] array; try { array = File.ReadAllBytes(SourceFile); } catch (Exception ex) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, ex.Message); return; } GH_LooseChunk val = new GH_LooseChunk("Grasshopper Data"); val.Deserialize_Binary(array); if (val.ItemCount == 0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Source data file is corrupt."); return; } GH_Structure <IGH_Goo> gH_Structure = new GH_Structure <IGH_Goo>(); GH_IReader val2 = val.FindChunk("Block", 0); if (val2 == null) { base.Params.Output[0].NickName = "?"; DA.SetDataTree(0, gH_Structure); } bool boolean = val2.GetBoolean("Empty"); if (!boolean) { GH_IReader val3 = val2.FindChunk("Data"); if (val3 == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Source file is corrupt."); } else if (!gH_Structure.Read(val3)) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Data could not be deserialized."); } } DA.SetDataTree(0, gH_Structure); }
public string SaveState() { if (container == null) { return("null"); } GH_LooseChunk gH_LooseChunk = new GH_LooseChunk("graph"); container.Write((GH_IWriter)gH_LooseChunk); return(gH_LooseChunk.Serialize_Xml()); }
public void LoadState(string state) { Container = null; if (state.Equals("null", StringComparison.OrdinalIgnoreCase)) { ExpireSolution(false); return; } GH_LooseChunk gH_LooseChunk = new GH_LooseChunk("graph"); gH_LooseChunk.Deserialize_Xml(state); GH_GraphContainer val = new GH_GraphContainer(null); if (val.Read(gH_LooseChunk)) { Container = val; ExpireSolution(false); } }
// 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()); }
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; } }