Sample custom immutable class used to represents Revit internal FilterRule. This class and its variables will help display the contents of UI controls. This class can build its data caches to Revit FilterRule object.
        /// <summary>
        /// Enable or disable some buttons according to current status/values of controls
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ViewFiltersForm_MouseMove(object sender, MouseEventArgs e)
        {
            // Enable and disable some buttons:
            // . If current selected parameter in rulesListBox equals to parameter in Parameter ComboBox:
            // Update/Delete is allowed; Otherwise, New and delete is allowed.
            // . If rulesListBox has no any rule, only New button is allowed
            FilterRuleBuilder currentRule = GetCurrentRuleData();

            if (null == currentRule)
            {
                updateButton.Enabled = deleRuleButton.Enabled = false;
                if (!HasFilterData() || null == paramerComboBox.SelectedItem ||
                    (paramerComboBox.SelectedItem as String) == m_noneParam)
                {
                    newRuleButton.Enabled = false;
                }
                else
                {
                    newRuleButton.Enabled = true;
                }
            }
            else
            {
                BuiltInParameter selParam       = currentRule.Parameter;
                BuiltInParameter selComboxParam = EnumParseUtility <BuiltInParameter> .Parse(paramerComboBox.SelectedItem as String);

                bool paramEquals = (selParam == selComboxParam);
                //
                // new button is available only when user select new parameter
                newRuleButton.Enabled = !paramEquals;
                updateButton.Enabled  = deleRuleButton.Enabled = paramEquals;
            }
        }
        /// <summary>
        /// Update change rule criteria and values for current filter rule
        /// Filter criteria and values won't be changed until you click this button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void updateButton_Click(object sender, EventArgs e)
        {
            FilterRuleBuilder currentRule = GetCurrentRuleData();

            if (null == currentRule)
            {
                return;
            }
            BuiltInParameter  selParam = currentRule.Parameter;
            FilterRuleBuilder newRule  = CreateNewFilterRule(selParam);

            if (null == newRule)
            {
                return;
            }
            int oldRuleIndex = GetCurrentRuleDataIndex();

            if (oldRuleIndex > m_currentFilterData.RuleData.Count)
            {
                return;
            }
            //
            // Update rule value
            m_currentFilterData.RuleData[oldRuleIndex] = newRule;
        }
        /// <summary>
        /// Create one new rule
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newRuleButton_Click(object sender, EventArgs e)
        {
            if (!HasFilterData() || paramerComboBox.SelectedText == m_noneParam)
            {
                return;
            }
            //
            // check if rule value is specified or exist
            BuiltInParameter curParam = EnumParseUtility <BuiltInParameter> .Parse(paramerComboBox.SelectedItem as String);

            if (ParameterAlreadyExist(curParam))
            {
                MyMessageBox("Filter rule for this parameter already exists, no sense to add new rule for this parameter again.");
                return;
            }
            FilterRuleBuilder newRule = CreateNewFilterRule(curParam);

            if (null == newRule)
            {
                return;
            }
            //
            // Create and reserve this rule and reset controls
            m_currentFilterData.RuleData.Add(newRule);
            String ruleName = String.Format(String.Format("{0} {1}", m_ruleNamePrefix, rulesListBox.Items.Count + 1));

            rulesListBox.Items.Add(ruleName);
            rulesListBox.SelectedIndex = rulesListBox.Items.Count - 1;
            rulesListBox.Enabled       = true;
            ViewFiltersForm_MouseMove(null, null);
        }
