Exemple #1
0
        public override Brep GetBoundingBrep(double offset = 0.0)
        {
            //Curve CL = Centreline.Extend(CurveEnd.Both, offset, CurveExtensionStyle.Line);
            Curve  CL     = Centreline;
            double Length = CL.GetLength();
            double hW     = Data.NumWidth * Data.LamWidth / 2 + offset;
            double hH     = Data.NumHeight * Data.LamHeight / 2 + offset;

            double[] DivParams = new double[] { CL.Domain.Min, CL.Domain.Max };

            GenerateCorners(offset);

            Curve[][] LoftCurves = new Curve[4][];

            for (int i = 0; i < 4; ++i)
            {
                LoftCurves[i] = new Curve[2];
            }

            Rhino.Geometry.Transform xform;
            Line l1 = new Line(m_section_corners[0], m_section_corners[1]);
            Line l2 = new Line(m_section_corners[1], m_section_corners[2]);
            Line l3 = new Line(m_section_corners[2], m_section_corners[3]);
            Line l4 = new Line(m_section_corners[3], m_section_corners[0]);
            Line temp;

            for (int i = 0; i < DivParams.Length; ++i)
            {
                Plane p = GetPlane(DivParams[i]);
                xform            = Rhino.Geometry.Transform.PlaneToPlane(Plane.WorldXY, p);
                temp             = l1; temp.Transform(xform);
                LoftCurves[0][i] = temp.ToNurbsCurve();
                temp             = l2; temp.Transform(xform);
                LoftCurves[1][i] = temp.ToNurbsCurve();
                temp             = l3; temp.Transform(xform);
                LoftCurves[2][i] = temp.ToNurbsCurve();
                temp             = l4; temp.Transform(xform);
                LoftCurves[3][i] = temp.ToNurbsCurve();
            }

            Brep brep = new Brep();

            for (int i = 0; i < 4; ++i)
            {
                Brep[] loft = Brep.CreateFromLoft(LoftCurves[i], Point3d.Unset, Point3d.Unset, LoftType.Normal, false);
                if (loft == null || loft.Length < 1)
                {
                    continue;
                }
                for (int j = 0; j < loft.Length; ++j)
                {
                    brep.Append(loft[j]);
                }
            }

            brep.JoinNakedEdges(Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);
            brep = brep.CapPlanarHoles(Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);

            return(brep);
        }
Exemple #2
0
        public override bool CastTo <Q>(ref Q target)
        {
            if (typeof(Q).IsAssignableFrom(typeof(GH_Mesh)))
            {
                Mesh[] meshes = Value.GetMesh();
                Mesh   m      = new Mesh();
                for (int i = 0; i < meshes.Length; ++i)
                {
                    m.Append(meshes[i]);
                }
                object mesh = new GH_Mesh(m);

                target = (Q)mesh;
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GlulamWorkpiece)))
            {
                object blank = Value;
                target = (Q)blank;
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GH_Brep)))
            {
                Brep[] breps = Value.GetBrep();
                Brep   b     = new Brep();
                for (int i = 0; i < breps.Length; ++i)
                {
                    b.Append(breps[i]);
                }
                object brep = new GH_Brep(b);
                target = (Q)brep;
                return(true);
            }
            //if (typeof(Q).IsAssignableFrom(typeof(GH_)))
            if (typeof(Q).IsAssignableFrom(typeof(GH_Curve)))
            {
                Curve[] crvs = Value.Blank.GetAllGlulams().Select(x => x.Centreline).ToArray();
                //target = crvs.Select(x => new GH_Curve(x)).ToList() as Q;
                object crv = new GH_Curve(crvs.FirstOrDefault());
                target = (Q)(crv);
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(List <GH_Curve>)))
            {
                Curve[] crvs = Value.Blank.GetAllGlulams().Select(x => x.Centreline).ToArray();
                //target = crvs.Select(x => new GH_Curve(x)).ToList() as Q;
                object crv = crvs.Select(x => new GH_Curve(x)).ToList();
                target = (Q)(crv);
                return(true);
            }

            return(base.CastTo <Q>(ref target));
        }
        public static Brep ToAllPlaneBrep(this Brep roomBrep, double tolerance = 0.0001)
        {
            var tol   = tolerance;
            var surfs = roomBrep.Faces;

            surfs.ShrinkFaces();
            var checkedSrfs = new List <Brep>();

            foreach (var srf in surfs)
            {
                var s = srf.UnderlyingSurface();
                if (s is PlaneSurface ps)
                {
                    checkedSrfs.Add(ps.ToBrep());
                }
                else if (srf.IsPlanar())
                {
                    var cv = srf.OuterLoop.To3dCurve();
                    var p  = Brep.CreatePlanarBreps(cv, tol).First();
                    checkedSrfs.Add(p);
                }
                else
                {
                    throw new ArgumentException("Non-planar surfaces are not accepted!");
                }
            }

            //Method 1
            var solid = Brep.CreateSolid(checkedSrfs, tol).OrderBy(_ => _.Faces.Count).LastOrDefault();

            if (solid.IsSolid)
            {
                return(solid);
            }

            //Method 2
            solid = new Brep();
            checkedSrfs.ToList().ForEach(_ => solid.Append(_));
            solid.JoinNakedEdges(tol);
            if (solid.IsSolid)
            {
                return(solid);
            }

            //Method 3
            var joined = Brep.JoinBreps(checkedSrfs, tol).OrderBy(_ => _.Faces.Count).ToList();

            if (!joined.LastOrDefault().IsSolid)
            {
                solid = joined.Select(_ => _.CapPlanarHoles(tol)).SkipWhile(_ => _ == null).FirstOrDefault();
            }
            return(solid);
        }
