Revit二次开发_对参数进行文本替换、加前缀、加后缀

我一般都用Dynamo做简单的参数批量修改,但也不能要求项目上每个人都会用Dynamo,这需求偶尔又会有,于是写成插件。

想慢慢写成一个通用一点的参数管理工具,批量写入或者修改什么的,看精力了。

目前功能还是针对特定参数进行文本替换、加前缀、加后缀。

Revit二次开发_对参数进行文本替换、加前缀、加后缀_第1张图片

代码:

    class ModifyValue : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document document = commandData.Application.ActiveUIDocument.Document;

            ModifyValueForm uiForm = new ModifyValueForm(document);
            System.Windows.Interop.WindowInteropHelper thisForm = new System.Windows.Interop.WindowInteropHelper(uiForm)
            {
                Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle
            };
            if (uiForm.ShowDialog() == true)
            {
                using (Transaction tran = new Transaction(document, "修改 参数值"))
                {
                    tran.Start();

                    //0——文本替换
                    //1——加前缀
                    //2——加后缀
                    switch (uiForm.ModifyType)
                    {
                        case 0:
                            foreach (Element element in uiForm.GetElements)
                            {
                                Parameter parameter = element.get_Parameter(uiForm.ParameterDefinition);
                                if (parameter != null)
                                {
                                    parameter.Set(parameter.AsString().Replace(uiForm.Text1, uiForm.Text2));
                                }

                            }
                            break;
                        case 1:
                            foreach (Element element in uiForm.GetElements)
                            {
                                Parameter parameter = element.get_Parameter(uiForm.ParameterDefinition);
                                if (parameter != null)
                                {
                                    if (parameter.AsString() != "")
                                    {
                                        parameter.Set(uiForm.Text1);
                                    }
                                    else
                                    {
                                        parameter.Set(parameter.AsString().Insert(0, uiForm.Text1));
                                    }

                                }
                            }
                            break;
                        case 2:
                            foreach (Element element in uiForm.GetElements)
                            {
                                Parameter parameter = element.get_Parameter(uiForm.ParameterDefinition);
                                if (parameter != null)
                                {
                                    parameter.Set(parameter.AsString() + uiForm.Text1);
                                }
                            }
                            break;
                    }

                    tran.Commit();
                }
            }

            return Result.Succeeded;
        }

    }

WPF XAML部分


    
        
            
            
            
        
        
            
            
            
            
        
        
            
        
            
        
            
        
            
            
        
        
            

