Exemplo n.º 1
0
        /// <summary>
        /// 创建未修剪的 Rhino PlaneSurface
        /// </summary>
        /// <param name="face"></param>
        /// <returns></returns>
        static PlaneSurface ToRhinoPlaneSurface(NXOpen.Face face)
        {
            double[] facePt    = new double[3];
            double[] direction = new double[3];
            double[] box       = new double[6];
            TheUfSession.Modl.AskFaceData(face.Tag, out int type, facePt, direction, box, out double radius, out double radData, out int normDir);

            Plane plane = new Plane(new Point3d(facePt[0], facePt[1], facePt[2]), new Vector3d(direction[0], direction[1], direction[2]));

            return(PlaneSurface.CreateThroughBox(plane, new BoundingBox(new Point3d(box[0], box[1], box[2]), new Point3d(box[3], box[4], box[5]))));
        }
Exemplo n.º 2
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            //First, collect all the breps to split
            ObjRef[] obj_refs;
            var      rc = RhinoGet.GetMultipleObjects("Select breps to split", false, ObjectType.Brep, out obj_refs);

            if (rc != Result.Success || obj_refs == null)
            {
                return(rc);
            }

            // Get the final plane
            Plane plane;

            rc = RhinoGet.GetPlane(out plane);
            if (rc != Result.Success)
            {
                return(rc);
            }

            //Iterate over all object references
            foreach (var obj_ref in obj_refs)
            {
                var brep = obj_ref.Brep();
                var bbox = brep.GetBoundingBox(false);

                //Grow the boundingbox in all directions
                //If the boundingbox is flat (zero volume or even zero area)
                //then the CreateThroughBox method will fail.
                var min_point = bbox.Min;
                min_point.X -= 1.0;
                min_point.Y -= 1.0;
                min_point.Z -= 1.0;
                bbox.Min     = min_point;
                var max_point = bbox.Max;
                max_point.X += 1.0;
                max_point.Y += 1.0;
                max_point.Z += 1.0;
                bbox.Max     = max_point;

                var plane_surface = PlaneSurface.CreateThroughBox(plane, bbox);
                if (plane_surface == null)
                {
                    //This is rare, it will most likely not happen unless either the plane or the boundingbox are invalid
                    RhinoApp.WriteLine("Cutting plane could not be constructed.");
                }
                else
                {
                    var breps = brep.Split(plane_surface.ToBrep(), doc.ModelAbsoluteTolerance);
                    if (breps == null || breps.Length == 0)
                    {
                        RhinoApp.Write("Plane does not intersect brep (id:{0})", obj_ref.ObjectId);
                        continue;
                    }
                    foreach (var brep_piece in breps)
                    {
                        doc.Objects.AddBrep(brep_piece);
                    }
                    doc.Objects.AddSurface(plane_surface);
                    doc.Objects.Delete(obj_ref, false);
                }
            }

            doc.Views.Redraw();
            return(Result.Success);
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            MaterialProperties mp = null;
            Brep   BBrep          = new Brep();
            Curve  spCrv          = null;
            int    N     = 0;
            double scale = 0;

            if (!DA.GetData(0, ref mp))
            {
                return;
            }
            if (!DA.GetData(1, ref BBrep))
            {
                return;
            }
            if (!DA.GetData(2, ref spCrv))
            {
                return;
            }
            if (!DA.GetData(3, ref N))
            {
                return;
            }
            if (!DA.GetData(4, ref scale))
            {
                return;
            }


            if (N < 2)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input n must be an integer greater than 1");
                return;
            }

            //Copy to each analysis plugin - extracts material properties from MP input
            double fc = mp.fC; double Ec = mp.EC; double ec = mp.eC; double rhoc = mp.rhoC; double EEc = mp.EEC;
            double fy = mp.fY; double Es = mp.ES; double es = mp.eS; double rhos = mp.rhoS; double EEs = mp.EES;

            if (spCrv.PointAtStart.X > spCrv.PointAtEnd.X)
            {
                spCrv.Reverse();
            }
            //double tStart = 0;
            //spCrv.ClosestPoint(spCrv.PointAtEnd, out tStart);

            //Extract perpendicular planes to segment Brep
            Double[] spCrvDiv = spCrv.DivideByCount(N, false);
            Plane[]  splitPls = spCrv.GetPerpendicularFrames(spCrvDiv);

            double L  = spCrv.GetLength();
            double dl = L / (N);

            BoundingBox boundBBrep  = BBrep.GetBoundingBox(Plane.WorldXY);
            List <Brep> splitPlns   = new List <Brep>();
            List <Brep> splitBBreps = new List <Brep>();
            Brep        smallBBrep  = BBrep.DuplicateBrep();
            Brep        add         = new Brep();

            List <double>  qw       = new List <double>();
            List <Point3d> qwPts    = new List <Point3d>();
            List <Point3d> spCrvPts = new List <Point3d>();


            //qw.Add(0);
            //qwPts.Add(spCrv.PointAtStart);

            for (int i = 0; i < splitPls.Length; i++)
            {
                PlaneSurface plSrf = PlaneSurface.CreateThroughBox(splitPls[i], boundBBrep);
                splitPlns.Add(plSrf.ToBrep());
            }

            Brep[]   splitBBrep = smallBBrep.Split(splitPlns, DocumentTolerance());
            double[] sortSplit  = new double[splitBBrep.Length];
            for (int i = 0; i < splitBBrep.Length; i++)
            {
                splitBBrep[i] = splitBBrep[i].CapPlanarHoles(DocumentTolerance());
                double xLoc = VolumeMassProperties.Compute(splitBBrep[i]).Centroid.X;
                sortSplit[i] = xLoc;
            }

            Array.Sort(sortSplit, splitBBrep);

            //for (int i = 1; i <= splitPls.Length - 1; i++)
            //{
            //    PlaneSurface plSrf = PlaneSurface.CreateThroughBox(splitPls[i], boundBBrep);
            //    splitPlns.Add(plSrf.ToBrep());

            //    Brep[] split = smallBBrep.Split(plSrf.ToBrep(), DocumentTolerance());

            //    if (split.Length > 0)
            //    {
            //        if (split[0].GetVolume() < split[1].GetVolume())
            //        {
            //            add = split[0].CapPlanarHoles(DocumentTolerance());
            //            smallBBrep = split[1].CapPlanarHoles(DocumentTolerance());
            //        }
            //        else if (split[0].GetVolume() > split[1].GetVolume())
            //        {
            //            add = split[1].CapPlanarHoles(DocumentTolerance());
            //            smallBBrep = split[0].CapPlanarHoles(DocumentTolerance());
            //        }
            //        splitBBreps.Add(add);
            //    }
            //    //foreach(Brep b in split) { splitBBreps.Add(b.CapPlanarHoles(DocumentTolerance())); }
            //}
            //qwPts.Add(spCrv.PointAtEnd);

            //Point3d qwPt0 = spCrv.PointAt(edge0div[0] * 0.5); qwPts.Add(qwPt0);

            for (int i = 0; i < splitBBrep.Length; i++)
            {
                Point3d pt = new Point3d();
                if (i < splitBBrep.Length - 1)
                {
                    pt = spCrv.PointAt(spCrvDiv[i] + spCrvDiv[0] * 0.5);
                }
                else if (i == splitBBrep.Length - 1)
                {
                    pt = spCrv.PointAt(spCrvDiv[0] * 0.5);
                }
                spCrvPts.Add(pt);
                double dqw = Math.Abs(splitBBrep[i].GetVolume()) * rhoc * 0.00980665 / dl;

                qw.Add(dqw);
                Point3d qwPt = new Point3d(pt.X, pt.Y, dqw * scale);
                qwPts.Add(qwPt);
            }

            List <Curve> graphActual = new List <Curve>();
            //Curve qwCrv = null;
            Curve qwCrv = Curve.CreateInterpolatedCurve(qwPts, 1);

            graphActual.Add(qwCrv);

            for (int i = 0; i < qwPts.Count; i++)
            {
                Line crv = new Line(qwPts[i], spCrvPts[i]);
                graphActual.Add(crv.ToNurbsCurve());
            }


            double qwSum = 0;

            foreach (double qwi in qw)
            {
                qwSum += qwi;
            }
            double qwAve = qwSum / qw.Count;

            List <Point3d> qwAvePts = new List <Point3d>();

            for (int i = 0; i < qw.Count; i++)
            {
                //Point3d pt = spCrv.PointAt((spCrvDiv[i] + spCrvDiv[1] * 0.5)); // spCrv.GetLength());
                Point3d pt      = spCrvPts[i];
                Point3d qwAvePt = new Point3d(pt.X, pt.Y, qwAve * scale);
                qwAvePts.Add(qwAvePt);
            }

            List <Curve> graphAverage = new List <Curve>();
            Curve        qwAveCrv     = Curve.CreateInterpolatedCurve(qwAvePts, 1);

            graphAverage.Add(qwAveCrv);

            for (int i = 0; i < qwPts.Count; i++)
            {
                Line crv = new Line(qwAvePts[i], spCrvPts[i]);
                graphAverage.Add(crv.ToNurbsCurve());
            }

            List <Vector3d> Pw = new List <Vector3d>();

            foreach (double q in qw)
            {
                Pw.Add(new Vector3d(0, 0, -q * dl));
            }

            //Brep[] splitBBrep = BBrep.Split(splitPlns, DocumentTolerance());

            Double[] PwSpCrvDiv   = spCrv.DivideByCount(2 * N, true);
            Curve[]  PwSpCrvSplit = spCrv.Split(PwSpCrvDiv);

            Double[] qwSpCrvDiv   = spCrv.DivideByCount(N, true);
            Curve[]  qwSpCrvSplit = spCrv.Split(qwSpCrvDiv);

            List <Curve> qwGraphActual  = new List <Curve>();
            List <Curve> qwGraphAverage = new List <Curve>();
            Curve        crvAct         = null;
            Curve        crvAve         = null;

            for (int i = 0; i < qwSpCrvSplit.Length; i++)
            {
                Transform moveAct = Transform.Translation(0, 0, qw[i] * scale);
                Transform moveAve = Transform.Translation(0, 0, qwAve * scale);

                crvAct = qwSpCrvSplit[i].DuplicateCurve();
                crvAct.Transform(moveAct);
                qwGraphActual.Add(crvAct);

                crvAve = qwSpCrvSplit[i].DuplicateCurve();
                crvAve.Transform(moveAve);
                qwGraphAverage.Add(crvAve);
            }

            //Curve[] spCrvTEST = new Curve[1] { spCrv};

            for (int i = 0; i < qw.Count; i++)
            {
                qw[i] *= -1;
            }

            DA.SetDataList(0, qw);
            DA.SetDataList(1, qwSpCrvSplit);

            //DA.SetDataList(2, Pw);
            //DA.SetDataList(3, PwSpCrvSplit);
            //DA.SetDataList(4, spCrvPts);

            DA.SetDataList(2, qwGraphActual);
            DA.SetDataList(3, qwGraphAverage);
            DA.SetDataList(4, splitBBrep);
        }