Example #1
0
 public override void RemovedFromDocument(GH_Document document)
 {
     // let's be polite and pick up our garbage
     if (wormDoc != null)
     {
         wormDoc.Enabled = false;
         wormDoc.RemoveObject(wormCluster, false);
         wormDoc.Dispose();
         wormDoc = null;
     }
     base.RemovedFromDocument(document);
 }
Example #2
0
        public static MenuBarManager.CallbackStatus ReadFromFile(string filePath, out GH_Document definition)
        {
            definition = null;

            try
            {
                var archive = new GH_Archive();
                if (!archive.ReadFromFile(filePath))
                {
                    return(MenuBarManager.CallbackStatus.Error);
                }

                definition = new GH_Document();
                if (archive.ExtractObject(definition, "Definition"))
                {
                    return(MenuBarManager.CallbackStatus.Continue);
                }

                definition?.Dispose();
                definition = null;
                return(MenuBarManager.CallbackStatus.Error);
            }
            catch (Exception)
            {
                return(MenuBarManager.CallbackStatus.Error);
            }
        }
        public static Result ReadFromFile(string filePath, out GH_Document definition)
        {
            definition = null;

            var CurrentCulture = Thread.CurrentThread.CurrentCulture;

            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

                var archive = new GH_Archive();
                if (!archive.ReadFromFile(filePath))
                {
                    return(Result.Failed);
                }

                definition = new GH_Document();
                if (archive.ExtractObject(definition, "Definition"))
                {
                    return(Result.Succeeded);
                }

                definition?.Dispose();
                definition = null;
                return(Result.Failed);
            }
            catch (Exception)
            {
                return(Result.Failed);
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = CurrentCulture;
            }
        }
Example #4
0
        private DataTree <object> SolutionTrigger()
        {
            DataTree <object> dataTree = null;

            GH_Document doc = m_document;

            if (doc == null)
            {
                throw new Exception("File could not be opened.");
            }

            doc.Enabled = true;
            doc.NewSolution(true, GH_SolutionMode.Silent);

            GH_ClusterOutputHook[] outputs = doc.ClusterOutputHooks();

            dataTree = new DataTree <object>();
            var hint = new GH_NullHint();

            dataTree.MergeStructure(outputs[0].VolatileData, hint);

            doc.Dispose();

            return(dataTree);
        }
Example #5
0
 protected override void OnHandleDestroyed(EventArgs e)
 {
     if (definition != null)
     {
         definition.Dispose();
         definition = null;
     }
     if (rhinoCore != null)
     {
         rhinoCore.Dispose();
         rhinoCore = null;
     }
     base.OnHandleDestroyed(e);
 }
Example #6
0
        public GH_Document Definition(string fileName)
        {
            var filePath = FindFile(fileName, new string[] { });

            if (string.IsNullOrEmpty(filePath))
            {
                return(null);
            }

            if (_docs.TryGetValue(filePath, out var doc))
            {
                var definition = new GH_Document();
                if (doc.ExtractObject(definition, "Definition"))
                {
                    return(definition);
                }
                definition?.Dispose();
            }
            return(null);
        }
Example #7
0
        /// <summary>
        /// Handler for "grasshopper/solve-jsonobject" POST
        /// </summary>
        /// <param name="input">
        /// JSON data generated in Rhino/Grasshopper for a given definition
        /// {
        ///   "input": [
        ///     {
        ///       "nickname": "CtxPt",
        ///       "instanceGuid": "6a67a2dc-3fb9-49da-92fb-c11bec3323f9",
        ///       "points": [
        ///         [4.67739437581401,-4.93724961891479,0]
        ///       ]
        ///     },
        ///     ...
        ///   ],
        ///   "definition": base64 encoded string representing a gh file
        /// }
        /// </param>
        /// <returns>
        /// JSON array
        /// [
        ///   {
        ///     "instanceGuid": "fa19bc8b-5115-4b27-9d73-13eaaff3b8bc",
        ///     "geometry": [
        ///       "base64 encoded Rhino.Runtime.CommonObject",
        ///       ...
        ///     ]
        ///   },
        ///   ...
        /// ]
        /// </returns>
        public static Newtonsoft.Json.Linq.JArray Solve(Newtonsoft.Json.Linq.JObject input)
        {
            GH_Document definition = null;

            Newtonsoft.Json.Linq.JToken token;
            if (input.TryGetValue("definition", out token))
            {
                string encoded   = token.ToString();
                byte[] byteArray = Convert.FromBase64String(encoded);
                var    archive   = new GH_IO.Serialization.GH_Archive();
                if (archive.Deserialize_Binary(byteArray))
                {
                    definition = new GH_Document();
                    if (!archive.ExtractObject(definition, "Definition"))
                    {
                        definition.Dispose();
                        definition = null;
                    }
                }
            }

            var rc = new Newtonsoft.Json.Linq.JArray();

            if (definition != null && input.TryGetValue("input", out token))
            {
                foreach (var obj in token)
                {
                    var    item         = obj as Newtonsoft.Json.Linq.JObject;
                    string instanceGuid = item.GetValue("instanceGuid").ToString();
                    var    id           = new System.Guid(instanceGuid);
                    var    component    = definition.FindComponent(id);
                    Newtonsoft.Json.Linq.JToken pointsToken;
                    if (item.TryGetValue("points", out pointsToken))
                    {
                        var method = component.GetType().GetMethod("SetSolvePoints");
                        List <Rhino.Geometry.Point3d> points = new List <Rhino.Geometry.Point3d>();
                        foreach (var pointToken in pointsToken)
                        {
                            var    pts = pointToken as Newtonsoft.Json.Linq.JArray;
                            double x   = (double)pts[0];
                            double y   = (double)pts[1];
                            double z   = (double)pts[2];
                            points.Add(new Rhino.Geometry.Point3d(x, y, z));
                        }
                        method.Invoke(component, new object[] { points });
                    }
                }

                definition.Enabled = true;
                definition.NewSolution(true, GH_SolutionMode.CommandLine);
                var components = definition.ActiveObjects();

                var jobject = new Newtonsoft.Json.Linq.JObject();
                for (int i = 0; i < components.Count; i++)
                {
                    var method = components[i].GetType().GetMethod("GetCollectedGeometry");
                    if (method != null)
                    {
                        var geometryArray = method.Invoke(components[i], null) as Rhino.Geometry.GeometryBase[];
                        var id            = components[i].InstanceGuid;
                        jobject.Add("instanceId", id);
                        var ja = new Newtonsoft.Json.Linq.JArray();
                        foreach (var geometry in geometryArray)
                        {
                            string s = Newtonsoft.Json.JsonConvert.SerializeObject(geometry, GeometryResolver.Settings);
                            var    g = Newtonsoft.Json.Linq.JObject.Parse(s);
                            //var g = Newtonsoft.Json.Linq.JObject.FromObject(geometry);
                            ja.Add(g);
                        }
                        jobject.Add("geometry", ja);
                        rc.Add(jobject);
                    }
                }
            }

            return(rc);
        }