Revit二次开发之创建共享参数及绑定共享参数【比目鱼原创】

=========【更多高级应用请关注公众号】========


===================================

无论是手工创建共享参数,或者用代码创建,都需要一个共享参数的文件,格式是txt,内容要按照revit定义好的模板才能正确读取。要创建这个共享参数的txt模板文件,可在“”revit-管理-共享参数-创建“”这里面操作。

手动创建并绑定共享参数的流程是:

1、创建或读取共享参数文件(txt)

2、创建共享参数组

3、创建共享参数

4、在“”revit-管理-项目参数-添加-共享参数-选择“操作,然后设置好参数分组方式,设置好绑定的类别(Category)。


PS:共享参数都是一对多的关系,也即:一个共享参数文件下可创建多个共享参数组,组下可创建多个共享参数,共享参数可自定义名称、规程、参数类型。


以下这段代码实现了创建共享参数并写入属性值。

大家也可以在我上传的代码资源中下载试用,地址是http://download.csdn.net/detail/bbkxw001/9856697,支持下


using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;
using System.IO;
using System.Reflection;
using Autodesk.Revit.DB.Events;


namespace CreatSharedParams
{
    [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        Document _doc;
        UIDocument _uidoc;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            _doc = doc;
            _uidoc = uidoc;

            var refe = uidoc.Selection.PickObject(ObjectType.Element, "Please select element");
            Element selectedElement = doc.GetElement(refe) as Element;

            string shapeFile = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\SharedParameterFiles.txt";
            this._doc.Application.SharedParametersFilename = shapeFile;

            Transaction trans = new Transaction(doc, "CreatSharedParams");
            trans.Start();
            CreatShareParam("CSDN【BIM_er】", ParameterType.Text, false, BuiltInParameterGroup.PG_TEXT, selectedElement);
            SetParamValue("CSDN【BIM_er】", "我开发了很多Revit插件和功能", selectedElement);
            trans.Commit();
            return Result.Succeeded;
        }

        public bool CreatShareParam(string parameterName, ParameterType parameterType, bool canModify, BuiltInParameterGroup parameterGroup, Element element)
        {
            if (element.Category == null)
            {
                return false;
            }
            try
            {
                Application app = this._doc.Application;
                DefinitionFile dfile = app.OpenSharedParameterFile();
                DefinitionGroup dg;
                if (!element.Category.AllowsBoundParameters)
                {
                    return false;
                }
                Category category = element.Category;
                try
                {
                    dg = dfile.Groups.Create("测试共享参数组");
                }
                catch (Exception)
                {
                    dg = dfile.Groups.get_Item("测试共享参数组");
                }
                if (dg != null)
                {
                    ExternalDefinitionCreationOptions edco = new ExternalDefinitionCreationOptions(parameterName, parameterType);
                    edco.UserModifiable = canModify;
                    Definition df;
                    try
                    {
                        df = dg.Definitions.Create(edco);
                    }
                    catch (Exception)
                    {
                        df = dg.Definitions.get_Item(edco.Name);
                    }

                    var instanceBinding = _doc.ParameterBindings.get_Item(df) as InstanceBinding;
                    if (instanceBinding != null)
                    {
                        if (!instanceBinding.Categories.Contains(category))
                            instanceBinding.Categories.Insert(category);

                        _doc.ParameterBindings.ReInsert(df, instanceBinding);
                    }
                    else
                    {
                        Autodesk.Revit.DB.CategorySet categorySet = app.Create.NewCategorySet();
                        categorySet.Insert(category);

                        instanceBinding = app.Create.NewInstanceBinding(categorySet);
                        _doc.ParameterBindings.Insert(df, instanceBinding);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                TaskDialog.Show("error in creating parameters:", "name:" + parameterName + "\r\n" + "id:" + element.Id.ToString());
                throw;
            }
        }

        public void SetParamValue(string parameterName, string parameterValue, Element element)
        {
            try
            {
                if (element.LookupParameter(parameterName) != null && element.LookupParameter(parameterName).IsReadOnly == false)
                {
                    element.LookupParameter(parameterName).Set(parameterValue);
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Set Parameter Value Error", ex.ToString());
                return;
            }
        }
    }

}


你可能感兴趣的:(共享参数)