Exemple #4
0
        public Brep CreateBrep()
        {
            Brep cellBrep = new Brep();

            foreach (var face in Faces)
            {
                var faceBrep = face.CreateBrep();
                if (faceBrep != null)
                {
                    cellBrep.Append(faceBrep);
                }
            }
            cellBrep.JoinNakedEdges(Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);
            return(cellBrep);
        }
        public DrawPFBrepConduit(IList <Brep> breps, Color color, double transparency)
        {
            pfBrep = new List <Brep>(breps) ?? throw new System.ArgumentNullException(nameof(breps));


            var allBrep = new Brep();

            for (int i = 0; i < pfBrep.Count; i++)
            {
                DisplayMaterial mat = new DisplayMaterial(color, 0.8);
                material.Add(mat);
                allBrep.Append(pfBrep[i]);
            }
            bbox = allBrep.GetBoundingBox(false);
        }
Exemple #6
0
        public override bool CastTo <Q>(ref Q target)
        {
            if (typeof(Q).IsAssignableFrom(typeof(GH_Mesh)))
            {
                Mesh[] meshes = Value.ToMesh();
                Mesh   m      = new Mesh();
                for (int i = 0; i < meshes.Length; ++i)
                {
                    m.Append(meshes[i]);
                }
                object mesh = new GH_Mesh(m);

                target = (Q)mesh;
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GlulamAssembly)))
            {
                object blank = Value;
                target = (Q)blank;
                return(true);
            }
            if (typeof(Q).IsAssignableFrom(typeof(GH_Brep)))
            {
                Brep[] breps = Value.ToBrep();
                Brep   b     = new Brep();
                for (int i = 0; i < breps.Length; ++i)
                {
                    b.Append(breps[i]);
                }
                object brep = new GH_Brep(b);
                target = (Q)brep;
                return(true);
            }

            return(base.CastTo <Q>(ref target));
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // Select planar surface
            var gs = new GetObject();

            gs.SetCommandPrompt("Select planar surface");
            gs.GeometryFilter  = ObjectType.Surface;
            gs.SubObjectSelect = false;
            gs.Get();
            if (gs.CommandResult() != Result.Success)
            {
                return(gs.CommandResult());
            }

            var brep_ref = gs.Object(0);
            var brep     = brep_ref.Brep();

            if (null == brep)
            {
                return(Result.Failure);
            }

            // Verify underlying surface is a PlaneSurface
            var plane_surface = brep.Faces[0].UnderlyingSurface() as PlaneSurface;

            if (null == plane_surface)
            {
                return(Result.Nothing);
            }

            // Select trimming curves on planar surface
            var gc = new GetObject();

            gc.SetCommandPrompt("Select trimming curves on planar surface");
            gc.GeometryFilter  = ObjectType.Curve;
            gc.GroupSelect     = true;
            gc.SubObjectSelect = false;
            gc.EnablePreSelect(false, true);
            gc.DeselectAllBeforePostSelect = false;
            gc.GetMultiple(1, 0);
            if (gc.CommandResult() != Result.Success)
            {
                return(gc.CommandResult());
            }

            // Make a copy of the selected Brep
            var new_brep = new Brep();

            new_brep.Append(brep);

            // Add each selected curve a a inner planar face loop
            var boundary = new Curve[1];

            for (var i = 0; i < gc.ObjectCount; i++)
            {
                var curve = gc.Object(i).Curve();
                if (null != curve)
                {
                    boundary[0] = curve;
                    new_brep.Loops.AddPlanarFaceLoop(0, BrepLoopType.Inner, boundary);
                }
            }

            new_brep.Compact();

            // IF all is well, then replace the selecte Brep with our new one
            if (new_brep.IsValid)
            {
                doc.Objects.Replace(brep_ref.ObjectId, new_brep);
                doc.Views.Redraw();
            }

            return(Result.Success);
        }
