Exemplo n.º 1
0
        /// <summary>
        /// Returns an untrimmed solidworks surface.
        /// </summary>
        /// <param name="swBSplineSurfaceParams"></param>
        /// <returns></returns>
        public ISurface ToSurface()
        {
            var vOrder = BitConverter.GetBytes(OrderV);
            var uOrder = BitConverter.GetBytes(OrderU);

            var swControlPointList = ControlPointList
                                     .EnumerateColumnWise()
                                     .SelectMany(v => new[] { v.X / v.W, v.Y / v.W, v.Z / v.W, v.W }.Take(SurfaceDimension))
                                     .ToArray();

            var uLength = ControlPointList.GetLength(0);
            var vLength = ControlPointList.GetLength(1);

            var numUCtrPts = BitConverter.GetBytes(uLength);
            var numVCtrPts = BitConverter.GetBytes(vLength);

            var uPeriodicity = BitConverter.GetBytes(UIsPeriodic ? 1 : 0);
            var vPeriodicity = BitConverter.GetBytes(VIsPeriodic ? 1 : 0);

            var dimControlPoints = BitConverter.GetBytes(SurfaceDimension);
            var unusedParameter  = BitConverter.GetBytes(0);

            var props = new[]
            {
                BitConverter.ToDouble(uOrder.Concat(vOrder).ToArray(), 0),
                BitConverter.ToDouble(numVCtrPts.Concat(numUCtrPts).ToArray(), 0),
                BitConverter.ToDouble(uPeriodicity.Concat(vPeriodicity).ToArray(), 0),
                BitConverter.ToDouble(dimControlPoints.Concat(unusedParameter).ToArray(), 0)
            };

            var bsplineSurface = (Surface)SwAddinBase.Active.Modeler
                                 .CreateBsplineSurface
                                     (props
                                     , KnotsU
                                     , KnotsV
                                     , swControlPointList
                                     );

            Debug.Assert(bsplineSurface != null);

#if DEBUG
            var p = bsplineSurface.Parameterization2();
            Debug.Assert(Math.Abs(p.UMax - KnotsU.Last()) < 1e-9);
            Debug.Assert(Math.Abs(p.UMin - KnotsU.First()) < 1e-9);
            Debug.Assert(Math.Abs(p.VMax - KnotsV.Last()) < 1e-9);
            Debug.Assert(Math.Abs(p.VMin - KnotsV.First()) < 1e-9);
#endif

            return(bsplineSurface);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Internal constructor used to validate the NURBS surface.
        /// </summary>
        /// <param name="degreeU">The degree in the U direction.</param>
        /// <param name="degreeV">The degree in the V direction.</param>
        /// <param name="knotsU">The knotVector in the U direction.</param>
        /// <param name="knotsV">The knotVector in the V direction.</param>
        /// <param name="controlPts">Two dimensional array of points.</param>
        internal NurbsSurface(int degreeU, int degreeV, KnotVector knotsU, KnotVector knotsV, List <List <Point4> > controlPts)
        {
            if (controlPts == null)
            {
                throw new ArgumentNullException("Control points array connot be null!");
            }
            if (degreeU < 1)
            {
                throw new ArgumentException("DegreeU must be greater than 1!");
            }
            if (degreeV < 1)
            {
                throw new ArgumentException("DegreeV must be greater than 1!");
            }
            if (knotsU == null)
            {
                throw new ArgumentNullException("KnotU cannot be null!");
            }
            if (knotsV == null)
            {
                throw new ArgumentNullException("KnotV cannot be null!");
            }
            if (knotsU.Count != controlPts.Count() + degreeU + 1)
            {
                throw new ArgumentException("Points count + degreeU + 1 must equal knotsU count!");
            }
            if (knotsV.Count != controlPts.First().Count() + degreeV + 1)
            {
                throw new ArgumentException("Points count + degreeV + 1 must equal knotsV count!");
            }
            if (!knotsU.IsValid(degreeU, controlPts.Count()))
            {
                throw new ArgumentException("Invalid knotsU!");
            }
            if (!knotsV.IsValid(degreeV, controlPts.First().Count()))
            {
                throw new ArgumentException("Invalid knotsV!");
            }

            DegreeU = degreeU;
            DegreeV = degreeV;
            KnotsU  = (Math.Abs(knotsU.GetDomain(degreeU).Length - 1.0) > GSharkMath.Epsilon) ? knotsU.Normalize() : knotsU;
            KnotsV  = (Math.Abs(knotsV.GetDomain(degreeV).Length - 1.0) > GSharkMath.Epsilon) ? knotsV.Normalize() : knotsV;
            Weights = Point4.GetWeights2d(controlPts);
            ControlPointLocations = Point4.PointDehomogenizer2d(controlPts);
            ControlPoints         = controlPts;
            DomainU = new Interval(KnotsU.First(), KnotsU.Last());
            DomainV = new Interval(KnotsV.First(), KnotsV.Last());
        }