Esempio n. 1
0
        /// <summary>
        /// Initializes element or type parameter binding.
        /// </summary>
        /// <param name="uiapp"></param>
        /// <param name="elm"></param>
        /// <param name="paramPath"></param>
        public static void InitParamList(this UIApplication uiapp, Element elm, string paramPath)
        {
            if (uiapp == null)
            {
                throw new ArgumentNullException(nameof(uiapp));
            }

            if (elm == null)
            {
                throw new ArgumentNullException(nameof(elm));
            }

            if (paramPath == null)
            {
                throw new ArgumentNullException(nameof(paramPath));
            }

            var doc        = uiapp.ActiveUIDocument.Document;
            var bindingMap = doc.ParameterBindings;
            var gs         = uiapp.GetGroupList(paramPath);
            var elmCtgs    = new CategorySet();

            elmCtgs.Insert(elm.Category);

            foreach (var group in GetGroups(paramPath))
            {
                var paramGroup = gs.GetGroup(group.GroupName);

                foreach (var param in group.Params)
                {
                    var definition = paramGroup.GetDefinition(param.ParamName, param.CanEdit);
                    var binding    = bindingMap.get_Item(definition);

                    // If the parameter group's name contains type key, it's means type binding.
                    if (!paramGroup.Name.Contains("Group"))
                    {
                        if (binding is InstanceBinding instanceBinding)
                        {
                            bindingMap.ReInsert(definition, instanceBinding);
                        }
                        else
                        {
                            instanceBinding = uiapp.Application.Create.NewInstanceBinding(elmCtgs);
                            bindingMap.Insert(definition, instanceBinding);
                        }
                    }
                    else
                    {
                        if (binding is TypeBinding typeBinding)
                        {
                            bindingMap.ReInsert(definition, typeBinding);
                        }
                        else
                        {
                            typeBinding = uiapp.Application.Create.NewTypeBinding(elmCtgs);
                            bindingMap.Insert(definition, typeBinding);
                        }
                    }
                }
            }
        }