Exemple #8
0
        /// <summary>
        /// The Method that generates Navigation Model for the PathFinding algorithm.
        /// </summary>
        /// <param name="searchAreaMesh"> A Mesh generated from the input Breps representing the search area.</param>
        /// <param name="context"> A Brep that represents context of the navigation model.</param>
        /// <param name="scaleFactor"> A double larger than 1.</param>
        /// <param name="cellSize"> A double for defining the size of the navigation model cells.</param>
        /// <param name="subdivision"> Number of subdivision of navigation model's cells.</param>
        /// <returns> A list of generic types. The first item of the list is a DataTree of Curves for visualizing the navigation model.
        /// The second item of the list is a DataTree of zeros and ones related to the location of the obstacles and walls in the
        /// navigation model, which is called wallData.
        /// The final item of the list is a custom object (WallInformation) that contains the wallData in it. This object will be used
        /// to generate and save a Json file from the wallData.
        /// </returns>
        private List <object> NavigationModel(Mesh searchAreaMesh, Brep context, double scaleFactor, double cellSize, int subdivision)
        {
            List <object> result = new List <object>();

            int[,] wallDataArray;
            DataTree <int>      wallData        = new DataTree <int>();
            DataTree <Polyline> navigationCells = new DataTree <Polyline>();

            Rectangle3d cell = new Rectangle3d();

            Brep scaledContext = new Brep();

            scaledContext.Append(context);
            scaledContext.Transform(Transform.Scale(scaledContext.GetBoundingBox(true).Center, scaleFactor));

            int cellCount = Convert.ToInt32(System.Math.Round((context.Edges[0].Domain[1] * scaleFactor) / cellSize));

            // Array for serializing to Json
            wallDataArray = new int[cellCount, cellCount];

            Plane modelOrigin = new Plane(scaledContext.GetBoundingBox(true).Corner(true, true, true), Vector3d.ZAxis);

            Point3d sample = new Point3d();

            for (int i = 0; i < cellCount; i++)
            {
                for (int j = 0; j < cellCount; j++)
                {
                    cell = new Rectangle3d(new Plane(new Point3d(modelOrigin.OriginX + (i * cellSize), modelOrigin.OriginY + (j * cellSize), modelOrigin.OriginZ), modelOrigin.Normal), cellSize, cellSize);
                    navigationCells.Add(cell.ToPolyline(), new GH_Path(i));

                    int intersect = 0;

                    for (double k = 0; k < (subdivision * 4); k++)
                    {
                        sample = cell.PointAt(k * 4 / (subdivision * 4));

                        if (Rhino.Geometry.Intersect.Intersection.MeshRay(searchAreaMesh, new Ray3d(sample, Vector3f.ZAxis)) >= 0)
                        {
                            intersect++;
                        }
                    }
                    if (intersect >= subdivision * 4)
                    {
                        wallData.Add(0, new GH_Path(i));
                        wallDataArray[i, j] = 0;
                    }
                    else
                    {
                        wallData.Add(1, new GH_Path(i));
                        wallDataArray[i, j] = 1;
                    }
                }
            }

            WallInformation wallInfo = new WallInformation();

            wallInfo.WallData = wallDataArray;

            result.Add(navigationCells);
            result.Add(wallData);
            result.Add(wallInfo);

            return(result);
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            double tolerance      = doc.ModelAbsoluteTolerance;
            double angleTolerance = doc.ModelAngleToleranceRadians;


            List <Curve> icur = new List <Curve>();

            GetObject gcr = new Rhino.Input.Custom.GetObject();

            gcr.SetCommandPrompt("Select reference circles for prongs. (No curves)");
            gcr.GeometryFilter              = Rhino.DocObjects.ObjectType.Curve;
            gcr.GroupSelect                 = true;
            gcr.SubObjectSelect             = true;
            gcr.DeselectAllBeforePostSelect = true;
            gcr.OneByOnePostSelect          = false;
            gcr.GetMultiple(1, 0);

            for (int ie = 0; ie < gcr.ObjectCount; ie++)
            {
                Rhino.DocObjects.ObjRef      objref = gcr.Object(ie);
                Rhino.DocObjects.RhinoObject obj    = objref.Object();
                if (obj == null)
                {
                    return(Result.Failure);
                }
                Curve refcr = objref.Curve();
                if (refcr == null)
                {
                    return(Result.Failure);
                }
                obj.Select(false);

                icur.Add(refcr);
            }

            while (true)
            {
                m_escape_key_pressed       = false;
                RhinoApp.EscapeKeyPressed += RhinoApp_EscapeKeyPressed;

                Point3d pt0 = new Point3d(0, 0, hight);
                Point3d pt1 = new Point3d(0, 0, -(length - hight));

                double rotationRadiant = ((rotation / 180) * 3.1415);

                List <Point3d> curvePoints = new List <Point3d>()
                {
                    pt0, pt1
                };
                Curve prongCurve = Curve.CreateInterpolatedCurve(curvePoints, 3);

                if (!capBool)
                {
                    capMode = PipeCapMode.Flat;
                }
                else if (capBool)
                {
                    capMode = PipeCapMode.Round;
                }

                Brep[] cylBrep1 = Brep.CreatePipe(prongCurve, prongDia / 2, false, capMode, false, tolerance, angleTolerance);
                Brep   cylBrep2 = cylBrep1[0];
                cylBrep2.Faces.RemoveAt(2);
                Brep cylBrep3 = cylBrep2.CapPlanarHoles(tolerance);

                if (capBool)
                {
                    Vector3d     scaleVec   = new Vector3d(0, 0, 1);
                    var          scalePlane = new Plane(pt0, scaleVec);
                    var          capScale   = Transform.Scale(scalePlane, 1.0, 1.0, capHight);
                    var          surf2      = cylBrep3.Faces[1].UnderlyingSurface();
                    NurbsSurface nurbSurf2  = surf2.ToNurbsSurface();
                    nurbSurf2.Transform(capScale);
                    Brep capBrep = nurbSurf2.ToBrep();
                    cylBrep3.Append(capBrep);
                    cylBrep3.Faces.RemoveAt(1);
                }

                cylBrep3.Compact();
                cylBrep3.JoinNakedEdges(tolerance);
                string instDefCount = DateTime.Now.ToString("ddMMyyyyHHmmss");
                Brep[] brepArray    = new Brep[1] {
                    cylBrep3
                };
                var prongIndex = doc.InstanceDefinitions.Add("Prong" + instDefCount, "RefProng", Point3d.Origin, brepArray);

                foreach (Curve c in icur)
                {
                    Circle circleProngs = new Circle();
                    c.TryGetCircle(out circleProngs, tolerance);
                    NurbsCurve curveProngs = circleProngs.ToNurbsCurve();

                    Plane    planeNew = circleProngs.Plane;
                    Vector3d plVec    = planeNew.Normal;

                    if (planeNew == null)
                    {
                        curveProngs.DivideByCount(3, true, out Point3d[] curveProngsPoints);
                        planeNew = new Plane(curvePoints[0], curveProngsPoints[1], curveProngsPoints[2]);
                    }


                    Curve[] offsetCurveArray = curveProngs.Offset(planeNew, prongDia / 4, tolerance, CurveOffsetCornerStyle.Sharp);

                    Curve offsetCurve = offsetCurveArray[0];

                    double[] segParameters = offsetCurve.DivideByCount(prongCount, true);

                    List <Point3d> prongPoints = new List <Point3d>();
                    List <Guid>    idsInter    = new List <Guid>();

                    foreach (double p in segParameters)
                    {
                        Point3d zen        = new Point3d(0, 0, 0);
                        Point3d prongPoint = offsetCurve.PointAt(p);

                        Point3d  center  = circleProngs.Center;
                        Vector3d moveV   = new Vector3d(prongPoint);
                        Vector3d zaxis   = new Vector3d(0.0, 0.0, 1.0);
                        Plane    planeOr = new Plane(zen, zaxis);
                        Plane    plane   = new Plane(prongPoint, plVec);

                        var transform1 = Transform.PlaneToPlane(planeOr, plane);
                        var transform2 = Transform.Rotation(rotationRadiant, plVec, center);

                        var prongA = doc.Objects.AddInstanceObject(prongIndex, transform1);
                        var prongB = doc.Objects.Transform(prongA, transform2, true);

                        ids.Add(prongB);
                    }
                    doc.Views.Redraw();
                }


                GetOption go = new Rhino.Input.Custom.GetOption();
                go.SetCommandPrompt("Set prong parameters.");
                go.AcceptNothing(true);

                var countProng  = new Rhino.Input.Custom.OptionInteger(prongCount);
                var diaProng    = new Rhino.Input.Custom.OptionDouble(prongDia);
                var hightProng  = new Rhino.Input.Custom.OptionDouble(hight);
                var lengthProng = new Rhino.Input.Custom.OptionDouble(length);
                var rotProng    = new Rhino.Input.Custom.OptionDouble(rotation);
                var capProng    = new Rhino.Input.Custom.OptionToggle(capBool, "Flat", "Round");
                var hightCap    = new Rhino.Input.Custom.OptionDouble(capHight);

                go.AddOptionInteger("Count", ref countProng);
                go.AddOptionDouble("Diameter", ref diaProng);
                go.AddOptionDouble("Hight", ref hightProng);
                go.AddOptionDouble("Length", ref lengthProng);
                go.AddOptionDouble("Rotation", ref rotProng);
                go.AddOptionToggle("Cap", ref capProng);
                if (capBool)
                {
                    go.AddOptionDouble("Caphight", ref hightCap);
                }

                GetResult res = go.Get();

                if (m_escape_key_pressed)
                {
                    break;
                }

                hight      = hightProng.CurrentValue;
                length     = lengthProng.CurrentValue;
                prongCount = countProng.CurrentValue;
                prongDia   = diaProng.CurrentValue;
                rotation   = rotProng.CurrentValue;
                capBool    = capProng.CurrentValue;
                capHight   = hightCap.CurrentValue;

                if (length <= 0.0)
                {
                    length = 1.0;
                }
                if (prongCount <= 0)
                {
                    prongCount = 1;
                }
                if (prongDia <= 0.0)
                {
                    prongDia = 1.0;
                }
                if (res == GetResult.Nothing)
                {
                    break;
                }

                doc.Objects.Delete(ids, true);
            }
            RhinoApp.EscapeKeyPressed -= RhinoApp_EscapeKeyPressed;

            doc.Groups.Add(ids);

            return(Result.Success);
        }