Пример #1
0
        public static GH_NumberSlider CreateNumbersilder(string name, decimal min, decimal max, int precision = 0, int length = 174)
        {
            var nS = new GH_NumberSlider();

            nS.ClearData();

            //Naming
            nS.Name     = name;
            nS.NickName = name;

            nS.Slider.Minimum = min;
            nS.Slider.Maximum = max;

            nS.Slider.DecimalPlaces = Axis.Util.LimitToRange(precision, 0, 12);

            if (precision == 0)
            {
                nS.Slider.Type = Grasshopper.GUI.Base.GH_SliderAccuracy.Integer;
            }
            else
            {
                nS.Slider.Type = Grasshopper.GUI.Base.GH_SliderAccuracy.Float;
            }

            nS.CreateAttributes();
            var bounds = nS.Attributes.Bounds;

            bounds.Width         = length;
            nS.Attributes.Bounds = bounds;

            nS.SetSliderValue(min);
            return(nS);
        }
        /// <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);
        }
 //UPDATE VALUE
 //Change variable's value and update its corresponding slider
 public void UpdateValue(double x)
 {
     try
     {
         Slider.SetSliderValue((decimal)x);
     }
     catch
     {
     }
 }
        public List <DesignVM> GenerateAndSelectTop(int number, int popsize, double rate)
        {
            // get new generation
            DesignVM      myFirstDesign = this.myDesignToolVM.InitialDesign;
            int           rowIndex      = this.DesignGrid.RowDefinitions.Count - 1;
            List <Design> seeds         = new List <Design>();

            if (rowIndex >= 0)
            {
                seeds = this.GetSeeds(rowIndex);
            }
            List <Design>   Designs          = EvolutionaryUtilities.NewGeneration(myFirstDesign.Design, seeds, EvolutionaryUtilities.NormalGenerator, popsize, rate);//, null);
            List <DesignVM> generatedDesigns = new List <DesignVM>();

            // Run designs through grasshopper solution
            foreach (Design design in Designs)
            {
                for (int i = 0; i < Component.Params.Input[2].Sources.Count; i++)
                {
                    GH_NumberSlider slider = Component.Params.Input[2].Sources[i] as GH_NumberSlider;
                    if (slider != null)
                    {
                        slider.SetSliderValue((decimal)design.DesignVariables[i].Value);
                    }
                }
                Grasshopper.Instances.ActiveCanvas.Document.NewSolution(true);
                List <Rhino.Geometry.Line>  lines  = Component.DesignLines; // write csv here??
                List <Rhino.Geometry.Curve> curves = Component.DesignCurves;
                List <Rhino.Geometry.Mesh>  meshes = Component.DesignMeshes;
                List <Rhino.Geometry.Brep>  breps  = Component.DesignBreps;

                double   normscore = Component.Score / this.myDesignToolVM.getinitscore();
                DesignVM genDesign = new DesignVM(lines, curves, meshes, breps, false, true, normscore, design.DesignClone());
                this.myDesignToolVM.ExplorationRec.AppendLine(csvWriter.writeDesign(genDesign));
                generatedDesigns.Add(genDesign);
            }
            // sort and identify top performers
            List <DesignVM> best = EvolutionaryUtilities.FindTopDesignsVM(generatedDesigns, number, rate);

            // Change main viewport design to top best design
            DesignVM topbest = best[0];

            for (int i = 0; i < Component.Params.Input[2].Sources.Count; i++)
            {
                GH_NumberSlider slider = Component.Params.Input[2].Sources[i] as GH_NumberSlider;
                if (slider != null)
                {
                    slider.SetSliderValue((decimal)topbest.Design.DesignVariables[i].Value);
                }
            }
            Grasshopper.Instances.ActiveCanvas.Document.NewSolution(true);

            // number = number of designs to select
            return(best);
        }
 private void ControlGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 {
     for (int i = 0; i < Component.Params.Input[2].Sources.Count; i++)
     {
         GH_NumberSlider slider = Component.Params.Input[2].Sources[i] as GH_NumberSlider;
         if (slider != null)
         {
             slider.SetSliderValue((decimal)this.myViewModel.Design.DesignVariables[i].Value);
         }
     }
     Grasshopper.Instances.ActiveCanvas.Document.NewSolution(true);
 }
