Beispiel #1
0
        /***************************************************/

        public static bool IsEqual(this BHG.Point bhPoint, RHG.ControlPoint rhPoint, double tolerance = BHG.Tolerance.Distance)
        {
            if (bhPoint == null & rhPoint.Equals(default(RHG.ControlPoint)))
            {
                return(true);
            }

            return(Math.Abs(bhPoint.X - rhPoint.Location.X) < tolerance &&
                   Math.Abs(bhPoint.Y - rhPoint.Location.Y) < tolerance &&
                   Math.Abs(bhPoint.Z - rhPoint.Location.Z) < tolerance);
        }
Beispiel #2
0
        // ************************************************ RevitModelBuilder Data Functions *****************************************************
        public HbCurve Hb(NurbsCurve nurbsCurveRhino)
        {
            HbNurbSpline nurbSplineRevit = new HbNurbSpline();

            Rhino.Geometry.Collections.NurbsCurvePointList points = nurbsCurveRhino.Points;
            for (int k = 0; k < points.Count; k++)
            {
                Rhino.Geometry.ControlPoint controlPoint = points[k];
                Point3d xyzRhino = controlPoint.Location;
                HbXYZ   xyzRevit = new HbXYZ(xyzRhino.X, xyzRhino.Y, xyzRhino.Z);
                nurbSplineRevit.Points.Add(xyzRevit);
            }
            HbCurve hbCurve = nurbSplineRevit;

            return(hbCurve);
        }
