Exemplo 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)
        {
            IGH_Component Component           = this;
            GH_Document   GrasshopperDocument = this.OnPingDocument();

            // Create input parameters
            bool   trigger     = false;
            string sourceGroup = null;
            string targetGroup = null;
            int    sourceIndex = 0;
            int    targetIndex = 0;

            // Assign input data
            DA.GetData(0, ref sourceGroup);
            DA.GetData(1, ref targetGroup);
            DA.GetData(2, ref sourceIndex);
            DA.GetData(3, ref targetIndex);
            DA.GetData(4, ref trigger);


            // --------------------------------------------------------------------------------------------------------------------------
            if (!trigger)
            {
                return;
            }

            List <IGH_Param> targetParam = new List <IGH_Param>();
            List <IGH_Param> sourceParam = new List <IGH_Param>();

            foreach (IGH_DocumentObject docObject in GrasshopperDocument.Objects) //for every GH component in document
            {
                // if the pathname of the actual object is the same as the Pathname of the targetgroup
                // put the docObject in the group "gp" as a GH special group
                // then take out the Params of the target objects
                if (docObject.Attributes.PathName == "Group (" + targetGroup + ")")
                {
                    Grasshopper.Kernel.Special.GH_Group gp = docObject as Grasshopper.Kernel.Special.GH_Group;
                    targetParam = getParams(gp.Objects(), targetIndex, sourceIndex, true);
                }
                // the same as above but for the source objects
                else if (docObject.Attributes.PathName == "Group (" + sourceGroup + ")")
                {
                    Grasshopper.Kernel.Special.GH_Group gp = docObject as Grasshopper.Kernel.Special.GH_Group;
                    sourceParam = getParams(gp.Objects(), sourceIndex, sourceIndex, false);
                }
            }

            int nbI = targetParam.Count();
            int nbO = sourceParam.Count();
            int nb  = Math.Max(nbI, nbO);

            for (int i = 0; i < nb; i++)
            {
                targetParam[i % nbI].AddSource(sourceParam[i % nbO]);
            }
        }
