Example #1
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List <double> manuallySteps = DA.FetchList <double>("steps");


            InputSelector inputSelector = new InputSelector(manuallySteps);

            DA.SetData(0, inputSelector);
        }
Example #2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            double?from  = DA.Fetch <double?>("From");
            double?to    = DA.Fetch <double?>("To");
            int    steps = DA.Fetch <int>("Steps");

            InputSelector inputSelector = new InputSelector(steps, from, to);

            DA.SetData(0, inputSelector);
        }
Example #3
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            #region updateInputs
            //if (!cap && this.Params.Input.Count ==7)
            //{
            //    this.Params.Input[5].RemoveAllSources();
            //    this.Params.UnregisterInputParameter(this.Params.Input[5]);
            //    this.Params.Input[6].RemoveAllSources();
            //    this.Params.UnregisterInputParameter(this.Params.Input[6]);

            //    Params.OnParametersChanged();
            //}
            //if (cap && this.Params.Input.Count == 5)
            //{
            //    this.Params.RegisterInputParam(new Param_Colour
            //    {
            //        Name = "MinColor",
            //        NickName = "MinColor",
            //        Description = "MinColor",
            //        Access = GH_ParamAccess.item,
            //        Optional = true
            //    });
            //    this.Params.RegisterInputParam(new Param_Colour
            //    {
            //        Name = "MaxColor",
            //        NickName = "MaxColor",
            //        Description = "MinColor",
            //        Access = GH_ParamAccess.item,
            //        Optional = true
            //    });

            //    Params.OnParametersChanged();
            //}

            #endregion updateInputs

            //bool caps = DA.Fetch<bool>("Cap");
            Color?maxColor   = DA.Fetch <Color?>(i_inputSelecterMax);
            Color?minColor   = DA.Fetch <Color?>(i_inputSelectorMin);
            var   allResults = DA.FetchTree <GH_Number>("Results");
            var   grids      = DA.FetchList <Grid>("Grids");
            //var gradientRange = DA.Fetch<string>("GradientRange");
            //int maxCount = DA.Fetch<int>("MaxCount");
            int maxCount = 200;
            //var inStepSize = DA.Fetch<int>("StepSize");
            //var inSteps = DA.Fetch<int>("Steps");
            InputSelector inputSelector = DA.Fetch <InputSelector>("_Section Type");

            double globalMin = double.MaxValue;
            double globalMax = double.MinValue;

            for (int g = 0; g < grids.Count; g++)
            {
                globalMin = Math.Min(globalMin, ((List <GH_Number>)allResults.get_Branch(g)).Select(r => r.Value).Min());
                globalMax = Math.Max(globalMax, ((List <GH_Number>)allResults.get_Branch(g)).Select(r => r.Value).Max());
            }


            if (inputSelector == null)
            {
                inputSelector = new InputSelector(10, globalMin, globalMax);
            }



            if (allResults.Branches.Count != grids.Count)
            {
                throw new Exception("Grid count doesnt match results");
            }


            //var colorDomain = Misc.AutoDomain(gradientRange, allResults);
            //Rhino.RhinoApp.WriteLine($"{range}  ->  {domain[0]} to {domain[1]}");

            GH_GradientControl gc;
            try
            {
                gc = (GH_GradientControl)Params.Input[i_inputGradient].Sources[0].Attributes.GetTopLevel.DocObject;
            }
            catch (System.ArgumentOutOfRangeException)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Remember to add a gradient component in grasshopper!");
                gc = null;
            }

            GradientParser gp = new GradientParser(gc)
            {
                //Cap = caps,
                AboveMax = maxColor == default(Color) ? null : maxColor,
                BelowMin = minColor == default(Color) ? null : minColor,
                //Min = domain[0],
                //Max = domain[1],
                Reverse = Params.Input[i_inputGradient].Reverse
            };



            IDictionary <string, Color> colorDescriptions = new Dictionary <string, Color>();
            IDictionary <string, int>   colorPaths        = new Dictionary <string, int>();



            #region coloredMesh
            var outMeshes = new List <Mesh>();



            for (int i = 0; i < grids.Count; i++)
            {
                //AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, $"Mesh vertices: {grids[i].SimMesh.Vertices.Count}, colors = {gp.GetColors(allResults.Branches[i].Select(p => p.Value).ToArray()).Length} f");


                outMeshes.Add(grids[i].GetColoredMesh(gp.GetColors(allResults.Branches[i].Select(p => p.Value).ToArray())));

                Mesh      m      = grids[i].SimMesh;
                Point3d[] points = grids[i].SimPoints.ToArray();
                outMeshes[outMeshes.Count - 1].Translate(0, 0, Units.ConvertFromMeter(0.001));
            }


            DA.SetDataList(0, outMeshes);

            #endregion coloredMesh



            #region layeredMesh

            if (grids[0].UseCenters == true)
            {
                return;
            }

            //Outputs
            GH_Structure <GH_Mesh>  oLayeredMeshes = new GH_Structure <GH_Mesh>();
            List <GH_Mesh>          previewMeshes  = new List <GH_Mesh>();
            List <GH_Plane>         outPlanes      = new List <GH_Plane>();
            GH_Structure <GH_Curve> outCurves      = new GH_Structure <GH_Curve>();

            GH_Structure <GH_String> outValues = new GH_Structure <GH_String>();
            GH_Structure <GH_Colour> outColors = new GH_Structure <GH_Colour>();

            const double SCALAR = 1; // don't change.



            if (((GH_Structure <GH_Number>)gc.Params.Input[1].VolatileData)[0][0].Value == 1)
            {
                this.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "The gradient connected has 1 as max. Is that on purpose? Check the inputs of your gradient component." +
                                       $"\nI suggest you set your max somewhere around {globalMax:0.0}");
            }


            for (int g = 0; g < grids.Count; g++)
            {
                //GH_Structure<GH_Curve> curves = new GH_Structure<GH_Curve>();
                Grid grid      = grids[g];
                Mesh inputMesh = grids[g].SimMesh.DuplicateMesh();
                //Mesh meshToCut = grids[g].SimMesh;

                List <double> results = ((List <GH_Number>)allResults.get_Branch(g)).Select(r => r.Value).ToList();

                if (grids[g].UseCenters == true)
                {
                    results = RTreeSolver.FindClosestWeightedValues(grids[g], results, true).ToList();
                    // ADD CONVERSION TODO:
                }

                inputMesh.Normals.ComputeNormals();

                Vector3f normal = inputMesh.FaceNormals[0];

                Plane basePlane = new Plane(inputMesh.Vertices[0], normal);

                Transform ProjectToBase = Transform.PlanarProjection(basePlane);

                Plane cuttingPlane = new Plane(basePlane);

                Mesh meshToCut = CreateMeshToBeCut(SCALAR, inputMesh, results, cuttingPlane);

                previewMeshes.Add(new GH_Mesh(inputMesh));

                MeshingParameters mp = new MeshingParameters(0);

                List <Mesh> layeredMeshesThisGrid = new List <Mesh>();


                double valueForSmallAreas = double.MinValue;

                double resultsMin = results.Min();

                foreach (var item in inputSelector)
                {
                    if (resultsMin >= item)
                    {
                        valueForSmallAreas = item;
                        break;
                    }
                }

                //Color col = gp.GetColors(new List<double>() { inputSelector.Min.Value })[0];
                Color col = gp.GetColors(new List <double>()
                {
                    gp.BelowMin.HasValue&& inputSelector.Min.Value <= gp.Min ? resultsMin > gp.Min ? valueForSmallAreas : double.MinValue :  inputSelector.Min.Value
                })[0];

                Polyline[] outlinePolylines = inputMesh.GetNakedEdges();

                PolylineCurve[] curvesFromOutline = new PolylineCurve[outlinePolylines.Length];

                for (int i = 0; i < outlinePolylines.Length; i++)
                {
                    curvesFromOutline[i] = new PolylineCurve(outlinePolylines[i]);
                    curvesFromOutline[i].Transform(ProjectToBase);
                }


                Mesh meshFromCurves = GetMeshFromCurves(curvesFromOutline, mp, in col);

                GH_Path startPath = new GH_Path(g, -1);
                oLayeredMeshes.Append(new GH_Mesh(meshFromCurves), startPath);


                string lessThanKey = gp.BelowMin.HasValue && inputSelector.Min.Value < gp.Min ? $"<{gp.Min:0.0}" : $"<{inputSelector.Min.Value:0.0}";
                if (!colorDescriptions.ContainsKey(lessThanKey) && inputSelector.First() < gp.Min)
                {
                    colorDescriptions.Add(lessThanKey, col);
                    colorPaths.Add(lessThanKey, -1);
                }

                ////outColors.Append(new GH_Colour(col), startPath);
                ////outValues.Append(new GH_Number(double.MinValue), startPath);

                //Mesh[] meshesFromCurves = GetMeshesFromCurves(curvesFromOutline, mp, in col);

                //oLayeredMeshes.AppendRange(meshesFromCurves.Select(m => new GH_Mesh(m)), new GH_Path(g, -1));



                int    cuttingCount  = 0;
                double previousValue = 0;

                foreach (double currentValue in inputSelector)
                {
                    if (cuttingCount > maxCount)
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Too many steps... I reached  {maxCount} and then stopped");
                        break;
                    }

                    if (gp.BelowMin.HasValue && currentValue < gp.Min)
                    {
                        continue;
                    }


                    if (currentValue > results.Max())
                    {
                        break;
                    }


                    // Create planes

                    Vector3f moveUpVector = normal * (float)((currentValue - previousValue) * SCALAR);

                    Transform t = Transform.Translation(moveUpVector);

                    GH_Path path = new GH_Path(g, cuttingCount);

                    cuttingPlane.Transform(t);

                    outPlanes.Add(new GH_Plane(cuttingPlane));



                    // Create boundary intersected curves

                    Curve[] intersectedCurves = GetIntersectedCurves(inputMesh, cuttingPlane);



                    if (intersectedCurves != null)
                    {
                        outCurves.AppendRange(intersectedCurves.Select(c => new GH_Curve(c.DuplicateCurve())), path);

                        foreach (var curve in intersectedCurves)
                        {
                            curve.Transform(ProjectToBase);
                        }


                        // Create meshes

                        col = gp.GetColors(new List <double>()
                        {
                            currentValue
                        })[0];



                        meshFromCurves = GetMeshFromCurves(intersectedCurves, mp, in col);

                        meshFromCurves.Transform(Transform.Translation(0, 0, (cuttingCount + 1) * Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance * 12.0));



                        if (meshFromCurves != null)
                        {
                            //oLayeredMeshes.AppendRange(meshesFromCurves.Select(m => new GH_Mesh(m)), path);
                            oLayeredMeshes.Append(new GH_Mesh(meshFromCurves), path);
                            string key = currentValue >= gp.Max.Value ? $">{currentValue:0.0}" : $"{currentValue:0.0}";
                            if (!colorDescriptions.ContainsKey(key))
                            {
                                colorDescriptions.Add(key, col);
                                colorPaths.Add(key, cuttingCount);
                            }
                        }

                        if (currentValue >= gp.Max.Value)
                        {
                            break;
                        }
                    }



                    previousValue = currentValue;

                    cuttingCount++;
                }
            }

            foreach (KeyValuePair <string, Color> valuePair in colorDescriptions)
            {
                GH_Path path = new GH_Path(colorPaths[valuePair.Key]);


                outColors.Append(new GH_Colour(valuePair.Value), path);
                outValues.Append(new GH_String(valuePair.Key), path);
            }



            DA.SetDataTree(1, oLayeredMeshes);
            DA.SetDataTree(2, outCurves);
            DA.SetDataList("Planes", outPlanes);
            DA.SetDataList("TempMeshes", previewMeshes);
            DA.SetDataTree(6, outValues);
            DA.SetDataTree(5, outColors);

            #endregion layeredMesh
        }