protected override void SolveInstance(IGH_DataAccess DA) { // Declare a variable for the input String string filename = null; bool activate = false; Rhino.Geometry.Point3d point = Rhino.Geometry.Point3d.Unset; // 1. Declare placeholder variables and assign initial invalid data. // This way, if the input parameters fail to supply valid data, we know when to abort. // 2. Retrieve input data. if (!DA.GetData(0, ref filename)) { return; } if (!DA.GetData(1, ref activate)) { return; } if (!DA.GetData(2, ref point)) { return; } // If the retrieved data is Nothing, we need to abort. if (filename == null) { return; } if (!File.Exists(filename)) { return; } if (activate) { GH_Cluster cluster = new GH_Cluster(); cluster.CreateFromFilePath(filename); GH_Document doc = OnPingDocument(); doc.AddObject(cluster, false); Grasshopper.Kernel.Parameters.Param_Point paramIn = new Grasshopper.Kernel.Parameters.Param_Point(); Grasshopper.Kernel.Parameters.Param_Geometry paramOut = new Grasshopper.Kernel.Parameters.Param_Geometry(); paramIn.SetPersistentData(point); cluster.Params.RegisterInputParam(paramIn); cluster.Params.RegisterOutputParam(paramOut); cluster.CollectData(); cluster.ComputeData(); //Grasshopper.DataTree<object> test = new DataTree<object>(); //test.Add(paramIn, 0); DA.SetData(1, paramOut); } }
private void SolutionCallback(GH_Document doc) { //read file, deserialize json, send variables out of the extracted class string jsonstring = File.ReadAllText(_path); ParamsData paramdata = new ParamsData(); paramdata = JsonConvert.DeserializeObject <ParamsData>(jsonstring); _n = paramdata.NumSliders; _dataIn = paramdata.SliderVals; _pointsdata = paramdata.Points; Random rnd = new Random(); List <IGH_DocumentObject> deletions = new List <IGH_DocumentObject>(); //list of objects to delete from grasshopper document List <OutputParam> outputParams = new List <OutputParam>(); //list of the slider grouping params and their output connections List <IGH_Param> PointRecvParams = new List <IGH_Param>(); //list of what the point param is connected to foreach (IGH_DocumentObject obj in GrasshopperDocument.Objects) { if (obj.NickName.StartsWith(_controlComponentName)) //the point and integer params i've created { deletions.Add(obj); IGH_Param tempParam = obj as IGH_Param; //cast obj into a param to locate sources and recipients if (tempParam.SourceCount > 0) { deletions.AddRange(tempParam.Sources); //add source sliders to deletions list } if (obj.NickName.StartsWith(_controlComponentName + "points")) { foreach (IGH_Param recip in tempParam.Recipients) { PointRecvParams.Add(recip); } } if (obj.NickName.StartsWith(_controlComponentName + "slids")) //the integer params { int ObjectIndex; Int32.TryParse(System.Text.RegularExpressions.Regex.Match(obj.NickName, @"(\d+)\z").Value, out ObjectIndex); //regex to extract index number from end of param name List <IGH_Param> receivingParams = new List <IGH_Param>(); foreach (IGH_Param recip in tempParam.Recipients) { receivingParams.Add(recip); } outputParams.Add(new OutputParam(ObjectIndex, receivingParams)); //put output param index and recipients into an object in a list } } } foreach (IGH_DocumentObject delobj in deletions) //delete the stuff { GrasshopperDocument.RemoveObject(delobj, false); } List <Grasshopper.Kernel.Parameters.Param_Integer> targetParam = new List <Grasshopper.Kernel.Parameters.Param_Integer>(); //holds the new output params as we build them //rename targetParam for (int index = 0; index < _n.Count; index++) //this loop runs once per slider bank //rename index { targetParam.Add(new Grasshopper.Kernel.Parameters.Param_Integer()); //create the new output param targetParam[index].NickName = _controlComponentName + "slids" + index; //assign the name to the output param including the index number GrasshopperDocument.AddObject(targetParam[index], false); if (index == 0) //put param in place { targetParam[index].Attributes.Pivot = new System.Drawing.PointF(Component.Attributes.Pivot.X + 20, Component.Attributes.Pivot.Y + 110); } else { _n[index] = _n[index] + _n[index - 1]; //aggregate list of number of sliders per bank to create slider index breakpoints targetParam[index].Attributes.Pivot = new System.Drawing.PointF(Component.Attributes.Pivot.X + 20, Component.Attributes.Pivot.Y + 110 + _n[index - 1] * 20 + index * 10); } if (outputParams.Exists(opar => opar.outParam == index)) //looks in the list of deleted output params and determines if one has the same index as the param being created { foreach (IGH_Param receivingParam in outputParams.Find(opar => opar.outParam == index).recvParams) { receivingParam.AddSource(targetParam[index]); //connects the new param to the old param stuff } } } Grasshopper.Kernel.Parameters.Param_Point pointsParam = new Grasshopper.Kernel.Parameters.Param_Point(); pointsParam.NickName = _controlComponentName + "points"; GrasshopperDocument.AddObject(pointsParam, false); pointsParam.Attributes.Pivot = new System.Drawing.PointF(Component.Attributes.Pivot.X + 20, Component.Attributes.Pivot.Y + 70); foreach (IGH_Param receivingParam in PointRecvParams) { receivingParam.AddSource(pointsParam); } pointsParam.SetPersistentData(_pointsdata.ToArray()); int Yoffset = -12; int CurrentParam = 0; for (int i = 0; i < _n[_n.Count - 1]; i++) { if (_n.Exists(x => x == i)) { Yoffset = Yoffset + 10; CurrentParam++; } //instantiate new slider Grasshopper.Kernel.Special.GH_NumberSlider slid = new Grasshopper.Kernel.Special.GH_NumberSlider(); slid.CreateAttributes(); //sets up default values, and makes sure your slider doesn't crash rhino //customise slider (position, ranges etc) //targetParam.Attributes.Bounds slid.Attributes.Pivot = new System.Drawing.PointF((float)targetParam[0].Attributes.Pivot.X - slid.Attributes.Bounds.Width - 70, (float)targetParam[0].Attributes.Pivot.Y + i * 20 + Yoffset); slid.Slider.Maximum = 100; slid.Slider.Minimum = 0; slid.Slider.DecimalPlaces = 0; // slid.SetSliderValue((decimal) (rnd.Next(-50, 51))); if (i + 1 > _dataIn.Count) { slid.SetSliderValue(_dataIn[_dataIn.Count - 1]); } else { slid.SetSliderValue(_dataIn[i]); } //Until now, the slider is a hypothetical object. // This command makes it 'real' and adds it to the canvas. GrasshopperDocument.AddObject(slid, false); //Connect the new slider to this component targetParam[CurrentParam].AddSource(slid); } }