Ejemplo n.º 1
0
 public override bool Draw(RenderingParameters parameters)
 {
     if (parameters is RhinoRenderingParameters && RenderCurve != null)
     {
         RhinoRenderingParameters rParams = (RhinoRenderingParameters)parameters;
         if (RenderCurve is RC.LineCurve && Dotted) //?
         {
             RC.Line line = ((RC.LineCurve)RenderCurve).Line;
             if (Dotted)
             {
                 rParams.Display.DrawDottedLine(line, Material.Diffuse);
             }
             else
             {
                 rParams.Display.DrawLine(line, Material.Diffuse);
             }
         }
         else
         {
             rParams.Display.DrawCurve(RenderCurve, Material.Diffuse, 3);
         }
         double arrS = ToRC.Convert(0.3);
         if (ArrowStart)
         {
             rParams.Display.DrawArrowHead(RenderCurve.PointAtStart - arrS * RenderCurve.TangentAtStart, -RenderCurve.TangentAtStart, Material.Diffuse, 0, arrS);
         }
         if (ArrowEnd)
         {
             rParams.Display.DrawArrowHead(RenderCurve.PointAtEnd + arrS * RenderCurve.TangentAtEnd, RenderCurve.TangentAtEnd, Material.Diffuse, 0, arrS);
         }
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
        public bool MeshLineIntersect(Rhino.Geometry.Mesh x, Rhino.Geometry.Line L) //this is now for reading triangles from Rhino Mesh objects; can be eventually replaced with something, which reads OBJ mesh objects
        {
            //List<Point3d[]> TVs = new List<Point3d[]>();
            //List of Triangle Vertices
            Point3d[] TV = new Point3d[3];
            //Triangle Vertices
            Point3f av = default(Point3f);
            Point3f bv = default(Point3f);
            Point3f cv = default(Point3f);
            Point3f dv = default(Point3f);

            //Each face As MeshFace In x.Faces
            bool does = false;

            for (int k = 0; k <= x.Faces.Count - 1; k++)
            {
                x.Faces.GetFaceVertices(k, out av, out bv, out cv, out dv);
                TV = new Point3d[] { new Point3d(av), new Point3d(bv), new Point3d(cv) };
                //TVs.Add(TV);
                if (TriangleLineIntersect(TV, x.FaceNormals[k], L))
                {
                    does = true;
                }
            }
            return(does);//TVs.Exists(Lambda => TriangleLineIntersect(Lambda, L));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a Windows Media Line from a Rhinocommon Line
        /// </summary>
        /// <param name="input">Rhinocommon Vector3d</param>
        /// <returns>System Windows Vector</returns>
        public static Sh.Path ToPath(this Rg.Line input)
        {
            Sh.Path path = new Sh.Path();

            path.Data = input.ToGeometry();

            return(path);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Replace an existing object in the Rhino document with a line object
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="line"></param>
        /// <returns></returns>
        public static bool ReplaceLine(Guid obj, RC.Line line)
        {
            bool result = false;

            Writing = true;
            result  = RhinoDoc.ActiveDoc.Objects.Replace(obj, line);
            Writing = false;
            return(result);
        }
Ejemplo n.º 5
0
    public static Rhino.Commands.Result ConstrainedCopy(Rhino.RhinoDoc doc)
    {
        // Get a single planar closed curve
        var go = new Rhino.Input.Custom.GetObject();

        go.SetCommandPrompt("Select curve");
        go.GeometryFilter          = Rhino.DocObjects.ObjectType.Curve;
        go.GeometryAttributeFilter = Rhino.Input.Custom.GeometryAttributeFilter.ClosedCurve;
        go.Get();
        if (go.CommandResult() != Rhino.Commands.Result.Success)
        {
            return(go.CommandResult());
        }
        var objref      = go.Object(0);
        var base_curve  = objref.Curve();
        var first_point = objref.SelectionPoint();

        if (base_curve == null || !first_point.IsValid)
        {
            return(Rhino.Commands.Result.Cancel);
        }

        Rhino.Geometry.Plane plane;
        if (!base_curve.TryGetPlane(out plane))
        {
            return(Rhino.Commands.Result.Cancel);
        }

        // Get a point constrained to a line passing through the initial selection
        // point and parallel to the plane's normal
        var gp = new Rhino.Input.Custom.GetPoint();

        gp.SetCommandPrompt("Offset point");
        gp.DrawLineFromPoint(first_point, true);
        var line = new Rhino.Geometry.Line(first_point, first_point + plane.Normal);

        gp.Constrain(line);
        gp.Get();
        if (gp.CommandResult() != Rhino.Commands.Result.Success)
        {
            return(gp.CommandResult());
        }
        var second_point = gp.Point();

        Rhino.Geometry.Vector3d vec = second_point - first_point;
        if (vec.Length > 0.001)
        {
            var  xf = Rhino.Geometry.Transform.Translation(vec);
            Guid id = doc.Objects.Transform(objref, xf, false);
            if (id != Guid.Empty)
            {
                doc.Views.Redraw();
                return(Rhino.Commands.Result.Success);
            }
        }
        return(Rhino.Commands.Result.Cancel);
    }
Ejemplo n.º 6
0
        //Gets all the members in the model and bakes them as breps to the rhino.
        public static Tuple <Member[], Dlubal.RFEM5.CrossSection[]> GetMembers(string comment)
        {
            OpenConnection();
            try
            {
                IModelData rData = RModel.GetModelData();

                Dlubal.RFEM5.Line[]         lines     = rData.GetLines();
                Dlubal.RFEM5.CrossSection[] crossSecs = rData.GetCrossSections();
                Member[] members = rData.GetMembers();
                members = members.Where(o => o.Comment == comment).ToArray();
                List <Member> mList = new List <Member>();

                Dictionary <int, Brep> rCrossSecs = new Dictionary <int, Brep>();
                foreach (Dlubal.RFEM5.CrossSection crossSec in crossSecs)
                {
                    rCrossSecs.Add(crossSec.No, GetCrscDBShape(crossSec.TextID));
                }

                foreach (Member member in members)
                {
                    Dlubal.RFEM5.Line   line      = rData.GetLine(member.LineNo, ItemAt.AtNo).GetData();
                    Rhino.Geometry.Line rhLine    = lineRfemToRhino(line, rData);
                    Vector3d            direction = new Vector3d(rhLine.To - rhLine.From);
                    Plane     plane     = new Plane(rhLine.From, direction);
                    Brep      tempCross = (Brep)rCrossSecs[member.StartCrossSectionNo].Duplicate();
                    Transform tr        = Transform.PlaneToPlane(Plane.WorldXY, plane);
                    tempCross.Transform(tr);

                    Brep extruded = tempCross.Faces[0].CreateExtrusion(rhLine.ToNurbsCurve(), true);
                    ProjectPlugIn.Instance.beamBreps.Add(Tuple.Create(member.No, extruded));
                }

                foreach (Member m in members)
                {
                    Dlubal.RFEM5.CrossSection c = Array.Find(crossSecs, o => o.No == m.StartCrossSectionNo);
                    if (c.TextID.Split(' ')[0] == "Rechteck" || c.TextID.Split(' ')[0] == "Kreis")
                    {
                        mList.Add(m);
                    }
                }
                CloseConnection();
                return(Tuple.Create(mList.ToArray(), crossSecs));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
                //Cleans Garbage collector for releasing all COM interfaces and objects
                System.GC.Collect();
                System.GC.WaitForPendingFinalizers();
                CloseConnection();
            }


            return(Tuple.Create <Member[], Dlubal.RFEM5.CrossSection[]>(null, null));
        }
Ejemplo n.º 7
0
        public bool TriangleLineIntersect(Rhino.Geometry.Point3d[] Vx, Rhino.Geometry.Line L)
        {
            if (Vx.Length != 3)
            {
                throw new Exception("Triangle is not valid!");
            }
            Point3d  O = Vx[0];
            Vector3d U = Vx[1] - O;
            Vector3d V = Vx[2] - O;
            Vector3d N = Vector3d.CrossProduct(U, V);
            //plane normal

            Point3d PS = L.From; //start point of the line
            Point3d PE = L.To;   //end point of the line

            double Nomin = ((O - PS) * N);
            //operator * for dot product
            double Denom = N * (PE - PS);

            // only if the line is not paralell to the plane containing triangle T
            if (Denom != 0)
            {
                double alpha = Nomin / Denom;
                // parameter along the line where it intersects the plane in question, only if not paralell to the plane
                Point3d P = PS + alpha * (PE - PS);
                //L.PointAt(alpha) '

                Vector3d W = P - O;

                double UU = U * U;
                double VV = V * V;
                double UV = U * V;
                double WU = W * U;
                double WV = W * V;

                double STDenom = Math.Pow(UV, 2) - UU * VV;

                double s = (UV * WV - VV * WU) / STDenom;
                double t = (UV * WU - UU * WV) / STDenom;

                Point3d Point = O + s * U + t * V;

                if (s >= 0 & t >= 0 & s + t <= 1)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 8
0
        private static Rhino.Geometry.Line lineRfemToRhino(Dlubal.RFEM5.Line line, IModelData rData)
        {
            Point3D point1 = line.ControlPoints[0];
            Point3D point2 = line.ControlPoints[line.ControlPoints.Length - 1];


            Rhino.Geometry.Line rhLine = new Rhino.Geometry.Line(new Point3d(point1.X, point1.Y, point1.Z),
                                                                 new Point3d(point2.X, point2.Y, point2.Z));
            return(rhLine);
        }
Ejemplo n.º 9
0
        /***************************************************/

        public static bool IsEqual(this BHG.Line bhLine, RHG.Line rhLine, double tolerance = BHG.Tolerance.Distance)
        {
            if (bhLine == null & rhLine == default(RHG.Line))
            {
                return(true);
            }

            return(bhLine.Start.IsEqual(rhLine.PointAt(0), tolerance) &&
                   bhLine.End.IsEqual(rhLine.PointAt(1), tolerance));
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Convert a Rhino line to a Nucleus one
 /// </summary>
 /// <param name="line">The line to be converted</param>
 /// <returns>A new line if the input is valid, else null</returns>
 public static Line Convert(RC.Line line)
 {
     if (line.IsValid)
     {
         return(new Line(Convert(line.From), Convert(line.To)));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 11
0
 // Casting to GH Data Types
 public bool ConvertToGH_Line <T>(ref T target)
 {
     if ((NodeCount == 2) && (Type == LineType.PolylineType))
     {
         Rhino.Geometry.Line pline = new Rhino.Geometry.Line(ControlPoints[0], ControlPoints[1]);
         object obj = new GH_Line(pline);
         target = (T)obj;
         return(true);
     }
     return(false);
 }
Ejemplo n.º 12
0
        //Create Bottom and Top Chord Pts
        public static List <List <Point3d> > TrussPtsFromLines(Curve PCCurve, Curve SCCurve, int Segments, int ForceVert)
        {
            List <List <Point3d> > ChordPts = new List <List <Point3d> >();

            //Check if truss is planar



            //define Primary and Secondary cord points
            List <Point3d> PCPts = new List <Point3d>();
            List <Point3d> SCPts = new List <Point3d>();

            //Create PC Points
            Point3d[] PCPtArray = new Point3d[] { };
            var       t         = PCCurve.DivideByCount(Segments, true, out PCPtArray);

            PCPts = PCPtArray.ToList();

            //splits boths curves evenly and joins lines
            if (ForceVert == 0)
            {
                Point3d[] SCPtArray = new Point3d[] { };
                var       breaks    = SCCurve.DivideByCount(Segments, true, out SCPtArray);
                SCPts = SCPtArray.ToList();
            }

            //Creates points on SC curve based on verticals to a directions z,x,y defualt to y for now.
            else if (ForceVert == 1)
            {
                foreach (Point3d pt in PCPts)
                {
                    //create temp line for intersection. <<<This needs improvement>>>!!!
                    Point3d tempPT = pt;
                    tempPT.Y = pt.Y + 5;
                    Line tempLine = new Rhino.Geometry.Line(pt, tempPT);
                    //intersection event
                    var tempevent = Rhino.Geometry.Intersect.Intersection.CurveCurve(SCCurve, tempLine.ToNurbsCurve(), 0.001, 0.001);
                    SCPts.Add(tempevent[0].PointA);
                }
            }

            //splits PC curves evenly and produces verticals perpendicular to the line
            //else if(ForceVert == 2)
            //{}

            ChordPts.Add(PCPts);
            ChordPts.Add(SCPts);

            return(ChordPts);
        }
Ejemplo n.º 13
0
        public static string ToSVG(this Hp.Geometry input)
        {
            string path = string.Empty;

            switch (input.CurveType)
            {
            case Geometry.CurveTypes.Arc:
                Rg.Arc arc = new Rg.Arc();
                input.Curve.TryGetArc(out arc);
                path = arc.ToSVG();
                break;

            case Geometry.CurveTypes.Circle:
                Rg.Circle circle = new Rg.Circle();
                input.Curve.TryGetCircle(out circle);
                path = circle.ToSVG();
                break;

            case Geometry.CurveTypes.Ellipse:
                Rg.Ellipse ellipse = new Rg.Ellipse();
                input.Curve.TryGetEllipse(out ellipse);
                path = ellipse.ToSVG();
                break;

            case Geometry.CurveTypes.Line:
                Rg.Line line = new Rg.Line(input.Curve.PointAtStart, input.Curve.PointAtEnd);
                path = line.ToSVG();
                break;

            case Geometry.CurveTypes.Polyline:
                Rg.Polyline polyline = new Rg.Polyline();
                input.Curve.TryGetPolyline(out polyline);
                path = polyline.ToSVG();
                break;

            case Geometry.CurveTypes.Rectangle:
                Rg.Polyline pline = new Rg.Polyline();
                input.Curve.TryGetPolyline(out pline);
                path = pline.ToSVG();
                break;

            default:
                path = input.Curve.ToSVG();
                break;
            }

            return(path);
        }
Ejemplo n.º 14
0
        public static Sm.Geometry ToGeometry(this Geometry input)
        {
            Sm.Geometry geometry = null;
            switch (input.CurveType)
            {
            case Geometry.CurveTypes.Arc:
                Rg.Arc arc = new Rg.Arc();
                input.Curve.TryGetArc(out arc);
                geometry = arc.ToGeometry();
                break;

            case Geometry.CurveTypes.Circle:
                Rg.Circle circle = new Rg.Circle();
                input.Curve.TryGetCircle(out circle);
                geometry = circle.ToGeometry();
                break;

            case Geometry.CurveTypes.Ellipse:
                Rg.Ellipse ellipse = new Rg.Ellipse();
                input.Curve.TryGetEllipse(out ellipse);
                geometry = ellipse.ToGeometry();
                break;

            case Geometry.CurveTypes.Line:
                Rg.Line line = new Rg.Line(input.Curve.PointAtStart, input.Curve.PointAtEnd);
                geometry = line.ToGeometry();
                break;

            case Geometry.CurveTypes.Polyline:
                Rg.Polyline polyline = new Rg.Polyline();
                input.Curve.TryGetPolyline(out polyline);
                geometry = polyline.ToGeometry();
                break;

            case Geometry.CurveTypes.Rectangle:
                Rg.Polyline pline = new Rg.Polyline();
                input.Curve.TryGetPolyline(out pline);
                Rg.Rectangle3d rectangle = new Rg.Rectangle3d(Rg.Plane.WorldXY, pline[0], pline[2]);
                geometry = rectangle.ToGeometry();
                break;

            default:
                geometry = input.Curve.ToGeometry();
                break;
            }
            return(geometry);
        }
    public static Rhino.Commands.Result LineBetweenCurves(Rhino.RhinoDoc doc)
    {
        Rhino.Input.Custom.GetObject go = new Rhino.Input.Custom.GetObject();
        go.SetCommandPrompt("Select two curves");
        go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve;
        go.GetMultiple(2, 2);
        if (go.CommandResult() != Rhino.Commands.Result.Success)
        {
            return(go.CommandResult());
        }

        Rhino.DocObjects.ObjRef objRef0 = go.Object(0);
        Rhino.DocObjects.ObjRef objRef1 = go.Object(1);

        double t0 = Rhino.RhinoMath.UnsetValue;
        double t1 = Rhino.RhinoMath.UnsetValue;

        Rhino.Geometry.Curve curve0 = objRef0.CurveParameter(out t0);
        Rhino.Geometry.Curve curve1 = objRef1.CurveParameter(out t1);
        if (null == curve0 || !Rhino.RhinoMath.IsValidDouble(t0) ||
            null == curve1 || !Rhino.RhinoMath.IsValidDouble(t1))
        {
            return(Rhino.Commands.Result.Failure);
        }

        Rhino.Geometry.Line line = Rhino.Geometry.Line.Unset;
        bool rc = Rhino.Geometry.Line.TryCreateBetweenCurves(curve0, curve1, ref t0, ref t1, false, false, out line);

        if (rc)
        {
            if (Guid.Empty != doc.Objects.AddLine(line))
            {
                doc.Views.Redraw();
                return(Rhino.Commands.Result.Success);
            }
        }
        return(Rhino.Commands.Result.Failure);
    }
Ejemplo n.º 16
0
        public static Edge ToTopologic(this Rhino.Geometry.Line line)
        {
            if (line == null)
            {
                return(null);
            }

            Vertex vertex_1 = line.From.ToTopologic();

            if (vertex_1 == null)
            {
                return(null);
            }

            Vertex vertex_2 = line.To.ToTopologic();

            if (vertex_2 == null)
            {
                return(null);
            }

            return(Edge.ByStartVertexEndVertex(vertex_1, vertex_2));
        }
Ejemplo n.º 17
0
        private void SunSliderController(DrawEventArgs e)
        {
            Rectangle bounds = e.Viewport.Bounds;

            if (this._dateParam != 0.0)
            {
                Point3d[] point3dArray = new Point3d[3];
                for (int index = 0; index < 3; ++index)
                {
                    double              num1  = (double)(bounds.Width / 2) + Math.Sin(Math.PI - Math.PI / 2.0 * (double)index) * (double)this._dateSlideRadius;
                    double              num2  = (double)(bounds.Height / 2) + Math.Cos(Math.PI - Math.PI / 2.0 * (double)index) * (double)this._dateSlideRadius;
                    Point3d             local = point3dArray[index];
                    Rhino.Geometry.Line world = e.Viewport.ClientToWorld(new System.Drawing.Point((int)num1, (int)num2));
                    Point3d             from  = world.From;
                    local = from;
                }
                e.Display.DrawArc(new Arc(new Circle(point3dArray[0], point3dArray[1], point3dArray[2]), 2.0 * Math.PI * this._dateParam), this._dateArcColor, 10);
            }
            if (this._timeParam != 0.0)
            {
                Point3d[] point3dArray = new Point3d[3];
                for (int index = 0; index < 3; ++index)
                {
                    double  num1  = (double)(bounds.Width / 2) + Math.Sin(Math.PI - Math.PI / 2.0 * (double)index) * (double)this._timeSlideRadious;
                    double  num2  = (double)(bounds.Height / 2) + Math.Cos(Math.PI - Math.PI / 2.0 * (double)index) * (double)this._timeSlideRadious;
                    Point3d local = point3dArray[index];
                    Line    world = e.Viewport.ClientToWorld(new System.Drawing.Point((int)num1, (int)num2));
                    Point3d from  = world.From;
                    local = from;
                }
                e.Display.DrawArc(new Arc(new Circle(point3dArray[0], point3dArray[1], point3dArray[2]), 2.0 * Math.PI * this._timeParam), this._timeArcColor, 10);
            }
            e.Display.Draw2dText(this._datetime.ToString("m"), Color.White, new Point2d((double)(bounds.Width / 2), (double)(bounds.Height / 2 - 13)), true, 20);
            e.Display.Draw2dText(this._datetime.ToString("t"), Color.White, new Point2d((double)(bounds.Width / 2), (double)(bounds.Height / 2 + 13)), true, 20);
            e.Display.Draw2dText("Date", this._dateArcColor, new Point2d((double)(bounds.Width / 2), (double)(bounds.Height / 2 - this._dateSlideRadius + 20)), true, 10);
            e.Display.Draw2dText("Time", this._timeArcColor, new Point2d((double)(bounds.Width / 2), (double)(bounds.Height / 2 - this._timeSlideRadious - 20)), true, 10);
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Gets the world coordinate line in the view frustum
 /// that projects to a point on the screen.
 /// </summary>
 /// <param name="screenX">(screenx,screeny) = screen location.</param>
 /// <param name="screenY">(screenx,screeny) = screen location.</param>
 /// <returns>3d world coordinate line segment starting on the near clipping plane and ending on the far clipping plane.</returns>
 public Rhino.Geometry.Line GetFrustumLine( double screenX, double screenY)
 {
   Rhino.Geometry.Line line = new Rhino.Geometry.Line();
   IntPtr pConstThis = ConstPointer();
   if (!UnsafeNativeMethods.ON_Viewport_GetFrustumLine(pConstThis, screenX, screenY, ref line))
     line = Rhino.Geometry.Line.Unset;
   return line;
 }
        public void SetInputs(List <DataTree <ResthopperObject> > values)
        {
            foreach (var tree in values)
            {
                if (!_input.TryGetValue(tree.ParamName, out var inputGroup))
                {
                    continue;
                }

                if (inputGroup.AlreadySet(tree))
                {
                    Console.WriteLine("Skipping input tree... same input");
                    continue;
                }

                inputGroup.CacheTree(tree);
                inputGroup.Param.VolatileData.Clear();
                inputGroup.Param.ExpireSolution(true);

                if (inputGroup.Param is Param_Point)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject       restobj = entree.Value[i];
                            Rhino.Geometry.Point3d rPt     = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data);
                            GH_Point data = new GH_Point(rPt);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Vector)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject        restobj  = entree.Value[i];
                            Rhino.Geometry.Vector3d rhVector = JsonConvert.DeserializeObject <Rhino.Geometry.Vector3d>(restobj.Data);
                            GH_Vector data = new GH_Vector(rhVector);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Integer)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            int        rhinoInt      = JsonConvert.DeserializeObject <int>(restobj.Data);
                            GH_Integer data          = new GH_Integer(rhinoInt);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Number)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                            GH_Number        data     = new GH_Number(rhNumber);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_String)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            string           rhString = restobj.Data;
                            GH_String        data     = new GH_String(rhString);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Line)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Line rhLine  = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data);
                            GH_Line             data    = new GH_Line(rhLine);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Curve)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            GH_Curve         ghCurve;
                            try
                            {
                                Rhino.Geometry.Polyline data = JsonConvert.DeserializeObject <Rhino.Geometry.Polyline>(restobj.Data);
                                Rhino.Geometry.Curve    c    = new Rhino.Geometry.PolylineCurve(data);
                                ghCurve = new GH_Curve(c);
                            }
                            catch
                            {
                                var dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(restobj.Data);
                                var c    = (Rhino.Geometry.Curve)Rhino.Runtime.CommonObject.FromJSON(dict);
                                ghCurve = new GH_Curve(c);
                            }
                            inputGroup.Param.AddVolatileData(path, i, ghCurve);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Circle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject      restobj  = entree.Value[i];
                            Rhino.Geometry.Circle rhCircle = JsonConvert.DeserializeObject <Rhino.Geometry.Circle>(restobj.Data);
                            GH_Circle             data     = new GH_Circle(rhCircle);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Plane)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject     restobj = entree.Value[i];
                            Rhino.Geometry.Plane rhPlane = JsonConvert.DeserializeObject <Rhino.Geometry.Plane>(restobj.Data);
                            GH_Plane             data    = new GH_Plane(rhPlane);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Rectangle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject           restobj     = entree.Value[i];
                            Rhino.Geometry.Rectangle3d rhRectangle = JsonConvert.DeserializeObject <Rhino.Geometry.Rectangle3d>(restobj.Data);
                            GH_Rectangle data = new GH_Rectangle(rhRectangle);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Box)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject   restobj = entree.Value[i];
                            Rhino.Geometry.Box rhBox   = JsonConvert.DeserializeObject <Rhino.Geometry.Box>(restobj.Data);
                            GH_Box             data    = new GH_Box(rhBox);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Surface)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject       restobj   = entree.Value[i];
                            Rhino.Geometry.Surface rhSurface = JsonConvert.DeserializeObject <Rhino.Geometry.Surface>(restobj.Data);
                            GH_Surface             data      = new GH_Surface(rhSurface);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Brep)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Brep rhBrep  = JsonConvert.DeserializeObject <Rhino.Geometry.Brep>(restobj.Data);
                            GH_Brep             data    = new GH_Brep(rhBrep);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Mesh)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Mesh rhMesh  = JsonConvert.DeserializeObject <Rhino.Geometry.Mesh>(restobj.Data);
                            GH_Mesh             data    = new GH_Mesh(rhMesh);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is GH_NumberSlider)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                            GH_Number        data     = new GH_Number(rhNumber);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Boolean || inputGroup.Param is GH_BooleanToggle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            bool             boolean = JsonConvert.DeserializeObject <bool>(restobj.Data);
                            GH_Boolean       data    = new GH_Boolean(boolean);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is GH_Panel)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            string           rhString = JsonConvert.DeserializeObject <string>(restobj.Data);
                            GH_String        data     = new GH_String(rhString);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }
            }
        }
