Ejemplo n.º 1
0
        static Grasshopper.Kernel.Types.IGH_Goo GooFromReshopperObject(ResthopperObject obj)
        {
            string data = obj.Data.Trim('"');

            if (obj.Type.StartsWith("Rhino.Geometry"))
            {
                var    pt    = new Rhino.Geometry.Point3d();
                string s     = pt.GetType().AssemblyQualifiedName;
                int    index = s.IndexOf(",");
                string sType = $"{obj.Type}{s.Substring(index)}";

                System.Type type = System.Type.GetType(sType);
                if (type != null && typeof(GeometryBase).IsAssignableFrom(type))
                {
                    Dictionary <string, string> dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(data);
                    var     geometry = Rhino.Runtime.CommonObject.FromJSON(dict);
                    Surface surface  = geometry as Surface;
                    if (surface != null)
                    {
                        geometry = surface.ToBrep();
                    }
                    if (geometry is Brep)
                    {
                        return(new Grasshopper.Kernel.Types.GH_Brep(geometry as Brep));
                    }
                    if (geometry is Curve)
                    {
                        return(new Grasshopper.Kernel.Types.GH_Curve(geometry as Curve));
                    }
                    if (geometry is Mesh)
                    {
                        return(new Grasshopper.Kernel.Types.GH_Mesh(geometry as Mesh));
                    }
                    if (geometry is SubD)
                    {
                        return(new Grasshopper.Kernel.Types.GH_SubD(geometry as SubD));
                    }
                }
            }

            switch (obj.Type)
            {
            case "System.Double":
                return(new Grasshopper.Kernel.Types.GH_Number(double.Parse(data)));

            case "System.String":
                return(new Grasshopper.Kernel.Types.GH_String(data));

            case "System.Int32":
                return(new Grasshopper.Kernel.Types.GH_Integer(int.Parse(data)));

            case "Rhino.Geometry.Circle":
                return(new Grasshopper.Kernel.Types.GH_Circle(JsonConvert.DeserializeObject <Circle>(data)));

            case "Rhino.Geometry.Line":
                return(new Grasshopper.Kernel.Types.GH_Line(JsonConvert.DeserializeObject <Line>(data)));

            case "Rhino.Geometry.Point3d":
                return(new Grasshopper.Kernel.Types.GH_Point(JsonConvert.DeserializeObject <Point3d>(data)));

            case "Rhino.Geometry.Vector3d":
                return(new Grasshopper.Kernel.Types.GH_Vector(JsonConvert.DeserializeObject <Vector3d>(data)));

            case "Rhino.Geometry.Brep":
            case "Rhino.Geometry.Curve":
            case "Rhino.Geometry.Extrusion":
            case "Rhino.Geometry.Mesh":
            case "Rhino.Geometry.PolyCurve":
            case "Rhino.Geometry.NurbsCurve":
            case "Rhino.Geometry.PolylineCurve":
            case "Rhino.Geometry.SubD":
            {
                Dictionary <string, string> dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(data);
                var     geometry = Rhino.Runtime.CommonObject.FromJSON(dict);
                Surface surface  = geometry as Surface;
                if (surface != null)
                {
                    geometry = surface.ToBrep();
                }
                if (geometry is Brep)
                {
                    return(new Grasshopper.Kernel.Types.GH_Brep(geometry as Brep));
                }
                if (geometry is Curve)
                {
                    return(new Grasshopper.Kernel.Types.GH_Curve(geometry as Curve));
                }
                if (geometry is Mesh)
                {
                    return(new Grasshopper.Kernel.Types.GH_Mesh(geometry as Mesh));
                }
                if (geometry is SubD)
                {
                    return(new Grasshopper.Kernel.Types.GH_SubD(geometry as SubD));
                }
            }
            break;
            }
            throw new Exception("unable to convert resthopper data");
        }
