Esempio n. 1
0
        internal static bool SetParameter(IGH_ActiveObject obj, DB.Parameter parameter, IGH_Goo goo)
        {
            if (goo is null)
            {
                return(false);
            }

            try
            {
                var element  = parameter.Element;
                var value    = goo.ScriptVariable();
                var document = default(DB.Document);
                if (value is DB.Parameter paramValue)
                {
                    switch (paramValue.StorageType)
                    {
                    case DB.StorageType.Integer: value = paramValue.AsInteger(); break;

                    case DB.StorageType.Double: value = paramValue.AsDoubleInRhinoUnits(); break;

                    case DB.StorageType.String: value = paramValue.AsString(); break;

                    case DB.StorageType.ElementId: value = paramValue.AsElementId(); document = paramValue.Element.Document; break;
                    }
                }

                switch (parameter.StorageType)
                {
                case DB.StorageType.Integer:
                {
                    switch (value)
                    {
                    case bool boolean: parameter.Set(boolean ? 1 : 0); break;

                    case int integer: parameter.Set(integer); break;

                    case double real: parameter.SetDoubleInRhinoUnits((int)Clamp(Round(real), int.MinValue, int.MaxValue)); break;

                    case System.Drawing.Color color: parameter.Set(((int)color.R) | ((int)color.G << 8) | ((int)color.B << 16)); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.Double:
                {
                    switch (value)
                    {
                    case int integer: parameter.Set((double)integer); break;

                    case double real: parameter.SetDoubleInRhinoUnits(real); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.String:
                {
                    switch (value)
                    {
                    case string str: parameter.Set(str); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.ElementId:
                {
                    switch (value)
                    {
                    case DB.Element ele:
                        if (element.Document.Equals(ele.Document))
                        {
                            parameter.Set(ele.Id);
                        }
                        else
                        {
                            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Referencing elements from other documents is not valid");
                        }
                        break;

                    case DB.Category cat:
                        if (element.Document.Equals(cat.Document()))
                        {
                            parameter.Set(cat.Id);
                        }
                        else
                        {
                            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Referencing categories from other documents is not valid");
                        }
                        break;

                    case DB.ElementId id:
                        if (document is object)
                        {
                            if (element.Document.Equals(document))
                            {
                                parameter.Set(id);
                            }
                            else
                            {
                                obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Referencing elements from other documents is not valid");
                            }
                        }
                        else
                        {
                            element = null;
                        }
                        break;

                    default: element = null; break;
                    }
                    break;
                }

                default:
                {
                    element = null;
                    break;
                }
                }

                if (element is null && parameter is object)
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Failed to cast from {value.GetType().Name} to {parameter.StorageType.ToString()}.");
                    return(false);
                }
            }
            catch (Autodesk.Revit.Exceptions.InvalidOperationException e)
            {
                obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Failed to set '{parameter.Definition.Name}' value : {e.Message}");
                return(false);
            }

            return(true);
        }