Ejemplo n.º 20
0
 public static string ToSVG(this Rg.Line input)
 {
     return("M " + input.From.ToSVG() + input.To.ToSVG() + " ");
 }
Ejemplo n.º 21
0
 public static Line ToLine(this rg.Line line)
 {
     return(new Line(line.From.ToVector3(), line.To.ToVector3()));
 }
Ejemplo n.º 22
0
        /***************************************************/

        public static void RenderRhinoWires(RHG.Line line, Rhino.Display.DisplayPipeline pipeline, Color bhColour, int thickness)
        {
            pipeline.DrawLine(line, bhColour, thickness);
        }
Ejemplo n.º 23
0
 private Topologic.Edge ByLine(Rhino.Geometry.Line ghLine)
 {
     Topologic.Vertex vertex1 = ByPoint(ghLine.From);
     Topologic.Vertex vertex2 = ByPoint(ghLine.To);
     return(Topologic.Edge.ByStartVertexEndVertex(vertex1, vertex2));
 }
Ejemplo n.º 24
0
 //RHINO TO REVIT
 public Line RhinoToRevitLine(rg.Line line)
 {
     return(Line.CreateBound(RhinoToRevitPoint(line.From), RhinoToRevitPoint(line.To)));
 }
Ejemplo n.º 25
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;

            double scale = 304.8;

            #region TEST

            ////cut line endpoints
            //XYZ v1 = new XYZ(0, 2000, 0)/scale;
            //XYZ v2 = new XYZ(6018, 2000, 0) / scale;


            ////rhino cut line endpoints
            //         rg.Point3d rv0 = new rg.Point3d(0, 2000, 0);
            //         rg.Point3d rv1 = new rg.Point3d(6018, 2000, 0);
            //rg.Point3d rv2 = new rg.Point3d(0, 2000, 1);

            ////rhino cut plane
            //rg.Plane cutPlane = new rg.Plane(rv0, rv1, rv2);



            ////wip points
            //rg.Point3d rv3 = new rg.Point3d(857, 203, 4597);
            //rg.Point3d rv4 = new rg.Point3d(857, 6221, 4597);
            //rg.Point3d rv5 = new rg.Point3d(9818, 6221, 4597);


            //         //wip plane
            //         rg.Plane rhinoPlane = new rg.Plane(rv3, rv4, rv5);

            //         rg.Line intLine;

            //         var result = rg.Intersect.Intersection.PlanePlane(cutPlane, rhinoPlane, out intLine);

            //         using (Transaction t = new Transaction(doc, "Project Line"))
            //         {
            //             t.Start();

            //             SketchPlane sp = SketchPlane.Create(doc, RhinoToRevitPlane(cutPlane));

            //             doc.Create.NewModelCurve(RhinoToRevitLine(intLine), sp);

            //             t.Commit();
            //         }

            //         TaskDialog.Show("r", "Done");

            #endregion


            #region Revit

            //	SELECT THE LINE TO PROJECT AND CONSTRUCT THE PLANE TO INTERSECT THE MESH
            Reference lineToProject = uidoc.Selection.PickObject(ObjectType.Element, "Select cut Line");
            Element   lineElement   = doc.GetElement(lineToProject);

            CurveElement  curveLine    = lineElement as CurveElement;
            LocationCurve lineLocation = lineElement.Location as LocationCurve;

            Line line = lineLocation.Curve as Line;

            rg.Line rhinoCutLine = new rg.Line(RevitToRhinoPoint(line.GetEndPoint(0)), RevitToRhinoPoint(line.GetEndPoint(1)));

            XYZ perpDir = line.Direction.CrossProduct(XYZ.BasisZ).Normalize();

            Plane cutPlane = Plane.CreateByNormalAndOrigin(perpDir, line.GetEndPoint(0));

            // GET THE MESH GEOMETRY

            Reference meshRef = uidoc.Selection.PickObject(ObjectType.Element, "Select Mesh");
            Element   e       = doc.GetElement(meshRef);

            Options opt = new Options();
            opt.ComputeReferences        = true;
            opt.IncludeNonVisibleObjects = false;
            opt.View = doc.ActiveView;

            GeometryElement geomElem = e.get_Geometry(opt);

            Mesh geomMesh = null;

            foreach (GeometryObject geomObj in geomElem)
            {
                GeometryInstance gi = geomObj as GeometryInstance;

                if (gi != null)
                {
                    foreach (GeometryObject element in gi.GetInstanceGeometry())
                    {
                        geomMesh = element as Mesh;
                    }
                }
                else
                {
                    geomMesh = geomObj as Mesh;
                }
            }


            int counter = 0;

            using (Transaction t = new Transaction(doc, "Project Line"))
            {
                t.Start();
                SketchPlane sp = SketchPlane.Create(doc, cutPlane);

                XYZ spOrigin = sp.GetPlane().Origin;
                Debug.Write($"Sketch plane origin: {spOrigin}\n");

                XYZ spNormal = sp.GetPlane().Normal;
                Debug.Write($"Sketch plane normal: {spNormal}\n");

                Line spX = Line.CreateBound(spOrigin, spOrigin + XYZ.BasisZ * 100);
                Debug.Write($"line start: {spX.GetEndParameter(0)}\n");
                Debug.Write($"line end: {spX.GetEndParameter(1)}\n");

                ModelCurve mc = doc.Create.NewModelCurve(spX, sp);
                Debug.Write($"mc: {mc.Id}\n");

                List <XYZ> intersectionPoints = new List <XYZ>();

                for (int i = 0; i < geomMesh.NumTriangles; i++)
                {
                    MeshTriangle triangle = geomMesh.get_Triangle(i);
                    XYZ          vertex1  = triangle.get_Vertex(0);
                    XYZ          vertex2  = triangle.get_Vertex(1);
                    XYZ          vertex3  = triangle.get_Vertex(2);

                    rg.Point3d rhinoVertex1 = RevitToRhinoPoint(vertex1);
                    rg.Point3d rhinoVertex2 = RevitToRhinoPoint(vertex2);
                    rg.Point3d rhinoVertex3 = RevitToRhinoPoint(vertex3);

                    List <rg.Line> rhinoEdges = new List <rg.Line> {
                        new rg.Line(rhinoVertex1, rhinoVertex2), new rg.Line(rhinoVertex1, rhinoVertex2), new rg.Line(rhinoVertex1, rhinoVertex2)
                    };

                    XYZ[] pts = new XYZ[2];

                    int ptscount = 0;

                    foreach (rg.Line edge in rhinoEdges)
                    {
                        double a = 0;
                        double b = 0;

                        bool result = rg.Intersect.Intersection.LineLine(edge, rhinoCutLine, out a, out b);

                        if (result)
                        {
                            intersectionPoints.Add(RhinoToRevitPoint(edge.PointAt(a)));
                            pts[ptscount] = RhinoToRevitPoint(edge.PointAt(a));
                            ptscount      = 1;
                            counter++;
                        }
                    }

                    if (pts[0] != null)
                    {
                        try
                        {
                            Line temp = Line.CreateBound(pts[0], pts[0] + XYZ.BasisZ * 100);
                            //Line li = Line.CreateBound(pts[0], new XYZ(pts[0].X, pts[0].Y, pts[0].Z +100));
                            doc.Create.NewModelCurve(temp, sp);
                        }
                        catch { }
                    }
                }


                t.Commit();
            }
            TaskDialog.Show("Intersections found", counter.ToString());

            #endregion

            return(Result.Succeeded);
        }
