An example offset curve in Grasshopper

How to offset a curve in Grasshopper using the C# component

An example offset curve in Grasshopper
How to generate an offset curve in Grasshopper using only C#, either in the C# component or with Visual Studio:

NurbsCurve crv = new NurbsCurve(inputCurve.ToNurbsCurve()); //input curve to use as offset base
Point3d origin = new Point3d(0, 0, 0);
Vector3d vectxy = new Vector3d(0, 0, 1);
Plane xy = new Plane(origin, vectxy);
Curve[] crv4 = crv.Offset(xy, 1, 0.01, 0); //Get the offset curve array
NurbsCurve[] crv3 = new NurbsCurve[crv4.Count()]; //Make a new array with the size of crv4.count
for (int x = 0; x < crv4.Count(); x++)
{
    //Loop through the Curve[]
    crv3[x] = new NurbsCurve(crv4[x].ToNurbsCurve()); //Convert each Curve into a NurbyCurvey
}

Note that the offset function returns as a Curve[], and that the ToNurbsCurve() method can’t be used directly with arrays. The above code attempts to get around these limitations.

8 thoughts on “How to offset a curve in Grasshopper using the C# component”

  1. Hello, James. I have a question that the crv.Offset(xy, 1, 0.01, 0) will generate an array like crv4 []. How can I create a series of offset curves like using a for loop, and the offset distance is i*offset distance. But I can’t directly write crv 4 = crv.Offset(xy,i*offsetdis, 0.1,0). How can I still have the offset curve array?

    1. Hi YY, I would do this by looping through the code, and building a list of curves with each iteration. So, something like this…?

        private void RunScript(Curve inputCurve, object y, ref object A)
        {
      
          NurbsCurve crv = new NurbsCurve(inputCurve.ToNurbsCurve()); //input curve to use as offset base
          Point3d origin = new Point3d(0, 0, 0);
          Vector3d vectxy = new Vector3d(0, 0, 1);
          Plane xy = new Plane(origin, vectxy);
      
          //set up a list to contain our output curves
          List<NurbsCurve> rtnCurves = new List<NurbsCurve>();
      
          for (double i = 1; i < 5; i += 1.0) //define the iterations we want with a 'for' loop
          {
            Curve[] crv4 = crv.Offset(xy, i, 0.01, 0); //Get the offset curve array. 
            NurbsCurve[] crv3 = new NurbsCurve[crv4.Count()]; //Make a new array with the size of crv4.count
            for (int x = 0; x < crv4.Count(); x++)
            {
              //convert the curve to NurbsCurve and add it to our list
              rtnCurves.Add(new NurbsCurve(crv4[x].ToNurbsCurve()));
            }
          }
      
          A = rtnCurves;
      
        }
      
      1. Hi, James. I am also trying to write the method in additional code:

        private void RunScript(Curve inCrv, int num, ref object A)
          {
        
            A = offset(inCrv, num);
          }
        
          //  
          public List<NurbsCurve> offset(Curve crv1, int step){
            NurbsCurve crv = new NurbsCurve(crv1.ToNurbsCurve());
            List<NurbsCurve> rtnCurves = new List<NurbsCurve>();
            Point3d origin = new Point3d(0, 0, 0);
            Vector3d vectxy = new Vector3d(0, 0, 1);
            Plane xy = new Plane(origin, vectxy);
            for (int i = 0; i &lt; step; i++){
              Curve[] crv4 = crv.Offset(xy, i * 20, 0.01, 0);
              // NurbsCurve[] crv3 = new NurbsCurve[crv4.Count()];
              for (int x = 0; x &lt; crv4.Count(); x++)
              {
                rtnCurves.Add(new NurbsCurve(crv4[x].ToNurbsCurve()));
              }
        
            }
            return rtnCurves;
          }
        

        But it tells me that value cannot be null. Parameter name:source(line:0)

        1. That was quite tricky, but I think I found the problem. Are you testing on a closed curve? If the offset is too large, and the direction is offsetting towards the centre of the curve, then the offset can fail. If the offset fails, then there is no curve to convert to a NurbsCurve, hence the ‘null parameter’. Try turning your i*20 down, e.g. to i * 0.2. This worked (eventually!) for me.

          1. Hi, James.
            The problem seems to be that the “return” cannot return a list of nurbscurve….so I change the code to:

            private void RunScript(Curve inCrv, int num, ref object A)
            {
            Point3d origin = new Point3d(0, 0, 0);
            Vector3d vectxy = new Vector3d(0, 0, 1);
            Plane xy = new Plane(origin, vectxy);
            List offsetCrv = new List();
            for (int i = 0; i < num; i++){
            offsetCrv.Add(offset(inCrv, i, xy));
            }

            A = offsetCrv;

            }

            //
            public NurbsCurve offset(Curve crv1, int step, Plane a){
            NurbsCurve crv = new NurbsCurve(crv1.ToNurbsCurve());
            Curve[] crv4 = crv.Offset(a, step * 5, 0.01, 0);
            NurbsCurve rtnCurves = new NurbsCurve(crv4[0].ToNurbsCurve());

            return rtnCurves;
            }
            But then it says object reference not set to an instance of an object in line NurbsCurve(crv4[0].ToNurbsCurve());.
            I really want to figure out how to write the offset method in additional code……

          2. HI, James, I tried your method, but I could not make it either reduce the step or enlarge the original curve. It seems it is not the problem……. I think there is problems in how I write additional code…..

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: