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 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;
            }
        }
Esempio n. 3
0
        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;
            }
        }
Esempio n. 4
0
        // This is the method that actually does the work.
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // Creating this component in the grasshopperdocument
            IGH_Component Component           = this;
            GH_Document   GrasshopperDocument = this.OnPingDocument();

            // Creating input parameters
            //object component = null;
            int    nbCopy    = 0;
            string groupName = null;
            bool   trigger   = false;

            // Getting the data from Grasshopper
            //DA.GetData<object>(0, ref component);
            DA.GetData <string>(1, ref groupName);
            DA.GetData <int>(2, ref nbCopy);
            DA.GetData <bool>(3, ref trigger);

            // If the botton is pressed it will proceed
            if (!trigger)
            {
                return;
            }

            Grasshopper.Kernel.IGH_Param         selNumsInput = Component.Params.Input[0];
            IList <Grasshopper.Kernel.IGH_Param> sources      = selNumsInput.Sources;

            if (!sources.Any())
            {
                return;
            }
            IGH_DocumentObject comp = sources[0].Attributes.GetTopLevel.DocObject;

            // Gets component attributes like the bounds of the component which is used to shift
            // the next one and get the size of the panels
            IGH_Attributes att    = comp.Attributes;
            RectangleF     bounds = att.Bounds;
            int            vShift = (int)Math.Round(bounds.Height) + 10;
            int            vStart = 30 + vShift;

            List <Guid> objectsToCopy = new List <Guid>();

            objectsToCopy.Add(comp.InstanceGuid);

            // Creating a Grasshopper Group g and assignning a nickname and color to it.
            // Adding group g to the GrasshopperDocument
            Grasshopper.Kernel.Special.GH_Group g = new Grasshopper.Kernel.Special.GH_Group();
            g.NickName = groupName;
            g.Colour   = Grasshopper.GUI.Canvas.GH_Skin.group_back;
            GrasshopperDocument.AddObject(g, false);
            List <IGH_Component> components = new List <IGH_Component>();

            // For-loop used to duplicate component and to assign properties to it (size, datalist...)
            for (int i = 0; i < nbCopy; i++)
            {
                GH_DocumentIO documentIO = new GH_DocumentIO(GrasshopperDocument);
                documentIO.Copy(GH_ClipboardType.System, objectsToCopy);
                documentIO.Paste(GH_ClipboardType.System);

                documentIO.Document.TranslateObjects(new Size(0, vStart + i * vShift), false);
                documentIO.Document.SelectAll();
                documentIO.Document.MutateAllIds();

                GrasshopperDocument.DeselectAll();
                GrasshopperDocument.MergeDocument(documentIO.Document);
                g.AddObject(documentIO.Document.Objects[0].InstanceGuid);
            }
            GrasshopperDocument.DeselectAll();
        }
Esempio n. 5
0
        // This is the method that actually does the work.
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            IGH_Component Component           = this;
            GH_Document   GrasshopperDocument = this.OnPingDocument();

            // Creating input parameters
            List <string> data      = new List <string>();
            string        groupName = "";
            bool          trigger   = false;
            object        template  = null;

            // Getting the data from Grasshopper
            DA.GetDataList <string>(0, data);
            DA.GetData <string>(1, ref groupName);
            DA.GetData <object>(2, ref template);
            DA.GetData <bool>(3, ref trigger);

            // If the botton is pressed it will proceed
            if (!trigger)
            {
                return;
            }

            // Detecting the the source parameter for the templateInput
            Grasshopper.Kernel.IGH_Param         templateInput = Component.Params.Input[2];
            IList <Grasshopper.Kernel.IGH_Param> sources       = templateInput.Sources;

            if (!sources.Any())
            {
                return;
            }
            IGH_DocumentObject templateComp = sources[0].Attributes.GetTopLevel.DocObject;


            // Gets component attributes like the bounds of the Panel which is used to shift
            //the next one and get the size of the panels
            IGH_Attributes att    = templateComp.Attributes;
            RectangleF     bounds = att.Bounds;
            int            vShift = (int)Math.Round(bounds.Height) + 10;
            float          refX   = bounds.X;
            float          refY   = bounds.Y + 30 + vShift;

            // Creating a Grasshopper Group g and assignning a nickname and color to it.
            //Adding group g to the GrasshopperDocument
            Grasshopper.Kernel.Special.GH_Group g = new Grasshopper.Kernel.Special.GH_Group();
            g.NickName = groupName;
            g.Colour   = Grasshopper.GUI.Canvas.GH_Skin.group_back;
            GrasshopperDocument.AddObject(g, false);
            List <IGH_Component> components = new List <IGH_Component>();


            // For-loop used to create panels and assign properties to it (size, datalist...)
            int nbCopy = data.Count;

            for (int i = 0; i < nbCopy; i++)
            {
                Grasshopper.Kernel.Special.GH_Panel panel = new Grasshopper.Kernel.Special.GH_Panel();
                panel.CreateAttributes();
                panel.SetUserText(data[i]);
                panel.Attributes.Bounds = bounds;
                panel.Attributes.Pivot  = new PointF(refX, refY + i * vShift);
                GrasshopperDocument.AddObject(panel, false);

                g.AddObject(panel.InstanceGuid);
            }
            GrasshopperDocument.DeselectAll();
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            IGH_Component Component           = this;
            GH_Document   GrasshopperDocument = this.OnPingDocument();


            List <object> data      = new List <object>();
            string        groupName = "";
            bool          trigger   = false;
            object        template  = null;

            DA.GetDataList <object>(0, data);
            DA.GetData <string>(1, ref groupName);
            DA.GetData <object>(2, ref template);
            DA.GetData <bool>(3, ref trigger);


            // Trigger input
            if (!trigger)
            {
                return;
            }

            // Taking out the position and attributes of the template panel
            Grasshopper.Kernel.IGH_Param         templateInput = Component.Params.Input[2];
            IList <Grasshopper.Kernel.IGH_Param> sources       = templateInput.Sources;

            if (!sources.Any())
            {
                return;
            }
            IGH_DocumentObject templateComp = sources[0].Attributes.GetTopLevel.DocObject;
            IGH_Attributes     att          = templateComp.Attributes;



            // taking out the measures from the template panel and adding a shift
            RectangleF bounds = att.Bounds;
            int        vShift = (int)Math.Round(bounds.Height) + 10;
            float      refX   = bounds.X;
            float      refY   = bounds.Y + 30 + vShift;

            int nbCopy = data.Count;


            // Creating a group, naming it, assigning color, adding it to the document
            Grasshopper.Kernel.Special.GH_Group g = new Grasshopper.Kernel.Special.GH_Group();
            g.NickName = groupName;
            g.Colour   = Grasshopper.GUI.Canvas.GH_Skin.group_back;
            GrasshopperDocument.AddObject(g, false);

            // Putting in all the new components in the document and grouping them
            for (int i = 0; i < nbCopy; i++)
            {
                Grasshopper.Kernel.Parameters.Param_GenericObject comp = new Grasshopper.Kernel.Parameters.Param_GenericObject();
                comp.CreateAttributes();
                comp.SetPersistentData(data[i]);
                float w = comp.Attributes.Bounds.Width;
                comp.Attributes.Pivot = new PointF(refX + w / 2, refY + i * vShift);
                GrasshopperDocument.AddObject(comp, false);
                g.AddObject(comp.InstanceGuid);
            }


            GrasshopperDocument.DeselectAll();
        }