Ejemplo n.º 1
0
        protected override void Process(IFCAnyHandle ifcCurve)
        {
            base.Process(ifcCurve);
            KnotMultiplicities = IFCAnyHandleUtil.GetAggregateIntAttribute <List <int> >(ifcCurve, "KnotMultiplicities");
            Knots = IFCAnyHandleUtil.GetAggregateDoubleAttribute <List <double> >(ifcCurve, "Knots");

            if (KnotMultiplicities == null || Knots == null)
            {
                Importer.TheLog.LogError(ifcCurve.StepId, "Cannot find the KnotMultiplicities or Knots attribute of this IfcBSplineCurveWithKnots", true);
            }

            if (KnotMultiplicities.Count != Knots.Count)
            {
                Importer.TheLog.LogError(ifcCurve.StepId, "The number of knots and knot multiplicities are not the same", true);
            }

            IList <double> revitKnots = IFCGeometryUtil.ConvertIFCKnotsToRevitKnots(KnotMultiplicities, Knots);

            Curve nurbsSpline = NurbSpline.CreateCurve(Degree, revitKnots, ControlPointsList);

            SetCurve(nurbsSpline);

            if (nurbsSpline == null)
            {
                Importer.TheLog.LogWarning(ifcCurve.StepId, "Cannot get the curve representation of this IfcCurve", false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// create a horizontal nurbspline instance with specified z coordinate value
        /// </summary>
        public Curve CreateNurbSpline(double z)
        {
            // create control points with same z value
            List <XYZ> ctrPoints = new List <XYZ>();

            Autodesk.Revit.DB.XYZ xyz1 = new Autodesk.Revit.DB.XYZ(-41.887503610431267, -9.0290629129782189, z);
            Autodesk.Revit.DB.XYZ xyz2 = new Autodesk.Revit.DB.XYZ(-9.27600019217055, 0.32213521486563046, z);
            Autodesk.Revit.DB.XYZ xyz3 = new Autodesk.Revit.DB.XYZ(9.27600019217055, 0.32213521486563046, z);
            Autodesk.Revit.DB.XYZ xyz4 = new Autodesk.Revit.DB.XYZ(41.887503610431267, 9.0290629129782189, z);

            ctrPoints.Add(xyz1); ctrPoints.Add(xyz2); ctrPoints.Add(xyz3);
            ctrPoints.Add(xyz4);

            IList <double> weights = new List <double>();
            double         w1 = 1, w2 = 1, w3 = 1, w4 = 1;

            weights.Add(w1); weights.Add(w2); weights.Add(w3);
            weights.Add(w4);

            IList <double> knots = new List <double>();
            double         k0 = 0, k1 = 0, k2 = 0, k3 = 0, k4 = 34.425128, k5 = 34.425128, k6 = 34.425128, k7 = 34.425128;

            knots.Add(k0); knots.Add(k1); knots.Add(k2); knots.Add(k3);
            knots.Add(k4); knots.Add(k5); knots.Add(k6);
            knots.Add(k7);

            Curve detailNurbSpline = NurbSpline.CreateCurve(3, knots, ctrPoints, weights);

            m_revit.ActiveUIDocument.Document.Regenerate();

            return(detailNurbSpline);
        }
Ejemplo n.º 3
0
        private static Autodesk.Revit.DB.Curve CreateNurbSpline(IList <XYZ> controlPoints, IList <double> weights, IList <double> knots, int degree, bool closed, bool rational)
        {
            if (rational)
            {
                return(NurbSpline.CreateCurve(degree, knots, controlPoints, weights));
            }

            return(NurbSpline.CreateCurve(degree, knots, controlPoints));
        }
Ejemplo n.º 4
0
        public void CreateInDynamoModifyInRevitToCauseFailure()
        {
            //Create a wall in Dynamo
            string dynFilePath = Path.Combine(workingDirectory, @".\ElementBinding\CreateWallInDynamo.dyn");
            string testPath    = Path.GetFullPath(dynFilePath);

            ViewModel.OpenCommand.Execute(testPath);

            RunCurrentModel();


            //Modify the wall in Revit
            using (var trans = new Transaction(DocumentManager.Instance.CurrentUIDocument.Document, "ModifyInRevit"))
            {
                bool hasError = false;
                trans.Start();

                try
                {
                    IList <Element> rps = GetAllWallElements();
                    Assert.AreEqual(1, rps.Count);
                    Wall       wall     = rps.First() as Wall;
                    List <XYZ> ctrlPnts = new List <XYZ>();
                    ctrlPnts.Add(new XYZ(0.0, 1.0, 0.0));
                    ctrlPnts.Add(new XYZ(1.0, 0.0, 0.0));
                    ctrlPnts.Add(new XYZ(2.0, 0.0, 0.0));
                    ctrlPnts.Add(new XYZ(3.0, 1.0, 0.0));
                    List <double> weights = new List <double>();
                    weights.Add(1.0);
                    weights.Add(1.0);
                    weights.Add(1.0);
                    weights.Add(1.0);
                    var spline       = NurbSpline.CreateCurve(ctrlPnts, weights);
                    var wallLocation = wall.Location as LocationCurve;
                    wallLocation.Curve = spline;
                }
                catch (Exception e)
                {
                    hasError = true;
                    trans.RollBack();
                }

                if (!hasError)
                {
                    trans.Commit();
                }
            }


            RunCurrentModel();

            IList <Element> rps2 = GetAllWallElements();

            Assert.AreEqual(1, rps2.Count);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Converts a List of Grevit Curves to Revit Curves
        /// </summary>
        /// <param name="document">Active Document</param>
        /// <param name="grevitCurves">List of Grevit Curves</param>
        /// <returns>List of Revit Curves</returns>
        public static List <Curve> GrevitCurvesToRevitCurves(Component component, CoordinatesOverride coords = null)
        {
            List <Curve> curvesOut = new List <Curve>();


            if (component.GetType() == typeof(Grevit.Types.Line))
            {
                Grevit.Types.Line line = (Grevit.Types.Line)component;
                curvesOut.Add(Autodesk.Revit.DB.Line.CreateBound(line.from.ToXYZ(coords), line.to.ToXYZ(coords)));
            }
            else if (component.GetType() == typeof(Grevit.Types.Arc))
            {
                Grevit.Types.Arc arc = (Grevit.Types.Arc)component;
                curvesOut.Add(Autodesk.Revit.DB.Arc.Create(arc.center.ToXYZ(coords), arc.radius, arc.start, arc.end, XYZ.BasisX, XYZ.BasisY));
            }
            else if (component.GetType() == typeof(Grevit.Types.Curve3Points))
            {
                Grevit.Types.Curve3Points curve3points = (Grevit.Types.Curve3Points)component;
                curvesOut.Add(Autodesk.Revit.DB.Arc.Create(curve3points.a.ToXYZ(coords), curve3points.c.ToXYZ(coords), curve3points.b.ToXYZ(coords)));
            }
            else if (component.GetType() == typeof(Grevit.Types.PLine))
            {
                Grevit.Types.PLine pline = (Grevit.Types.PLine)component;

                for (int i = 0; i < pline.points.Count - 1; i++)
                {
                    curvesOut.Add(Autodesk.Revit.DB.Line.CreateBound(pline.points[i].ToXYZ(coords), pline.points[i + 1].ToXYZ(coords)));
                }
            }
            else if (component.GetType() == typeof(Spline))
            {
                Spline      spline = (Spline)component;
                IList <XYZ> points = new List <XYZ>();
                foreach (Grevit.Types.Point point in spline.controlPoints)
                {
                    points.Add(point.ToXYZ(coords));
                }
#if (Revit2015 || Revit2016 || Revit2017)
                NurbSpline ns = NurbSpline.Create(points, spline.weights);
#else
                NurbSpline ns = (NurbSpline)NurbSpline.CreateCurve(points, spline.weights);
#endif
                ns.isClosed = spline.isClosed;
                curvesOut.Add(ns);
            }


            return(curvesOut);
        }
Ejemplo n.º 6
0
        internal static Curve ToNurbsSpline(RG.NurbsCurve value, double factor)
        {
            var degree        = value.Degree;
            var knots         = ToDoubleArray(value.Knots, degree);
            var controlPoints = ToXYZArray(value.Points, factor);

            if (value.IsRational)
            {
                var weights = value.Points.ConvertAll(x => x.Weight);
                return(NurbSpline.CreateCurve(value.Degree, knots, controlPoints, weights));
            }
            else
            {
                return(NurbSpline.CreateCurve(value.Degree, knots, controlPoints));
            }
        }
Ejemplo n.º 7
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;

            List <RG.Point3d> pts = new List <RG.Point3d> {
                new RG.Point3d(0, 0, 0), new RG.Point3d(5, 10, 0), new RG.Point3d(15, 0, 0), new RG.Point3d(20, 0, 0)
            };


            RG.PolylineCurve pc = new RG.PolylineCurve(pts);


            RG.NurbsCurve nurb = pc.ToNurbsCurve();

            nurb.IncreaseDegree(3);

            var knots         = ToDoubleArray(nurb.Knots, nurb.Degree);
            var controlPoints = ToXYZArray(nurb.Points, 1);
            var weights       = nurb.Points.ConvertAll(x => x.Weight);

            XYZ normal = new XYZ(0, 0, 1);
            XYZ origin = new XYZ(0, 0, 0);

            Plane rvtPlane = Plane.CreateByNormalAndOrigin(normal, origin);

            //var plane = sketchPlane.GetPlane().ToPlane();
            Curve rvtN = NurbSpline.CreateCurve(nurb.Degree, knots, controlPoints);

            using (Transaction t = new Transaction(doc, "a"))
            {
                t.Start();
                SketchPlane sketchPlane = SketchPlane.Create(doc, rvtPlane);
                ModelCurve  mc          = doc.Create.NewModelCurve(rvtN, sketchPlane);
                TaskDialog.Show("r", mc.Id.ToString());
                t.Commit();
            }



            return(Result.Succeeded);
        }
Ejemplo n.º 8
0
        public DB.Curve CurveToNative(Curve speckleCurve)
        {
            var pts = new List <XYZ>();

            for (int i = 0; i < speckleCurve.points.Count; i += 3)
            {
                //use PointToNative for conversion as that takes into account the Project Base Point
                var point = new Point(speckleCurve.points[i], speckleCurve.points[i + 1], speckleCurve.points[i + 2], speckleCurve.units);
                pts.Add(PointToNative(point));
            }
            try
            {
                if (speckleCurve.knots != null && speckleCurve.weights != null && speckleCurve.knots.Any() && speckleCurve.weights.Any())
                {
                    var weights      = speckleCurve.weights.GetRange(0, pts.Count);
                    var speckleKnots = new List <double>(speckleCurve.knots);
                    if (speckleKnots.Count != pts.Count + speckleCurve.degree + 1)
                    {
                        // Curve has rhino knots, repeat first and last.
                        speckleKnots.Insert(0, speckleKnots[0]);
                        speckleKnots.Add(speckleKnots[speckleKnots.Count - 1]);
                    }

                    //var knots = speckleKnots.GetRange(0, pts.Count + speckleCurve.degree + 1);
                    var curve = NurbSpline.CreateCurve(speckleCurve.degree, speckleKnots, pts, weights);
                    return(curve);
                }
                else
                {
                    var weights = speckleCurve.weights.GetRange(0, pts.Count);
                    var curve   = NurbSpline.CreateCurve(pts, weights);
                    return(curve);
                }
            }
            catch (Exception e)
            {
                if (e is Autodesk.Revit.Exceptions.ArgumentException)
                {
                    throw e;                                           // prob a closed, periodic curve
                }
                return(null);
            }
        }
Ejemplo n.º 9
0
        public DB.Curve CurveToNative(Curve speckleCurve)
        {
            var pts = new List <XYZ>();

            for (int i = 0; i < speckleCurve.points.Count; i += 3)
            {
                pts.Add(new XYZ(
                            ScaleToNative(speckleCurve.points[i], speckleCurve.units),
                            ScaleToNative(speckleCurve.points[i + 1], speckleCurve.units),
                            ScaleToNative(speckleCurve.points[i + 2], speckleCurve.units)));
            }
            try
            {
                if (speckleCurve.knots != null && speckleCurve.weights != null && speckleCurve.knots.Any() && speckleCurve.weights.Any())
                {
                    var weights      = speckleCurve.weights.GetRange(0, pts.Count);
                    var speckleKnots = new List <double>(speckleCurve.knots);
                    if (speckleKnots.Count != pts.Count + speckleCurve.degree + 1)
                    {
                        // Curve has rhino knots, repeat first and last.
                        speckleKnots.Insert(0, speckleKnots[0]);
                        speckleKnots.Add(speckleKnots[speckleKnots.Count - 1]);
                    }
                    //var knots = speckleKnots.GetRange(0, pts.Count + speckleCurve.degree + 1);
                    var curve = NurbSpline.CreateCurve(speckleCurve.degree, speckleKnots, pts, weights);
                    return(curve);
                }
                else
                {
                    var weights = speckleCurve.weights.GetRange(0, pts.Count);
                    var curve   = NurbSpline.CreateCurve(pts, weights);
                    return(curve);
                }
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Ejemplo n.º 10
0
        private BRepBuilder CreateNurbsSurface()
        {
            //Note that we are not creating a closed solid here. It is an open shell.
            BRepBuilder brepBuilder = new BRepBuilder(BRepType.OpenShell);

            // Create NURBS surface
            IList <double> knotsU = new List <double> {
                0, 0, 0, 0, 0, 1, 1, 1, 1, 1
            };
            IList <double> knotsV = new List <double> {
                0, 0, 0, 0, 0, 1, 1, 1, 1, 1
            };
            int degreeU = 4;
            int degreeV = 4;

            IList <XYZ> controlPoints = new List <XYZ>
            {
                new XYZ(0, 0, 0), new XYZ(0, 20, 0), new XYZ(0, 40, 0), new XYZ(0, 60, 0), new XYZ(0, 80, 0),
                new XYZ(20, 0, 100), new XYZ(20, 20, 200), new XYZ(20, 40, 300), new XYZ(20, 60, 200), new XYZ(20, 80, 100),
                new XYZ(40, 0, -100), new XYZ(40, 20, -250), new XYZ(40, 40, -300), new XYZ(40, 60, -250), new XYZ(40, 80, -100),
                new XYZ(60, 0, 100), new XYZ(60, 20, 200), new XYZ(60, 40, 300), new XYZ(60, 60, 200), new XYZ(60, 80, 100),
                new XYZ(80, 0, 0), new XYZ(80, 20, 0), new XYZ(80, 40, 0), new XYZ(80, 60, 0), new XYZ(80, 80, 0)
            };

            BRepBuilderSurfaceGeometry nurbsSurface = BRepBuilderSurfaceGeometry.CreateNURBSSurface(degreeU, degreeV, knotsU, knotsV, controlPoints, false /*bReverseOrientation*/, null /*pSurfaceEnvelope*/);

            // Create 4 NURBS curves defining the boundary of the NURBS surface that has just been created
            IList <double> weights = new List <double> {
                1, 1, 1, 1, 1
            };

            IList <XYZ> backEdgeControlPoints = new List <XYZ> {
                new XYZ(0, 0, 0), new XYZ(0, 20, 0), new XYZ(0, 40, 0), new XYZ(0, 60, 0), new XYZ(0, 80, 0)
            };
            Curve backNurbs = NurbSpline.CreateCurve(4, knotsU, backEdgeControlPoints, weights);
            BRepBuilderEdgeGeometry backEdge = BRepBuilderEdgeGeometry.Create(backNurbs);

            IList <XYZ> frontEdgeControlPoints = new List <XYZ> {
                new XYZ(80, 0, 0), new XYZ(80, 20, 0), new XYZ(80, 40, 0), new XYZ(80, 60, 0), new XYZ(80, 80, 0)
            };
            Curve frontNurbs = NurbSpline.CreateCurve(4, knotsU, frontEdgeControlPoints, weights);
            BRepBuilderEdgeGeometry frontEdge = BRepBuilderEdgeGeometry.Create(frontNurbs);

            IList <XYZ> leftEdgeControlPoints = new List <XYZ> {
                new XYZ(0, 0, 0), new XYZ(20, 0, 100), new XYZ(40, 0, -100), new XYZ(60, 0, 100), new XYZ(80, 0, 0)
            };
            Curve leftNurbs = NurbSpline.CreateCurve(4, knotsU, leftEdgeControlPoints, weights);
            BRepBuilderEdgeGeometry leftEdge = BRepBuilderEdgeGeometry.Create(leftNurbs);

            IList <XYZ> rightEdgeControlPoints = new List <XYZ> {
                new XYZ(0, 80, 0), new XYZ(20, 80, 100), new XYZ(40, 80, -100), new XYZ(60, 80, 100), new XYZ(80, 80, 0)
            };
            Curve rightNurbs = NurbSpline.CreateCurve(4, knotsU, rightEdgeControlPoints, weights);
            BRepBuilderEdgeGeometry rightEdge = BRepBuilderEdgeGeometry.Create(rightNurbs);

            // Add the geometries to the brepBuilder
            BRepBuilderGeometryId nurbSplineFaceId = brepBuilder.AddFace(nurbsSurface, false);
            BRepBuilderGeometryId loopId           = brepBuilder.AddLoop(nurbSplineFaceId);

            BRepBuilderGeometryId backEdgeId  = brepBuilder.AddEdge(backEdge);
            BRepBuilderGeometryId frontEdgeId = brepBuilder.AddEdge(frontEdge);
            BRepBuilderGeometryId leftEdgeId  = brepBuilder.AddEdge(leftEdge);
            BRepBuilderGeometryId rightEdgeId = brepBuilder.AddEdge(rightEdge);

            // Add each edge to the loop
            brepBuilder.AddCoEdge(loopId, backEdgeId, true);
            brepBuilder.AddCoEdge(loopId, leftEdgeId, false);
            brepBuilder.AddCoEdge(loopId, frontEdgeId, false);
            brepBuilder.AddCoEdge(loopId, rightEdgeId, true);
            brepBuilder.FinishLoop(loopId);
            brepBuilder.FinishFace(nurbSplineFaceId);

            brepBuilder.Finish();
            return(brepBuilder);
        }
Ejemplo n.º 11
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Document      doc   = uidoc.Document;


            Reference r = uidoc.Selection.PickObject(ObjectType.Element, "Select Element");

            Options geometryOptions = new Options();

            geometryOptions.ComputeReferences = false;

            GeometryElement geomElem = doc.GetElement(r).get_Geometry(geometryOptions);

            List <NurbSpline> cadSplines = new List <NurbSpline>();

            IList <XYZ>   controlPoints = new List <XYZ>();
            List <double> weights       = new List <double>();
            List <double> knots         = new List <double>();

            if (null != geomElem)
            {
                foreach (var o in geomElem)
                {
                    GeometryInstance gi = o as GeometryInstance;
                    GeometryElement  instanceGeometryElement = gi.GetInstanceGeometry();

                    foreach (GeometryObject instanceObj in instanceGeometryElement)
                    {
                        if (instanceObj.GetType().ToString().Contains("NurbSpline"))
                        {
                            //TaskDialog.Show("r", instanceObj.GetType().ToString());
                            NurbSpline nurb = instanceObj as NurbSpline;
                            cadSplines.Add(nurb);
                            controlPoints = nurb.CtrlPoints;
                            //weights = nurb.Weights;
                            weights = nurb.Weights.Cast <double>().ToList();
                            //knots = nurb.Knots;
                            knots = nurb.Knots.Cast <double>().ToList();
                        }
                        break;
                    }
                }
            }

            double scale = 0.3048;

            #region Test
            //List<XYZ> controlPoints = new List<XYZ>();
            //controlPoints.Add(new XYZ(0 / scale, 0 / scale, 0 / scale));
            //controlPoints.Add(new XYZ(5 / scale, 5 / scale, 2 / scale));
            //controlPoints.Add(new XYZ(10 / scale, 10 / scale, 5 / scale));
            //controlPoints.Add(new XYZ(15 / scale, 10 / scale, 5 / scale));
            //controlPoints.Add(new XYZ(20 / scale, 5 / scale, 4 / scale));
            //controlPoints.Add(new XYZ(25 / scale, 5 / scale, 3 / scale));

            //List<double> weights = new List<double>();
            //weights.Add(1.0);
            //weights.Add(1.0);
            //weights.Add(1.0);
            //weights.Add(1.0);
            //weights.Add(1.0);
            //weights.Add(1.0);

            //List<double> knots = new List<double>();
            //knots.Add(0); //1revit
            //knots.Add(0); //2
            //knots.Add(0); //3
            //knots.Add(0); //4
            //knots.Add(10.76); //5
            //knots.Add(21.51); //6
            //knots.Add(32.27); //7
            //knots.Add(32.27);
            //knots.Add(32.27); //9
            //knots.Add(32.27);//revit
            #endregion

            HermiteSpline hermspline = HermiteSpline.Create(controlPoints, false);

            //Curve nurbSpline = NurbSpline.Create(hermspline);
            Curve nurbSpline = NurbSpline.CreateCurve(3, knots, controlPoints, weights);

            //XYZ startPoint = nurbSpline.GetEndPoint(0);

            Transform nurbsTr = nurbSpline.ComputeDerivatives(0, true);

            XYZ startPoint = nurbsTr.Origin;
            //PrintPoint("a", nurbsTr.Origin);

            #region Test Plane
            //Plane geomPlane = Autodesk.Revit.DB.Plane.CreateByOriginAndBasis(nurbsTr.Origin, nurbsTr.BasisY.Normalize(), nurbsTr.BasisZ.Normalize());
            //Plane geomPlane = Autodesk.Revit.DB.Plane.CreateByOriginAndBasis(nurbSpline.GetEndPoint(0), nurbsTr.BasisY.Normalize(), nurbsTr.BasisZ.Normalize());
            //Frame f = new Frame(nurbSpline.GetEndPoint(0), nurbsTr.BasisY.Normalize(), nurbsTr.BasisZ.Normalize(), nurbsTr.BasisX.Normalize());
            //Plane geomPlane = Autodesk.Revit.DB.Plane.CreateByThreePoints(XYZ.Zero, XYZ.BasisX, XYZ.BasisZ);
            //Plane geomPlane = Plane.CreateByNormalAndOrigin(nurbsTr.BasisX.Normalize(), nurbSpline.GetEndPoint(1)); funziona
            //Plane geomPlane = Plane.CreateByThreePoints(startPoint, startPoint + nurbsTr.BasisX.Normalize(), startPoint + XYZ.BasisZ);
            #endregion
            //XYZ curveDir = controlPoints[1] - controlPoints[0];
            XYZ   curveDir  = nurbsTr.BasisX;
            XYZ   perpDir   = curveDir.CrossProduct(startPoint + XYZ.BasisZ).Normalize();
            Plane perpPlane = Plane.CreateByNormalAndOrigin(curveDir, startPoint);
            //Plane vertPlane = Plane.CreateByThreePoints(startPoint, perpPlane.XVec, XYZ.BasisZ);
            Plane vertPlane = perpPlane;

            //PrintPoint("per", perpDir);

            List <PtCoord> pointsCoordinates = new List <PtCoord>();

            using (var form = new FormAddActiveView("Enter coordinates in clockwise order"))
            {
                form.ShowDialog();

                //if the user hits cancel just drop out of macro
                if (form.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    return(Result.Cancelled);
                }

                string[] inputs = form.TextString.Split(';');

                foreach (string coord in inputs)
                {
                    string[] xy = coord.Split(',');
                    pointsCoordinates.Add(new PtCoord(Double.Parse(xy[0]) / (scale * 1000), Double.Parse(xy[1]) / (scale * 1000)));
                }
            }

//			List<PtCoord> pointsCoordinates = new List<PtCoord>(){new PtCoord(5,0), new PtCoord(2,2), new PtCoord(-14,0), new PtCoord(2,-2)};

            List <XYZ> pts = VertexPoints(nurbsTr.Origin, pointsCoordinates, vertPlane);

            XYZ pt1 = nurbsTr.Origin;
            XYZ pt2 = pt1 + vertPlane.XVec * 5;
            XYZ pt3 = pt2 + vertPlane.YVec * 2 + vertPlane.XVec * 2;
            XYZ pt4 = pt3 - vertPlane.XVec * 12;
            XYZ pt5 = pt4 - vertPlane.YVec * 2 + vertPlane.XVec * 2;



            Line l1 = Line.CreateBound(pt1, pt2);
            Line l2 = Line.CreateBound(pt2, pt3);
            Line l3 = Line.CreateBound(pt3, pt4);
            Line l4 = Line.CreateBound(pt4, pt5);
            Line l5 = Line.CreateBound(pt5, pt1);
            //
            //			var profileLoop = CurveLoop.Create(new List<Curve>{l1, l2, l3, l4, l5});

            var profileLoop = LoopPoints(pts);

            //double rotAngle = -2.543 * Math.PI / 180;
            double rotAngle  = -15 * Math.PI / 180;
            var    transform = Transform.CreateRotationAtPoint(nurbsTr.BasisX, rotAngle, nurbsTr.Origin);

            profileLoop.Transform(transform);

            var loops = new List <CurveLoop> {
                profileLoop
            };

            var path = CurveLoop.Create(new List <Curve> {
                nurbSpline
            });

            WireframeBuilder builder = new WireframeBuilder();

            builder.AddCurve(nurbSpline);


            //Solid solid = GeometryCreationUtilities.CreateSweptGeometry(path,0,nurbSpline.GetEndParameter(0),loops);
            Solid solid = GeometryCreationUtilities.CreateFixedReferenceSweptGeometry(path, 0, nurbSpline.GetEndParameter(0), loops, XYZ.BasisZ);


            using (Transaction t = new Transaction(doc, "create spline"))
            {
                t.Start();

                ElementId categoryId = new ElementId(BuiltInCategory.OST_Floors);

                DirectShape ds = DirectShape.CreateElement(doc, categoryId);

                ds.SetShape(builder);

                ds.Name = "RhinoSpline";

                SketchPlane sp = SketchPlane.Create(doc, vertPlane);
                uidoc.ActiveView.SketchPlane = sp;

                //uidoc.ActiveView.ShowActiveWorkPlane();

                ModelLine line1 = doc.Create.NewModelCurve(l1, sp) as ModelLine;
                ModelLine line2 = doc.Create.NewModelCurve(l2, sp) as ModelLine;
                ModelLine line3 = doc.Create.NewModelCurve(l3, sp) as ModelLine;
                ModelLine line4 = doc.Create.NewModelCurve(l4, sp) as ModelLine;
                ModelLine line5 = doc.Create.NewModelCurve(l5, sp) as ModelLine;

                List <GeometryObject> gs = new List <GeometryObject>();
                gs.Add(solid);

                //DirectShape directShape = DirectShape.CreateElement(doc, categoryId);
                ds.AppendShape(gs);

                t.Commit();
            }

            return(Result.Succeeded);
        }
Ejemplo n.º 12
0
        private static DirectShapeInfo ConvertBrep(Document doc, RhinoObjectInfo rhinoInfo)
        {
            DirectShapeInfo shapeInfo = new DirectShapeInfo();

            try
            {
                BRepBuilder         brepBuilder = null;
                Rhino.Geometry.Brep rhinoBrep   = rhinoInfo.Geometry as Rhino.Geometry.Brep;
                if (rhinoBrep.IsSolid)
                {
                    brepBuilder = new BRepBuilder(BRepType.Solid);
                }
                else if (rhinoBrep.IsSurface)
                {
                    brepBuilder = new BRepBuilder(BRepType.OpenShell);
                }

                foreach (Rhino.Geometry.BrepFace brepFace in rhinoBrep.Faces)
                {
                    BRepBuilderGeometryId nurbSplineFaceId = BRepBuilderGeometryId.InvalidGeometryId();
                    BRepBuilderGeometryId loopId           = BRepBuilderGeometryId.InvalidGeometryId();

                    bool reverse = brepFace.OrientationIsReversed;
                    if (brepFace.ObjectType == Rhino.DocObjects.ObjectType.Surface)
                    {
                        Rhino.Geometry.NurbsSurface nurbsSurface = brepFace.ToNurbsSurface();

                        Rhino.Geometry.Collections.NurbsSurfacePointList points = nurbsSurface.Points;
                        int dirU    = points.CountU;
                        int dirV    = points.CountV;
                        int degreeU = nurbsSurface.Degree(0);
                        int degreeV = nurbsSurface.Degree(1);

                        // knots
                        Rhino.Geometry.Collections.NurbsSurfaceKnotList knotsU = nurbsSurface.KnotsU;
                        Rhino.Geometry.Collections.NurbsSurfaceKnotList knotsV = nurbsSurface.KnotsV;

                        List <XYZ> controlPoints = new List <XYZ>();
                        XYZ[][]    rvtPoints     = new XYZ[dirU][];
                        double[][] rvtWeights    = new double[dirU][];
                        for (int u = 0; u < dirU; u++)
                        {
                            rvtPoints[u]  = new XYZ[dirV];
                            rvtWeights[u] = new double[dirV];
                            for (int v = 0; v < dirV; v++)
                            {
                                // point coordinates at u, v
                                Rhino.Geometry.Point3d pt = points.GetControlPoint(u, v).Location;

                                XYZ xyz = new XYZ(pt.X, pt.Y, pt.Z);
                                rvtPoints[u][v] = xyz;
                                controlPoints.Add(xyz);
                                // weights at u, v
                                rvtWeights[u][v] = points.GetControlPoint(u, v).Weight;
                            }
                        }

                        // knots U
                        List <double> rvt_knotsU = new List <double>();
                        rvt_knotsU.Add(knotsU[0]);
                        for (int i = 0; i < knotsU.Count; i++)
                        {
                            rvt_knotsU.Add(knotsU[i]);
                        }
                        rvt_knotsU.Add(knotsU[knotsU.Count - 1]);

                        // knots V
                        List <double> rvt_knotsV = new List <double>();
                        rvt_knotsV.Add(knotsV[0]);
                        for (int i = 0; i < knotsV.Count; i++)
                        {
                            rvt_knotsV.Add(knotsV[i]);
                        }
                        rvt_knotsV.Add(knotsV[knotsV.Count - 1]);

                        BRepBuilderSurfaceGeometry brepSurface = BRepBuilderSurfaceGeometry.CreateNURBSSurface(degreeU, degreeV, rvt_knotsU, rvt_knotsV, controlPoints, reverse, null);
                        nurbSplineFaceId = brepBuilder.AddFace(brepSurface, reverse);
                        loopId           = brepBuilder.AddLoop(nurbSplineFaceId);
                    }

                    foreach (Rhino.Geometry.BrepLoop loop in brepFace.Loops)
                    {
                        Rhino.Geometry.Curve      curve      = loop.To3dCurve();
                        Rhino.Geometry.NurbsCurve nurbsCurve = curve.ToNurbsCurve();

                        int           degree        = nurbsCurve.Degree;
                        List <XYZ>    controlPoints = new List <XYZ>();
                        List <double> weights       = new List <double>();
                        List <double> knots         = new List <double>();

                        for (int i = 0; i < nurbsCurve.Points.Count; i++)
                        {
                            Rhino.Geometry.ControlPoint ctrlPoint = nurbsCurve.Points[i];
                            Rhino.Geometry.Point3d      point     = ctrlPoint.Location;
                            XYZ xyz = new XYZ(point.X, point.Y, point.Z);

                            controlPoints.Add(xyz);
                            weights.Add(ctrlPoint.Weight);
                        }

                        knots.Add(nurbsCurve.Knots[0]);
                        for (int i = 0; i < nurbsCurve.Knots.Count; i++)
                        {
                            double knot = nurbsCurve.Knots[i];
                            knots.Add(knot);
                        }
                        knots.Add(nurbsCurve.Knots[nurbsCurve.Knots.Count - 1]);



                        Curve rvtCurve = NurbSpline.CreateCurve(degree, knots, controlPoints, weights);
                        BRepBuilderEdgeGeometry edgeGeo = BRepBuilderEdgeGeometry.Create(rvtCurve);
                        BRepBuilderGeometryId   edgeId  = brepBuilder.AddEdge(edgeGeo);
                        brepBuilder.AddCoEdge(loopId, edgeId, false);
                    }

                    brepBuilder.FinishLoop(loopId);
                    brepBuilder.FinishFace(nurbSplineFaceId);
                }

                brepBuilder.Finish();


                DirectShape shape = DirectShape.CreateElement(doc, new ElementId((int)BuiltInCategory.OST_GenericModel));
                shape.ApplicationId     = "RhinoBrep";
                shape.ApplicationDataId = rhinoInfo.ObjectId.ToString();

                if (null != shape)
                {
                    shape.SetShape(brepBuilder);
                }

                shapeInfo.DirectShapeId = shape.Id;
                shapeInfo.RhinoObjectId = rhinoInfo.ObjectId;
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                MessageBox.Show("Cannot Conver To Brep.\n" + ex.Message);
            }
            return(shapeInfo);
        }
Ejemplo n.º 13
0
 private bool ConvertHbCurve(HbCurve hbCurve, ref Curve curve)
 {
     try {
         curve = null;
         XYZ xyz1, xyz2, xyz3;
         if (hbCurve is HbLine)
         {
             HbLine hbLine = (HbLine)hbCurve;
             if (!ConvertHbXyz(hbLine.PointStart, out xyz1))
             {
                 return(false);
             }
             if (!ConvertHbXyz(hbLine.PointEnd, out xyz2))
             {
                 return(false);
             }
             curve = Line.CreateBound(xyz1, xyz2);
         }
         else if (hbCurve is HbArc)
         {
             HbArc hbArc = (HbArc)hbCurve;
             if (!ConvertHbXyz(hbArc.PointStart, out xyz1))
             {
                 return(false);
             }
             if (!ConvertHbXyz(hbArc.PointEnd, out xyz2))
             {
                 return(false);
             }
             if (!ConvertHbXyz(hbArc.PointMid, out xyz3))
             {
                 return(false);
             }
             curve = Arc.Create(xyz1, xyz2, xyz3);
         }
         else if (hbCurve is HbEllipse)
         {
             HbEllipse hbEllipse = (HbEllipse)hbCurve;
             if (!ConvertHbXyz(hbEllipse.PointFirst, out xyz1))
             {
                 return(false);
             }
             if (!ConvertHbXyz(hbEllipse.PointSecond, out xyz2))
             {
                 return(false);
             }
             XYZ    center, xVec, yVec;
             double radX, radY, param0, param1;
             CalculateEllipsePoints(xyz1, xyz2, hbEllipse.RadiusY, hbEllipse.Mode, out center, out radX, out radY, out xVec, out yVec, out param0, out param1);
             curve = Ellipse.CreateCurve(center, radX, radY, xVec, yVec, param0, param1);
             //curve = Ellipse.Create(center, radX, radY, xVec, yVec, param0, param1);  //2018 upgrade
         }
         else if (hbCurve is HbNurbSpline)
         {
             HbNurbSpline hbNurbSpline = (HbNurbSpline)hbCurve;
             List <XYZ>   points       = new List <XYZ>();
             foreach (HbXYZ point in hbNurbSpline.Points)
             {
                 if (!ConvertHbXyz(point, out xyz1))
                 {
                     return(false);
                 }
                 points.Add(xyz1);
             }
             List <double> weights = new List <double>();
             // Note that we are just using fixed weights for now.  It doesn't appear that Revit is actually using these values anyway.
             for (int i = 0; i < hbNurbSpline.Points.Count; i++)
             {
                 weights.Add(1.0);
             }
             //curve = NurbSpline.Create(points, weights);  // Fails if < 4 points although manually can create with 2 or 3 points.
             curve = NurbSpline.CreateCurve(points, weights);
             //TODO This failed in older versions if < 4 points although manually can create with 2 or 3 points.  Is this fixed in 2017?
         }
         else if (hbCurve is HbHermiteSpline)
         {
             HbHermiteSpline hbHermiteSpline = (HbHermiteSpline)hbCurve;
             List <XYZ>      points          = new List <XYZ>();
             foreach (HbXYZ point in hbHermiteSpline.Points)
             {
                 if (!ConvertHbXyz(point, out xyz1))
                 {
                     return(false);
                 }
                 points.Add(xyz1);
             }
             curve = HermiteSpline.Create(points, false);
         }
         return(true);
     }
     catch (Exception exception) {
         this.ErrorMessage = "Error in ConvertHbCurve: " + exception.Message + ".";
         curve             = null;
         return(false);
     }
 }
Ejemplo n.º 14
0
        static internal IEnumerable <Autodesk.Revit.DB.Curve> ToHost(this Rhino.Geometry.Curve curve, double curveTolerance = double.PositiveInfinity)
        {
            curveTolerance = Math.Min(Revit.ShortCurveTolerance, Math.Abs(curveTolerance));
            Debug.Assert(!curve.IsShort(curveTolerance));

            var simplifiedCurve = curve.Simplify(CurveSimplifyOptions.SplitAtFullyMultipleKnots, curveTolerance, Revit.AngleTolerance);

            if (simplifiedCurve != null)
            {
                curve = simplifiedCurve;
            }

            switch (curve)
            {
            case Rhino.Geometry.LineCurve line:

                yield return(Autodesk.Revit.DB.Line.CreateBound(line.PointAtStart.ToHost(), line.PointAtEnd.ToHost()));

                break;

            case Rhino.Geometry.PolylineCurve polyline:

                for (int p = 1; p < polyline.PointCount; ++p)
                {
                    yield return(Autodesk.Revit.DB.Line.CreateBound(polyline.Point(p - 1).ToHost(), polyline.Point(p).ToHost()));
                }
                break;

            case Rhino.Geometry.ArcCurve arc:

                if (arc.IsClosed)
                {
                    yield return(Autodesk.Revit.DB.Arc.Create(arc.Arc.Plane.ToHost(), arc.Arc.Radius, 0.0, (2.0 * Math.PI) - 2e-8));
                }
                else
                {
                    yield return(Autodesk.Revit.DB.Arc.Create(arc.Arc.StartPoint.ToHost(), arc.Arc.EndPoint.ToHost(), arc.Arc.MidPoint.ToHost()));
                }
                break;

            case Rhino.Geometry.PolyCurve polyCurve:

                polyCurve.RemoveNesting();
                polyCurve.RemoveShortSegments(curveTolerance);
                for (int s = 0; s < polyCurve.SegmentCount; ++s)
                {
                    foreach (var segment in polyCurve.SegmentCurve(s).ToHost())
                    {
                        yield return(segment);
                    }
                }
                break;

            case Rhino.Geometry.NurbsCurve nurbsCurve:

                if (nurbsCurve.IsLinear(Revit.VertexTolerance))
                {
                    yield return(Autodesk.Revit.DB.Line.CreateBound(nurbsCurve.PointAtStart.ToHost(), nurbsCurve.PointAtEnd.ToHost()));

                    yield break;
                }

                if (nurbsCurve.TryGetPolyline(out var polylineSegment))
                {
                    polylineSegment.ReduceSegments(curveTolerance);
                    foreach (var segment in polylineSegment.GetSegments())
                    {
                        yield return(Autodesk.Revit.DB.Line.CreateBound(segment.From.ToHost(), segment.To.ToHost()));
                    }

                    yield break;
                }

                if (nurbsCurve.TryGetArc(out var arcSegment, Revit.VertexTolerance))
                {
                    yield return(Autodesk.Revit.DB.Arc.Create(arcSegment.StartPoint.ToHost(), arcSegment.EndPoint.ToHost(), arcSegment.MidPoint.ToHost()));

                    yield break;
                }

                if (nurbsCurve.IsClosed)
                {
                    if (nurbsCurve.TryGetCircle(out var circle, Revit.VertexTolerance))
                    {
                        yield return(Autodesk.Revit.DB.Arc.Create(circle.Plane.ToHost(), circle.Radius, 0.0, 2.0 * (2.0 * Math.PI) - 2e-8));

                        yield break;
                    }

                    if (nurbsCurve.TryGetEllipse(out var ellipse, Revit.VertexTolerance))
                    {
                        yield return(Autodesk.Revit.DB.Ellipse.CreateCurve(ellipse.Plane.Origin.ToHost(), ellipse.Radius1, ellipse.Radius2, ellipse.Plane.XAxis.ToHost(), ellipse.Plane.YAxis.ToHost(), 0.0, (2.0 * Math.PI) - 2e-8));

                        yield break;
                    }

                    foreach (var segment in nurbsCurve.Split(nurbsCurve.Domain.Mid))
                    {
                        foreach (var c in segment.ToHost())
                        {
                            yield return(c);
                        }
                    }
                }
                else
                {
                    nurbsCurve.Knots.RemoveMultipleKnots(1, nurbsCurve.Degree, Revit.VertexTolerance);

                    var degree        = nurbsCurve.Degree;
                    var knots         = nurbsCurve.Knots.ToHost();
                    var controlPoints = nurbsCurve.Points.ToHost();

                    Debug.Assert(degree >= 1);
                    Debug.Assert(controlPoints.Count > nurbsCurve.Degree);
                    Debug.Assert(knots.Count == nurbsCurve.Degree + controlPoints.Count + 1);

                    Autodesk.Revit.DB.Curve nurbSpline = null;
                    try
                    {
                        if (nurbsCurve.IsRational)
                        {
                            var weights = new List <double>(controlPoints.Count);
                            foreach (var p in nurbsCurve.Points)
                            {
                                Debug.Assert(p.Weight > 0.0);
                                weights.Add(p.Weight);
                            }

                            Debug.Assert(weights.Count == controlPoints.Count);

                            nurbSpline = NurbSpline.CreateCurve(nurbsCurve.Degree, knots, controlPoints, weights);
                        }
                        else
                        {
                            nurbSpline = NurbSpline.CreateCurve(nurbsCurve.Degree, knots, controlPoints);
                        }
                    }
                    catch (Autodesk.Revit.Exceptions.ApplicationException e)
                    {
                        Debug.Fail(e.Source, e.Message);
                    }

                    yield return(nurbSpline);
                }
                break;

            default:
                foreach (var c in curve.ToNurbsCurve().ToHost())
                {
                    yield return(c);
                }
                break;
            }
        }
Ejemplo n.º 15
0
        static internal IEnumerable <Autodesk.Revit.DB.Curve> ToHost(this Rhino.Geometry.Curve curve)
        {
            var simplifiedCurve = curve.Simplify(CurveSimplifyOptions.SplitAtFullyMultipleKnots, Revit.ModelAbsoluteTolerance, Math.PI / 1800.0);

            if (simplifiedCurve != null)
            {
                curve = simplifiedCurve;
            }

            switch (curve)
            {
            case Rhino.Geometry.PolyCurve polyCurve:

                polyCurve.RemoveNesting();
                for (int s = 0; s < polyCurve.SegmentCount; ++s)
                {
                    foreach (var segment in polyCurve.SegmentCurve(s).ToHost())
                    {
                        yield return(segment);
                    }
                }
                break;

            case Rhino.Geometry.NurbsCurve nurbsCurve:

                if (nurbsCurve.IsClosed)
                {
                    foreach (var segment in nurbsCurve.Split(nurbsCurve.Domain.Mid))
                    {
                        foreach (var c in segment.ToHost())
                        {
                            yield return(c);
                        }
                    }
                }
                else
                {
                    nurbsCurve.Knots.RemoveMultipleKnots(1, nurbsCurve.Degree, Revit.ModelAbsoluteTolerance);

                    var degree        = nurbsCurve.Degree;
                    var knots         = nurbsCurve.Knots.ToHost();
                    var controlPoints = nurbsCurve.Points.ToHost();

                    Debug.Assert(degree >= 1);
                    Debug.Assert(controlPoints.Count > nurbsCurve.Degree);
                    Debug.Assert(knots.Count == nurbsCurve.Degree + controlPoints.Count + 1);

                    Autodesk.Revit.DB.Curve nurbSpline = null;
                    try
                    {
                        if (nurbsCurve.IsRational)
                        {
                            var weights = new List <double>(controlPoints.Count);
                            foreach (var p in nurbsCurve.Points)
                            {
                                Debug.Assert(p.Weight > 0.0);
                                weights.Add(p.Weight);
                            }

                            Debug.Assert(weights.Count == controlPoints.Count);

                            nurbSpline = NurbSpline.CreateCurve(nurbsCurve.Degree, knots, controlPoints, weights);
                        }
                        else
                        {
                            nurbSpline = NurbSpline.CreateCurve(nurbsCurve.Degree, knots, controlPoints);
                        }
                    }
                    catch (Autodesk.Revit.Exceptions.ApplicationException e)
                    {
                        Debug.Fail(e.Source, e.Message);
                    }

                    yield return(nurbSpline);
                }
                break;

            default:
                foreach (var c in ToHost(curve.ToNurbsCurve()))
                {
                    yield return(c);
                }
                break;
            }
        }
Ejemplo n.º 16
0
        /***************************************************/

        public static List <Curve> ToRevitCurves(this BH.oM.Geometry.NurbsCurve curve)
        {
            if (curve.ControlPoints.Count == 2)
            {
                return new List <Curve> {
                           new oM.Geometry.Line {
                               Start = curve.ControlPoints[0], End = curve.ControlPoints[1]
                           }.ToRevit()
                }
            }
            ;
            else
            {
                List <double> knots = curve.Knots.ToList();

                knots.Insert(0, knots[0]);
                knots.Add(knots[knots.Count - 1]);
                List <XYZ> controlPoints = curve.ControlPoints.Select(x => x.ToRevit()).ToList();

                try
                {
                    Curve nc = NurbSpline.CreateCurve(curve.Degree(), knots, controlPoints, curve.Weights);

                    //Split the curve in half when it is closed.
                    if (nc.GetEndPoint(0).DistanceTo(nc.GetEndPoint(1)) <= BH.oM.Adapters.Revit.Tolerance.Vertex)
                    {
                        double param1 = nc.GetEndParameter(0);
                        double param2 = nc.GetEndParameter(1);
                        Curve  c1     = nc.DeepClone();
                        Curve  c2     = nc.DeepClone();
                        c1.MakeBound(param1, (param1 + param2) * 0.5);
                        c2.MakeBound((param1 + param2) * 0.5, param2);
                        return(new List <Curve> {
                            c1, c2
                        });
                    }
                    else
                    {
                        return new List <Curve> {
                                   nc
                        }
                    };
                }
                catch
                {
                    BH.Engine.Reflection.Compute.RecordWarning("Conversion of a nurbs curve from BHoM to Revit failed. An approximate, 100-segment polyline has been created instead.");

                    List <XYZ> pts = new List <XYZ>();
                    int        k   = 100;
                    for (int i = 0; i <= k; i++)
                    {
                        double t = i / (double)k;
                        pts.Add(curve.PointAtParameter(t).ToRevit());
                    }

                    List <Curve> result = new List <Curve>();
                    for (int i = 1; i < pts.Count; i++)
                    {
                        result.Add(Line.CreateBound(pts[i - 1], pts[i]));
                    }

                    return(result);
                }
            }
        }
        private Autodesk.Revit.DB.NurbSpline CreateaSpiral(Spiral Sp)
        {
            var Splength    = Sp.length;
            var spEndRadius = Sp.radiusEnd;
            var SpStartRad  = Sp.radiusStart;
            var SpType      = Sp.spiType;
            var SpTheta     = Sp.theta;
            var SpTotalx    = Sp.totalX;
            var SpTotaly    = Sp.totalY;
            var spTanLong   = Sp.tanLong;
            var spTanShort  = Sp.tanShort;
            var Rot         = Sp.rot;

            var startPoint = ExtractPoint(Sp.Items[0]);
            var EndPoint   = ExtractPoint(Sp.Items[2]);
            var PiPoint    = ExtractPoint(Sp.Items[1]);

            double Radius = default(double);

            bool StraightPartAtStart = false;

            if (double.IsInfinity(SpStartRad))
            {
                Radius = spEndRadius;
            }
            else
            {
                Radius = SpStartRad;
                StraightPartAtStart = true;
            }

            var A   = Math.Sqrt(Radius * Splength);
            var tao = Math.Pow(A, 2) / (2 * Math.Pow(Radius, 2));

            // double SubDivisions = Math.Round((Splength / ObjectPlacement.Stationincrement));
            //Change
            double SubDivisions = Math.Round((Splength * 20));
            var    step         = tao / SubDivisions;

            List <XYZ> ControlPoints = new List <XYZ>();



            for (double i = 0.0; i < tao; i = i + step)
            {
                var x = A * Math.Sqrt(2 * i) * (1 - (Math.Pow(i, 2) / 10) + (Math.Pow(i, 4) / 216));
                var y = A * Math.Sqrt(2 * i) * ((i / 3) - (Math.Pow(i, 3) / 42) + (Math.Pow(i, 5) / 1320));

                //var Point = new XYZ(x, y, 0);
                if (Rot == clockwise.ccw)
                {
                    var Point = new XYZ(y, x, 0) + EndPoint;
                    ControlPoints.Add(Point);
                }
                else
                {
                    var Point = new XYZ(y, x, 0) + startPoint;
                    ControlPoints.Add(Point);
                }
            }

            var V1    = (ControlPoints.Last() - ControlPoints.First()).Normalize();
            var V2    = (EndPoint - startPoint).Normalize();
            var Angle = V2.AngleTo(V1);

            Angle = ((Math.PI / 2) - Angle);
            List <double> Weights = Enumerable.Repeat(1.0, ControlPoints.Count).ToList();
            var           P       = NurbSpline.CreateCurve(ControlPoints, Weights);


            NurbSpline RotatedCurve = null;

            if (Rot != clockwise.ccw)
            {
                var TransForm = Transform.CreateRotationAtPoint(XYZ.BasisZ, (Angle - Math.PI / 2), startPoint);
                RotatedCurve = (NurbSpline)P.CreateTransformed(TransForm);
            }
            else
            {
                var TransForm = Transform.CreateRotationAtPoint(XYZ.BasisZ, (Angle + Math.PI / 2), EndPoint);
                RotatedCurve = (NurbSpline)P.CreateTransformed(TransForm);

                var PointsReversed = RotatedCurve.CtrlPoints.Reverse();
                RotatedCurve = (NurbSpline)NurbSpline.CreateCurve(PointsReversed.ToList(), Weights);
            }



            RevitSegmentElement = RotatedCurve;
            return(RotatedCurve);
        }
        private void CreateRevitElement()
        {
            if (this.AlignmentSegmentElement is Line)
            {
                Autodesk.Revit.DB.Line L = Autodesk.Revit.DB.Line.CreateBound(this.GetStartPoint(), this.GetEndPoint());
                RevitSegmentElement = L;

                if ((bool)ObjectPlacement.TransForm.CreateAlignment)
                {
                    try
                    {
                        var ConvertedPointStart             = RevitPlacmenElement.ConvertPointToInternal(this.GetStartPoint());
                        var ConvertedEndPoint               = RevitPlacmenElement.ConvertPointToInternal(this.GetEndPoint());
                        Autodesk.Revit.DB.Line ConvrtedLine = Autodesk.Revit.DB.Line.CreateBound(ConvertedPointStart, ConvertedEndPoint);
                        CreateRevitElementInRevit(ConvrtedLine);
                    }
                    catch (Exception)
                    {
                    }
                    // Create a ModelArc element using the created geometry arc and sketch plane
                }
            }
            if (this.AlignmentSegmentElement is IrregularLine)
            {
                // Autodesk.Revit.DB.Line L = Autodesk.Revit.DB.Line.CreateBound(this.GetStartPoint().PlacementPoint, this.GetEndPoint().PlacementPoint);
                //  RevitSegmentElement = L;
            }
            if (this.AlignmentSegmentElement is Curve)
            {
                var StartPoint = this.GetStartPoint();
                var EndPoint   = this.GetEndPoint();
                var Radius     = this.GetCurveRadius();

                Arc C = CreateArc(StartPoint, EndPoint, Radius, (bool)false);
                RevitSegmentElement = C;

                if ((bool)ObjectPlacement.TransForm.CreateAlignment)
                {
                    try
                    {
                        var ConvertedPointStart = RevitPlacmenElement.ConvertPointToInternal(this.GetStartPoint());
                        var ConvertedEndPoint   = RevitPlacmenElement.ConvertPointToInternal(this.GetEndPoint());
                        var ConvertedRadius     = RevitPlacmenElement.ConvertDoubleToInternal(Radius);
                        Arc ConcertedCurve      = CreateArc(ConvertedPointStart, ConvertedEndPoint, ConvertedRadius, (bool)false);
                        CreateRevitElementInRevit(ConcertedCurve);
                    }
                    catch (Exception)
                    {
                    }
                    // Create a ModelArc element using the created geometry arc and sketch plane
                }
            }
            if (this.AlignmentSegmentElement is Spiral)
            {
                var Sp          = (this.AlignmentSegmentElement as Spiral);
                var NurbsSpline = CreateaSpiral(Sp);
                RevitSegmentElement = NurbsSpline;

                if ((bool)ObjectPlacement.TransForm.CreateAlignment)
                {
                    try
                    {
                        List <XYZ> ConvertedPoints = new List <XYZ>();
                        foreach (XYZ item in NurbsSpline.CtrlPoints)
                        {
                            var ConvertedPointStart = RevitPlacmenElement.ConvertPointToInternal(item);

                            ConvertedPoints.Add(ConvertedPointStart);
                        }
                        List <double> Weights             = Enumerable.Repeat(1.0, ConvertedPoints.Count).ToList();
                        var           ConvertedNurbsCurve = NurbSpline.CreateCurve(ConvertedPoints, Weights);
                        CreateRevitElementInRevit(ConvertedNurbsCurve);
                    }
                    catch (Exception)
                    {
                    }
                    // Create a ModelArc element using the created geometry arc and sketch plane
                }
            }
            if (this.AlignmentSegmentElement is Chain)
            {
                //Review
                //return ExtractPoint((this.AlignmentElement as Chain).Text);
            }
        }
Ejemplo n.º 19
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;

            //Reference adaptive = uidoc.Selection.PickObject(ObjectType.Element, "Select adaptive family");

            //Element adaptiveEle = doc.GetElement(adaptive);

            //PlanarFace pf = GetFace(adaptiveEle);

            //TaskDialog.Show("tr", "I am done");

            //TaskDialog.Show("tr", "so done");

            //TaskDialog.Show("tr", "done done");


            List <RG.Point3d> pts = new List <RG.Point3d> {
                new RG.Point3d(0, 0, 0), new RG.Point3d(5, 10, 0), new RG.Point3d(15, 5, 0), new RG.Point3d(20, 0, 0)
            };


            RG.PolylineCurve pc = new RG.PolylineCurve(pts);

            RG.Interval d = pc.Domain;

            //TaskDialog.Show("r", d.ToString());

            RG.NurbsCurve value = pc.ToNurbsCurve();

            value.IncreaseDegree(3);

            int newDegree     = 3;
            var degree        = value.Degree;
            var knots         = ToDoubleArray(value.Knots, newDegree);
            var controlPoints = ToXYZArray(value.Points, 1);
            var weights       = value.Points.ConvertAll(x => x.Weight);

            XYZ normal = new XYZ(0, 0, 1);
            XYZ origin = new XYZ(0, 0, 0);

            Plane rvtPlane = Plane.CreateByNormalAndOrigin(normal, origin);


            string knot = "";

            foreach (var item in knots)
            {
                knot += item.ToString() + "\n";
            }

            //TaskDialog.Show("r", knot);

            //TaskDialog.Show("R", $"ControlPoints > Degree: {controlPoints.Length} > {degree}\nKnots = degree + control points + 1 = {controlPoints.Length + degree + 1} ");

            Curve rvtN = NurbSpline.CreateCurve(newDegree, knots, controlPoints);

            //Trace.WriteLine()
            using (Transaction t = new Transaction(doc, "a"))
            {
                t.Start();
                SketchPlane sketchPlane = SketchPlane.Create(doc, rvtPlane);
                doc.Create.NewModelCurve(rvtN, sketchPlane);

                t.Commit();
            }

            return(Result.Succeeded);
        }