Beispiel #3
0
        public SurfaceConverter(CurveConverter curveConv, Vector3DConverter vecConv, Point3dConverter ptConv)
        {
            //extrusion surfaces
            AddConverter(new PipeConverter <rh.Extrusion, pps.Extrusion>(
                             (rhE) => {
                ppc.Line path = (ppc.Line)curveConv.ConvertToPipe <rh.Curve, ppc.Curve>(rhE.PathLineCurve());

                pps.Extrusion extr = new pps.Extrusion(curveConv.ConvertToPipe <rh.Curve, ppc.Curve>(rhE.Profile3d(0, 0)),
                                                       vecConv.ConvertToPipe <rh.Vector3d, pp.Vec>(rhE.PathTangent), path.Length);

                for (int i = 1; i < rhE.ProfileCount; i++)
                {
                    extr.Holes.Add(curveConv.ConvertToPipe <rh.Curve, ppc.Curve>(rhE.Profile3d(i, 0)));
                }

                extr.CappedAtStart = rhE.IsCappedAtBottom;
                extr.CappedAtEnd   = rhE.IsCappedAtTop;
                extr.SurfaceNormal = vecConv.ToPipe <rh.Vector3d, pp.Vec>(rhE.NormalAt(rhE.Domain(0).Mid, rhE.Domain(1).Mid));

                return(extr);
            },
                             (ppE) => {
                if (1 - ppE.Direction.Dot(new pp.Vec(0, 0, 1)) > 1e-3)
                {
                    //the extrusion is not vertical
                    throw new InvalidOperationException("Cannot create this extrusion. " +
                                                        "Try converting it into a polysurface and pushing it again");
                }
                var profile       = curveConv.FromPipe <rh.Curve, ppc.Curve>(ppE.ProfileCurve);
                rh.Extrusion extr = rh.Extrusion.Create(profile, ppE.Height, ppE.CappedAtEnd || ppE.CappedAtStart);
                ppE.Holes.ForEach((h) => extr.AddInnerProfile(curveConv.FromPipe <rh.Curve, ppc.Curve>(h)));
                //extr.SetOuterProfile(profile, false);
                //extr.SetPathAndUp(profile.PointAtStart, profile.PointAtStart + pathVec, pathVec);

                string msg;
                if (!extr.IsValidWithLog(out msg))
                {
                    System.Diagnostics.Debug.WriteLine(msg);
                    throw new InvalidOperationException("Cannot create a valid extrusion from the received data: \n" + msg);
                }

                var rhNorm = extr.NormalAt(extr.Domain(0).Mid, extr.Domain(1).Mid);
                if (rh.Vector3d.Multiply(rhNorm, vecConv.FromPipe <rh.Vector3d, pp.Vec>(ppE.SurfaceNormal)) < 0)
                {
                    //extrusions don't need to be flipped;
                }

                return(extr);
            }
                             ));

            //NurbsSurfaces
            AddConverter(new PipeConverter <rh.NurbsSurface, pps.NurbsSurface>(
                             (rns) =>
            {
                pps.NurbsSurface nurbs = new pps.NurbsSurface(rns.Points.CountU, rns.Points.CountV, rns.Degree(0), rns.Degree(1));

                for (int u = 0; u < rns.Points.CountU; u++)
                {
                    for (int v = 0; v < rns.Points.CountV; v++)
                    {
                        nurbs.SetControlPoint(ptConv.ToPipe <rh.Point3d, pp.Vec>(rns.Points.GetControlPoint(u, v).Location), u, v);
                        nurbs.SetWeight(rns.Points.GetControlPoint(u, v).Weight, u, v);
                    }
                }
                rh.Interval uDomain = rns.Domain(0);
                rh.Interval vDomain = rns.Domain(1);
                Func <double, rh.Interval, double> scaleKnot = (k, domain) => (k - domain.Min) / (domain.Length);
                nurbs.UKnots = rns.KnotsU.Select((k) => scaleKnot.Invoke(k, uDomain)).ToList();
                nurbs.VKnots = rns.KnotsV.Select((k) => scaleKnot.Invoke(k, vDomain)).ToList();

                nurbs.IsClosedInU = rns.IsClosed(0);
                nurbs.IsClosedInV = rns.IsClosed(1);

                nurbs.SurfaceNormal = vecConv.ToPipe <rh.Vector3d, pp.Vec>(rns.NormalAt(rns.Domain(0).Mid, rns.Domain(1).Mid));

                return(nurbs);
            },
                             (pns) => {
                if (pns.IsClosedInU)
                {
                    pns.WrapPointsToCloseSurface(0);
                }
                if (pns.IsClosedInV)
                {
                    pns.WrapPointsToCloseSurface(1);
                }

                var nurbs = rh.NurbsSurface.Create(3, true, pns.UDegree + 1, pns.VDegree + 1, pns.UCount, pns.VCount);

                for (int u = 0; u < pns.UCount; u++)
                {
                    for (int v = 0; v < pns.VCount; v++)
                    {
                        var cp = new rh.ControlPoint(ptConv.FromPipe <rh.Point3d, pp.Vec>(pns.GetControlPointAt(u, v)),
                                                     pns.GetWeightAt(u, v));
                        nurbs.Points.SetControlPoint(u, v, cp);
                    }
                }

                rh.Interval uDomain = nurbs.Domain(0);
                rh.Interval vDomain = nurbs.Domain(1);
                Func <double, rh.Interval, double> scaleKnot = (k, domain) => k * (domain.Length) + domain.Min;
                if (nurbs.KnotsU.Count == pns.UKnots.Count)
                {
                    for (int i = 0; i < nurbs.KnotsU.Count; i++)
                    {
                        nurbs.KnotsU[i] = scaleKnot.Invoke(pns.UKnots[i], uDomain);
                    }
                }
                if (nurbs.KnotsV.Count == pns.VKnots.Count)
                {
                    for (int i = 0; i < nurbs.KnotsV.Count; i++)
                    {
                        nurbs.KnotsV[i] = scaleKnot.Invoke(pns.VKnots[i], vDomain);
                    }
                }

                string msg;
                if (!nurbs.IsValidWithLog(out msg))
                {
                    System.Diagnostics.Debug.WriteLine(msg);
                    if (!nurbs.IsPeriodic(0))
                    {
                        nurbs.KnotsU.CreateUniformKnots(1.0 / (nurbs.Points.CountU));
                    }
                    else
                    {
                        nurbs.KnotsU.CreatePeriodicKnots(1.0 / (nurbs.Points.CountU));
                    }
                    if (!nurbs.IsPeriodic(1))
                    {
                        nurbs.KnotsV.CreateUniformKnots(1.0 / (nurbs.Points.CountV));
                    }
                    else
                    {
                        nurbs.KnotsV.CreatePeriodicKnots(1.0 / (nurbs.Points.CountV));
                    }

                    if (!nurbs.IsValid)
                    {
                        throw new InvalidOperationException("Cannot create a valid NURBS surface: \n" + msg);
                    }
                }

                var rhNorm = nurbs.NormalAt(nurbs.Domain(0).Mid, nurbs.Domain(1).Mid);
                if (rh.Vector3d.Multiply(rhNorm, vecConv.FromPipe <rh.Vector3d, pp.Vec>(pns.SurfaceNormal)) < 0)
                {
                    //need not flip rhino surfaces
                }

                return(nurbs);
            }
                             ));
        }