Ejemplo n.º 2
0
        static IGH_Goo GooFromReshopperObject(ResthopperObject obj)
        {
            if (obj.ResolvedData != null)
            {
                return(obj.ResolvedData as Grasshopper.Kernel.Types.IGH_Goo);
            }

            string data = obj.Data.Trim('"');

            switch (obj.Type)
            {
            case "System.Boolean":
            {
                var boolResult = new Grasshopper.Kernel.Types.GH_Boolean(bool.Parse(data));
                obj.ResolvedData = boolResult;
                return(boolResult);
            }

            case "System.Double":
            {
                var doubleResult = new Grasshopper.Kernel.Types.GH_Number(double.Parse(data));
                obj.ResolvedData = doubleResult;
                return(doubleResult);
            }

            case "System.String":
            {
                string unescaped = data;
                // TODO: This is a a hack. I understand that JSON needs to escape
                // embedded JSON, but I'm not particularly happy with the following code
                if (unescaped.Trim().StartsWith("{") && unescaped.Contains("\\"))
                {
                    unescaped = System.Text.RegularExpressions.Regex.Unescape(data);
                }
                var stringResult = new Grasshopper.Kernel.Types.GH_String(unescaped);
                obj.ResolvedData = stringResult;
                return(stringResult);
            }

            case "System.Int32":
            {
                var intResult = new Grasshopper.Kernel.Types.GH_Integer(int.Parse(data));
                obj.ResolvedData = intResult;
                return(intResult);
            }

            case "Rhino.Geometry.Circle":
            {
                var circleResult = new Grasshopper.Kernel.Types.GH_Circle(JsonConvert.DeserializeObject <Circle>(data));
                obj.ResolvedData = circleResult;
                return(circleResult);
            }

            case "Rhino.Geometry.Line":
            {
                var lineResult = new Grasshopper.Kernel.Types.GH_Line(JsonConvert.DeserializeObject <Line>(data));
                obj.ResolvedData = lineResult;
                return(lineResult);
            }

            case "Rhino.Geometry.Plane":
            {
                var planeResult = new Grasshopper.Kernel.Types.GH_Plane(JsonConvert.DeserializeObject <Plane>(data));
                obj.ResolvedData = planeResult;
                return(planeResult);
            }

            case "Rhino.Geometry.Point3d":
            {
                var pointResult = new Grasshopper.Kernel.Types.GH_Point(JsonConvert.DeserializeObject <Point3d>(data));
                obj.ResolvedData = pointResult;
                return(pointResult);
            }

            case "Rhino.Geometry.Vector3d":
            {
                var vectorResult = new Grasshopper.Kernel.Types.GH_Vector(JsonConvert.DeserializeObject <Vector3d>(data));
                obj.ResolvedData = vectorResult;
                return(vectorResult);
            }

            case "Rhino.Geometry.Brep":
            case "Rhino.Geometry.Curve":
            case "Rhino.Geometry.Extrusion":
            case "Rhino.Geometry.Mesh":
            case "Rhino.Geometry.PolyCurve":
            case "Rhino.Geometry.NurbsCurve":
            case "Rhino.Geometry.PolylineCurve":
            case "Rhino.Geometry.SubD":
            {
                Dictionary <string, string> dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(data);
                var     geometry = Rhino.Runtime.CommonObject.FromJSON(dict);
                Surface surface  = geometry as Surface;
                if (surface != null)
                {
                    geometry = surface.ToBrep();
                }
                if (geometry is Brep)
                {
                    return(new Grasshopper.Kernel.Types.GH_Brep(geometry as Brep));
                }
                if (geometry is Curve)
                {
                    return(new Grasshopper.Kernel.Types.GH_Curve(geometry as Curve));
                }
                if (geometry is Mesh)
                {
                    return(new Grasshopper.Kernel.Types.GH_Mesh(geometry as Mesh));
                }
                if (geometry is SubD)
                {
                    return(new Grasshopper.Kernel.Types.GH_SubD(geometry as SubD));
                }
            }
            break;
            }

            if (obj.Type.StartsWith("Rhino.Geometry"))
            {
                var    pt    = new Rhino.Geometry.Point3d();
                string s     = pt.GetType().AssemblyQualifiedName;
                int    index = s.IndexOf(",");
                string sType = $"{obj.Type}{s.Substring(index)}";

                System.Type type = System.Type.GetType(sType);
                if (type != null && typeof(GeometryBase).IsAssignableFrom(type))
                {
                    Dictionary <string, string> dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(data);
                    var     geometry = Rhino.Runtime.CommonObject.FromJSON(dict);
                    Surface surface  = geometry as Surface;
                    if (surface != null)
                    {
                        geometry = surface.ToBrep();
                    }
                    if (geometry is Brep)
                    {
                        return(new Grasshopper.Kernel.Types.GH_Brep(geometry as Brep));
                    }
                    if (geometry is Curve)
                    {
                        return(new Grasshopper.Kernel.Types.GH_Curve(geometry as Curve));
                    }
                    if (geometry is Mesh)
                    {
                        return(new Grasshopper.Kernel.Types.GH_Mesh(geometry as Mesh));
                    }
                    if (geometry is SubD)
                    {
                        return(new Grasshopper.Kernel.Types.GH_SubD(geometry as SubD));
                    }
                }
            }

            throw new Exception("unable to convert resthopper data");
        }