Exemplo n.º 2
0
        private void AddToggle()
        {
            var toggle = new GH.Kernel.Special.GH_BooleanToggle();

            toggle.CreateAttributes();
            toggle.Value            = false;
            toggle.NickName         = "Release the fly...";
            toggle.Attributes.Pivot = new PointF((float)(this.Attributes.Bounds.Left - 200), (float)(this.Attributes.Bounds.Top + 30));
            doc.AddObject(toggle, false);
            this.Params.Input[1].AddSource(toggle);
            toggle.ExpireSolution(true);

            grp = new GH.Kernel.Special.GH_Group();
            grp.CreateAttributes();
            grp.Border = GH.Kernel.Special.GH_GroupBorder.Blob;
            grp.AddObject(toggle.InstanceGuid);
            grp.Colour   = System.Drawing.Color.IndianRed;
            grp.NickName = "";
            doc.AddObject(grp, false);
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
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)
        {   ///The logic for finding the different chains is far from perfect
            docu = this.OnPingDocument();

            //Get al components in the document
            List <IGH_DocumentObject> objects = new List <IGH_DocumentObject>();

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

            //Filter those components to only get active components (remove groups, scribbles etc)
            List <IGH_ActiveObject> activeObjects = new List <IGH_ActiveObject>();

            foreach (IGH_DocumentObject obji in objects)
            {
                IGH_ActiveObject actiObj = obji as IGH_ActiveObject;
                if (actiObj != null & obji != this)
                {
                    activeObjects.Add(actiObj);
                }
            }


            //Sort element in chains
            List <List <IGH_ActiveObject> > chains = new List <List <IGH_ActiveObject> >();

            while (activeObjects.Count > 0)
            {
                //Start with the first document of the list

                IGH_ActiveObject        obj   = activeObjects[0];
                List <IGH_ActiveObject> chain = new List <IGH_ActiveObject>();
                //List<IGH_DocumentObject> longest_set = new List<IGH_DocumentObject>();

                //Retrieve all the components downstream and get last elements, once the last elements is found
                //all the upstream elements will be colected from here
                List <IGH_ActiveObject> downObjects = docu.FindAllDownstreamObjects(obj);
                downObjects.Insert(0, obj);



                //Retrieve all the component upstream from the last element, this process is repeated until
                // all the downstream elements have been added to the chain.
                while (downObjects.Count != 0)
                {
                    IGH_ActiveObject lastElement = downObjects[downObjects.Count - 1];

                    List <IGH_DocumentObject> upstream = new List <IGH_DocumentObject>();
                    Helpers.UpStreamObjects(upstream, lastElement, docu);
                    foreach (GH_DocumentObject ob in upstream)
                    {
                        IGH_ActiveObject ob_Active = ob as IGH_ActiveObject;
                        downObjects.Remove(ob_Active);
                        activeObjects.Remove(ob_Active);
                        if (!chain.Contains(ob_Active))
                        {
                            chain.Add(ob_Active);
                        }
                    }
                }

                //Add the chain to the collection of chains
                chains.Add(chain);
            }

            //Previous logic is quite shitty and you may still end up with two chains which are not totally independant,
            //To fix this chains are compared to see if any component if common to both, if that happend both chains are merged.
            //List<List<IGH_ActiveObject>> cleanChains = new List<List<IGH_ActiveObject>>();

            List <List <IGH_ActiveObject> > cleanChains = new List <List <IGH_ActiveObject> >();

            while (chains.Count > 0)
            {
                bool flag = true;
                List <IGH_ActiveObject> checkingChain = chains[0];
                for (int i = 1; i < chains.Count; i++)
                {
                    List <IGH_ActiveObject> otherChain = chains[i];
                    if (checkingChain.Intersect(otherChain).Any())
                    {
                        flag = false;
                        foreach (IGH_ActiveObject chainObj in otherChain)
                        {
                            if (!checkingChain.Contains(chainObj))
                            {
                                checkingChain.Add(chainObj);
                            }
                            chains.Remove(otherChain);
                        }
                        break;
                    }
                }

                if (flag)
                {
                    cleanChains.Add(checkingChain);
                    chains.Remove(checkingChain);
                }
            }

            chains = cleanChains;
            //Group each of the chains
            int    counter = 1;
            Random random  = new Random();

            foreach (List <IGH_ActiveObject> chain in chains)
            {
                Grasshopper.Kernel.Special.GH_Group group = new Grasshopper.Kernel.Special.GH_Group();

                group.Colour   = System.Drawing.Color.FromArgb(200, (int)(random.NextDouble() * 255), (int)(random.NextDouble() * 255), (int)(random.NextDouble() * 255));
                group.Name     = string.Format(("Chain {0}"), counter.ToString());
                group.NickName = string.Format(("Chain {0}"), counter.ToString());
                docu.AddObject(group, false, docu.ObjectCount);

                foreach (IGH_ActiveObject obj in chain)
                {
                    group.AddObject(obj.Attributes.InstanceGuid);
                }

                group.ExpireCaches();
                counter += 1;
            }
        }
Exemplo 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();
        }
Exemplo n.º 7
0
  private void AddToggle()
  {
    var toggle = new Grasshopper.Kernel.Special.GH_BooleanToggle();
    toggle.CreateAttributes();
    toggle.Value = false;
    toggle.NickName = "Release the fly...";
    toggle.Attributes.Pivot = new PointF((float) (Component.Attributes.Bounds.Left - 200), (float) (Component.Attributes.Bounds.Top + 30));
    GrasshopperDocument.AddObject(toggle, false);
    Component.Params.Input[1].AddSource(toggle);
    toggle.ExpireSolution(true);

    grp = new Grasshopper.Kernel.Special.GH_Group();
    grp.CreateAttributes();
    grp.Border = Grasshopper.Kernel.Special.GH_GroupBorder.Blob;
    grp.AddObject(toggle.InstanceGuid);
    grp.Colour = System.Drawing.Color.IndianRed;
    grp.NickName = "";
    GrasshopperDocument.AddObject(grp, false);
  }