Ejemplo n.º 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)
        {
            //Get imput curve
            Curve refCurve = null;

            DA.GetData(0, ref refCurve);

            //Get document

            docu = this.OnPingDocument();

            //Add all components to a new list and then filter to only selected elements
            List <IGH_DocumentObject> objects = new List <IGH_DocumentObject>();

            try
            {
                objects = docu.Objects.ToList <IGH_DocumentObject>();

                objects = GH_Document.FilterSelected(objects);
            }
            catch
            {
            }


            //If more than one component are selected run main logic

            if (objects.Count > 1)
            {
                int elements = objects.Count();

                //Get max and min coordinates of the existing components, this values will be used to remap
                // the points from the input curve

                float minX = objects[0].Attributes.Pivot.X;
                float maxX = objects[0].Attributes.Pivot.X;
                float minY = objects[0].Attributes.Pivot.Y;
                float maxY = objects[0].Attributes.Pivot.Y;



                foreach (IGH_DocumentObject obj in objects)
                {
                    if (obj != this)
                    {
                        if (obj.Attributes.Pivot.X < minX)
                        {
                            minX = obj.Attributes.Pivot.X;
                        }
                        if (obj.Attributes.Pivot.X > maxX)
                        {
                            maxX = obj.Attributes.Pivot.X;
                        }
                        if (obj.Attributes.Pivot.Y < minY)
                        {
                            minY = obj.Attributes.Pivot.Y;
                        }
                        if (obj.Attributes.Pivot.Y > maxY)
                        {
                            maxY = obj.Attributes.Pivot.Y;
                        }
                    }
                }


                //Only max amplitude (x or y) will be used to remap curve
                float xAmplitude = Math.Abs(minX - maxX);
                float yAmplitude = Math.Abs(minY - maxY);

                float minMapping;
                float maxMapping;

                if (xAmplitude >= yAmplitude)
                {
                    minMapping = minX;
                    maxMapping = maxX;
                }

                else
                {
                    minMapping = minY;
                    maxMapping = maxY;
                }


                //Get points along input curve
                refCurve.Domain = new Interval(0, 1);
                List <double> parameters = new List <double>();
                for (double i = 0; i < 1; i += 1.0 / elements)
                {
                    parameters.Add(i);
                }

                List <Point3d> insertionPoints = new List <Point3d>();



                List <double> Xs = new List <double>();
                List <double> Ys = new List <double>();

                //Add coordinates of the points
                foreach (double param in parameters)
                {
                    Point3d pt = refCurve.PointAt(param);
                    Xs.Add(pt.X);
                    Ys.Add(pt.Y);
                }

                List <double> mappedXs = new List <double>();
                List <double> mappedYs = new List <double>();

                double minFX = Xs.Min();
                double maxFX = Xs.Max();
                double minFY = Ys.Min();
                double maxFY = Ys.Max();

                //Remap values to the domain extracted from the components coordinates
                foreach (double x in Xs)
                {
                    mappedXs.Add(x.Remap(minFX, maxFX, minMapping, maxMapping));
                }

                foreach (double y in Ys)
                {
                    mappedYs.Add(y.Remap(minFY, maxFY, maxMapping, minMapping));
                }


                //Set the coordiantes of the components

                for (int i = 0; i < objects.Count(); i++)
                {
                    objects[i].Attributes.Pivot = new System.Drawing.PointF((float)mappedXs[i], (float)mappedYs[i]);
                }
            }
        }