Ejemplo n.º 26
0
 static public Autodesk.Revit.DB.Line ToHost(this Rhino.Geometry.Line line)
 {
     return(Autodesk.Revit.DB.Line.CreateBound(line.From.ToHost(), line.To.ToHost()));
 }
Ejemplo n.º 27
0
 static internal Rhino.Geometry.Line Scale(this Rhino.Geometry.Line l, double factor)
 {
     return(new Rhino.Geometry.Line(l.From.Scale(factor), l.To.Scale(factor)));
 }
Ejemplo n.º 28
0
        public void SetInputs(List <DataTree <ResthopperObject> > values)
        {
            foreach (var tree in values)
            {
                if (!_input.TryGetValue(tree.ParamName, out var inputGroup))
                {
                    continue;
                }

                if (inputGroup.AlreadySet(tree))
                {
                    LogDebug("Skipping input tree... same input");
                    continue;
                }

                inputGroup.CacheTree(tree);

                IGH_ContextualParameter contextualParameter = inputGroup.Param as IGH_ContextualParameter;
                if (contextualParameter != null)
                {
                    switch (ParamTypeName(inputGroup.Param))
                    {
                    case "Number":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            double[] doubles = new double[entree.Value.Count];
                            for (int i = 0; i < doubles.Length; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                doubles[i] = JsonConvert.DeserializeObject <double>(restobj.Data);
                            }
                            contextualParameter.AssignContextualData(doubles);
                            break;
                        }
                    }
                    break;

                    case "Integer":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            int[] integers = new int[entree.Value.Count];
                            for (int i = 0; i < integers.Length; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                integers[i] = JsonConvert.DeserializeObject <int>(restobj.Data);
                            }
                            contextualParameter.AssignContextualData(integers);
                            break;
                        }
                    }
                    break;

                    case "Point":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            Point3d[] points = new Point3d[entree.Value.Count];
                            for (int i = 0; i < entree.Value.Count; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                points[i] = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data);
                            }
                            contextualParameter.AssignContextualData(points);
                            break;
                        }
                    }
                    break;

                    case "Line":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            Line[] lines = new Line[entree.Value.Count];
                            for (int i = 0; i < entree.Value.Count; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                lines[i] = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data);
                            }
                            contextualParameter.AssignContextualData(lines);
                            break;
                        }
                    }
                    break;

                    case "Text":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            string[] strings = new string[entree.Value.Count];
                            for (int i = 0; i < entree.Value.Count; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                strings[i] = restobj.Data.Trim(new char[] { '"' });
                            }
                            contextualParameter.AssignContextualData(strings);
                            break;
                        }
                    }
                    break;

                    case "Geometry":
                    {
                        foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                        {
                            GeometryBase[] geometries = new GeometryBase[entree.Value.Count];
                            for (int i = 0; i < entree.Value.Count; i++)
                            {
                                ResthopperObject restobj = entree.Value[i];
                                var dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(restobj.Data);
                                geometries[i] = Rhino.Runtime.CommonObject.FromJSON(dict) as GeometryBase;
                            }
                            contextualParameter.AssignContextualData(geometries);
                            break;
                        }
                    }
                    break;
                    }
                    continue;
                }

                inputGroup.Param.VolatileData.Clear();
                inputGroup.Param.ExpireSolution(false); // mark param as expired but don't recompute just yet!

                if (inputGroup.Param is Param_Point)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject       restobj = entree.Value[i];
                            Rhino.Geometry.Point3d rPt     = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data);
                            GH_Point data = new GH_Point(rPt);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Vector)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject        restobj  = entree.Value[i];
                            Rhino.Geometry.Vector3d rhVector = JsonConvert.DeserializeObject <Rhino.Geometry.Vector3d>(restobj.Data);
                            GH_Vector data = new GH_Vector(rhVector);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Integer)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            int        rhinoInt      = JsonConvert.DeserializeObject <int>(restobj.Data);
                            GH_Integer data          = new GH_Integer(rhinoInt);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Number)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                            GH_Number        data     = new GH_Number(rhNumber);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_String)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            string           rhString = restobj.Data;
                            GH_String        data     = new GH_String(rhString);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Line)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Line rhLine  = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data);
                            GH_Line             data    = new GH_Line(rhLine);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Curve)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            GH_Curve         ghCurve;
                            try
                            {
                                Rhino.Geometry.Polyline data = JsonConvert.DeserializeObject <Rhino.Geometry.Polyline>(restobj.Data);
                                Rhino.Geometry.Curve    c    = new Rhino.Geometry.PolylineCurve(data);
                                ghCurve = new GH_Curve(c);
                            }
                            catch
                            {
                                var dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(restobj.Data);
                                var c    = (Rhino.Geometry.Curve)Rhino.Runtime.CommonObject.FromJSON(dict);
                                ghCurve = new GH_Curve(c);
                            }
                            inputGroup.Param.AddVolatileData(path, i, ghCurve);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Circle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject      restobj  = entree.Value[i];
                            Rhino.Geometry.Circle rhCircle = JsonConvert.DeserializeObject <Rhino.Geometry.Circle>(restobj.Data);
                            GH_Circle             data     = new GH_Circle(rhCircle);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Plane)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject     restobj = entree.Value[i];
                            Rhino.Geometry.Plane rhPlane = JsonConvert.DeserializeObject <Rhino.Geometry.Plane>(restobj.Data);
                            GH_Plane             data    = new GH_Plane(rhPlane);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Rectangle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject           restobj     = entree.Value[i];
                            Rhino.Geometry.Rectangle3d rhRectangle = JsonConvert.DeserializeObject <Rhino.Geometry.Rectangle3d>(restobj.Data);
                            GH_Rectangle data = new GH_Rectangle(rhRectangle);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Box)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject   restobj = entree.Value[i];
                            Rhino.Geometry.Box rhBox   = JsonConvert.DeserializeObject <Rhino.Geometry.Box>(restobj.Data);
                            GH_Box             data    = new GH_Box(rhBox);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Surface)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject       restobj   = entree.Value[i];
                            Rhino.Geometry.Surface rhSurface = JsonConvert.DeserializeObject <Rhino.Geometry.Surface>(restobj.Data);
                            GH_Surface             data      = new GH_Surface(rhSurface);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Brep)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Brep rhBrep  = JsonConvert.DeserializeObject <Rhino.Geometry.Brep>(restobj.Data);
                            GH_Brep             data    = new GH_Brep(rhBrep);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Mesh)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject    restobj = entree.Value[i];
                            Rhino.Geometry.Mesh rhMesh  = JsonConvert.DeserializeObject <Rhino.Geometry.Mesh>(restobj.Data);
                            GH_Mesh             data    = new GH_Mesh(rhMesh);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is GH_NumberSlider)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                            GH_Number        data     = new GH_Number(rhNumber);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is Param_Boolean || inputGroup.Param is GH_BooleanToggle)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj = entree.Value[i];
                            bool             boolean = JsonConvert.DeserializeObject <bool>(restobj.Data);
                            GH_Boolean       data    = new GH_Boolean(boolean);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }

                if (inputGroup.Param is GH_Panel)
                {
                    foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                    {
                        GH_Path path = new GH_Path(GhPath.FromString(entree.Key));
                        for (int i = 0; i < entree.Value.Count; i++)
                        {
                            ResthopperObject restobj  = entree.Value[i];
                            string           rhString = JsonConvert.DeserializeObject <string>(restobj.Data);
                            GH_String        data     = new GH_String(rhString);
                            inputGroup.Param.AddVolatileData(path, i, data);
                        }
                    }
                    continue;
                }
            }
        }
