Beispiel #1
0
 public bool GetInput(int dataAccessIndex, out Grasshopper.Kernel.Data.GH_Structure <GH_Point> dataTree, bool checkNull, bool checkEmpty, bool silent)
 {
     if (!this.dataAccess.GetDataTree <GH_Point>(dataAccessIndex, out dataTree))
     {
         if (!silent)
         {
             Print("DataAccess.GetData() failed at index: " + dataAccessIndex.ToString() + ".");
         }
         return(false);
     }
     if (checkNull)
     {
         if (dataTree == null)
         {
             if (!silent)
             {
                 Print("DataAccess.GetData(" + dataAccessIndex.ToString() + ", dataTree) returned null.");
             }
             return(false);
         }
     }
     if (checkEmpty)
     {
         if (dataTree.Branches.Count == 0)
         {
             if (!silent)
             {
                 Print("DataAccess.GetData() returned data tree with no branches at index: " + dataAccessIndex.ToString() + ".");
             }
             return(false);
         }
     }
     return(true);
 }
Beispiel #2
0
 public bool GetInput(int dataAccessIndex, out Grasshopper.Kernel.Data.GH_Structure <GH_Point> dataTree)
 {
     return(GetInput(dataAccessIndex, out dataTree, true, true, false));
 }
        /// <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)
        {
            Grasshopper.Kernel.Data.GH_Structure <IGH_Goo> elementsToAdd = new Grasshopper.Kernel.Data.GH_Structure <IGH_Goo>();
            double        width          = 0;
            double        height         = 0;
            List <string> rowDefinitions = new List <string>();
            List <string> colDefinitions = new List <string>();
            List <int>    memberships    = new List <int>();


            if (!DA.GetDataTree <IGH_Goo>(0, out elementsToAdd))
            {
                return;
            }
            bool hasMemberships = DA.GetDataList <int>("Grid Membership", memberships);
            bool hasWidth       = DA.GetData <double>("Width", ref width);
            bool hasHeight      = DA.GetData <double>("Height", ref height);

            bool hasRowDefs = DA.GetDataList <string>("Row Definitions", rowDefinitions);
            bool hasColDefs = DA.GetDataList <string>("Column Definitions", colDefinitions);

            if (hasMemberships)
            {
                if (memberships.Count != elementsToAdd.Branches.Count)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Grid Membership list length must equal the number of Elements branches.");
                    return;
                }
            }
            else
            {
                // Create a default list of a single branch grouping to apply to all trees
                memberships = new List <int>()
                {
                    0
                };
            }

            int[] defaultMembership = { 0 };

            // Create an array of the membership values
            int[] membershipArray = hasMemberships ? memberships.ToArray() : defaultMembership;

            // Create empty list of branch metadata and populate with values
            List <BranchMetadata> branchMetaData = new List <BranchMetadata>();

            for (int i = 0; i < elementsToAdd.Branches.Count(); i++)
            {
                int myMembership = hasMemberships ? membershipArray[i] : 0;

                BranchMetadata bm = new BranchMetadata(i, myMembership);
                branchMetaData.Add(bm);
            }

            // Sort and group the metadata
            var branchGroupings = branchMetaData
                                  .OrderBy(b => b.membershipIndex)
                                  .GroupBy(b => b.membershipIndex);


            // create an empty List of grids and populate

            List <Grid> grids = new List <Grid>();

            foreach (var group in branchGroupings)
            {
                //initialize a grid
                Grid grid = new Grid();
                grid.HorizontalAlignment = HorizontalAlignment.Left;
                grid.VerticalAlignment   = VerticalAlignment.Top;
                grid.Name = "GH_Grid";
                if (hasWidth)
                {
                    grid.Width = width;
                }
                else
                {
                    grid.HorizontalAlignment = HorizontalAlignment.Stretch;
                }
                if (hasHeight)
                {
                    grid.Height = height;
                }
                else
                {
                    grid.VerticalAlignment = VerticalAlignment.Stretch;
                }

                // Add grid to List
                grids.Add(grid);
            }

            // Populate the Grids (should this be done before adding the grids to the List?)

            foreach (var group in branchGroupings)
            {
                // Count the number of branches in this array
                int currentBranchCount = group.Count();

                //set up a "GridLengthConverter" to handle parsing our strings.
                GridLengthConverter gridLengthConverter = new GridLengthConverter();

                //set up rows and columns if present
                if (hasColDefs)
                {
                    for (int i = 0; i < currentBranchCount; i++)
                    {
                        ColumnDefinition cd = new ColumnDefinition();
                        cd.Width = (GridLength)gridLengthConverter.ConvertFromString(colDefinitions[i % colDefinitions.Count]);  // use repeating pattern of supplied list
                        // Note: group.Key is the index of the group/grid
                        grids[group.Key].ColumnDefinitions.Add(cd);
                    }
                }

                if (hasRowDefs)
                {
                    int maxCount = 0;

                    // Find the count of the longest list
                    foreach (BranchMetadata md in group)
                    {
                        // get the count of data from the branch
                        var myCount = elementsToAdd.get_Branch(elementsToAdd.get_Path(md.branchIndex)).Count;

                        if (myCount > maxCount)
                        {
                            maxCount = myCount;
                        }
                    }

                    // Build up the row heights based on a repeating pattern
                    for (int i = 0; i < maxCount; i++)
                    {
                        RowDefinition rd = new RowDefinition();
                        rd.Height = (GridLength)gridLengthConverter.ConvertFromString(rowDefinitions[i % rowDefinitions.Count]);  // use repeating pattern of supplied list
                        // Note: group.Key is the index of the group/grid
                        grids[group.Key].RowDefinitions.Add(rd);
                    }
                }

                // Set up a counter for iterating through the appropriate number of columns
                int currentColumn = 0;

                // Populate the Grids with Elements
                foreach (BranchMetadata md in group)
                {
                    // Get each branch referenced in the metadata
                    var branch = elementsToAdd.get_Branch(elementsToAdd.get_Path(md.branchIndex));


                    //for all the elements in each branch
                    for (int j = 0; j < branch.Count; j++)
                    {
                        UIElement_Goo u = branch[j] as UIElement_Goo;
                        //make sure it doesn't already have a parent
                        HUI_Util.removeParent(u.element);
                        FrameworkElement fe = u.element as FrameworkElement;
                        if (fe != null)
                        {
                            // set its alignment to stretch
                            // this will allow elements like sliders, pulldowns, and separators to fill the cell
                            fe.HorizontalAlignment = HorizontalAlignment.Stretch;
                            fe.VerticalAlignment   = VerticalAlignment.Stretch;

                            //set up row and column positioning
                            Grid.SetColumn(fe, currentColumn);
                            Grid.SetRow(fe, j);
                        }

                        //add it to the grid
                        grids[group.Key].Children.Add(u.element);
                    }

                    // Increment the column index
                    currentColumn++;
                }
            }

            // Create the list of Elements and add each grid
            List <UIElement_Goo> output = new List <UIElement_Goo>();

            foreach (Grid g in grids)
            {
                output.Add(new UIElement_Goo(g, "Simple Grid", InstanceGuid, DA.Iteration));
            }

            //pass the grids out
            DA.SetDataList("Simple Grid", output);
        }