// Curve
        // NOTE: Autocad defines spline knots  as a vector of size # control points + degree + 1. (# at start and end should match degree)
        // Conversions for autocad need to make sure this is satisfied, otherwise will cause protected mem crash.
        public NurbCurve3d NurbcurveToNative(Curve curve)
        {
            var points = new Point3dCollection(PointListToNative(curve.points, curve.units));
            var knots  = new KnotCollection();

            for (int i = 0; i < curve.knots.Count; i++)
            {
                knots.Add(curve.knots[i]);
                if (curve.knots.Count == points.Count + curve.degree - 1)
                {
                    if (i == 0 || i == curve.knots.Count - 1)
                    {
                        knots.Add(curve.knots[i]);
                    }
                }
            }
            var weights = new DoubleCollection(curve.weights.ToArray());

            NurbCurve3d _curve = new NurbCurve3d(curve.degree, knots, points, weights, curve.periodic);

            if (curve.closed)
            {
                _curve.MakeClosed();
            }

            return(_curve);
        }
Example #2
0
        // Curve
        public NurbCurve3d NurbcurveToNative(Curve curve)
        {
            // process control points
            // NOTE: for **closed periodic** curves that have "n" control pts, curves sent from rhino will have n+degree points.
            // Remove extra pts for autocad.
            var _points = PointListToNative(curve.points, curve.units).ToList();

            if (curve.closed && curve.periodic)
            {
                _points = _points.GetRange(0, _points.Count - curve.degree);
            }
            var points = new Point3dCollection(_points.ToArray());

            // process knots
            // NOTE: Autocad defines spline knots  as a vector of size # control points + degree + 1. (# at start and end should match degree)
            // Conversions for autocad need to make sure this is satisfied, otherwise will cause protected mem crash.
            // NOTE: for **closed periodic** curves that have "n" control pts, # of knots should be n + 1. Remove degree = 3 knots from start and end.
            var _knots = curve.knots;

            if (curve.knots.Count == _points.Count + curve.degree - 1) // handles rhino format curves
            {
                _knots.Insert(0, _knots[0]);
                _knots.Insert(_knots.Count - 1, _knots[_knots.Count - 1]);
            }
            if (curve.closed && curve.periodic) // handles closed periodic curves
            {
                _knots = _knots.GetRange(curve.degree, _knots.Count - curve.degree * 2);
            }
            var knots = new KnotCollection();

            foreach (var _knot in _knots)
            {
                knots.Add(_knot);
            }

            // process weights
            // NOTE: if all weights are the same, autocad convention is to pass an empty list (this will assign them a value of -1)
            // NOTE: for closed curves that have "n" control pts, curves sent from rhino will have n+degree points. Remove corresponding weights for autocad.
            var _weights = curve.weights;

            if (curve.closed && curve.periodic) // handles closed periodic curves
            {
                _weights = curve.weights.GetRange(0, _points.Count);
            }
            DoubleCollection weights;

            weights = (_weights.Distinct().Count() == 1) ? new DoubleCollection() : new DoubleCollection(_weights.ToArray());

            NurbCurve3d _curve = new NurbCurve3d(curve.degree, knots, points, weights, curve.periodic);

            if (curve.closed)
            {
                _curve.MakeClosed();
            }
            _curve.SetInterval(IntervalToNative(curve.domain));

            return(_curve);
        }
Example #3
0
        // Splines
        public Curve SplineToSpeckle(Spline spline)
        {
            var curve = new Curve();

            // get nurbs and geo data
            var data    = spline.NurbsData;
            var _spline = spline.GetGeCurve() as NurbCurve3d;

            // handle the display polyline
            try
            {
                var      poly         = spline.ToPolyline(false, true);
                Polyline displayValue = ConvertToSpeckle(poly) as Polyline;
                curve.displayValue = displayValue;
            }
            catch { }

            // get weights: autocad assigns unweighted points a value of -1, and will return an empty list in the spline's nurbsdata if no points are weighted
            var weights = new List <double>();

            for (int i = 0; i < spline.NumControlPoints; i++)
            {
                double weight = spline.WeightAt(i);
                if (weight < 0)
                {
                    weights.Add(1);
                }
                else
                {
                    weights.Add(weight);
                }
            }

            // set nurbs curve info
            curve.points   = PointsToFlatArray(data.GetControlPoints().OfType <Point3d>().ToList()).ToList();
            curve.knots    = data.GetKnots().OfType <double>().ToList();
            curve.weights  = weights;
            curve.degree   = spline.Degree;
            curve.periodic = spline.IsPeriodic;
            curve.rational = spline.IsRational;
            curve.closed   = spline.Closed;
            curve.length   = _spline.GetLength(_spline.StartParameter, _spline.EndParameter, tolerance);
            curve.domain   = IntervalToSpeckle(_spline.GetInterval());
            curve.bbox     = BoxToSpeckle(spline.GeometricExtents, true);
            curve.units    = ModelUnits;

            return(curve);
        }
        public Curve NurbsToSpeckle(NurbCurve3d curve)
        {
            var _curve = new Curve();

            // get control points
            var points = new List <Point3d>();

            for (int i = 0; i < curve.NumberOfControlPoints; i++)
            {
                points.Add(curve.ControlPointAt(i));
            }

            // get knots
            var knots = new List <double>();

            for (int i = 0; i < curve.NumberOfKnots; i++)
            {
                knots.Add(curve.KnotAt(i));
            }

            // get weights
            var weights = new List <double>();

            for (int i = 0; i < curve.NumWeights; i++)
            {
                weights.Add(curve.GetWeightAt(i));
            }

            // set nurbs curve info
            _curve.points   = PointsToFlatArray(points).ToList();
            _curve.knots    = knots;
            _curve.weights  = weights;
            _curve.degree   = curve.Degree;
            _curve.periodic = curve.IsPeriodic(out double period);
            _curve.rational = curve.IsRational;
            _curve.closed   = curve.IsClosed();
            _curve.length   = curve.GetLength(curve.StartParameter, curve.EndParameter, tolerance);
            _curve.domain   = IntervalToSpeckle(curve.GetInterval());
            _curve.bbox     = BoxToSpeckle(curve.OrthoBoundBlock);
            _curve.units    = ModelUnits;

            return(_curve);
        }
Example #5
0
 public AcadDB.Curve NurbsToNativeDB(Curve curve)
 {
     // test to see if this is polyline convertible first?
     return(AcadDB.Curve.CreateFromGeCurve(NurbcurveToNative(curve)));
 }