Example #1
0
        /***************************************************/
        /****               Public Methods              ****/
        /***************************************************/

        public static Element ToRevitElement(this ModelInstance modelInstance, Document document, RevitSettings settings = null, Dictionary <Guid, List <int> > refObjects = null)
        {
            if (modelInstance == null || document == null)
            {
                return(null);
            }

            Element element = refObjects.GetValue <Element>(document, modelInstance.BHoM_Guid);

            if (element != null)
            {
                return(element);
            }

            if (modelInstance.Properties == null)
            {
                Compute.NullObjectPropertiesWarning(modelInstance);
                return(null);
            }

            settings = settings.DefaultIfNull();

            BuiltInCategory builtInCategory = modelInstance.BuiltInCategory(document);

            if (modelInstance.Location is ISurface || modelInstance.Location is ISolid)
            {
                Solid brep = ToRevit(modelInstance.Location as dynamic);
                if (brep == null)
                {
                    Compute.GeometryConvertFailed(modelInstance);
                    return(null);
                }

                DirectShape directShape = DirectShape.CreateElement(document, new ElementId((int)builtInCategory));
                directShape.AppendShape(new List <GeometryObject> {
                    brep
                });
                element = directShape;
            }
            else
            {
                ElementType elementType = modelInstance.Properties.ElementType(document, new List <BuiltInCategory> {
                    builtInCategory
                }, settings);
                element = modelInstance.IToRevitElement(elementType, settings);
            }

            if (element == null)
            {
                return(null);
            }

            // Copy parameters from BHoM object to Revit element
            element.CopyParameters(modelInstance, settings);

            refObjects.AddOrReplace(modelInstance, element);
            return(element);
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;

            Autodesk.Revit.UI.Selection.Selection sel = commandData.Application.ActiveUIDocument.Selection;
            Reference   reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
            Transaction trans     = new Transaction(doc);
            Element     elem      = doc.GetElement(reference);

            trans.Start("Create solid");
            LocationCurve lc = elem.Location as LocationCurve;

            if (lc == null)
            {
                throw new InvalidOperationException("Current element is not a curve-dirven element");
            }
            //Create the sweep path
            var   pathCurveLoop = new CurveLoop();
            Curve curve         = lc.Curve;

            pathCurveLoop.Append(curve);
            //get the point to create a plane for the profile.
            XYZ xyzFirstPoint = curve.GetEndPoint(0);
            //Create the plane
            Transform transform    = curve.ComputeDerivatives(0, true);
            XYZ       xyzDirection = transform.BasisX;
            Plane     profilePlane = Plane.CreateByNormalAndOrigin(xyzDirection.Normalize(), transform.Origin);
            //Create a profile which is a rectangle with height and width of 2000mm and 900mm

            double    dblHeight = 2000 / 304.8;
            double    dblWidth  = 900 / 304.8;
            UV        uvPt0     = new UV(0, 0) - new UV(dblWidth / 2, 0);
            UV        uvPt1     = uvPt0 + new UV(dblWidth, 0);
            UV        uvPt2     = uvPt1 + new UV(0, dblHeight);
            UV        uvPt3     = uvPt2 - new UV(dblWidth, 0);
            XYZ       xVec      = profilePlane.XVec;
            XYZ       yVec      = profilePlane.YVec;
            XYZ       xyzPt0    = profilePlane.Origin + profilePlane.XVec.Multiply(uvPt0.U) + profilePlane.YVec.Multiply(uvPt0.V);
            XYZ       xyzPt1    = profilePlane.Origin + profilePlane.XVec.Multiply(uvPt1.U) + profilePlane.YVec.Multiply(uvPt1.V);
            XYZ       xyzPt2    = profilePlane.Origin + profilePlane.XVec.Multiply(uvPt2.U) + profilePlane.YVec.Multiply(uvPt2.V);
            XYZ       xyzPt3    = profilePlane.Origin + profilePlane.XVec.Multiply(uvPt3.U) + profilePlane.YVec.Multiply(uvPt3.V);
            Line      l1        = Line.CreateBound(xyzPt0, xyzPt1);
            Line      l2        = Line.CreateBound(xyzPt1, xyzPt2);
            Line      l3        = Line.CreateBound(xyzPt2, xyzPt3);
            Line      l4        = Line.CreateBound(xyzPt3, xyzPt0);
            CurveLoop loop      = new CurveLoop();

            loop.Append(l1);
            loop.Append(l2);
            loop.Append(l3);
            loop.Append(l4);
            List <CurveLoop> newloops = new List <CurveLoop>()
            {
                loop
            };
            //Create the swept solid
            Solid sweepSolid = GeometryCreationUtilities.CreateSweptGeometry(pathCurveLoop, 0, lc.Curve.GetEndParameter(0), newloops);
            //Create a directShape to visualize the solid
            DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel));

            ds.AppendShape(new Solid[1] {
                sweepSolid
            });
            trans.Commit();
            return(Result.Succeeded);
        }
Example #3
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);
        }