Exemple #1
0
        /// <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)
        {
            double      num  = 0;
            List <Brep> list = new List <Brep>();

            if (DA.GetDataList <Brep>(0, list) && DA.GetData <double>(1, ref num))
            {
                Brep[] brepArray = null;
                brepArray = Brep.CreateBooleanUnion(list.ToArray(), 0.1);
                MeshingParameters meshingParameters = new MeshingParameters
                {
                    MaximumEdgeLength = num
                };
                Mesh[] meshArray = Mesh.CreateFromBrep(brepArray[0], meshingParameters);
                Mesh   msh       = new Mesh();
                int    num3      = meshArray.Length - 1;
                for (int i = 0; i <= num3; i++)
                {
                    msh.Append(meshArray[i]);
                }
                msh.Vertices.CullUnused();
                msh.Vertices.CombineIdentical(true, true);
                msh.UnifyNormals();
                msh = this.triangulation(msh);
                DA.SetData(0, msh);
            }
        }
        /// <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)
        {
            surfsToExtract = new List <Brep>();

            DA.GetData(0, ref marchingVertices);
            DA.GetDataList(1, surfsToExtract);

            Brep[] booleanSurfs = Brep.CreateBooleanUnion(surfsToExtract, 0.01);

            marchingVertices.ResetPoints();
            marchingVertices.ExtractSurfs(booleanSurfs);

            DA.SetDataList(0, marchingVertices.GeoGetSurfPoints());
        }
        public static Brep CreateHollowBrep(Curve curve1, Curve curve2)
        {
            Brep     brep1     = Brep.CreatePlanarBreps(new[] { curve1 })[0];
            Brep     brep2     = Brep.CreatePlanarBreps(new[] { curve2 })[0];
            RhinoDoc doc       = RhinoDoc.ActiveDoc;
            double   area      = Brep.CreateBooleanUnion(new[] { brep1, brep2 }, 0.001)[0].GetArea();
            double   brep1Area = brep1.GetArea();
            double   brep2Area = brep2.GetArea();

            if (area > 0.999 * brep1Area && area < 1.001 * brep1Area)
            {
                return(Brep.CreateBooleanDifference(brep1, brep2, doc.ModelAbsoluteTolerance)[0]);
            }

            else if (area > 0.999 * brep2Area && area < 1.001 * brep2Area)
            {
                return(Brep.CreateBooleanDifference(brep1, brep2, doc.ModelAbsoluteTolerance)[0]);
            }
            else
            {
                return(null);
            }
        }
Exemple #4
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //Create class instances
            Curve  centreCrv = new PolylineCurve();
            double thickness = 1;
            double fillet    = 0;

            //Get Data
            if (!DA.GetData(0, ref centreCrv))
            {
                return;
            }
            if (!DA.GetData(1, ref thickness))
            {
                return;
            }
            if (!DA.GetData(2, ref fillet))
            {
                return;
            }


            //Set properties
            PlotPlanning.ObjectModel.Street street = new ObjectModel.Street();
            street.CentreCurve  = centreCrv;
            street.Width        = thickness;
            street.CornerFillet = fillet;

            Brep [] b = Engine.Geometry.Compute.Sweep(centreCrv, thickness);

            //TODO: in order to merge all of the streets we need to change to list item and run the method on the list....
            Brep[] bUnion = Brep.CreateBooleanUnion(b, ObjectModel.Tolerance.Distance);

            //Set data
            DA.SetData(0, bUnion[0]);
        }
Exemple #5
0
        public static double GetCoverageArea(this List <Polyline> polys)
        {
            List <PolylineCurve> polylinecurves = polys.Select(n => new PolylineCurve(n.Select(m => new Point3d(m.X, m.Y, 0)))).ToList();
            var cunion = Curve.CreateBooleanUnion(polylinecurves);

            if (cunion.Length != 0)
            {
                return(AreaMassProperties.Compute(cunion).Area);
            }

            List <Brep> tomerge = new List <Brep>();

            for (int i = 1; i < polys.Count; i++)
            {
                Polyline temp  = new Polyline(polys[i].Select(n => new Point3d(n.X, n.Y, 0)));
                Brep     tempb = Brep.CreateEdgeSurface(new PolylineCurve(temp).DuplicateSegments());
                tomerge.Add(tempb);
            }

            var union = Brep.CreateBooleanUnion(tomerge, 0.1);

            Rhino.RhinoDoc.ActiveDoc.Objects.Add(union[0]);
            return(union[0].GetArea());
        }
