private void SetBooleanToFalse(GH.Kernel.Special.GH_BooleanToggle boolTrigger) { if (boolTrigger == null) { return; } boolTrigger.Value = false; //set trigger value to false boolTrigger.ExpireSolution(true); }
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; } }