Beispiel #4
0
        public bool ReadDataTreeBranch(List <Grasshopper.Kernel.Types.GH_Curve> branches, ref List <List <HbCurve> > curvesListListRevit, bool AllowNurbs, bool NestBranch)
        {
            try {
                Rhino.Geometry.NurbsCurve nurbsCurveRhino;
                Rhino.Geometry.LineCurve  lineCurveRhino;
                Rhino.Geometry.ArcCurve   arcCurveRhino;
                List <HbCurve>            curvesListRevit = new List <HbCurve>();

                for (int i = 0; i < branches.Count; i++)
                {
                    //if (NestBranch)  curvesListRevit.Clear();
                    if (NestBranch)
                    {
                        curvesListRevit = new List <HbCurve>();
                    }
                    GH_Curve branch       = branches[i];
                    Curve    curveBranch  = null;
                    Curve    curveBranch2 = null;
                    GH_Convert.ToCurve(branch, ref curveBranch, GH_Conversion.Both);
                    if (curveBranch == null)
                    {
                        Print("Null branch value in ReadDataTreeBranch() ignored.");
                        continue;
                    }

                    switch (curveBranch.GetType().Name)
                    {
                    case "PolyCurve":
                        // Note:  The simplify functions only used with a PolyCureve so moved inside the switch statement.
                        // Note:  Logic of this step is not clear.  It may be that only the CurveSimplifyOptions.All
                        if (curveBranch.Degree == 1)
                        {
                            curveBranch2 = curveBranch.Simplify(CurveSimplifyOptions.RebuildLines, 0, 0);
                            if (curveBranch2 == null)
                            {
                                Print("Simplify() with CurveSimplifyOptions.RebuildLines returned null.");
                            }
                            else
                            {
                                curveBranch = curveBranch2;
                            }
                        }
                        if (curveBranch.Degree == 2)
                        {
                            curveBranch2 = curveBranch.Simplify(CurveSimplifyOptions.RebuildArcs, 0, 0);
                            if (curveBranch2 == null)
                            {
                                Print("Simplify() with CurveSimplifyOptions.RebuildArcs returned null.");
                            }
                            else
                            {
                                curveBranch = curveBranch2;
                            }
                        }
                        curveBranch2 = curveBranch.Simplify(CurveSimplifyOptions.All, .0001, 1);
                        if (curveBranch2 == null)
                        {
                            Print("Simplify() with CurveSimplifyOptions.All returned null.");
                        }
                        else
                        {
                            curveBranch = curveBranch2;
                        }
                        PolyCurve polyCurve = (PolyCurve)curveBranch;
                        for (int j = 0; j < polyCurve.SegmentCount; j++)
                        {
                            Curve curveRhino = polyCurve.SegmentCurve(j);
                            switch (curveRhino.GetType().Name)
                            {
                            case "LineCurve":
                                lineCurveRhino = (LineCurve)curveRhino;
                                curvesListRevit.Add(Hb(lineCurveRhino));
                                break;

                            case "NurbsCurve":
                                if (!AllowNurbs)
                                {
                                    break;
                                }

                                nurbsCurveRhino = (NurbsCurve)curveRhino;
                                curvesListRevit.Add(Hb(nurbsCurveRhino));
                                break;

                            case "ArcCurve":
                                arcCurveRhino = (ArcCurve)curveRhino;
                                curvesListRevit.Add(Hb(arcCurveRhino));
                                break;

                            case "PolylineCurve":
                                //NurbsCurve nurbsLineCurve = (NurbsCurve)curveBranch.ToNurbsCurve();
                                NurbsCurve nurbsLineCurve = (NurbsCurve)curveRhino.ToNurbsCurve();
                                Rhino.Geometry.Collections.NurbsCurvePointList points = nurbsLineCurve.Points;

                                for (int k = 0; k < points.Count - 1; k++)
                                {
                                    HbLine lineRevit = new HbLine();
                                    Rhino.Geometry.ControlPoint controlPointStart = points[k];
                                    Rhino.Geometry.ControlPoint controlPointEnd   = points[k + 1];
                                    Point3d xyzRhinoStart = controlPointStart.Location;
                                    Point3d xyzRhinoEnd   = controlPointEnd.Location;

                                    HbXYZ xyzRevitStart = new HbXYZ(xyzRhinoStart.X, xyzRhinoStart.Y, xyzRhinoStart.Z);
                                    HbXYZ xyzRevitEnd   = new HbXYZ(xyzRhinoEnd.X, xyzRhinoEnd.Y, xyzRhinoEnd.Z);

                                    lineRevit.PointStart = new HbXYZ(xyzRevitStart.X, xyzRevitStart.Y, xyzRevitStart.Z);
                                    lineRevit.PointEnd   = new HbXYZ(xyzRevitEnd.X, xyzRevitEnd.Y, xyzRevitEnd.Z);
                                    curvesListRevit.Add(lineRevit);
                                }

                                break;

                            default:
                                Print("Unknown Rhino PolyCurve curve type: " + curveRhino.GetType().Name);
                                break;
                            }
                        }
                        break;

                    case "NurbsCurve":
                        if (!AllowNurbs)
                        {
                            break;
                        }

                        nurbsCurveRhino = (NurbsCurve)curveBranch;
                        //if !nurbsCurveRhino.IsClosed()
                        curvesListRevit.Add(Hb(nurbsCurveRhino));
                        break;

                    case "PolylineCurve":
                        NurbsCurve nurbsLineCurve2 = (NurbsCurve)curveBranch.ToNurbsCurve();
                        Rhino.Geometry.Collections.NurbsCurvePointList points2 = nurbsLineCurve2.Points;

                        for (int k = 0; k < points2.Count - 1; k++)
                        {
                            HbLine lineRevit = new HbLine();
                            Rhino.Geometry.ControlPoint controlPointStart = points2[k];
                            Rhino.Geometry.ControlPoint controlPointEnd   = points2[k + 1];
                            Point3d xyzRhinoStart = controlPointStart.Location;
                            Point3d xyzRhinoEnd   = controlPointEnd.Location;

                            HbXYZ xyzRevitStart = new HbXYZ(xyzRhinoStart.X, xyzRhinoStart.Y, xyzRhinoStart.Z);
                            HbXYZ xyzRevitEnd   = new HbXYZ(xyzRhinoEnd.X, xyzRhinoEnd.Y, xyzRhinoEnd.Z);

                            lineRevit.PointStart = new HbXYZ(xyzRevitStart.X, xyzRevitStart.Y, xyzRevitStart.Z);
                            lineRevit.PointEnd   = new HbXYZ(xyzRevitEnd.X, xyzRevitEnd.Y, xyzRevitEnd.Z);
                            curvesListRevit.Add(lineRevit);
                        }

                        break;

                    case "LineCurve":
                        lineCurveRhino = (LineCurve)curveBranch;
                        curvesListRevit.Add(Hb(lineCurveRhino));
                        break;

                    case "ArcCurve":
                        arcCurveRhino = (ArcCurve)curveBranch;
                        curvesListRevit.Add(Hb(arcCurveRhino));
                        break;

                    default:
                        Print("Unknown Rhino curve type: " + curveBranch.GetType().Name);
                        break;
                    }
                    if (NestBranch)
                    {
                        curvesListListRevit.Add(curvesListRevit);
                    }
                }

                if (!NestBranch)
                {
                    curvesListListRevit.Add(curvesListRevit);
                }

                return(true);
            }
            catch (Exception exception) {
                Print("Exception in 'ReadDataTreeBranch' w/ curves: " + exception.Message);
                return(false);
            }
        }
Beispiel #5
0
        /***************************************************/

        public static BHG.Point FromRhino(this RHG.ControlPoint rhinoPoint)
        {
            return(new BHG.Point {
                X = rhinoPoint.Location.X, Y = rhinoPoint.Location.Y, Z = rhinoPoint.Location.Z
            });
        }