WPF CS部分

    public partial class ModifyValueForm : Window
    {
        Document _document;
        List categories = new List();
        ObservableCollection parameters = new ObservableCollection();
        ObservableCollection viewObjects = new ObservableCollection();

        public ModifyValueForm(Document document)
        {
            InitializeComponent();

            _document = document;

            //读取所有类别
            foreach (Category category in _document.Settings.Categories)
            {
                if (category.Id.IntegerValue < 0)
                {
                    FilteredElementCollector cate_Type = new FilteredElementCollector(_document).OfCategory((BuiltInCategory)category.Id.IntegerValue).WhereElementIsElementType();
                    FilteredElementCollector cate_Ins = new FilteredElementCollector(_document).OfCategory((BuiltInCategory)category.Id.IntegerValue).WhereElementIsNotElementType();
                    if (cate_Type.Count() == 0 && cate_Ins.Count() == 0) continue;

                    categories.Add(category);
                }
            }
            categories.Sort((x, y) => x.Name.CompareTo(y.Name));

            Sel_Category.ItemsSource = categories;
            Sel_Parameter.ItemsSource = parameters;
            View_Objects.ItemsSource = viewObjects;
        }

        private void Click_Ok_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        //选择类别后更新数据
        private void Sel_Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Sel_Category.SelectedItem != null)
            {
                Sel_ParameterType.IsEnabled = true;
            }
            RefreshObjects();
            RefreshParameter();
        }

        //选择参数类型后更新数据
        private void Sel_ParameterType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Sel_ParameterType != null)
            {
                Sel_Parameter.IsEnabled = true;
            }
            RefreshObjects();
            RefreshParameter();

        }

        //选择参数后允许设置修改模式
        private void Sel_Parameter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Sel_Parameter.SelectedItem != null)
            {
                Sel_ModifyType.IsEnabled = true;
                if (Sel_ModifyType.SelectedIndex == 0)
                {
                    Input_Text1.IsEnabled = Input_Text2.IsEnabled = true;
                }
                else
                {
                    Input_Text1.IsEnabled = true;
                    Input_Text2.IsEnabled = false;
                }
            }
            else
            {
                Sel_ModifyType.IsEnabled = Input_Text1.IsEnabled = Input_Text2.IsEnabled = false;
            }
        }

        //选择对象后刷新参数选项
        private void View_Objects_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshParameter();
        }

        //设置修改模式后允许输入文本框
        private void Sel_ModifyType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Sel_ModifyType.IsEnabled)
            {
                if (Sel_ModifyType.SelectedIndex == 0)
                {
                    Input_Text1.IsEnabled = Input_Text2.IsEnabled = true;
                }
                else
                {
                    Input_Text1.IsEnabled = true;
                    Input_Text2.IsEnabled = false;
                }
            }
            //else
            //{
            //    Sel_ModifyType.IsEnabled = Input_Text1.IsEnabled = Input_Text2.IsEnabled = false;
            //}
        }

        //刷新参数选项
        private void RefreshParameter()
        {
            if (Sel_ParameterType.SelectedItem != null)
            {
                parameters.Clear();
                //如果选择了某个对象,使用此对象的参数,否则使用所有对象的共有参数
                if (View_Objects.SelectedItem != null)
                {
                    ViewObject viewObject = View_Objects.SelectedItem as ViewObject;
                    foreach (Parameter parameter in viewObject.Element.Parameters)
                    {
                        if (!parameter.IsReadOnly)
                        {
                            if (parameter.StorageType == StorageType.String)
                            {
                                parameters.Add(parameter);
                            }
                        }
                    }
                }
                else
                {
                    List set1 = new List();
                    foreach (ViewObject viewObject in View_Objects.Items)
                    {
                        List set2 = new List();
                        foreach (Parameter parameter in viewObject.Element.Parameters)
                        {
                            if (!parameter.IsReadOnly)
                            {
                                if (parameter.StorageType == StorageType.String)
                                {
                                    set2.Add(parameter.Id.IntegerValue);
                                }
                            }
                        }
                        if (set1.Count == 0)
                        {
                            set1 = set2.ToList();
                        }
                        else
                        {
                            set1 = set1.Intersect(set2).ToList();
                        }
                    }
                    foreach (int i in set1)
                    {
                        foreach (ViewObject viewObject in View_Objects.Items)
                        {
                            foreach (Parameter parameter in viewObject.Element.Parameters)
                            {
                                if (!parameter.IsReadOnly && parameter.StorageType == StorageType.String && parameter.Id.IntegerValue == i)
                                {
                                    parameters.Add(parameter);
                                    break;
                                }
                            }
                            break;
                        }

                    }
                }
            }
        }

        //刷新对象
        private void RefreshObjects()
        {
            if (Sel_Category.SelectedItem != null && Sel_ParameterType != null)
            {
                viewObjects.Clear();

                FilteredElementCollector elements = new FilteredElementCollector(_document);
                Category selCategory = Sel_Category.SelectedItem as Category;
                elements.OfCategory((BuiltInCategory)selCategory.Id.IntegerValue);
                if (Sel_ParameterType.SelectedIndex == 0)
                {
                    foreach (ElementType elementType in elements.WhereElementIsElementType())
                    {
                        ViewObject viewObject = new ViewObject(elementType);
                        viewObjects.Add(viewObject);
                    }
                }
                if (Sel_ParameterType.SelectedIndex == 1)
                {
                    foreach (Element element in elements.WhereElementIsNotElementType())
                    {
                        ViewObject viewObject = new ViewObject(element);
                        viewObjects.Add(viewObject);
                    }
                }
            }

        }

        #region 公共属性

        public List GetElements
        {
            get
            {
                List elements = new List();
                foreach (ViewObject vo in View_Objects.Items)
                {
                    elements.Add(vo.Element);
                }
                return elements;
            }
        }

        public Definition ParameterDefinition
        {
            get
            {
                if (Sel_Parameter.SelectedItem != null)
                {
                    Parameter parameter = Sel_Parameter.SelectedItem as Parameter;
                    return parameter.Definition;
                }
                return null;
            }
        }

        public int ModifyType
        {
            get
            {
                if (Sel_ModifyType.IsEnabled)
                {
                    return Sel_ModifyType.SelectedIndex;
                }
                return -1;
            }
        }

        public string Text1
        {
            get
            {
                if (Input_Text1.IsEnabled)
                {
                    return Input_Text1.Text;
                }
                return null;
            }
        }

        public string Text2
        {
            get
            {
                if (Input_Text2.IsEnabled)
                {
                    return Input_Text2.Text;
                }
                return null;
            }
        }

        #endregion
    }

    class ViewObject
    {
        public ViewObject(ElementType elementType)
        {
            KeyInformation = elementType.Id + " " + elementType.FamilyName + " : " + elementType.Name;
            Element = elementType;
        }

        public ViewObject(Element element)
        {
            KeyInformation = element.Id + " " + element.Name;
            Element = element;
        }

        public string KeyInformation { get; set; }
        public Element Element { get; set; }
    }

 

你可能感兴趣的:(Revit二次开发)