Ejemplo n.º 29
0
        private UI.FrixelReferenceData Regenerate()
        {
            MassingStructure massing = GenerateMassingStructure();

            if (massing == null)
            {
                return(null);
            }

            var Nodes = massing.Nodes;

            // Crawl across the point array and crete pixels
            var          nodeList  = Nodes.Values.ToList();
            List <Pixel> pixelList = new List <Pixel>();

            for (int x = 0; x < massing.xBayCount; x++)
            {
                for (int y = 0; y < massing.yBayCount; y++)
                {
                    if (!Nodes.ContainsKey(new Tuple <int, int>(x, y)))
                    {
                        continue;
                    }
                    var currentPoint = Nodes[new Tuple <int, int>(x, y)];
                    var topLeft      = new Tuple <int, int>(x, y + 1);
                    var topRight     = new Tuple <int, int>(x + 1, y + 1);
                    var botLeft      = new Tuple <int, int>(x, y);
                    var botRight     = new Tuple <int, int>(x + 1, y);
                    Core.Geometry.Point2d topLeftPt;
                    Core.Geometry.Point2d topRightPt;
                    Core.Geometry.Point2d botLeftPt;
                    Core.Geometry.Point2d botRightPt;

                    if (
                        Nodes.TryGetValue(botLeft, out botLeftPt) &&
                        Nodes.TryGetValue(topLeft, out topLeftPt) &&
                        Nodes.TryGetValue(topRight, out topRightPt) &&
                        Nodes.TryGetValue(botRight, out botRightPt) &&
                        Utilities.AtLeastXInside(topLeftPt.IsInside, topRightPt.IsInside, botLeftPt.IsInside, botRightPt.IsInside, 2)
                        )
                    {
                        pixelList.Add(new Pixel(
                                          nodeList.IndexOf(topLeftPt),
                                          nodeList.IndexOf(topRightPt),
                                          nodeList.IndexOf(botLeftPt),
                                          nodeList.IndexOf(botRightPt),
                                          PixelState.None
                                          ));
                        topLeftPt.IsPixeled  = true;
                        topRightPt.IsPixeled = true;
                        botLeftPt.IsPixeled  = true;
                        botRightPt.IsPixeled = true;
                    }
                }
            }

            // Remove unpixeled nodes and update nodeList
            var nodeListCopy = nodeList.Where(n => n.IsPixeled).ToList();

            foreach (var p in pixelList)
            {
                p.UpdateTopology(
                    nodeListCopy.IndexOf(nodeList[p.TopLeft]),
                    nodeListCopy.IndexOf(nodeList[p.TopRight]),
                    nodeListCopy.IndexOf(nodeList[p.BottomLeft]),
                    nodeListCopy.IndexOf(nodeList[p.BottomRight])
                    );
            }
            nodeList = nodeListCopy;

            // Find the closest node to the spine line
            Tuple <double, int> closestPoint = null;
            var crv = new Rhino.Geometry.Line(_spine.Item1, _spine.Item2).ToNurbsCurve();
            int spi = 0;

            foreach (var p in nodeList.Select(n => new Point3d(n.X, n.Y, 0)))
            {
                double dist = double.PositiveInfinity;
                double t    = 0;
                crv.ClosestPoint(p, out t, 100);
                var crvPt = crv.PointAt(t);
                dist = crvPt.DistanceTo(p);
                // First cycle
                if (closestPoint == null)
                {
                    closestPoint = new Tuple <double, int>(dist, spi); spi++; continue;
                }

                // If its closer
                if (!double.IsInfinity(dist) && dist < closestPoint.Item1)
                {
                    closestPoint = new Tuple <double, int>(dist, spi);
                }
                spi++;
            }
            var closestNode = nodeList[closestPoint.Item2];
            var coord       = Nodes.Where(x => x.Value == closestNode).First().Key;

            // Find orientation of spine curve, vert or horz, to create spine
            bool xDir = Math.Abs(_spine.Item1.X - _spine.Item2.X) > Math.Abs(_spine.Item1.Y - _spine.Item2.Y);

            if (xDir)
            {
                var horzPts = Nodes.Where(x => x.Key.Item2 == coord.Item2).ToList().Select(x => x.Value).ToList();
                List <Core.Geometry.Point2d> newPts = new List <Core.Geometry.Point2d>();
                foreach (var pt in horzPts)
                {
                    var myCoord = nodeList.IndexOf(pt);
                    foreach (var p in pixelList.Where(p => p.ContainsNode(myCoord, true)).ToList())
                    {
                        p.ChangeStateTo(PixelState.Moment);
                        p.LockedBrace = true;
                        newPts.Add(nodeList[p.BottomLeft]);
                        newPts.Add(nodeList[p.TopLeft]);
                    }
                }
                horzPts.AddRange(newPts);
                // Lock support points (LEFT)
                var horzPtsMin = horzPts.Select(p => p.X).Min();
                nodeList.Where(p => p.X.IsCloseTo(horzPtsMin)).ToList().ForEach(p => p.IsLocked = true);
            }
            else
            {
                var vertPts = Nodes.Where(x => x.Key.Item1 == coord.Item1).ToList().Select(x => x.Value).ToList();
                List <Core.Geometry.Point2d> newPts = new List <Core.Geometry.Point2d>();
                foreach (var pt in vertPts)
                {
                    var myCoord = nodeList.IndexOf(pt);
                    foreach (var p in pixelList.Where(p => p.ContainsNode(myCoord, true)).ToList())
                    {
                        p.ChangeStateTo(PixelState.Moment);
                        newPts.Add(nodeList[p.BottomLeft]);
                        newPts.Add(nodeList[p.BottomRight]);
                    }
                }
                vertPts.AddRange(newPts);
                // Lock support points (BOTTOM)
                var vertPtsMin = vertPts.Select(p => p.Y).Min();
                vertPts.Where(p => p.Y.IsCloseTo(vertPtsMin)).ToList().ForEach(p => p.IsLocked = true);
                pixelList.ForEach(p =>
                {
                    if (nodeList[p.BottomLeft].Y.IsCloseTo(vertPtsMin))
                    {
                        nodeList[p.BottomLeft].IsLocked = true;
                    }
                    if (nodeList[p.BottomRight].Y.IsCloseTo(vertPtsMin))
                    {
                        nodeList[p.BottomRight].IsLocked = true;
                    }
                });
            }

            foreach (var p in pixelList.Where(p => p.ContainsNode(spi)).ToList())
            {
                p.ChangeStateTo(PixelState.Moment);
            }

            // Create the pixel structure
            var pixelStruct = new Core.PixelStructure(nodeList, pixelList);


            // Return the data
            return(new UI.FrixelReferenceData(pixelStruct, massing));
        }
Ejemplo n.º 30
0
        public static Response Grasshopper(NancyContext ctx)
        {
            // load grasshopper file
            var archive = new GH_Archive();
            // TODO: stream to string
            var body = ctx.Request.Body.ToString();

            string json = string.Empty;

            using (var reader = new StreamReader(ctx.Request.Body))
            {
                json = reader.ReadToEnd();
            }

            //GrasshopperInput input = Newtonsoft.Json.JsonConvert.DeserializeObject<GrasshopperInput>(json);
            //JsonSerializerSettings settings = new JsonSerializerSettings();
            //settings.ContractResolver = new DictionaryAsArrayResolver();
            Schema input = JsonConvert.DeserializeObject <Schema>(json);

            string grasshopperXml = string.Empty;

            if (input.Algo != null)
            {
                // If request contains markup
                byte[] byteArray = Convert.FromBase64String(input.Algo);
                grasshopperXml = System.Text.Encoding.UTF8.GetString(byteArray);
            }
            else
            {
                // If request contains pointer
                string pointer = input.Pointer;
                grasshopperXml = GetGhxFromPointer(pointer);
            }
            if (!archive.Deserialize_Xml(grasshopperXml))
            {
                throw new Exception();
            }

            var definition = new GH_Document();

            if (!archive.ExtractObject(definition, "Definition"))
            {
                throw new Exception();
            }

            // Set input params
            foreach (var obj in definition.Objects)
            {
                var group = obj as GH_Group;
                if (group == null)
                {
                    continue;
                }

                if (group.NickName.Contains("RH_IN"))
                {
                    // It is a RestHopper input group!
                    GHTypeCodes code  = (GHTypeCodes)Int32.Parse(group.NickName.Split(':')[1]);
                    var         param = group.Objects()[0];
                    //GH_Param<IGH_Goo> goo = obj as GH_Param<IGH_Goo>;

                    // SetData
                    foreach (Resthopper.IO.DataTree <ResthopperObject> tree in input.Values)
                    {
                        string paramname = tree.ParamName;
                        if (group.NickName == paramname)
                        {
                            switch (code)
                            {
                            case GHTypeCodes.Boolean:
                                //PopulateParam<GH_Boolean>(goo, tree);
                                Param_Boolean boolParam = param as Param_Boolean;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path           path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Boolean> objectList = new List <GH_Boolean>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj = entree.Value[i];
                                        bool             boolean = JsonConvert.DeserializeObject <bool>(restobj.Data);
                                        GH_Boolean       data    = new GH_Boolean(boolean);
                                        boolParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Point:
                                //PopulateParam<GH_Point>(goo, tree);
                                Param_Point ptParam = param as Param_Point;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path         path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Point> objectList = new List <GH_Point>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject       restobj = entree.Value[i];
                                        Rhino.Geometry.Point3d rPt     = JsonConvert.DeserializeObject <Rhino.Geometry.Point3d>(restobj.Data);
                                        GH_Point data = new GH_Point(rPt);
                                        ptParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Vector:
                                //PopulateParam<GH_Vector>(goo, tree);
                                Param_Vector vectorParam = param as Param_Vector;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Vector> objectList = new List <GH_Vector>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject        restobj  = entree.Value[i];
                                        Rhino.Geometry.Vector3d rhVector = JsonConvert.DeserializeObject <Rhino.Geometry.Vector3d>(restobj.Data);
                                        GH_Vector data = new GH_Vector(rhVector);
                                        vectorParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Integer:
                                //PopulateParam<GH_Integer>(goo, tree);
                                Param_Integer integerParam = param as Param_Integer;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path           path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Integer> objectList = new List <GH_Integer>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj = entree.Value[i];
                                        int        rhinoInt      = JsonConvert.DeserializeObject <int>(restobj.Data);
                                        GH_Integer data          = new GH_Integer(rhinoInt);
                                        integerParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Number:
                                //PopulateParam<GH_Number>(goo, tree);
                                Param_Number numberParam = param as Param_Number;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Number> objectList = new List <GH_Number>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj  = entree.Value[i];
                                        double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                                        GH_Number        data     = new GH_Number(rhNumber);
                                        numberParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Text:
                                //PopulateParam<GH_String>(goo, tree);
                                Param_String stringParam = param as Param_String;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_String> objectList = new List <GH_String>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj  = entree.Value[i];
                                        string           rhString = JsonConvert.DeserializeObject <string>(restobj.Data);
                                        GH_String        data     = new GH_String(rhString);
                                        stringParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Line:
                                //PopulateParam<GH_Line>(goo, tree);
                                Param_Line lineParam = param as Param_Line;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path        path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Line> objectList = new List <GH_Line>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject    restobj = entree.Value[i];
                                        Rhino.Geometry.Line rhLine  = JsonConvert.DeserializeObject <Rhino.Geometry.Line>(restobj.Data);
                                        GH_Line             data    = new GH_Line(rhLine);
                                        lineParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Curve:
                                //PopulateParam<GH_Curve>(goo, tree);
                                Param_Curve curveParam = param as Param_Curve;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path         path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Curve> objectList = new List <GH_Curve>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj = entree.Value[i];
                                        GH_Curve         ghCurve;
                                        try
                                        {
                                            Rhino.Geometry.Polyline data = JsonConvert.DeserializeObject <Rhino.Geometry.Polyline>(restobj.Data);
                                            Rhino.Geometry.Curve    c    = new Rhino.Geometry.PolylineCurve(data);
                                            ghCurve = new GH_Curve(c);
                                        }
                                        catch
                                        {
                                            Rhino.Geometry.NurbsCurve data = JsonConvert.DeserializeObject <Rhino.Geometry.NurbsCurve>(restobj.Data);
                                            Rhino.Geometry.Curve      c    = new Rhino.Geometry.NurbsCurve(data);
                                            ghCurve = new GH_Curve(c);
                                        }
                                        curveParam.AddVolatileData(path, i, ghCurve);
                                    }
                                }
                                break;

                            case GHTypeCodes.Circle:
                                //PopulateParam<GH_Circle>(goo, tree);
                                Param_Circle circleParam = param as Param_Circle;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Circle> objectList = new List <GH_Circle>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject      restobj  = entree.Value[i];
                                        Rhino.Geometry.Circle rhCircle = JsonConvert.DeserializeObject <Rhino.Geometry.Circle>(restobj.Data);
                                        GH_Circle             data     = new GH_Circle(rhCircle);
                                        circleParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.PLane:
                                //PopulateParam<GH_Plane>(goo, tree);
                                Param_Plane planeParam = param as Param_Plane;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path         path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Plane> objectList = new List <GH_Plane>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject     restobj = entree.Value[i];
                                        Rhino.Geometry.Plane rhPlane = JsonConvert.DeserializeObject <Rhino.Geometry.Plane>(restobj.Data);
                                        GH_Plane             data    = new GH_Plane(rhPlane);
                                        planeParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Rectangle:
                                //PopulateParam<GH_Rectangle>(goo, tree);
                                Param_Rectangle rectangleParam = param as Param_Rectangle;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path             path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Rectangle> objectList = new List <GH_Rectangle>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject           restobj     = entree.Value[i];
                                        Rhino.Geometry.Rectangle3d rhRectangle = JsonConvert.DeserializeObject <Rhino.Geometry.Rectangle3d>(restobj.Data);
                                        GH_Rectangle data = new GH_Rectangle(rhRectangle);
                                        rectangleParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Box:
                                //PopulateParam<GH_Box>(goo, tree);
                                Param_Box boxParam = param as Param_Box;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path       path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Box> objectList = new List <GH_Box>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject   restobj = entree.Value[i];
                                        Rhino.Geometry.Box rhBox   = JsonConvert.DeserializeObject <Rhino.Geometry.Box>(restobj.Data);
                                        GH_Box             data    = new GH_Box(rhBox);
                                        boxParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Surface:
                                //PopulateParam<GH_Surface>(goo, tree);
                                Param_Surface surfaceParam = param as Param_Surface;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path           path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Surface> objectList = new List <GH_Surface>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject       restobj   = entree.Value[i];
                                        Rhino.Geometry.Surface rhSurface = JsonConvert.DeserializeObject <Rhino.Geometry.Surface>(restobj.Data);
                                        GH_Surface             data      = new GH_Surface(rhSurface);
                                        surfaceParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Brep:
                                //PopulateParam<GH_Brep>(goo, tree);
                                Param_Brep brepParam = param as Param_Brep;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path        path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Brep> objectList = new List <GH_Brep>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject    restobj = entree.Value[i];
                                        Rhino.Geometry.Brep rhBrep  = JsonConvert.DeserializeObject <Rhino.Geometry.Brep>(restobj.Data);
                                        GH_Brep             data    = new GH_Brep(rhBrep);
                                        brepParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Mesh:
                                //PopulateParam<GH_Mesh>(goo, tree);
                                Param_Mesh meshParam = param as Param_Mesh;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path        path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Mesh> objectList = new List <GH_Mesh>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject    restobj = entree.Value[i];
                                        Rhino.Geometry.Mesh rhMesh  = JsonConvert.DeserializeObject <Rhino.Geometry.Mesh>(restobj.Data);
                                        GH_Mesh             data    = new GH_Mesh(rhMesh);
                                        meshParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Slider:
                                //PopulateParam<GH_Number>(goo, tree);
                                GH_NumberSlider sliderParam = param as GH_NumberSlider;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path          path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Number> objectList = new List <GH_Number>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj  = entree.Value[i];
                                        double           rhNumber = JsonConvert.DeserializeObject <double>(restobj.Data);
                                        GH_Number        data     = new GH_Number(rhNumber);
                                        sliderParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.BooleanToggle:
                                //PopulateParam<GH_Boolean>(goo, tree);
                                GH_BooleanToggle toggleParam = param as GH_BooleanToggle;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path           path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Boolean> objectList = new List <GH_Boolean>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj   = entree.Value[i];
                                        bool             rhBoolean = JsonConvert.DeserializeObject <bool>(restobj.Data);
                                        GH_Boolean       data      = new GH_Boolean(rhBoolean);
                                        toggleParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;

                            case GHTypeCodes.Panel:
                                //PopulateParam<GH_String>(goo, tree);
                                GH_Panel panelParam = param as GH_Panel;
                                foreach (KeyValuePair <string, List <ResthopperObject> > entree in tree)
                                {
                                    GH_Path         path       = new GH_Path(GhPath.FromString(entree.Key));
                                    List <GH_Panel> objectList = new List <GH_Panel>();
                                    for (int i = 0; i < entree.Value.Count; i++)
                                    {
                                        ResthopperObject restobj  = entree.Value[i];
                                        string           rhString = JsonConvert.DeserializeObject <string>(restobj.Data);
                                        GH_String        data     = new GH_String(rhString);
                                        panelParam.AddVolatileData(path, i, data);
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }

            Schema OutputSchema = new Schema();

            OutputSchema.Algo = Utils.Base64Encode(string.Empty);

            // Parse output params
            foreach (var obj in definition.Objects)
            {
                var group = obj as GH_Group;
                if (group == null)
                {
                    continue;
                }

                if (group.NickName.Contains("RH_OUT"))
                {
                    // It is a RestHopper output group!
                    GHTypeCodes code  = (GHTypeCodes)Int32.Parse(group.NickName.Split(':')[1]);
                    var         param = group.Objects()[0] as IGH_Param;
                    if (param == null)
                    {
                        continue;
                    }

                    try
                    {
                        param.CollectData();
                        param.ComputeData();
                    }
                    catch (Exception)
                    {
                        param.Phase = GH_SolutionPhase.Failed;
                        // TODO: throw something better
                        throw;
                    }

                    // Get data
                    Resthopper.IO.DataTree <ResthopperObject> OutputTree = new Resthopper.IO.DataTree <ResthopperObject>();
                    OutputTree.ParamName = group.NickName;

                    var volatileData = param.VolatileData;
                    for (int p = 0; p < volatileData.PathCount; p++)
                    {
                        List <ResthopperObject> ResthopperObjectList = new List <ResthopperObject>();
                        foreach (var goo in volatileData.get_Branch(p))
                        {
                            if (goo == null)
                            {
                                continue;
                            }
                            else if (goo.GetType() == typeof(GH_Boolean))
                            {
                                GH_Boolean ghValue = goo as GH_Boolean;
                                bool       rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <bool>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Point))
                            {
                                GH_Point ghValue = goo as GH_Point;
                                Point3d  rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Point3d>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Vector))
                            {
                                GH_Vector ghValue = goo as GH_Vector;
                                Vector3d  rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Vector3d>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Integer))
                            {
                                GH_Integer ghValue = goo as GH_Integer;
                                int        rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <int>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Number))
                            {
                                GH_Number ghValue = goo as GH_Number;
                                double    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <double>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_String))
                            {
                                GH_String ghValue = goo as GH_String;
                                string    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <string>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Line))
                            {
                                GH_Line ghValue = goo as GH_Line;
                                Line    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Line>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Curve))
                            {
                                GH_Curve ghValue = goo as GH_Curve;
                                Curve    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Curve>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Circle))
                            {
                                GH_Circle ghValue = goo as GH_Circle;
                                Circle    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Circle>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Plane))
                            {
                                GH_Plane ghValue = goo as GH_Plane;
                                Plane    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Plane>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Rectangle))
                            {
                                GH_Rectangle ghValue = goo as GH_Rectangle;
                                Rectangle3d  rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Rectangle3d>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Box))
                            {
                                GH_Box ghValue = goo as GH_Box;
                                Box    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Box>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Surface))
                            {
                                GH_Surface ghValue = goo as GH_Surface;
                                Brep       rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Brep>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Brep))
                            {
                                GH_Brep ghValue = goo as GH_Brep;
                                Brep    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Brep>(rhValue));
                            }
                            else if (goo.GetType() == typeof(GH_Mesh))
                            {
                                GH_Mesh ghValue = goo as GH_Mesh;
                                Mesh    rhValue = ghValue.Value;
                                ResthopperObjectList.Add(GetResthopperObject <Mesh>(rhValue));
                            }
                        }

                        GhPath path = new GhPath(new int[] { p });
                        OutputTree.Add(path.ToString(), ResthopperObjectList);
                    }

                    OutputSchema.Values.Add(OutputTree);
                }
            }


            if (OutputSchema.Values.Count < 1)
            {
                throw new System.Exceptions.PayAttentionException("Looks like you've missed something..."); // TODO
            }
            string returnJson = JsonConvert.SerializeObject(OutputSchema);

            return(returnJson);
        }
