Exemple #1
0
            public int GetAtLeast()
            {
                IGH_ContextualParameter contextualParameter = Param as IGH_ContextualParameter;

                if (contextualParameter != null)
                {
                    return(contextualParameter.AtLeast);
                }
                return(1);
            }
Exemple #2
0
            public string GetDescription()
            {
                IGH_ContextualParameter contextualParameter = Param as IGH_ContextualParameter;

                if (contextualParameter != null)
                {
                    return(contextualParameter.Prompt);
                }
                return(null);
            }
Exemple #3
0
        private static GrasshopperDefinition Construct(GH_Archive archive)
        {
            var definition = new GH_Document();

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

            // raise DocumentServer.DocumentAdded event (used by some plug-ins)
            Grasshopper.Instances.DocumentServer.AddDocument(definition);

            GrasshopperDefinition rc = new GrasshopperDefinition(definition);

            foreach (var obj in definition.Objects)
            {
                IGH_ContextualParameter contextualParam = obj as IGH_ContextualParameter;
                if (contextualParam != null)
                {
                    IGH_Param param = obj as IGH_Param;
                    if (param != null)
                    {
                        rc._input[param.NickName] = new InputGroup(param);
                    }
                    continue;
                }

                var group = obj as GH_Group;
                if (group == null)
                {
                    continue;
                }

                string nickname     = group.NickName;
                var    groupObjects = group.Objects();
                if (nickname.Contains("RH_IN") && groupObjects.Count > 0)
                {
                    var param = groupObjects[0] as IGH_Param;
                    if (param != null)
                    {
                        rc._input[nickname] = new InputGroup(param);
                    }
                }

                if (nickname.Contains("RH_OUT") && groupObjects.Count > 0)
                {
                    var param = groupObjects[0] as IGH_Param;
                    if (param != null)
                    {
                        rc._output[nickname] = param;
                    }
                }
            }
            return(rc);
        }
Exemple #4
0
            public int GetAtMost()
            {
                IGH_ContextualParameter contextualParameter = Param as IGH_ContextualParameter;

                if (contextualParameter != null)
                {
                    return(contextualParameter.AtMost);
                }
                if (Param is GH_NumberSlider)
                {
                    return(1);
                }
                return(int.MaxValue);
            }
Exemple #5
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;
                }
            }
        }