Beispiel #1
0
        void GetRemoteDescription()
        {
            bool   performPost = false;
            string address     = Path;

            if (!PathIsAppServer)
            {
                if (_pathAsComponentGuid != Guid.Empty)
                {
                    // path provided is a guid to a component
                    address = Servers.GetDescriptionUrl(_pathAsComponentGuid);
                }
                else
                {
                    if (address.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                    {
                        address = Servers.GetDescriptionUrl(Path);
                    }
                    else
                    {
                        if (!System.IO.File.Exists(address))
                        {
                            return; // file no longer there...
                        }
                        performPost = true;
                    }
                }
            }

            IoResponseSchema responseSchema = null;

            System.Threading.Tasks.Task <System.Net.Http.HttpResponseMessage> responseTask = null;
            IDisposable contentToDispose = null;

            if (performPost)
            {
                string postUrl = Servers.GetDescriptionPostUrl();
                var    bytes   = System.IO.File.ReadAllBytes(address);
                var    schema  = new Schema();
                schema.Algo = Convert.ToBase64String(bytes);
                string inputJson = JsonConvert.SerializeObject(schema);
                var    content   = new System.Net.Http.StringContent(inputJson, Encoding.UTF8, "application/json");
                responseTask     = HttpClient.PostAsync(postUrl, content);
                contentToDispose = content;
            }
            else
            {
                responseTask = HttpClient.GetAsync(address);
            }

            if (responseTask != null)
            {
                var responseMessage  = responseTask.Result;
                var remoteSolvedData = responseMessage.Content;
                var stringResult     = remoteSolvedData.ReadAsStringAsync().Result;
                if (!string.IsNullOrEmpty(stringResult))
                {
                    responseSchema = JsonConvert.DeserializeObject <Resthopper.IO.IoResponseSchema>(stringResult);
                    _cacheKey      = responseSchema.CacheKey;
                }
            }

            if (contentToDispose != null)
            {
                contentToDispose.Dispose();
            }

            if (responseSchema != null)
            {
                _description = responseSchema.Description;
                _customIcon  = null;
                if (!string.IsNullOrWhiteSpace(responseSchema.Icon))
                {
                    try
                    {
                        byte[] bytes = Convert.FromBase64String(responseSchema.Icon);
                        using (var ms = new MemoryStream(bytes))
                        {
                            _customIcon = new System.Drawing.Bitmap(ms);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                _inputParams  = new Dictionary <string, Tuple <InputParamSchema, IGH_Param> >();
                _outputParams = new Dictionary <string, IGH_Param>();
                foreach (var input in responseSchema.Inputs)
                {
                    string inputParamName = input.Name;
                    if (inputParamName.StartsWith("RH_IN:"))
                    {
                        var chunks = inputParamName.Split(new char[] { ':' });
                        inputParamName = chunks[chunks.Length - 1];
                    }
                    _inputParams[inputParamName] = Tuple.Create(input, ParamFromIoResponseSchema(input));
                }
                foreach (var output in responseSchema.Outputs)
                {
                    string outputParamName = output.Name;
                    if (outputParamName.StartsWith("RH_OUT:"))
                    {
                        var chunks = outputParamName.Split(new char[] { ':' });
                        outputParamName = chunks[chunks.Length - 1];
                    }
                    _outputParams[outputParamName] = ParamFromIoResponseSchema(output);
                }
            }
        }
        public static Response GetIoNames(NancyContext ctx)
        {
            // load grasshopper file
            var archive = new GH_Archive();
            // TODO: stream to string
            var body = ctx.Request.Body.ToString();
            //
            //var body = input.Algo;

            string json = string.Empty;

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

            IoQuerySchema input          = JsonConvert.DeserializeObject <IoQuerySchema>(json);
            string        pointer        = input.RequestedFile;
            string        grasshopperXml = GetGhxFromPointer(pointer);

            if (!archive.Deserialize_Xml(grasshopperXml))
            {
                throw new Exception();
            }

            var definition = new GH_Document();

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

            // Parse input and output names
            List <string> InputNames  = new List <string>();
            List <string> OutputNames = new List <string>();

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

                if (group.NickName.Contains("RH_IN"))
                {
                    InputNames.Add(group.NickName);
                }
                else if (group.NickName.Contains("RH_OUT"))
                {
                    OutputNames.Add(group.NickName);
                }
            }

            IoResponseSchema response = new IoResponseSchema();

            response.InputNames  = InputNames;
            response.OutputNames = OutputNames;

            string jsonResponse = JsonConvert.SerializeObject(response);

            return(jsonResponse);
        }
        void GetRemoteDescription()
        {
            bool performPost = false;

            string address  = null;
            var    pathType = GetPathType();

            switch (pathType)
            {
            case PathType.GrasshopperDefinition:
            {
                if (Path.StartsWith("http", StringComparison.OrdinalIgnoreCase) ||
                    File.Exists(Path))
                {
                    address     = Path;
                    performPost = true;
                }
            }
            break;

            case PathType.ComponentGuid:
                address = Servers.GetDescriptionUrl(Guid.Parse(Path));
                break;

            case PathType.Server:
                address = Path;
                break;

            case PathType.NonresponsiveUrl:
                break;
            }
            if (address == null)
            {
                return;
            }

            IoResponseSchema responseSchema = null;

            System.Threading.Tasks.Task <System.Net.Http.HttpResponseMessage> responseTask;
            IDisposable contentToDispose = null;

            if (performPost)
            {
                string postUrl = Servers.GetDescriptionPostUrl();
                var    schema  = new Schema();
                if (Path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    schema.Pointer = address;
                }
                else
                {
                    var bytes = System.IO.File.ReadAllBytes(address);
                    schema.Algo = Convert.ToBase64String(bytes);
                }
                string inputJson = JsonConvert.SerializeObject(schema);
                var    content   = new System.Net.Http.StringContent(inputJson, Encoding.UTF8, "application/json");
                responseTask     = HttpClient.PostAsync(postUrl, content);
                contentToDispose = content;
            }
            else
            {
                responseTask = HttpClient.GetAsync(address);
            }

            if (responseTask != null)
            {
                var responseMessage  = responseTask.Result;
                var remoteSolvedData = responseMessage.Content;
                var stringResult     = remoteSolvedData.ReadAsStringAsync().Result;
                if (!string.IsNullOrEmpty(stringResult))
                {
                    responseSchema = JsonConvert.DeserializeObject <Resthopper.IO.IoResponseSchema>(stringResult);
                    _cacheKey      = responseSchema.CacheKey;
                }
            }

            if (contentToDispose != null)
            {
                contentToDispose.Dispose();
            }

            if (responseSchema != null)
            {
                _description = responseSchema.Description;
                _customIcon  = null;
                if (!string.IsNullOrWhiteSpace(responseSchema.Icon))
                {
                    try
                    {
                        // Use reflection until we update requirements for Hops to run on a newer service release of Rhino
                        string svg = responseSchema.Icon;
                        // Check for some hope that the string is svg. Pre-7.7 has a bug where it could crash
                        // Rhino with invalid svg
                        if (svg.IndexOf("svg", StringComparison.InvariantCultureIgnoreCase) < 0 || svg.IndexOf("xmlns", StringComparison.InvariantCultureIgnoreCase) < 0)
                        {
                            svg = null;
                        }

                        if (svg != null)
                        {
                            var method = typeof(Rhino.UI.DrawingUtilities).GetMethod("BitmapFromSvg");
                            if (method != null)
                            {
                                _customIcon = method.Invoke(null, new object[] { svg, 24, 24 }) as System.Drawing.Bitmap;
                            }
                            //_customIcon = Rhino.UI.DrawingUtilities.BitmapFromSvg(responseSchema.Icon, 24, 24);
                        }
                        if (_customIcon == null)
                        {
                            byte[] bytes = Convert.FromBase64String(responseSchema.Icon);
                            using (var ms = new MemoryStream(bytes))
                            {
                                _customIcon = new System.Drawing.Bitmap(ms);
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                _inputParams  = new Dictionary <string, Tuple <InputParamSchema, IGH_Param> >();
                _outputParams = new Dictionary <string, IGH_Param>();
                foreach (var input in responseSchema.Inputs)
                {
                    string inputParamName = input.Name;
                    if (inputParamName.StartsWith("RH_IN:"))
                    {
                        var chunks = inputParamName.Split(new char[] { ':' });
                        inputParamName = chunks[chunks.Length - 1];
                    }
                    _inputParams[inputParamName] = Tuple.Create(input, ParamFromIoResponseSchema(input));
                }
                foreach (var output in responseSchema.Outputs)
                {
                    string outputParamName = output.Name;
                    if (outputParamName.StartsWith("RH_OUT:"))
                    {
                        var chunks = outputParamName.Split(new char[] { ':' });
                        outputParamName = chunks[chunks.Length - 1];
                    }
                    _outputParams[outputParamName] = ParamFromIoResponseSchema(output);
                }
            }
        }
        static Response GetIoNames(NancyContext ctx)
        {
            string json = ctx.Request.Body.AsString();

            IoQuerySchema input   = JsonConvert.DeserializeObject <IoQuerySchema>(json);
            string        pointer = input.RequestedFile;
            GH_Archive    archive = DataCache.GetCachedArchive(pointer);

            var definition = new GH_Document();

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

            // Parse input and output names
            List <string> InputNames  = new List <string>();
            List <string> OutputNames = new List <string>();
            var           Inputs      = new List <IoParamSchema>();
            var           Outputs     = new List <IoParamSchema>();

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

                if (group.NickName.Contains("RH_IN"))
                {
                    InputNames.Add(group.NickName);

                    var i = new IoParamSchema
                    {
                        Name      = group.NickName,
                        ParamType = (group.Objects()[0] as IGH_Param).TypeName
                    };

                    Inputs.Add(i);
                }
                else if (group.NickName.Contains("RH_OUT"))
                {
                    OutputNames.Add(group.NickName);

                    var o = new IoParamSchema
                    {
                        Name      = group.NickName,
                        ParamType = (group.Objects()[0] as IGH_Param).TypeName
                    };

                    Outputs.Add(o);
                }
            }

            var response = new IoResponseSchema
            {
                InputNames  = InputNames,
                OutputNames = OutputNames,
                Inputs      = Inputs,
                Outputs     = Outputs
            };

            string jsonResponse = JsonConvert.SerializeObject(response);

            return(jsonResponse);
        }