Ejemplo n.º 31
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)
        {
            // Declare variables
            Point3d position   = new Point3d(0, 10, 0);
            Color   color      = Color.White;
            double  intensity  = 1;
            double  angle      = 0.35;
            double  distance   = 0;
            double  decay      = 1;
            Boolean castShadow = false;
            string  name       = "";

            // Reference the inputs
            DA.GetData(0, ref position);
            DA.GetData(1, ref color);
            DA.GetData(2, ref intensity);
            DA.GetData(3, ref angle);
            DA.GetData(4, ref distance);
            DA.GetData(5, ref decay);
            DA.GetData(6, ref castShadow);
            DA.GetData(7, ref name);

            // Build the light object
            dynamic lightObject = new ExpandoObject();

            lightObject.Uuid = Guid.NewGuid();
            lightObject.Type = "SpotLight";
            if (name.Length > 0)
            {
                lightObject.Name = name;
            }
            lightObject.Color      = Convert.ToInt32(color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2"), 16);;
            lightObject.Intensity  = intensity;
            lightObject.Angle      = (Math.PI / 180) * angle;
            lightObject.CastShadow = castShadow;
            lightObject.Distance   = distance;
            lightObject.Decay      = decay;
            lightObject.Matrix     = new Matrix(position).Array;

            // Create a helper to aid in visualizing the light's behavior
            List <Curve> helper = new List <Curve>();

            var line = new Rhino.Geometry.Line(position, new Point3d(0, 0, 0));

            helper.Add(line.ToNurbsCurve());

            Vector3d vector = new Vector3d(new Point3d(0, 0, 0) - position);
            Plane    plane  = new Plane(position, vector);
            double   radius = vector.Length * Math.Tan((Math.PI / 180) * angle);
            Cone     cone   = new Cone(plane, vector.Length, radius);

            Brep brep = cone.ToBrep(false);

            for (int i = 0; i < brep.Edges.Count; i++)
            {
                helper.Add(brep.Edges[i].EdgeCurve);
            }

            // Create object3d
            Object3d object3d = new Object3d(lightObject);

            // Set outputs
            DA.SetDataList(0, helper);
            DA.SetData(1, object3d);
        }