Пример #6
0
 public static void ChangeSliders(List <GH_NumberSlider> sliders, List <double> targetVals)
 {
     if (sliders.Count != targetVals.Count)
     {
         throw new Exception("Error: Number of sliders and number of target values must be equal.");
     }
     for (int i = 0; i < sliders.Count; i++)
     {
         GH_NumberSlider s = sliders[i];
         double          d = targetVals[i];
         s.SetSliderValue((decimal)d);
     }
     Grasshopper.Instances.ActiveCanvas.Document.NewSolution(true);
 }
Пример #7
0
        // 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();
        }
Пример #8
0
        private IGH_Param CreateIntegerSlider(int d)
        {
            var slider = new GH_NumberSlider();

            slider.CreateAttributes();

            // setup the slider according to the grid
            // each slider account for one grid dimension
            var t0 = ghGrid.Value.Intervals[d].T0;
            var t1 = ghGrid.Value.Intervals[d].T1;

            slider.Slider.Type    = Grasshopper.GUI.Base.GH_SliderAccuracy.Integer;
            slider.Slider.Minimum = (decimal)t0;
            slider.Slider.Maximum = (decimal)t1 - 1;
            slider.SetSliderValue((decimal)(0.5 * (t0 + t1)));

            return(slider);
        }
        public override void Evaluate(Solution solution)
        {
            // Current Solution
            List <double> currentSolution = new List <double>();

            double[] storeVar = new double[NumberOfVariables];
            double[] storeObj = new double[NumberOfObjectives];
            XReal    x        = new XReal(solution);

            // Reading x values
            double[] xValues = new double[NumberOfVariables];
            for (int i = 0; i < NumberOfVariables; i++)
            {
                xValues[i] = x.GetValue(i);
                var1Value.Add(x.GetValue(0));
                var2Value.Add(x.GetValue(1));

                currentSolution.Add(x.GetValue(i)); // add current variable value to current solution
            }
            GH_NumberSlider currentSlider = null;

            for (int i = 0; i < component.readSlidersList().Count; i++)
            {
                currentSlider = component.readSlidersList()[i];
                currentSlider.SetSliderValue((decimal)x.GetValue(i));
                //Grasshopper.Instances.ActiveCanvas.Document.NewSolution(true); //<- NOTE: Is this the part that's re-evaluating after every slider change??
            }

            Grasshopper.Instances.ActiveCanvas.Document.NewSolution(true);

            for (int i = 0; i < component.objectives.Count; i++)
            {
                solution.Objective[i] = component.objectives[i];

                currentSolution.Add(component.objectives[i]); //adding current i-objective to current solution
            }
            //component.allSolutions = component.allSolutions +"" + component.objectives[i] + " ";

            allSolutions.Add(currentSolution);
        }
Пример #10
0
        //Methods

        //Automatically add a slider to the canvas
        void addWeightSlider(bool addSlider)
        {
            if (addSlider)
            {
                //Instantiate new slider
                GH_NumberSlider slider = new GH_NumberSlider();
                slider.CreateAttributes();                              //set to default values

                //Customisation of values
                int inputCount = this.Params.Input[1].SourceCount;      //count the number of connected inputs
                slider.Attributes.Pivot     = new PointF((float)this.Attributes.DocObject.Attributes.Bounds.Left - slider.Attributes.Bounds.Width - 30, (float)this.Params.Input[1].Attributes.Bounds.Y + inputCount * 30);
                slider.Slider.Minimum       = -1;
                slider.Slider.Maximum       = 1;
                slider.Slider.DecimalPlaces = 2;
                slider.SetSliderValue((decimal)0.00);
                slider.NickName = String.Format("w{0}", inputCount);

                //Add the slider to the canvas
                OnPingDocument().AddObject(slider, false);
                this.Params.Input[1].AddSource(slider);
            }
        }
