Author Topic: How does one make a 2nd Degree Spline using Control Points in .net?  (Read 432 times)

0 Members and 1 Guest are viewing this topic.

dpeisenbeisz

  • Mosquito
  • Posts: 7
As a preface, I am using AutoCAD 2017 because it was the last year we could purchase a perpetual license.  We are going to ride this puppy until the wheels fall off, but that means I can't use anything from the AutoCAD .net API that is more recent AND I am kind of stuck with .net version 5.0 as the highest.  So far it hasn't been a big deal to be 6 years behind everyone else.  At least I don't have to deal with any NEW bugs introduced by Autodesk until we eventually get into a subscription license.

My question is about creating a 2nd degree spline.  I am trying to write a short little program that will layout a guardrail flare (or whatever parabolic flare is needed) using control points.  I have a formula from Caltrans that gives me a series of points on the guardrail from a baseline tangent to the start of the flare, but splines using fit points sometimes don't offset well and are forced into a degree 3 curve instead of a parabola.  My effort is mainly so I can dispute my business partner's claim that AutoCAD does not do parabolic flares like Microstation does (and he is actually correct), but I intend to show him it can be done using splines.  Civil 3d seems to think parabolas are only useful for vertical curves and it doesn't provide the same functionality for horizontal curves.

I can create a parabola easy enough in a drawing, but I want to automate guardrail layouts, left turn bay transitions, and other parabolic flares/transition as I have done for most of the other tedious road stuff we do.   I can't figure out how to create the same kind of parabola in .net as can be created in the drawing environment.  The closest I can get is by using the spline constructor like this:

new spline(points array, start vector, end vector, degree, tolerance), but that still uses a best-fit method.

I have also found that it requires me to include at least one point on the parabolic arc.  It would be so much easier to use the intersection of the start and end tangents (like the spline-cv command) , but as I said, the .net api does not seem to have this option.  Trying to add the control point after instantiating a spline object with or without constructor points is also not giving me the results I want.

Anybody have any ideas about how to create a parabola with control points in .net?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How does one make a 2nd Degree Spline using Control Points in .net?
« Reply #1 on: January 13, 2024, 02:35:42 AM »
Hi,

Here's a 'generic' method to draw a NURBS from a control points collection.
Code - C#: [Select]
  1.         public static Spline CreateNurbs(Point3dCollection controlPoints, int degree, bool closed)
  2.         {
  3.             if (controlPoints == null)
  4.                 throw new ArgumentNullException("controlPoints");
  5.             int numCtrlPts = controlPoints.Count;
  6.             if (numCtrlPts < (closed ? 3 : 2))
  7.                 throw new ArgumentException("To few control points");
  8.             if (25 < degree || degree < 1)
  9.                 throw new ArgumentOutOfRangeException("degree");
  10.  
  11.             degree = Math.Min(degree, numCtrlPts - 1);
  12.             var knots = new DoubleCollection();
  13.             if (closed)
  14.             {
  15.                 for (int i = 0; i <= numCtrlPts; i++) knots.Add(i);
  16.             }
  17.             else
  18.             {
  19.                 int j = 0;
  20.                 for (int i = 0; i <= degree; i++) knots.Add(j);
  21.                 for (j = 1; j < numCtrlPts - degree; j++) knots.Add(j);
  22.                 for (int i = 0; i <= degree; i++) knots.Add(j);
  23.             }
  24.             var weights = new DoubleCollection();
  25.             bool periodic = closed;
  26.             return new Spline(degree, false, closed, periodic, controlPoints, knots, weights, 0.0, 0.0);
  27.         }

About parabola, you can get some inspiration from CustomCurves.ParabolaJig.
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How does one make a 2nd Degree Spline using Control Points in .net?
« Reply #2 on: January 13, 2024, 03:35:30 AM »
Here's an example to create a parabola from start point, start tangent, end point, end tangent.
Code - C#: [Select]
  1. public static Spline CreateParabola(Point3d startPoint, Point3d endPoint, Vector3d startTangent, Vector3d endTangent)
  2. {
  3.     var line1 = new Line3d(startPoint, startTangent);
  4.     var line2 = new Line3d(endPoint, endTangent);
  5.     var intersectPoints = line1.IntersectWith(line2);
  6.     if (intersectPoints == null)
  7.         throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.DegenerateGeometry);
  8.     var summit = intersectPoints[0];
  9.     var controlPoints = new Point3dCollection { startPoint, summit, endPoint };
  10.     var knots = new DoubleCollection { 0, 0, 0, 1, 1, 1 };
  11.     return new Spline(2, false, false, false, controlPoints, knots, new DoubleCollection(), 0.0, 0.0);
  12. }
« Last Edit: January 13, 2024, 04:39:10 PM by gile »
Speaking English as a French Frog