Grasshopper Plugin Dev: 03 Modifying the Template

Software: 
Video Duration: 
15 minutes
Author: 
Zach Downey

In this tutorial, we modify the Grasshopper plugin template to add additional parameters to the spiral component. We begin by investigating how the current spiral code works and test how we might make the spiral go in the opposite direction. It turns out that flipping the spiral is a pretty easy thing to accomplish, we just need to change some plane/vector directions and we can do this by switching the plus (+) and minus (-) signs in the CreateSpiral() method. Because we would like to be able to control this flip we need to handle two separate cases--flipped and un-flipped. We will write a simple if statement to handle this. We will need to add a new parameter to our component that will allow us to flip the spiral, and a boolean (true/false) parameter will do nicely. In order for this to work we need make sure we let all of our other methods know what we are doing, so we need to modify the RegisterInputParams() and the SolveInstance() methods to add our new input to the component. See the modified code below. 

//New CreateSpiral() method
 private Curve CreateSpiral(Plane plane, double r0, double r1, Int32 turns, bool flip)
        {
            Line l0 = new Line(plane.Origin + r0 * plane.XAxis, plane.Origin + r1 * plane.XAxis);
            Line l1 = new Line(plane.Origin - r0 * plane.XAxis, plane.Origin - r1 * plane.XAxis);
 
            Point3d[] p0;
            Point3d[] p1;
 
            l0.ToNurbsCurve().DivideByCount(turns, true, out p0);
            l1.ToNurbsCurve().DivideByCount(turns, true, out p1);
 
            PolyCurve spiral = new PolyCurve();
 
            for (int i = 0; i < p0.Length - 1; i++)
            {
 
                Arc arc0;
                Arc arc1;
 
                if (flip == false) { 
                    arc0 = new Arc(p0[i], plane.YAxis, p1[i + 1]);
                    arc1 = new Arc(p1[i + 1], -plane.YAxis, p0[i + 1]);
                }
                else
                {
                    arc0 = new Arc(p0[i], -plane.YAxis, p1[i + 1]);
                    arc1 = new Arc(p1[i + 1], plane.YAxis, p0[i + 1]);
                }
 
                spiral.Append(arc0);
                spiral.Append(arc1);
            }
 
            return spiral;
        }
//New SolveInstance() method
protected override void SolveInstance(IGH_DataAccess DA)
        {
            // First, we need to retrieve all data from the input parameters.
            // We'll start by declaring variables and assigning them starting values.
            Plane plane = Plane.WorldXY;
            double radius0 = 0.0;
            double radius1 = 0.0;
            int turns = 0;
            bool flip = false;
 
            // Then we need to access the input parameters individually. 
            // When data cannot be extracted from a parameter, we should abort this method.
            if (!DA.GetData(0, ref plane)) return;
            if (!DA.GetData(1, ref radius0)) return;
            if (!DA.GetData(2, ref radius1)) return;
            if (!DA.GetData(3, ref turns)) return;
            if (!DA.GetData(4, ref flip)) return;
 
            // We should now validate the data and warn the user if invalid data is supplied.
            if (radius0 < 0.0)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Inner radius must be bigger than or equal to zero");
                return;
            }
            if (radius1 <= radius0)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Outer radius must be bigger than the inner radius");
                return;
            }
            if (turns <= 0)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Spiral turn count must be bigger than or equal to one");
                return;
            }
 
            // We're set to create the spiral now. To keep the size of the SolveInstance() method small, 
            // The actual functionality will be in a different method:
            Curve spiral = CreateSpiral(plane, radius0, radius1, turns, flip);
 
            // Finally assign the spiral to the output parameter.
            DA.SetData(0, spiral);
        }
//Add the following line to the RegisterInputParams() method
 
pManager.AddBooleanParameter("Flip", "F", "Flips the direction of the spiral", GH_ParamAccess.item, false);

Rating

Please rate this tutorial below. Thanks.

5
Average: 4.8 (5 votes)

Comments

yep......that's good tutorial
Thanks a lot for your effort :) Are you going to continue the tutorials for "Developing Your Own Grasshopper Plugin"? What if I want to create a Grasshopper plugin which can open up a windows form like "Galapagos" Also I wanna know if I can call other component functions within my plugin?

Want to Contribute?

Want to be an author? Drop us a line here we'd love to have you.

Already have a video you'd like to post? Send us a link and we'll get you going.

:)