Example #4
0
        /// <summary>
        /// Select parameter for current filter rule
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void paramerComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get criteria for selected parameter and reset rule criteria and rule values
            String selectItem = paramerComboBox.SelectedItem.ToString();
            bool   isNone     = (selectItem == m_noneParam);

            criteriaComboBox.Enabled = ruleValueComboBox.Enabled = newRuleButton.Enabled = !isNone;
            if (isNone)// is (none) selected
            {
                ResetControlByParamType(StorageType.None);
                return;
            }
            //
            // Check to see if this parameter is in use:
            // Switch to this parameter if parameter is in use, and reset controls with criteria and value for this parameter.
            BuiltInParameter curParam = EnumParseUtility <BuiltInParameter> .Parse(selectItem);

            StorageType paramType = m_doc.get_TypeOfStorage(curParam);

            ResetControlByParamType(paramType);
            ParameterInUse(curParam);
            //
            // New parameter was selected, reset controls with new criteria
            ICollection <String> possibleCritias = RuleCriteraNames.Criterions(paramType);

            criteriaComboBox.Items.Clear();
            foreach (String criteria in possibleCritias)
            {
                criteriaComboBox.Items.Add(criteria);
            }
            //
            // Display parameter values for current filter rule,
            // If current selected parameter equal to current filter rule's, reset controls with rule data
            FilterRuleBuilder currentRule = GetCurrentRuleData();

            if (null != currentRule && currentRule.Parameter == curParam)
            {
                criteriaComboBox.SelectedItem = currentRule.RuleCriteria;
                ruleValueComboBox.Text        = currentRule.RuleValue;
                caseSensitiveCheckBox.Checked = currentRule.CaseSensitive;
                epsilonTextBox.Text           = String.Format("{0:N6}", currentRule.Epsilon);
            }
            else
            {
                // set with default value
                criteriaComboBox.SelectedIndex = 0;
                ruleValueComboBox.Text         = String.Empty;
                caseSensitiveCheckBox.Checked  = false;
                epsilonTextBox.Text            = "0.0";
            }
        }
        /// <summary>
        /// Select one filter rule
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rulesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // do nothing if no any filter yet
            if (!HasFilterData())
            {
                return;
            }
            //
            // Change parameter for selected filter rule
            FilterRuleBuilder currentRule = GetCurrentRuleData();

            if (null == currentRule)
            {
                return;
            }
            String paramName = EnumParseUtility <BuiltInParameter> .Parse(currentRule.Parameter);

            paramerComboBox.SelectedItem = paramName;
        }
        /// <summary>
        /// Initialize filter data with existing view filters
        /// </summary>
        void InitializeFilterData()
        {
            // Get all existing filters
            ICollection <ParameterFilterElement> filters = FiltersUtil.GetViewFilters(m_doc);

            foreach (ParameterFilterElement filter in filters)
            {
                // Get all data of the current filter and create my FilterData
                ICollection <ElementId> catIds = filter.GetCategories();

                // Get the ElementFilter representing the set of FilterRules.
                ElementFilter elemFilter = filter.GetElementFilter();
                // Check that the ElementFilter represents a conjunction of ElementFilters.
                // We will then check that each child ElementFilter contains just one FilterRule.
                IList <FilterRule> filterRules = FiltersUtil.GetConjunctionOfFilterRulesFromElementFilter(elemFilter);
                int numFilterRules             = filterRules.Count;
                if (0 == numFilterRules)
                {
                    return; // Error
                }
                // Create filter rule data now
                List <FilterRuleBuilder> ruleDataSet = new List <FilterRuleBuilder>();
                foreach (FilterRule filterRule in filterRules)
                {
                    ElementId         paramId          = filterRule.GetRuleParameter();
                    int               parameterIdAsInt = paramId.IntegerValue;
                    BuiltInParameter  bip      = (BuiltInParameter)parameterIdAsInt;
                    FilterRuleBuilder ruleData = FiltersUtil.CreateFilterRuleBuilder(bip, filterRule);
                    ruleDataSet.Add(ruleData);
                }

                //
                // Create Filter data
                FilterData filterData = new FilterData(m_doc, catIds, ruleDataSet);
                m_dictFilters.Add(filter.Name, filterData);
                //
                // also add to control
                filtersListBox.Items.Add(filter.Name);
            }
        }
Example #7
0
        /// <summary>
        /// Initialize filter data with existing view filters
        /// </summary>
        void InitializeFilterData()
        {
            // Get all existing filters
            ICollection <ParameterFilterElement> filters = FiltersUtil.GetViewFilters(m_doc);

            foreach (ParameterFilterElement filter in filters)
            {
                // get all data of current filter and create my FilterData
                ICollection <ElementId>  catIds   = filter.GetCategories();
                ICollection <ElementId>  paramIds = filter.GetRuleParameters();
                ICollection <FilterRule> rules    = filter.GetRules();
                if (paramIds.Count != rules.Count)
                {
                    return;
                }
                //
                // create filter rule data now
                List <FilterRuleBuilder> ruleDataSet = new List <FilterRuleBuilder>();
                IEnumerator <ElementId>  paramIter   = paramIds.GetEnumerator();
                paramIter.Reset();
                foreach (FilterRule rule in rules)
                {
                    paramIter.MoveNext();
                    FilterRuleBuilder ruleData = FiltersUtil.CreateFilterRuleBuilder(
                        (BuiltInParameter)paramIter.Current.IntegerValue, rule);
                    ruleDataSet.Add(ruleData);
                }
                //
                // Create Filter data
                FilterData filterData = new FilterData(m_doc, catIds, ruleDataSet);
                m_dictFilters.Add(filter.Name, filterData);
                //
                // also add to control
                filtersListBox.Items.Add(filter.Name);
            }
        }