Exemple #6
0
        public static GeometryLarge CreateGeometry(MaterialType mType, string mName)

        {
            RhinoDoc doc = RhinoDoc.ActiveDoc;

            //Allow user to pick multiple objects
            const Rhino.DocObjects.ObjectType geometryFilter = Rhino.DocObjects.ObjectType.Curve;

            Rhino.Input.Custom.GetObject go = new Rhino.Input.Custom.GetObject()
            {
                GeometryFilter              = geometryFilter,
                GroupSelect                 = true,
                SubObjectSelect             = false,
                DeselectAllBeforePostSelect = false
            };
            go.SetCommandPrompt("Pick one or two closed curves.");
            go.EnableClearObjectsOnEntry(false);
            go.EnableUnselectObjectsOnExit(false);

            bool bHavePreselectedObjects = false;

            for (;;)
            {
                Rhino.Input.GetResult res = go.GetMultiple(1, 0);
                if (go.ObjectsWerePreselected)
                {
                    bHavePreselectedObjects = true;
                    go.EnablePreSelect(false, true);
                    continue;
                }
                break;
            }

            //Unselects the preselected objects
            if (bHavePreselectedObjects)
            {
                for (int i = 0; i < go.ObjectCount; i++)
                {
                    RhinoObject rhinoObject = go.Object(i).Object();
                    if (null != rhinoObject)
                    {
                        rhinoObject.Select(false);
                    }
                }

                doc.Views.Redraw();
            }

            ObjRef[]     objs           = go.Objects();
            List <Curve> selectedCurves = new List <Curve>();

            foreach (ObjRef objRef in objs)
            {
                if (objRef.Curve() == null)
                {
                    RhinoApp.WriteLine("One of the selected objects was invalid.");
                    continue;
                }
                else if (!objRef.Curve().IsClosed)
                {
                    RhinoApp.WriteLine("One of the selected curves was not closed.");
                    continue;
                }
                selectedCurves.Add(objRef.Curve());
            }

            GeometryLarge larg;

            //if the curve is not closed do nothing
            switch (selectedCurves.Count)
            {
            case 0:
                Rhino.RhinoApp.WriteLine("No valid geometries was found.");
                return(null);

            case 1:
                larg = DrawAndSaveUserAttr(Brep.CreatePlanarBreps(selectedCurves)[0], doc, mType, mName);
                break;

            case 2:
                Brep brep1 = Brep.CreatePlanarBreps(new[] { selectedCurves[0] })[0];
                Brep brep2 = Brep.CreatePlanarBreps(new[] { selectedCurves[1] })[0];

                double area      = Brep.CreateBooleanUnion(new[] { brep1, brep2 }, 0.001)[0].GetArea();
                double brep1Area = brep1.GetArea();
                double brep2Area = brep2.GetArea();
                if (area > 0.999 * brep1Area && area < 1.001 * brep1Area)
                {
                    Brep brep = Brep.CreateBooleanDifference(brep1, brep2, doc.ModelAbsoluteTolerance)[0];
                    larg = DrawAndSaveUserAttr(brep, doc, mType, mName);
                    break;
                }

                else if (area > 0.999 * brep2Area && area < 1.001 * brep2Area)
                {
                    Brep brep = Brep.CreateBooleanDifference(brep1, brep2, doc.ModelAbsoluteTolerance)[0];
                    larg = DrawAndSaveUserAttr(brep, doc, mType, mName);
                    break;
                }
                else
                {
                    RhinoApp.WriteLine("The curves were not inside one another.");
                    return(null);
                }

            default:
                return(null);
            }

            foreach (ObjRef objRef in objs)
            {
                doc.Objects.Delete(objRef, false);
            }
            return(larg);
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            if (TimePanel.Instance.Restarted && TimePanel.Instance.TrackbarValue == 0)
            {
                ObjRef[] objrefs;
                Result   rc = Rhino.Input.RhinoGet.GetMultipleObjects("Select the polysurfaces for the union",
                                                                      false, ObjectType.Brep, out objrefs);
                if (rc != Result.Success)
                {
                    return(rc);
                }
                if (objrefs == null || objrefs.Length <= 1)
                {
                    Dialogs.ShowMessage("You have to select 2 or more shapes.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                    return(Result.Failure);
                }


                List <Brep> in_breps0 = new List <Brep>();
                for (int i = 0; i < objrefs.Length; i++)
                {
                    int index = RigidBodyManager.GuidList.IndexOf(objrefs[i].ObjectId);
                    //Avoid to create a compound from another compound
                    if (RigidBodyManager.RigidBodies[index].Shape is CompoundShape)
                    {
                        Dialogs.ShowMessage("You cannot create compound shape from another compound shape. Try to create it at once.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                        return(Result.Failure);
                    }
                    //Accept shapes only if they intersect to each other
                    Brep brep = objrefs[i].Brep();
                    if (brep != null)
                    {
                        in_breps0.Add(brep);
                    }
                    RhinoDoc.ActiveDoc.Objects.Delete(objrefs[i], true);
                }

                //Create the rhino compound shape
                double tolerance = doc.ModelAbsoluteTolerance;
                Brep[] breps     = Brep.CreateBooleanUnion(in_breps0, tolerance);
                if (breps.Length > 1)
                {
                    Dialogs.ShowMessage("You cannot create more than a compound shape in once time.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                    return(Result.Failure);
                }

                Brep rhinoCompound = breps[0];
                // If the user create zero or more than 1 compound the command fails
                if (breps.Length != 1)
                {
                    return(Rhino.Commands.Result.Nothing);
                }

                Brep copyToAdd = rhinoCompound.DuplicateBrep();

                //Create the rigid compound shape
                CompoundShape.TransformedShape[] transformedShapes = new CompoundShape.TransformedShape[in_breps0.Count];

                for (int i = 0; i < in_breps0.Count; i++)
                {
                    Guid guid           = objrefs[i].ObjectId;
                    int  indexRigidBody = RigidBodyManager.GuidList.IndexOf(guid);
                    transformedShapes[i] = new CompoundShape.TransformedShape(RigidBodyManager.RigidBodies[indexRigidBody].Shape, RigidBodyManager.RigidBodies[indexRigidBody].Orientation, RigidBodyManager.RigidBodies[indexRigidBody].Position);
                }
                CompoundShape jCompound     = new CompoundShape(transformedShapes);
                RigidBody     jCompoundBody = new RigidBody(jCompound);

                //Move the center of mass of Jitter shape on the center of the BBox of rhino shape
                Point3d centerBbox = rhinoCompound.GetBoundingBox(true).Center;
                jCompoundBody.Position = RigidBodyManager.Point3dtoJVector(centerBbox);
                //Find the difference between rhino bbx center and jitter bbox center
                JVector bboxjitter = jCompoundBody.BoundingBox.Center;
                JVector diff       = bboxjitter - RigidBodyManager.Point3dtoJVector(centerBbox);
                //Align the center of both bboxes
                jCompoundBody.Position -= diff;

                //Translate the center of the Bbox to 0,0,0 and save it to geometry list
                Point3d bboxDoc = rhinoCompound.GetBoundingBox(true).Center;
                rhinoCompound.Translate(new Vector3d(-bboxDoc.X, -bboxDoc.Y, -bboxDoc.Z));

                RigidBodyManager.RigidBodies.Add(jCompoundBody);
                RigidBodyManager.GeometryList.Add(rhinoCompound);
                Guid guidToAdd = doc.Objects.Add(copyToAdd);
                RigidBodyManager.GuidList.Add(guidToAdd);

                doc.Views.Redraw();

                return(Result.Success);
            }
            else
            {
                Dialogs.ShowMessage("Press Restart before use other commands", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                return(Result.Success);
            }
        }
Exemple #8
0
        //Meshes Open or Closed
        //TODO

        public void SliceUnionRegions(List <Brep> things, Plane ZPlane, out List <Brep> closedRegions, out List <Curve> openRegions)
        {
            List <Brep>  sepregions  = new List <Brep>();
            List <Brep>  regions     = new List <Brep>();
            List <Curve> openregions = new List <Curve>();

            //Slice Solid Breps with Planes
            for (int k = 0; k < things.Count; k++)
            {
                if (things[k].IsSolid)
                {
                    #region Slice

                    Curve[]   iCurves;
                    Point3d[] iPts;

                    Intersection.BrepPlane(things[k], ZPlane, Tolerance, out iCurves, out iPts);

                    List <Brep> iRegion = Brep.CreatePlanarBreps(iCurves).ToList();

                    #endregion

                    #region Check Normal is natural
                    //Make sure Normal and World Z axis are aligned


                    foreach (Brep reg in iRegion)
                    {
                        Vector3d normal = reg.Faces[0].NormalAt(0, 0);
                        //unitize normal to compare with world Z axis
                        normal.Unitize();

                        if (normal.X != Plane.WorldXY.ZAxis.X || normal.Y != Plane.WorldXY.ZAxis.Y || normal.Z != Plane.WorldXY.ZAxis.Z)
                        {
                            reg.Flip();
                            Vector3d normalcheck = reg.Faces[0].NormalAt(0, 0);
                        }
                    }

                    #endregion


                    sepregions.AddRange(iRegion);
                }

                //If Brep is Open, create slice curves
                if (!things[k].IsSolid)
                {
                    openBreps = true;

                    #region Slice

                    Curve[]   iCurves;
                    Point3d[] iPts;

                    Intersection.BrepPlane(things[k], ZPlane, Tolerance, out iCurves, out iPts);


                    #endregion

                    openregions.AddRange(iCurves.ToList());
                }
            }


            //Union of Regions
            if (sepregions.Count > 0)
            {
                regions = Brep.CreateBooleanUnion(sepregions, 0.01).ToList();
                if (regions.Count < 1)
                {
                    regions = sepregions;
                }
            }

            closedRegions = regions;
            openRegions   = openregions;
        }
        private Result CreateNoseCone(RhinoDoc doc, XmlNode compNd, ref double stackLength)
        {
            Result      result     = Rhino.Commands.Result.Failure;
            XmlElement  compEle    = compNd as XmlElement;
            XmlNodeList subCompNds = compNd.SelectNodes("subcomponents/*");

            double            length               = 0;
            double            thickness            = 0;
            NoseConeShapeType shape                = NoseConeShapeType.Ogive;
            double            shapeParameter       = 0;
            double            aftRadius            = 0;
            double            aftShoulderRadius    = 0;
            double            aftShoulderLength    = 0;
            double            aftShoulderThickness = 0;
            bool aftShoulderCapped = false;

            foreach (XmlNode nd in compEle.ChildNodes)
            {
                if (nd.Name == "length")
                {
                    length = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "thickness")
                {
                    thickness = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "shape")
                {
                    shape = (NoseConeShapeType)Enum.Parse(typeof(NoseConeShapeType), nd.InnerText, true);
                }
                else if (nd.Name == "shapeparameter")
                {
                    shapeParameter = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "aftradius")
                {
                    if (nd.InnerText == "auto")
                    {
                        XmlNode sibNd = compNd.NextSibling;
                        foreach (XmlNode sibChild in sibNd.ChildNodes)
                        {
                            if (sibNd.Name == "bodytube" && sibChild.Name == "radius")
                            {
                                aftRadius = Double.Parse(sibChild.InnerText);
                            }
                        }
                    }
                    else
                    {
                        aftRadius = Double.Parse(nd.InnerText);
                    }
                }
                else if (nd.Name == "aftshoulderradius")
                {
                    aftShoulderRadius = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "aftshoulderlength")
                {
                    aftShoulderLength = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "aftshoulderthickness")
                {
                    aftShoulderThickness = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "aftshouldercapped")
                {
                    aftShoulderCapped = Boolean.Parse(nd.InnerText);
                }
            }

            // generate geometry and create solid.
            int            numberDivisions = 100;
            OgiveCurve     ogive           = new OgiveCurve(aftRadius, length);
            double         xa     = ogive.SphericalCapApex(0);
            double         delta  = (length - xa) / (numberDivisions - 1);
            double         x      = xa;
            double         y      = 0;
            List <Point3d> points = new List <Point3d>();

            for (int i = 0; i < numberDivisions; i++)
            {
                double angle = (double)i * System.Math.PI / (double)numberDivisions;
                y = ogive.Evaluate(x);

                points.Add(new Point3d(x, y, 0));

                x += delta;
            }

            Polyline curve = new Polyline(points);

            NurbsCurve nbCurve = curve.ToNurbsCurve();

            Curve[] offsetsCurves = nbCurve.Offset(new Plane(new Point3d(0, 0, 0), Vector3d.XAxis, Vector3d.YAxis), thickness, 0.0001, CurveOffsetCornerStyle.None);

            Plane plane = new Plane(new Point3d(0, 0, 0), Vector3d.XAxis, Vector3d.ZAxis);

            Curve[] splits = offsetsCurves[0].Split(new PlaneSurface(plane, new Interval(0, 100), new Interval(0, 100)), 0.0001);

            LineCurve line1 = new LineCurve(nbCurve.PointAtStart, splits[1].PointAtStart);
            LineCurve line2 = new LineCurve(nbCurve.PointAtEnd, splits[1].PointAtEnd);

            List <Curve> curves = new List <Curve>()
            {
                nbCurve, splits[1], line1, line2
            };

            Curve[] joined = Curve.JoinCurves(curves);

            RevSurface revsrf = RevSurface.Create(joined[0], new Line(new Point3d(0, 0, 0), new Point3d(length, 0, 0)), 0, 2 * Math.PI);
            Brep       brep   = Brep.CreateFromRevSurface(revsrf, true, true);

            brep.Flip();

            if (aftShoulderLength > 0)
            {
                double innerRadius = aftShoulderRadius - aftShoulderThickness;
                double outerRadius = aftShoulderRadius;
                Plane  planeCyl    = new Plane(new Point3d(0, 0, 0), Vector3d.XAxis);
                Circle innerCir    = new Circle(planeCyl, innerRadius);
                Circle outerCir    = new Circle(planeCyl, outerRadius);

                Cylinder innerCyl = new Cylinder(innerCir, aftShoulderThickness + aftShoulderLength);
                Cylinder outerCyl = new Cylinder(outerCir, aftShoulderThickness + aftShoulderLength);

                Brep brepInner = Brep.CreateFromCylinder(innerCyl, true, true);
                Brep brepOuter = Brep.CreateFromCylinder(outerCyl, true, true);

                Brep[]    tube  = Brep.CreateBooleanDifference(brepOuter, brepInner, 0.0001);
                Transform trans = Transform.Translation(new Vector3d(length - aftShoulderThickness, 0, 0));

                tube[0].Transform(trans);

                Brep[] withShoulder = Brep.CreateBooleanUnion(new List <Brep>()
                {
                    brep, tube[0]
                }, 0.001);
                brep = withShoulder[0];
            }


            if (doc.Objects.AddBrep(brep) != Guid.Empty)
            {
                doc.Views.Redraw();
                result = Rhino.Commands.Result.Success;
            }

            stackLength = length; // cone seems to start a new stack

            foreach (XmlNode subNd in subCompNds)
            {
                if (subNd.Name == "tubecoupler")
                {
                    result = CreateTubeCoupler(doc, subNd, stackLength);
                }
            }

            return(result);
        }
Exemple #10
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            //pick surface to orient to or block instance to relocate
            GetObject gs = new Rhino.Input.Custom.GetObject();

            gs.SetCommandPrompt("Surface to orient new object on or BlockInstance to move");
            gs.GeometryFilter              = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.InstanceReference;
            gs.SubObjectSelect             = true;
            gs.DeselectAllBeforePostSelect = false;
            gs.OneByOnePostSelect          = true;
            gs.Get();
            if (gs.CommandResult() != Result.Success)
            {
                return(gs.CommandResult());
            }

            Rhino.DocObjects.ObjRef      objref = gs.Object(0);
            Rhino.DocObjects.RhinoObject obj    = objref.Object();
            if (obj == null)
            {
                return(Result.Failure);
            }
            surface = objref.Surface();


            //relocate block instance
            if (surface == null)
            {
                Rhino.DocObjects.InstanceObject instance1 = objref.Object() as Rhino.DocObjects.InstanceObject;

                instancePoint = instance1.InsertionPoint;
                double g, h;
                surface2.ClosestPoint(instancePoint, out g, out h);
                var instanceDirection = surface2.NormalAt(g, h);
                instancePlane = new Plane(instancePoint, instanceDirection);

                Rhino.Input.Custom.GetPoint gpss = new Rhino.Input.Custom.GetPoint();
                gpss.SetCommandPrompt("Point on surface to orient to");
                gpss.Constrain(surface2, false);

                gpss.DynamicDraw += RefObjDraw;
                gpss.Tag          = instance1;

                gpss.Get();
                if (gpss.CommandResult() != Rhino.Commands.Result.Success)
                {
                    return(gpss.CommandResult());
                }
                Point3d ptss = gpss.Point();
                surface2.ClosestPoint(ptss, out g, out h);
                var       direction1 = surface2.NormalAt(g, h);
                Plane     pl11       = new Plane(ptss, direction1);
                Transform iform      = Rhino.Geometry.Transform.PlaneToPlane(instancePlane, pl11);
                doc.Objects.Transform(instance1, iform, true);

                return(Result.Success);
            }

            obj.Select(false);

            //pick objekt to orient
            var copy = new Rhino.Input.Custom.OptionToggle(false, "No", "Yes");

            GetObject go = new GetObject();

            go.SetCommandPrompt("Select object to orient.");
            go.AddOptionToggle("Copy", ref copy);
            go.SubObjectSelect             = true;
            go.DeselectAllBeforePostSelect = false;
            go.GroupSelect = true;

            for (; ;)
            {
                var res = go.GetMultiple(1, -1);
                if (gs.CommandResult() != Result.Success)
                {
                    return(gs.CommandResult());
                }
                if (res == GetResult.Option)
                {
                    copyBol = copy.CurrentValue;
                    continue;
                }
                if (gs.CommandResult() != Result.Success)
                {
                    return(gs.CommandResult());
                }

                break;
            }



            int    obCount      = go.ObjectCount;
            string instDefCount = DateTime.Now.ToString("ddMMyyyyHHmmss");

            //create block instance and plane for instance
            Rhino.Input.Custom.GetPoint gp = new Rhino.Input.Custom.GetPoint();
            gp.SetCommandPrompt("Point to orient from");
            gp.Get();
            if (gp.CommandResult() != Rhino.Commands.Result.Success)
            {
                return(gp.CommandResult());
            }

            Vector3d vt1 = new Vector3d(0, 0, 1);
            Point3d  pt1 = gp.Point();

            sourcePlane = new Plane(pt1, vt1);
            Plane     originPlane = new Plane(Point3d.Origin, vt1);
            Transform bform       = Rhino.Geometry.Transform.PlaneToPlane(sourcePlane, originPlane);

            //block instance
            GeometryBase[] obj1List = new GeometryBase[obCount];
            List <Brep>    opList   = new List <Brep>();

            for (int igo = 0; igo < obCount; igo++)
            {
                Rhino.DocObjects.ObjRef      objref1 = go.Object(igo);
                Rhino.DocObjects.RhinoObject obj1    = objref1.Object();
                if (obj == null)
                {
                    return(Result.Failure);
                }
                obj.Select(false);


                Rhino.Geometry.Brep opItem = objref1.Brep();
                opList.Add(opItem);

                GeometryBase obj1Base = obj1.Geometry;
                obj1Base.Transform(bform);

                obj1List[igo] = obj1Base;
            }

            var orientBlock = doc.InstanceDefinitions.Add("Block" + instDefCount, "OrientBlock", Point3d.Origin, obj1List);

            //get all go.Objects to .Tag
            Brep[] op = new Brep[obCount];
            op = Brep.CreateBooleanUnion(opList, 0.01);
            Brep od = new Brep();

            od = op[0];
            var odGuid = doc.Objects.AddBrep(od);

            Rhino.DocObjects.ObjRef      objref2 = new Rhino.DocObjects.ObjRef(odGuid);
            Rhino.DocObjects.RhinoObject objDrw  = objref2.Object();

            //orient plane to surface
            if (copyBol)
            {
                while (true)
                {
                    Rhino.Input.Custom.GetPoint gps = new Rhino.Input.Custom.GetPoint();
                    gps.SetCommandPrompt("Point on surface to orient to. Press enter when done.");
                    gps.Constrain(surface, false);
                    gps.AcceptNothing(true);
                    gps.DynamicDraw += RefObjDraw;
                    gps.Tag          = objDrw;

                    var res = gps.Get();

                    if (res == GetResult.Nothing)
                    {
                        break;
                    }
                    //else if (gps.CommandResult() != Rhino.Commands.Result.Success)
                    //    return gps.CommandResult();


                    Point3d pts = gps.Point();
                    double  u, v;
                    surface.ClosestPoint(pts, out u, out v);
                    Vector3d direction = surface.NormalAt(u, v);
                    Plane    pl1       = new Plane(pts, direction);

                    Rhino.Geometry.Transform xform = Rhino.Geometry.Transform.PlaneToPlane(originPlane, pl1);

                    doc.Objects.AddInstanceObject(orientBlock, xform);

                    doc.Objects.Delete(objDrw);
                }
                copyBol = false;
            }
            else
            {
                Rhino.Input.Custom.GetPoint gps = new Rhino.Input.Custom.GetPoint();
                gps.SetCommandPrompt("Point on surface to orient to");
                gps.Constrain(surface, false);

                gps.DynamicDraw += RefObjDraw;
                gps.Tag          = objDrw;

                gps.Get();
                if (gps.CommandResult() != Rhino.Commands.Result.Success)
                {
                    return(gps.CommandResult());
                }
                Point3d pts = gps.Point();
                double  u, v;
                surface.ClosestPoint(pts, out u, out v);
                Vector3d direction = surface.NormalAt(u, v);
                Plane    pl1       = new Plane(pts, direction);

                Rhino.Geometry.Transform xform = Rhino.Geometry.Transform.PlaneToPlane(originPlane, pl1);

                doc.Objects.AddInstanceObject(orientBlock, xform);

                doc.Objects.Delete(objDrw);
            }

            surface2 = surface;

            return(Result.Success);
        }
Exemple #11
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            double tolerance = doc.ModelAbsoluteTolerance;

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

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

            gcr.SetCommandPrompt("Select reference circles for drill. (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;


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

                Point3d centerTop = new Point3d(0, 0, (headDrill + topDrill) * drillScale);
                Circle  circleTop = new Circle(centerTop, ancheadDrill / 2 * drillScale);
                Curve   curveTop  = circleTop.ToNurbsCurve();
                curveDrill.Add(curveTop);
                Point3d centerMid = new Point3d(0, 0, topDrill * drillScale);
                Circle  circleMid = new Circle(centerMid, drillScale / 2);
                Curve   curveMid  = circleMid.ToNurbsCurve();
                curveDrill.Add(curveMid);
                Circle circleBase = new Circle(drillScale / 2);
                Curve  curveBase  = circleBase.ToNurbsCurve();
                curveDrill.Add(curveBase);
                Point3d centerLow = new Point3d(0, 0, -midDrill * drillScale);
                Circle  circleLow = new Circle(centerLow, ancDrill / 2 * drillScale);
                Curve   curveLow  = circleLow.ToNurbsCurve();
                curveDrill.Add(curveLow);
                if (bottDrill != 0)
                {
                    Point3d centerBot = new Point3d(0, 0, (-midDrill - bottDrill) * drillScale);
                    Circle  circleBot = new Circle(centerBot, ancDrill / 2 * drillScale);
                    Curve   curveBot  = circleBot.ToNurbsCurve();
                    curveDrill.Add(curveBot);
                }


                Brep[] cylBrep1     = Brep.CreateFromLoft(curveDrill, Point3d.Unset, Point3d.Unset, LoftType.Straight, false);
                Brep[] cylBrep2     = Brep.CreateBooleanUnion(cylBrep1, tolerance);
                Brep   cylBrepFinal = cylBrep1[0];
                cylBrepFinal = cylBrepFinal.CapPlanarHoles(tolerance);


                string instDefCount = DateTime.Now.ToString("ddMMyyyyHHmmss");
                Brep[] brepArray    = new Brep[1] {
                    cylBrepFinal
                };
                var drillIndex = doc.InstanceDefinitions.Add("Drill" + instDefCount, "RefDrill 1mm", Point3d.Origin, brepArray);

                foreach (Curve c in icur)
                {
                    Circle circleDrill = new Circle();
                    c.TryGetCircle(out circleDrill, tolerance);

                    double   radiusDrill = circleDrill.Diameter;
                    Point3d  center      = circleDrill.Center;
                    Vector3d moveV       = new Vector3d(center);
                    Vector3d zaxis       = new Vector3d(0.0, 0.0, 1.0);
                    Plane    planeOr     = new Plane(center, zaxis);
                    Plane    planeNew    = circleDrill.Plane;
                    var      transform1  = Transform.Translation(moveV);
                    var      transform2  = Transform.Scale(center, radiusDrill);
                    var      transform3  = Transform.PlaneToPlane(planeOr, planeNew);

                    var stoneA = doc.Objects.AddInstanceObject(drillIndex, transform1);
                    var stoneB = doc.Objects.Transform(stoneA, transform2, true);
                    var stoneC = doc.Objects.Transform(stoneB, transform3, true);

                    ids.Add(stoneC);

                    doc.Views.Redraw();
                }


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

                var drillHead    = new Rhino.Input.Custom.OptionDouble(headDrill);
                var drillAncHead = new Rhino.Input.Custom.OptionDouble(ancheadDrill);
                var drillTop     = new Rhino.Input.Custom.OptionDouble(topDrill);
                var drillMid     = new Rhino.Input.Custom.OptionDouble(midDrill);
                var drillAnc     = new Rhino.Input.Custom.OptionDouble(ancDrill);
                var drillBott    = new Rhino.Input.Custom.OptionDouble(bottDrill);
                var scaleDrill   = new Rhino.Input.Custom.OptionDouble(drillScale);
                go.AddOptionDouble("Top", ref drillHead);
                go.AddOptionDouble("TopAngle", ref drillAncHead);
                go.AddOptionDouble("Mid", ref drillTop);
                go.AddOptionDouble("Bottom", ref drillMid);
                go.AddOptionDouble("BottomAngle", ref drillAnc);
                go.AddOptionDouble("Tail", ref drillBott);
                go.AddOptionDouble("Scale", ref scaleDrill);


                GetResult res = go.Get();

                if (m_escape_key_pressed)
                {
                    break;
                }



                topDrill     = drillTop.CurrentValue;
                midDrill     = drillMid.CurrentValue;
                ancDrill     = drillAnc.CurrentValue;
                bottDrill    = drillBott.CurrentValue;
                drillScale   = scaleDrill.CurrentValue;
                headDrill    = drillHead.CurrentValue;
                ancheadDrill = drillAncHead.CurrentValue;


                if (res == GetResult.Nothing)
                {
                    break;
                }

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

            doc.Groups.Add(ids);

            return(Result.Success);
        }