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.
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?
Hi YY, I would do this by looping through the code, and building a list of curves with each iteration. So, something like this…?
Hi, James. I am also trying to write the method in additional code:
But it tells me that value cannot be null. Parameter name:source(line:0)
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.
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……
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…..
It worked….I am not sure why…. But thanks a lot!
Yes, Thanks a lot.