/// <summary> /// Gets the current sliders in Input[0] /// </summary> public bool GetSliders(List <GH_NumberSlider> sliders, List <GalapagosGeneListObject> genePools) { bool hasData = false; lock (syncLock) { // synchronize foreach (IGH_Param param in this.Params.Input[0].Sources) { Grasshopper.Kernel.Special.GH_NumberSlider slider = param as Grasshopper.Kernel.Special.GH_NumberSlider; if (slider != null) { sliders.Add(slider); hasData = true; } GalapagosGeneListObject genepool = param as GalapagosGeneListObject; if (genepool != null) { genePools.Add(genepool); hasData = true; } } } cSliders = sliders; cGenePools = genePools; return(hasData); }
private List <GH_NumberSlider> getConnectedSliders() { // Find the Guid for connected slides List <System.Guid> guids = new List <System.Guid>(); //empty list for guids GH.Kernel.IGH_Param selSlidersInput = this.Params.Input[0]; //ref for input where sliders are connected to this component IList <GH.Kernel.IGH_Param> sources = selSlidersInput.Sources; //list of things connected on this input bool isAnythingConnected = sources.Any(); //is there actually anything connected? // Find connected if (isAnythingConnected) { //if something's connected, foreach (var source in sources) //for each of these connected things: { IGH_DocumentObject component = source.Attributes.GetTopLevel.DocObject; //for this connected thing, bring it into the code in a way where we can access its properties GH.Kernel.Special.GH_NumberSlider mySlider = component as GH.Kernel.Special.GH_NumberSlider; //...then cast (?) it as a slider if (mySlider == null) //of course, if the thing isn't a slider, the cast doesn't work, so we get null. let's filter out the nulls { continue; } guids.Add(mySlider.InstanceGuid); //things left over are sliders and are connected to our input. save this guid. //we now have a list of guids of sliders connected to our input, saved in list var 'mySlider' } } // Find all sliders. List <GH.Kernel.Special.GH_NumberSlider> sliders = new List <GH.Kernel.Special.GH_NumberSlider>(); foreach (IGH_DocumentObject docObject in doc.Objects) { GH.Kernel.Special.GH_NumberSlider slider = docObject as GH.Kernel.Special.GH_NumberSlider; if (slider != null) { // check if the slider is in the selected list if (isAnythingConnected) { if (guids.Contains(slider.InstanceGuid)) { sliders.Add(slider); } } else { sliders.Add(slider); } } } /*foreach (GH.Kernel.Special.GH_NumberSlider slider in sliders) * { * names.Add(slider.NickName); * }*/ return(sliders); }
private bool MoveToNextPermutation(ref int index, List <GH.Kernel.Special.GH_NumberSlider> sliders) { if (index >= sliders.Count) { return(false); } GH.Kernel.Special.GH_NumberSlider slider = sliders[index]; if (slider.TickValue < slider.TickCount) { //Figure out which step to fly to... //look up the current slider's current sliderStepsPosition and target number int totalNumberOfSteps = sliderSteps[index]; int currentSliderStepsPosition = sliderStepsPositions[index]; int sliderMidStep = slider.TickCount / 2; int numTicksToAddAsInt = slider.TickCount / totalNumberOfSteps; double numTicksToAddAsDouble = (double)slider.TickCount / (double)totalNumberOfSteps; //find the closest tick int closestTick = 0; if (currentSliderStepsPosition + numTicksToAddAsInt >= sliderMidStep) { closestTick = (int)Math.Ceiling(numTicksToAddAsDouble * currentSliderStepsPosition); } else { closestTick = (int)Math.Floor(numTicksToAddAsDouble * currentSliderStepsPosition); } // Increment the slider. slider.TickValue = closestTick; //Increment the current step position sliderStepsPositions[index]++; //have we already computed this upcoming combination? If so, move on to the next one without expiring the solution if (computedValues.Contains(GetSliderVals(sliders))) { return(MoveToNextPermutation(ref index, sliders)); } return(true); } else { // The current slider is already at the maximum value. Reset it back to zero. slider.TickValue = 0; //set our slider steps position back to 0 sliderStepsPositions[index] = 0; // Move on to the next slider. index++; // If we've run out of sliders to modify, we're done permutatin' if (index >= sliders.Count) { return(false); } return(MoveToNextPermutation(ref index, sliders)); } }
private void OnSolutionEnd(object sender, GH_SolutionEventArgs e) { // Unregister the event, we don't want to get called again. e.Document.SolutionEnd -= OnSolutionEnd; // If we're not supposed to run, abort now. if (!_run) { return; } // If we're already running, abort now. if (_running) { return; } // Reset run and running states. _run = false; _running = true; try { // Find the Guid for connected slides List <System.Guid> guids = new List <System.Guid>(); //empty list for guids GH.Kernel.IGH_Param selSlidersInput = this.Params.Input[0]; //ref for input where sliders are connected to this component IList <GH.Kernel.IGH_Param> sources = selSlidersInput.Sources; //list of things connected on this input bool isAnythingConnected = sources.Any(); //is there actually anything connected? // Find connected GH.Kernel.IGH_Param trigger = this.Params.Input[2].Sources[0]; //ref for input where a boolean or a button is connected GH.Kernel.Special.GH_BooleanToggle boolTrigger = trigger as GH.Kernel.Special.GH_BooleanToggle; if (isAnythingConnected) { //if something's connected, foreach (var source in sources) //for each of these connected things: { IGH_DocumentObject component = source.Attributes.GetTopLevel.DocObject; //for this connected thing, bring it into the code in a way where we can access its properties GH.Kernel.Special.GH_NumberSlider mySlider = component as GH.Kernel.Special.GH_NumberSlider; //...then cast (?) it as a slider if (mySlider == null) //of course, if the thing isn't a slider, the cast doesn't work, so we get null. let's filter out the nulls { continue; } guids.Add(mySlider.InstanceGuid); //things left over are sliders and are connected to our input. save this guid. //we now have a list of guids of sliders connected to our input, saved in list var 'mySlider' } } // Find all sliders. List <GH.Kernel.Special.GH_NumberSlider> sliders = new List <GH.Kernel.Special.GH_NumberSlider>(); foreach (IGH_DocumentObject docObject in doc.Objects) { GH.Kernel.Special.GH_NumberSlider slider = docObject as GH.Kernel.Special.GH_NumberSlider; if (slider != null) { // check if the slider is in the selected list if (isAnythingConnected) { if (guids.Contains(slider.InstanceGuid)) { sliders.Add(slider); } } else { sliders.Add(slider); } } } if (sliders.Count == 0) { System.Windows.Forms.MessageBox.Show("No sliders could be found", "<harsh buzzing sound>", MessageBoxButtons.OK); return; } //we now have all sliders //ask the user to give a sanity check int counter = 0; int totalLoops = 1; string popupMessage = ""; // create progress bar by dots and | string pb = ".................................................."; //50 of "." - There should be a better way to create this in C# > 50 * "." does it in Python! char[] pbChars = pb.ToCharArray(); int dummyCounter = 0; foreach (GH.Kernel.Special.GH_NumberSlider slider in sliders) { totalLoops *= (sliderSteps[dummyCounter] + 1); popupMessage += slider.ImpliedNickName; popupMessage += "\n"; dummyCounter++; } if (System.Windows.Forms.MessageBox.Show(sliders.Count + " slider(s) connected:\n" + popupMessage + "\n" + totalLoops.ToString() + " iterations will be done. Continue?" + "\n\n (Press ESC to pause during progressing!)", "Start?", MessageBoxButtons.YesNo) == DialogResult.No) { SetBooleanToFalse(boolTrigger); this.Message = "Release the Colibri!"; //wipe out colibri variables and compute a new solution sliderNames = new List <string>(); sliderSteps = new List <int>(); sliderStepsPositions = new Dictionary <int, int>(); computedValues = new List <string>(); e.Document.NewSolution(false); Rhino.RhinoDoc.ActiveDoc.Views.Redraw(); return; } // Set all sliders back to first tick foreach (GH.Kernel.Special.GH_NumberSlider slider in sliders) { slider.TickValue = 0; } //start a stopwatch System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); // Start a giant loop in which we'll permutate our way across all slider layouts. while (true) { int idx = 0; // let the user cancel the process if (GH_Document.IsEscapeKeyDown()) { if (System.Windows.Forms.MessageBox.Show("Do you want to stop the process?\nSo far " + counter.ToString() + " out of " + totalLoops.ToString() + " iterations are done!", "Stop?", MessageBoxButtons.YesNo) == DialogResult.Yes) { // cancel the process by user input! SetBooleanToFalse(boolTrigger); this.Message += "\nCanceled by user! :|"; return; } } //add the current slider values to our list of already computed values var sliderVals = GetSliderVals(sliders); if (!computedValues.Contains(sliderVals)) { computedValues.Add(sliderVals); } //move to the next set of slider positions if (!MoveToNextPermutation(ref idx, sliders)) { // study is over! SetBooleanToFalse(boolTrigger); sw.Stop(); //stop start watch UpdateProgressBar(counter, totalLoops, sw, pbChars); this.Message += "\nFinished at " + DateTime.Now.ToShortTimeString(); //wipe out colibri variables sliderNames = new List <string>(); sliderSteps = new List <int>(); sliderStepsPositions = new Dictionary <int, int>(); computedValues = new List <string>(); e.Document.NewSolution(false); Rhino.RhinoDoc.ActiveDoc.Views.Redraw(); break; } // We've just got a new valid permutation. Solve the new solution. counter++; e.Document.NewSolution(false); Rhino.RhinoDoc.ActiveDoc.Views.Redraw(); UpdateProgressBar(counter, totalLoops, sw, pbChars); } } catch { // "something went wrong!"; } finally { // Always make sure that _running is switched off. _running = false; } }
private void OnSolutionEnd(object sender, GH_SolutionEventArgs e) { // Unregister the event, we don't want to get called again. e.Document.SolutionEnd -= OnSolutionEnd; // If we're not supposed to run, abort now. if (!_run) { return; } // If we're already running, abort now. if (_running) { return; } // Reset run and running states. _run = false; _running = true; try { // Find the Guid for connected slides List <System.Guid> guids = new List <System.Guid>(); //empty list for guids GH.Kernel.IGH_Param selSlidersInput = this.Params.Input[0]; //ref for input where sliders are connected to this component IList <GH.Kernel.IGH_Param> sources = selSlidersInput.Sources; //list of things connected on this input bool isAnythingConnected = sources.Any(); //is there actually anything connected? // Find connected GH.Kernel.IGH_Param trigger = this.Params.Input[2].Sources[0]; //ref for input where a boolean or a button is connected GH.Kernel.Special.GH_BooleanToggle boolTrigger = trigger as GH.Kernel.Special.GH_BooleanToggle; if (isAnythingConnected) { //if something's connected, foreach (var source in sources) //for each of these connected things: { IGH_DocumentObject component = source.Attributes.GetTopLevel.DocObject; //for this connected thing, bring it into the code in a way where we can access its properties GH.Kernel.Special.GH_NumberSlider mySlider = component as GH.Kernel.Special.GH_NumberSlider; //...then cast (?) it as a slider if (mySlider == null) //of course, if the thing isn't a slider, the cast doesn't work, so we get null. let's filter out the nulls { continue; } guids.Add(mySlider.InstanceGuid); //things left over are sliders and are connected to our input. save this guid. //we now have a list of guids of sliders connected to our input, saved in list var 'mySlider' } } // Find all sliders. List <GH.Kernel.Special.GH_NumberSlider> sliders = new List <GH.Kernel.Special.GH_NumberSlider>(); foreach (IGH_DocumentObject docObject in doc.Objects) { GH.Kernel.Special.GH_NumberSlider slider = docObject as GH.Kernel.Special.GH_NumberSlider; if (slider != null) { // check if the slider is in the selected list if (isAnythingConnected) { if (guids.Contains(slider.InstanceGuid)) { sliders.Add(slider); } } else { sliders.Add(slider); } } } if (sliders.Count == 0) { System.Windows.Forms.MessageBox.Show("No sliders could be found", "<harsh buzzing sound>", MessageBoxButtons.OK); return; } //we now have all sliders //ask the user to give a sanity check string popupMessage = ""; int dummyCounter = 0; foreach (GH_NumberSlider slider in sliders) { totalLoops *= (sliderSteps[dummyCounter]); popupMessage += slider.ImpliedNickName; popupMessage += "\n"; dummyCounter++; } if (MessageBox.Show(sliders.Count + " slider(s) connected:\n" + popupMessage + "\n" + totalLoops.ToString() + " iterations will be done. Continue?" + "\n\n (Press ESC to pause during progressing!)", "Start?", MessageBoxButtons.YesNo) == DialogResult.No) { SetBooleanToFalse(boolTrigger); this.Message = "Release the Colibri!"; //wipe out colibri variables and compute a new solution sliderNames = null; sliderSteps = null; sliderStepsPositions = new int[sliderNames.Count]; //computedValues = new List<string>(); e.Document.NewSolution(false); Rhino.RhinoDoc.ActiveDoc.Views.Redraw(); return; } // Set all sliders back to first tick foreach (GH_NumberSlider slider in sliders) { slider.TickValue = 0; } //start a stopwatch System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); // Start a giant loop in which we'll permutate our way across all slider layouts. RunPermutations(sliderSteps, boolTrigger, sliders, e); } catch (Exception ex) { // "something went wrong!"; } finally { // Always make sure that _running is switched off. _running = false; } }
/// <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) { //Get Inputs this.Coeff.Clear(); this.SynthVals.Clear(); run = false; readSlidersList(); var RawCoeff = new GH_Structure <GH_Number>(); if (!DA.GetDataTree(1, out RawCoeff)) { return; } this.Coeff = StructureToListOfLists(RawCoeff); //DA.GetDataList<List<double>>(1, this.Coeff); DA.GetDataList <double>(2, this.SynthVals); DA.GetData(3, ref Scale); DA.GetData(4, ref MakeSliders); if (MakeSliders) { for (int i = 0; i < NumSynths; i++) { //instantiate objective sliders Grasshopper.Kernel.Special.GH_NumberSlider slid = new Grasshopper.Kernel.Special.GH_NumberSlider(); slid.CreateAttributes(); //sets up default values, and makes sure your sli //customise slider (position, ranges etc) slid.Attributes.Pivot = new PointF((float)this.Attributes.DocObject.Attributes.Bounds.Left - slid.Attributes.Bounds.Width - 30, (float)this.Params.Input[2].Attributes.Bounds.Y + i * 30); slid.Slider.Maximum = 1; slid.Slider.Minimum = -1; slid.Slider.DecimalPlaces = 3; slid.SetSliderValue((decimal)0); // This command makes it 'real' and adds it to the canvas. Grasshopper.Instances.ActiveCanvas.Document.AddObject(slid, false); //Connect the new slider to this component this.Params.Input[2].AddSource(slid); } } NumVars = SlidersList.Count; NumSynths = Coeff.Count; for (int i = 0; i < NumVars; i++) { var slider = SlidersList[i] as Grasshopper.Kernel.Special.GH_NumberSlider; //try to cast that thing as a slider decimal mid = (slider.Slider.Maximum + slider.Slider.Minimum) / 2; decimal range = slider.Slider.Maximum - slider.Slider.Minimum; decimal value = mid; for (int j = 0; j < NumSynths; j++) { value = value + (decimal)SynthVals[j] * (decimal)Coeff[j][i] * range / 2 * (decimal)Scale; } if (slider != null) //if the component was successfully cast as a slider { slider.SetSliderValue(value); } } run = true; if (run) { RefreshSliders(); } int test = 0; }