protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            Plane  plane  = Plane.WorldXY;
            double radius = 10.0;
            double height = 2.0;

            Circle   circle   = new Circle(plane, radius);
            Cylinder cylinder = new Cylinder(circle, height);

            Brep brep = Brep.CreateFromCylinder(cylinder, true, true);

            SampleCsShadedBrepConduit conduit = new SampleCsShadedBrepConduit();

            conduit.BrepGeometry = brep;
            conduit.Enabled      = true;
            doc.Views.Redraw();

            string out_str = null;

            RhinoGet.GetString("Press <Enter> to continue", true, ref out_str);

            conduit.Enabled = false;

            doc.Objects.AddBrep(brep);
            doc.Views.Redraw();

            return(Result.Success);
        }
Ejemplo n.º 2
0
 public Brep ToBrep(bool capBottom, bool capTop)
 {
     return(Brep.CreateFromCylinder(this, capBottom, capTop));
 }
        private Result CreateCenteringRing(RhinoDoc doc, XmlNode compNd, double parentRadius, double xStart, double xEnd)
        {
            Result      result     = Rhino.Commands.Result.Failure;
            XmlElement  compEle    = compNd as XmlElement;
            XmlNodeList subCompNds = compNd.SelectNodes("subcomponents/*");

            double       length          = 0;
            PositionType positionType    = PositionType.Top;
            double       position        = 0;
            double       radialPosition  = 0;
            double       radialDirection = 0;
            double       innerRadius     = 0;
            double       outerRadius     = 0;

            foreach (XmlNode nd in compEle.ChildNodes)
            {
                if (nd.Name == "length")
                {
                    length = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "position")
                {
                    position     = Double.Parse(nd.InnerText);
                    positionType = (PositionType)Enum.Parse(typeof(PositionType), ((XmlElement)nd).GetAttribute("type"), true);
                }
                else if (nd.Name == "radialposition")
                {
                    radialPosition = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "radialdirection")
                {
                    radialDirection = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "outerradius")
                {
                    if (nd.InnerText == "auto")
                    {
                        outerRadius = parentRadius;
                    }
                    else
                    {
                        outerRadius = Double.Parse(nd.InnerText);
                    }
                }
                else if (nd.Name == "innerradius")
                {
                    if (nd.InnerText == "auto")
                    {
                        bool found = false;

                        foreach (XmlNode sibNd in compNd.ParentNode.ChildNodes)
                        {
                            if (sibNd.Name == "innertube")
                            {
                                foreach (XmlNode sibChild in sibNd.ChildNodes)
                                {
                                    if (sibChild.Name == "outerradius")
                                    {
                                        found       = true;
                                        innerRadius = Double.Parse(sibChild.InnerText);
                                    }
                                }
                                if (found)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        innerRadius = Double.Parse(nd.InnerText);
                    }
                }
            }

            Brep brep = null;

            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, length);
            Cylinder outerCyl = new Cylinder(outerCir, length);

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

            Brep[] tube = Brep.CreateBooleanDifference(brepOuter, brepInner, 0.0001);
            brep = tube[0];

            double xLoc = 0;

            switch (positionType)
            {
            case PositionType.Top:
                xLoc = xStart + position;
                break;

            case PositionType.Bottom:
                xLoc = xEnd - length + position;
                break;

            case PositionType.Middle:
                xLoc = (xEnd - xStart) / 2 + position;
                break;

            case PositionType.After:
                xLoc = xEnd - length + position;
                break;

            case PositionType.Absolute:
                xLoc = position;
                break;
            }
            Transform trans = Transform.Translation(new Vector3d(xLoc, 0, 0));

            brep.Transform(trans);

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

            return(result);
        }
        private Result CreateTubeCoupler(RhinoDoc doc, XmlNode compNd, double stackLength)
        {
            Result      result     = Rhino.Commands.Result.Failure;
            XmlElement  compEle    = compNd as XmlElement;
            XmlNodeList subCompNds = compNd.SelectNodes("subcomponents/*");

            double       length          = 0;
            double       thickness       = 0;
            double       position        = 0;
            PositionType positionType    = PositionType.Top;
            double       radialPosition  = 0;
            double       radialDirection = 0;
            double       outerRadius     = 0;

            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 == "position")
                {
                    position     = Double.Parse(nd.InnerText);
                    positionType = (PositionType)Enum.Parse(typeof(PositionType), ((XmlElement)nd).GetAttribute("type"), true);
                }
                else if (nd.Name == "radialposition")
                {
                    radialPosition = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "radialdirection")
                {
                    radialDirection = Double.Parse(nd.InnerText);
                }
                else if (nd.Name == "outerradius")
                {
                    outerRadius = Double.Parse(nd.InnerText);
                }
            }

            Brep brep = null;

            double innerRadius = outerRadius - thickness;

            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, length);
            Cylinder outerCyl = new Cylinder(outerCir, length);

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

            Brep[] tube = Brep.CreateBooleanDifference(brepOuter, brepInner, 0.0001);

            brep = tube[0];
            double xLoc = 0;

            switch (positionType)
            {
            case PositionType.Top:
                xLoc = 0 + length + position;
                break;

            case PositionType.Bottom:
                xLoc = stackLength - length + position;
                break;

            case PositionType.Middle:
                xLoc = stackLength - length / 2 + position;
                break;

            case PositionType.After:
                xLoc = stackLength + length + position;
                break;

            case PositionType.Absolute:
                xLoc = position;
                break;
            }
            Transform trans = Transform.Translation(new Vector3d(xLoc, 0, 0));

            brep.Transform(trans);

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

            foreach (XmlNode subNd in subCompNds)
            {
                if (subNd.Name == "bulkhead")
                {
                    result = CreateBulkhead(doc, subNd, outerRadius, xLoc, xLoc + length);
                }
            }

            return(result);
        }
        private Result CreateBodyTube(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;
            double radius    = 0;

            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 == "radius")
                {
                    if (nd.InnerText == "auto")
                    {
                        bool found = false;

                        foreach (XmlNode sibNd in compNd.ParentNode.ChildNodes)
                        {
                            foreach (XmlNode sibChild in sibNd.ChildNodes)
                            {
                                if (sibChild.Name == "radius" || sibChild.Name == "aftradius")
                                {
                                    found  = true;
                                    radius = Double.Parse(sibChild.InnerText);
                                }
                            }
                            if (found)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        radius = Double.Parse(nd.InnerText);
                    }
                }
            }

            Brep   brep        = null;
            double innerRadius = radius - thickness;
            Plane  planeCyl    = new Plane(new Point3d(0, 0, 0), Vector3d.XAxis);
            Circle innerCir    = new Circle(planeCyl, innerRadius);
            Circle outerCir    = new Circle(planeCyl, radius);

            Cylinder innerCyl = new Cylinder(innerCir, length);
            Cylinder outerCyl = new Cylinder(outerCir, length);

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

            Brep[] tube = Brep.CreateBooleanDifference(brepOuter, brepInner, 0.0001);
            brep = tube[0];

            Transform trans = Transform.Translation(new Vector3d(stackLength, 0, 0));

            brep.Transform(trans);

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

            stackLength += length;

            foreach (XmlNode subNd in subCompNds)
            {
                if (subNd.Name == "bulkhead")
                {
                    result = CreateBulkhead(doc, subNd, radius, stackLength - length, stackLength);
                }
                else if (subNd.Name == "tubecoupler")
                {
                    result = CreateTubeCoupler(doc, subNd, stackLength);
                }
                else if (subNd.Name == "centeringring")
                {
                    result = CreateCenteringRing(doc, subNd, radius, stackLength - length, stackLength);
                }
                else if (subNd.Name == "innertube")
                {
                    result = CreateInnerTube(doc, subNd, stackLength - length, stackLength);
                }
                else if (subNd.Name == "freeformfinset")
                {
                    result = CreateFreeFormFin(doc, subNd, radius, stackLength - length, stackLength);
                }
                else if (subNd.Name == "ellipticalfinset")
                {
                    result = CreateEllipticalFin(doc, subNd, radius, stackLength - length, stackLength);
                }
                else if (subNd.Name == "trapezoidfinset")
                {
                    result = CreateTrapezoidFin(doc, subNd, radius, stackLength - length, stackLength);
                }
            }

            return(result);
        }
        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);
        }