Пример #11
0
        // Remote controller setting up the solution
        private void PrepareSolution(GH_Document gH_Document)
        {
            System.Collections.DictionaryEntry t = JobQueue.Cast <DictionaryEntry>().ElementAt(0);
            CurrentJobClient = ( string )t.Key;

            foreach (dynamic param in ( IEnumerable )t.Value)
            {
                IGH_DocumentObject controller = null;
                try
                {
                    controller = Document.Objects.First(doc => doc.InstanceGuid.ToString() == param.guid);
                }
                catch { }
                if (controller != null)
                {
                    switch (( string )param.inputType)
                    {
                    case "TextPanel":
                        GH_Panel panel = controller as GH_Panel;
                        panel.UserText = ( string )param.value;
                        panel.ExpireSolution(false);
                        break;

                    case "Slider":
                        GH_NumberSlider slider = controller as GH_NumberSlider;
                        slider.SetSliderValue(decimal.Parse(param.value.ToString()));
                        break;

                    case "Toggle":
                        break;

                    default:
                        break;
                    }
                }
            }
            SolutionPrepared = true;
        }
        /// <summary>
        /// /
        /// </summary>
        /// <param name="popsize"></param>
        /// <param name="mutrate"></param>
        /// <returns></returns>

        //public List<DesignVM> NewGeneration(int popsize, double mutrate)
        //{
        //    List<DesignVM> newgen = new List<DesignVM>();
        //    Random r = new Random();
        //    for (int i = 0; i < popsize; i++)
        //    {
        //        List<decimal> paramvalues = new List<decimal>();
        //        foreach (IGH_Param param in Component.Params.Input[2].Sources) // dvar is input 2
        //        {
        //            GH_NumberSlider slider = param as GH_NumberSlider;
        //            decimal n = r.Next(10);
        //            Console.WriteLine(n);
        //            if (slider != null)
        //                slider.SetSliderValue(n);
        //            paramvalues.Add(n);
        //        }
        //        Grasshopper.Instances.ActiveCanvas.Document.NewSolution(true);
        //        //ComputedStructure comp = (ComputedStructure)Component.compstruc.CloneImpl();
        //        List<Rhino.Geometry.Line> lines = Component.DesignLines;
        //        newgen.Add(new DesignVM(lines, false,true, Component.Score, paramvalues));
        //    }
        //    List<DesignVM> newgensorted = newgen.OrderBy(x => x.Score).ToList(); //here?
        //    return newgensorted;
        //}

        public List <DesignVM> GenDesignVMs(List <Design> listDesigns)
        {
            List <DesignVM> generatedDesigns = new List <DesignVM>();

            foreach (Design design in listDesigns)
            {
                for (int i = 0; i < Component.Params.Input[2].Sources.Count; i++)
                {
                    GH_NumberSlider slider = Component.Params.Input[2].Sources[i] as GH_NumberSlider;
                    if (slider != null)
                    {
                        slider.SetSliderValue((decimal)design.DesignVariables[i].Value);
                    }
                }
                Grasshopper.Instances.ActiveCanvas.Document.NewSolution(true);
                List <Rhino.Geometry.Line>  lines  = Component.DesignLines;
                List <Rhino.Geometry.Curve> curves = Component.DesignCurves;
                List <Rhino.Geometry.Mesh>  meshes = Component.DesignMeshes;
                List <Rhino.Geometry.Brep>  breps  = Component.DesignBreps;

                generatedDesigns.Add(new DesignVM(lines, curves, meshes, breps, false, true, Component.Score, design));
            }
            return(generatedDesigns);
        }
Пример #13
0
        public void PrepareSolution(IEnumerable args)
        {
            var x = args;

            foreach (dynamic param in args)
            {
                IGH_DocumentObject controller = null;
                try
                {
                    controller = Document.Objects.First(doc => doc.InstanceGuid.ToString() == param.guid);
                }
                catch { }

                if (controller != null)
                {
                    switch (( string )param.type)
                    {
                    case "TextPanel":
                        GH_Panel panel = controller as GH_Panel;
                        panel.UserText = ( string )param.value;
                        panel.ExpireSolution(false);
                        break;

                    case "Slider":
                        GH_NumberSlider slider = controller as GH_NumberSlider;
                        slider.SetSliderValue(decimal.Parse(param.value.ToString()));
                        break;

                    case "Point":
                        PointController p    = controller as PointController;
                        var             xxxx = p;
                        p.setParam(( double )param.value.X, ( double )param.value.Y, ( double )param.value.Z);
                        break;

                    case "Toggle":
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    try
                    {
                        if (param.type == "MaterialTable")
                        {
                            var MatOut = ( GH_Panel )Document.Objects.FirstOrDefault(doc => doc.NickName == "MAT_OUT");
                            if (MatOut != null)
                            {
                                string mats = "";
                                foreach (var layer in param.layers)
                                {
                                    try
                                    {
                                        mats += layer.name + ":" + layer.material + ":" + layer.price + "\n";
                                    }
                                    catch { }
                                }

                                MatOut.UserText = mats;
                                MatOut.ExpireSolution(true);
                            }
                        }
                    }
                    catch { }
                }
            }
            // TODO: Expire component
            Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponentAction);
        }