Ejemplo n.º 1
0
        private object TypeCast(IGH_Goo data, IGH_TypeHint hint)
        {
            if (data == null)
            {
                return(null);
            }
            if (hint == null)
            {
                return(data.ScriptVariable());
            }
            object objectValue = data.ScriptVariable();
            object target;

            hint.Cast(objectValue, out target);
            return(target);
        }
Ejemplo n.º 2
0
        /*******************************************/
        /**** Private Methods                   ****/
        /*******************************************/

        private static T FromGoo <T>(this IGH_Goo goo, IGH_TypeHint hint = null)
        {
            if (goo == null)
            {
                return(default(T));
            }

            // Get the data out of the Goo
            object data = goo.ScriptVariable();

            while (data is IGH_Goo)
            {
                data = ((IGH_Goo)data).ScriptVariable();
            }

            if (data == null)
            {
                return(default(T));
            }

            if (data.GetType().Namespace.StartsWith("Rhino.Geometry") && !typeof(T).Namespace.StartsWith("Rhino.Geometry"))
            {
                data = BH.Engine.Rhinoceros.Convert.IFromRhino(data);
            }

            // Convert the data to an acceptable format
            if (hint != null)
            {
                object result;
                hint.Cast(RuntimeHelpers.GetObjectValue(data), out result);
                data = result;

                if (data.GetType().Namespace.StartsWith("Rhino.Geometry") && !typeof(T).Namespace.StartsWith("Rhino.Geometry"))
                {
                    data = BH.Engine.Rhinoceros.Convert.IFromRhino(data);
                }
            }
            else if (data is T)
            {
                return((T)data);
            }
            else if (data is IEnumerable)
            {
                UnderlyingType subType = data.GetType().UnderlyingType();
                if (typeof(T).IsAssignableFrom(subType.Type))
                {
                    List <T> list = ((IEnumerable)data).Cast <T>().ToList();
                    if (list.Count == 0)
                    {
                        return(default(T));
                    }
                    else
                    {
                        return(list.First());
                    }
                }
            }

            try
            {
                return((T)(data as dynamic));
            }
            catch
            {
                Engine.Base.Compute.RecordError("Failed to cast " + data.GetType().IToText() + " into " + typeof(T).IToText());
                return(default(T));
            }
        }