Exemplo n.º 1
0
        //set the value of a parameter
        public static void SetParameterValue(ref RevitLibrary.Parameter p, object value)
        {
            try
            {
                if (value.GetType().Equals(typeof(string)))
                {
                    if (p.SetValueString(value as string))
                    {
                        return;
                    }
                }

                switch (p.StorageType)
                {
                case StorageType.None:
                    break;

                case StorageType.Double:
                    if (value.GetType().Equals(typeof(string)))
                    {
                        p.Set(double.Parse(value as string));
                    }
                    else
                    {
                        p.Set(Convert.ToDouble(value));
                    }
                    break;

                case StorageType.Integer:
                    if (value.GetType().Equals(typeof(string)))
                    {
                        p.Set(int.Parse(value as string));
                    }
                    else
                    {
                        p.Set(Convert.ToInt32(value));
                    }
                    break;

                case StorageType.ElementId:
                    if (value.GetType().Equals(typeof(ElementId)))
                    {
                        p.Set(value as ElementId);
                    }
                    else if (value.GetType().Equals(typeof(string)))
                    {
                        p.Set(new ElementId(int.Parse(value as string)));
                    }
                    else
                    {
                        p.Set(new ElementId(Convert.ToInt32(value)));
                    }
                    break;

                case StorageType.String:
                    p.Set(value.ToString());
                    break;
                }
            }
            catch
            {
                throw new Exception("Invalid Value Input!");
            }
        }
        private void AddValueToRevit(Element element, Fields fields, List <string> validFields)
        {
            string paramName = "";

            try
            {
                foreach (string paramId in validFields)
                {
                    bool   isDouble = false;
                    string strValue = "";
                    double dblValue = 0;

                    if (paramIdMap.ContainsKey(paramId))
                    {
                        paramName = paramIdMap[paramId];
                    }
                    else
                    {
                        continue;
                    }

                    Field field = fields[paramId];
                    if (Convert.IsDBNull(field.Value))
                    {
                        continue;
                    }

                    if (field.Type == 7)
                    {
                        isDouble = true;
                        dblValue = field.Value;
                        if (decimalPlaces.ContainsKey(field.Name))
                        {
                            dblValue = Math.Round(dblValue, decimalPlaces[field.Name]);
                        }
                    }
                    else
                    {
                        isDouble = false; strValue = field.Value;
                    }

                    if (strValue == "NULL" || strValue == string.Empty)
                    {
                        continue;
                    }

#if RELEASE2013 || RELEASE2014
                    Autodesk.Revit.DB.Parameter parameter = element.get_Parameter(paramName);
#elif RELEASE2015 || RELEASE2016 || RELEASE2017 || RELEASE2018
                    Autodesk.Revit.DB.Parameter parameter = element.LookupParameter(paramName);
#endif

                    if (null != parameter && !parameter.IsReadOnly)
                    {
                        switch (parameter.StorageType)
                        {
                        case StorageType.Double:
                            if (isDouble)
                            {
                                parameter.Set(dblValue);
                            }
                            else
                            {
                                parameter.SetValueString(strValue);
                            }
                            //double dblVal = 0;
                            //double.TryParse(value, out dblVal);
                            //parameter.Set(dblVal);
                            break;

                        case StorageType.Integer:
                            int intVal = 0;
                            int.TryParse(strValue, out intVal);
                            parameter.Set(intVal);
                            break;

                        case StorageType.ElementId:
                            //ElementId eId = new ElementId(int.Parse(value));
                            //if (null != eId) { parameter.Set(eId); }
                            break;

                        case StorageType.String:
                            parameter.Set(strValue);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to Add Value into a FamilyInstance (Parameter Name: " + paramName + ")" + ex.Message, "SynchronizeToRevit Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }