/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { string jsonString = null; bool prevIsUpdating = isUpdating; if (!DA.GetData(0, ref jsonString)) { return; } if (prevJSONString == jsonString) { DA.SetData(0, isUpdating); return; } prevJSONString = jsonString; var data = JsonSerializer.Deserialize <UpdateMessage>(jsonString); var doc = OnPingDocument(); var guid = new Guid(data.guid); GH_NumberSlider sliderToExpire = null; foreach (IGH_DocumentObject docObject in doc.Objects) { if (guid == docObject.InstanceGuid) { sliderToExpire = docObject as GH_NumberSlider; break; } } if (sliderToExpire != null) { isUpdating = true; doc.ScheduleSolution(5, (GH_Document _doc) => { sliderToExpire.SetSliderValue((decimal)data.value); sliderToExpire.ExpireSolution(false); }); if (timer != null) { timer.Stop(); timer.Elapsed -= Timer_Elapsed; timer.Dispose(); } timer = new Timer() { Interval = 300 }; timer.AutoReset = false; timer.Elapsed += Timer_Elapsed; timer.Start(); } DA.SetData(0, isUpdating); }
// SetSliders: Sets the gene sliders to the given gene values internal void SetSliders(List <double> genes) { for (int i = 0; i < genes.Count; i++) { GH_NumberSlider slider = geneInputSliders[i]; slider.Slider.RaiseEvents = false; slider.SetSliderValue((decimal)genes[i]); slider.ExpireSolution(false); slider.Slider.RaiseEvents = true; } RecalculateSolution(); }
// ChangeGeneValue: Changes only one slider value for mutation and returns the new gene value internal double MutateGeneValue(int index) { double gene; GH_NumberSlider slider = geneInputSliders[index]; slider.Slider.RaiseEvents = false; slider.TickValue = random.Next(slider.TickCount + 1); // +1 (include Max value) gene = (double)slider.Slider.Value; slider.ExpireSolution(false); slider.Slider.RaiseEvents = true; RecalculateSolution(); return(gene); }