Beispiel #1
0
        private static Autodesk.Revit.DB.Curve Convert(Autodesk.DesignScript.Geometry.NurbsCurve crv)
        {
            // line
            if (crv.Degree == 1 && crv.ControlPoints().Length == 2 && !crv.IsRational)
            {
                return(Autodesk.Revit.DB.Line.CreateBound(crv.ControlPoints()[0].ToXyz(false),
                                                          crv.ControlPoints()[1].ToXyz(false)));
            }

            // polyline - not allowed
            if (crv.Degree == 1)
            {
                throw new Exception(
                          "Degree 1 Nurbs Curves are not allowed in Revit!  Try splitting the curve into "
                          +
                          "individual linear pieces");
            }

            // bezier
            if (crv.Degree == 2 && crv.ControlPoints().Count() == 3 && !crv.IsRational)
            {
                var converted = NurbsUtils.ElevateBezierDegree(crv, 3);

                return(Autodesk.Revit.DB.NurbSpline.Create(converted.ControlPoints().ToXyzs(false),
                                                           converted.Weights(),
                                                           converted.Knots(),
                                                           converted.Degree,
                                                           converted.IsClosed,
                                                           converted.IsRational));
            }

            // degree 2 curve
            if (crv.Degree == 2)
            {
                // TODO: general NURBS degree elevation
                var numSamples = crv.ControlPoints().Count() + 1;
                var pts        = Enumerable.Range(0, numSamples + 1).Select(x => x / (double)numSamples)
                                 .Select(crv.PointAtParameter);

                var tstart = crv.TangentAtParameter(0);
                var tend   = crv.TangentAtParameter(1);

                var resampledCrv = NurbsCurve.ByPointsTangents(pts, tstart.Normalized(), tend.Normalized());

                return(Autodesk.Revit.DB.NurbSpline.Create(resampledCrv.ControlPoints().ToXyzs(false),
                                                           resampledCrv.Weights(),
                                                           resampledCrv.Knots(),
                                                           resampledCrv.Degree,
                                                           resampledCrv.IsClosed,
                                                           resampledCrv.IsRational));
            }

            // general implementation
            return(Autodesk.Revit.DB.NurbSpline.Create(crv.ControlPoints().ToXyzs(false),
                                                       crv.Weights(),
                                                       crv.Knots(),
                                                       crv.Degree,
                                                       crv.IsClosed,
                                                       crv.IsRational));
        }
Beispiel #2
0
        /// <summary>
        /// Convert a DS BSplineCurve to a Revit NurbSpline
        /// </summary>
        /// <param name="crv"></param>
        /// <returns></returns>
        private static Autodesk.Revit.DB.Curve Convert(Autodesk.DesignScript.Geometry.NurbsCurve crv)
        {
            if (crv.Degree == 1 && crv.ControlPoints().Length == 2 && !crv.IsRational)
            {
                return(Autodesk.Revit.DB.Line.CreateBound(crv.ControlPoints()[0].ToXyz(),
                                                          crv.ControlPoints()[1].ToXyz()));
            }

            if (crv.Degree <= 2)
            {
                throw new Exception("Could not convert the curve to a Revit curve");
            }

            // presumably checking if the curve is circular is quite expensive, we don't do it
            return(Autodesk.Revit.DB.NurbSpline.Create(crv.ControlPoints().ToXyzs(),
                                                       crv.Weights(),
                                                       crv.Knots(),
                                                       crv.Degree,
                                                       crv.IsClosed,
                                                       crv.IsRational));
        }
Beispiel #3
0
        public void ToPDF(iTextSharp.text.pdf.PdfWriter w)
        {
            PdfContentByte cb = w.DirectContent;

            cb.SetLineWidth((float)Settings.Thickness);
            if (Settings.Fill != null)
            {
                cb.SetColorFill(Settings.Fill.ToPDFColor());
            }

            if (Settings.Stroke != null)
            {
                cb.SetColorStroke(Settings.Stroke.ToPDFColor());
            }

            if (Geometry.GetType() == typeof(Dyn.Arc))
            {
                Dyn.Arc arc = Geometry as Dyn.Arc;
                cb.MoveTo(arc.StartPoint.X, arc.EndPoint.Y);
                cb.CurveTo(arc.PointAtParameter(0.5).X, arc.PointAtParameter(0.5).Y, arc.EndPoint.X, arc.EndPoint.Y);
            }
            else if (Geometry.GetType() == typeof(Dyn.Line))
            {
                Dyn.Line line = Geometry as Dyn.Line;
                cb.MoveTo(line.StartPoint.X, line.StartPoint.Y);
                cb.LineTo(line.EndPoint.X, line.EndPoint.Y);
            }
            else if (Geometry.GetType() == typeof(Dyn.Circle))
            {
                Dyn.Circle circle = Geometry as Dyn.Circle;
                cb.Circle(circle.CenterPoint.X, circle.CenterPoint.Y, circle.Radius);
            }
            else if (Geometry.GetType() == typeof(Dyn.Ellipse))
            {
                Dyn.Ellipse ellipse = Geometry as Dyn.Ellipse;
                cb.Ellipse(ellipse.StartPoint.X, ellipse.StartPoint.Y, ellipse.EndPoint.X, ellipse.EndPoint.Y);
            }
            else if (Geometry.GetType() == typeof(Dyn.Rectangle))
            {
                Dyn.Rectangle rect = Geometry as Dyn.Rectangle;
                cb.Rectangle(rect.Center().X, rect.Center().Y, rect.Width, rect.Height);
            }
            else if (Geometry.GetType() == typeof(Dyn.Polygon))
            {
                Dyn.Polygon p = Geometry as Dyn.Polygon;
                foreach (var curve in p.Curves())
                {
                    CurveToPDF(curve, cb);
                }
            }
            else if (Geometry.GetType() == typeof(Dyn.PolyCurve))
            {
                Dyn.PolyCurve pc = Geometry as Dyn.PolyCurve;
                foreach (var curve in pc.Curves())
                {
                    CurveToPDF(curve, cb);
                }
            }
            else if (Geometry.GetType() == typeof(Dyn.NurbsCurve))
            {
                Dyn.NurbsCurve nc = Geometry as Dyn.NurbsCurve;

                foreach (var linearc in nc.ApproximateWithArcAndLineSegments())
                {
                    CurveToPDF(linearc, cb);
                }
            }
            else if (Geometry.GetType() == typeof(Dyn.Curve))
            {
                Dyn.Curve curve = Geometry as Dyn.Curve;
                CurveToPDF(curve, cb);
            }
            else
            {
                throw new Exception(Properties.Resources.NotSupported);
            }

            if (Settings.Fill != null && Settings.Stroke != null)
            {
                cb.FillStroke();
            }
            else
            {
                if (Settings.Stroke != null)
                {
                    cb.Stroke();
                }
                if (Settings.Fill != null)
                {
                    cb.Fill();
                }
            }
        }