Revit二次开发 - 创建共享参数

    /// <summary>
    /// 共享参数服务
    /// </summary>
    class SharedParameterService
    {
        Document _document = null;
        readonly string ShareParamFileName = "visual_ShareParam.txt";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="document"></param>
        /// <param name="shareParamFileName">
        /// 共享文件存储的名称
        /// </param>
        public SharedParameterService(Document document, string shareParamFileName = "")
        {
            System.Diagnostics.Debug.Assert(document != null);

            _document = document;

            if (!string.IsNullOrEmpty(shareParamFileName))
            {
                if (!shareParamFileName.ToLower().EndsWith(".txt"))
                    shareParamFileName += ".txt";

                ShareParamFileName = shareParamFileName;
            }
        }
        /// <summary>
        /// 类别绑定共享参数
        /// </summary>
        /// <param name="cates">类别集合</param>
        /// <param name="paramNames">共享参数名称集合</param>
        /// <param name="groupName">分组名称</param>
        /// <param name="paramType">参数类型</param>
        /// <param name="groupType">分组类型</param>
        /// <param name="isVisible">是否可见</param>
        /// <param name="isReadonly">是否只读</param>
        public void BindShareParam(
            IEnumerable<BuiltInCategory> cates,
            IEnumerable<string> paramNames,
            string groupName,
            ParameterType paramType = ParameterType.Text,
            BuiltInParameterGroup groupType = BuiltInParameterGroup.PG_DATA,
            bool isVisible = true,
            bool isReadonly = false)
        {
            System.Diagnostics.Debug.Assert(cates != null);
            System.Diagnostics.Debug.Assert(paramNames != null);
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(groupName));

            var group = GetGroup(groupName);

            using (var trans = new Transaction(_document, "CreateShareParam"))
            {
                trans.Start();

                //如果找到更新数据
                var paramDefs = paramNames.Select(o => CreateParamDefinition(group, o, paramType, isVisible, isReadonly));
                BindParam(cates, groupType, paramDefs);

                trans.Commit();
            }
        }
        /// <summary>
        /// 类别绑定共享参数
        /// </summary>
        /// <param name="cates">类别集合</param>
        /// <param name="paramName">共享参数名称</param>
        /// <param name="groupName">分组名称</param>
        /// <param name="paramType">参数类型</param>
        /// <param name="groupType">分组类型</param>
        /// <param name="isVisible">是否可见</param>
        /// <param name="isReadonly">是否只读</param>
        public void BindShareParam(
            IEnumerable<BuiltInCategory> cates,
            string paramName,
            string groupName,
            ParameterType paramType = ParameterType.Text,
            BuiltInParameterGroup groupType = BuiltInParameterGroup.PG_DATA,
            bool isVisible = true,
            bool isReadonly = false)
        {
            System.Diagnostics.Debug.Assert(cates != null);
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(paramName));
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(groupName));

            BindShareParam(cates, new List<string> { paramName }, groupName, paramType, groupType, isVisible, isReadonly);
        }

        public void RemoveShareParam(IEnumerable<BuiltInCategory> cates, IEnumerable<string> paramNames)
        {
            System.Diagnostics.Debug.Assert(cates != null);
            System.Diagnostics.Debug.Assert(paramNames != null);

            var definitionFile = GetOrCreateSharedParamsFile(_document.Application);

            var paramsList = paramNames.ToList();
            var findList = new List<Definition>();
            foreach (var group in definitionFile.Groups)
                foreach (var item in group.Definitions)
                    if (paramsList.Exists(o => o == item.Name))
                        findList.Add(item);

            var cateSet = ToCategory(cates);

            using (var trans = new Transaction(_document, "RemoveShareParam"))
            {
                trans.Start();

                findList.ForEach(o => RemoveShareParam(cateSet, o));

                trans.Commit();
            }
        }
        public void RemoveShareParam(IEnumerable<BuiltInCategory> cates, string paramName)
        {
            System.Diagnostics.Debug.Assert(cates != null);
            System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(paramName));

            RemoveShareParam(cates, new List<string>() { paramName });
        }
        void RemoveShareParam(CategorySet cateSet, Definition definition)
        {
            if (definition != null)
            {
                var binding = _document.ParameterBindings.get_Item(definition);
                var insBinding = binding as InstanceBinding;
                if (insBinding != null)
                {
                    foreach (Category cate in cateSet)
                        if (insBinding.Categories.Contains(cate))
                            insBinding.Categories.Erase(cate);

                    if (insBinding.Categories.Size > 0)
                        _document.ParameterBindings.ReInsert(definition, insBinding, definition.ParameterGroup);
                    else
                        _document.ParameterBindings.Remove(definition);
                }
            }
        }
        DefinitionGroup GetGroup(string groupName)
        {
            var definitionFile = GetOrCreateSharedParamsFile(_document.Application);

            return GetOrCreateSharedParamsGroup(definitionFile, groupName);
        }
        DefinitionFile GetOrCreateSharedParamsFile(Autodesk.Revit.ApplicationServices.Application app)
        {
            var asm = System.Reflection.Assembly.GetExecutingAssembly();
            var path = Path.GetDirectoryName(asm.Location);
            path = Path.Combine(path, ShareParamFileName);

            if (!File.Exists(path))
                using (var fs = new FileStream(path, FileMode.Create))
                using (var sw = new StreamWriter(fs))
                {
                    sw.Close();
                    fs.Close();
                }

            app.SharedParametersFilename = path;

            DefinitionFile file = app.OpenSharedParameterFile();
            if (file == null)
                throw new Exception(string.Format("打开共享文件失败,{0}", path));

            return file;
        }
        DefinitionGroup GetOrCreateSharedParamsGroup(DefinitionFile file, string groupName)
        {
            var definGroup = file.Groups.get_Item(groupName);
            if (null == definGroup)
                definGroup = file.Groups.Create(groupName);

            if (definGroup == null)
                throw new Exception(string.Format("读取或创建共享参数组失败。groupName: {0}", groupName));

            return definGroup;
        }
        Tuple<Definition, bool> CreateParamDefinition(
            DefinitionGroup group,
            string paramName,
            ParameterType paramType,
            bool isVisible,
            bool isReadonly)
        {
            var definition = group.Definitions.get_Item(paramName);
            bool exist = definition != null;
            //存在就不管了
            if (!exist)
            {
#if REVIT2014 || REVIT2015
                var opt = new ExternalDefinitonCreationOptions(paramName, paramType);
                opt.UserModifiable = !isReadonly;
                opt.Visible = isVisible;

                definition = group.Definitions.Create(opt);

                if (definition == null)
                    throw new Exception(string.Format("创建参数失败! {0}", paramName));
#else
                var opt = new ExternalDefinitionCreationOptions(paramName, paramType);
                opt.UserModifiable = !isReadonly;
                opt.Visible = isVisible;

                definition = group.Definitions.Create(opt);

                if (definition == null)
                    throw new Exception(string.Format("创建参数失败! {0}", paramName));
#endif
            }

            return new Tuple<Definition, bool>(definition, exist);
        }
        void BindParam(IEnumerable<BuiltInCategory> cates, BuiltInParameterGroup groupType, IEnumerable<Tuple<Definition, bool>> definitions)
        {
            var cateSet = ToCategory(cates);

            foreach (var def in definitions)
            {
                var insBinding = _document.ParameterBindings.get_Item(def.Item1) as InstanceBinding;
                bool insBindingExist = insBinding != null;
                if (!insBindingExist)
                    insBinding = _document.Application.Create.NewInstanceBinding(cateSet);
                else
                    foreach (Category cate in cateSet)
                        if (!insBinding.Categories.Contains(cate))
                            insBinding.Categories.Insert(cate);

                bool ret = def.Item2 && insBindingExist ?
                    _document.ParameterBindings.ReInsert(def.Item1, insBinding, groupType) :
                    _document.ParameterBindings.Insert(def.Item1, insBinding, groupType);

                if (!ret)
                    throw new Exception(string.Format("绑定参数 \"{0}\" 失败!", def.Item1.Name));
            }
        }
        CategorySet ToCategory(IEnumerable<BuiltInCategory> cates)
        {
            var cateSet = new CategorySet();

            //如果没有找到Category对象, 抛异常
            foreach (var cate in cates.Select(o => _document.Settings.Categories.get_Item(o)))
                cateSet.Insert(cate);

            return cateSet;
        }
    }

可以用上述接口创建只读的共享参数,注意:上诉接口创建的是实例的共享参数,稍稍改改就能创建类型的共享参数了。

相关推荐
<p> <b><span style="font-size:14px;"></span><span style="font-size:14px;background-color:#FFE500;">【Java面试宝典】</span></b><br /> <span style="font-size:14px;">1、68讲视频课,500道大厂Java常见面试题+100个Java面试技巧与答题公式+10万字核心知识解析+授课老师1对1面试指导+无限次回放</span><br /> <span style="font-size:14px;">2、这门课程基于胡书敏老师8年Java面试经验,调研近百家互联网公司及面试官的问题打造而成,从筛选简历和面试官角度,给出能帮助候选人能面试成功的面试技巧。</span><br /> <span style="font-size:14px;">3、通过学习这门课程,你能系统掌握Java核心、数据库、Java框架、分布式组件、Java简历准备、面试实战技巧等面试必考知识点。</span><br /> <span style="font-size:14px;">4、知识点+项目经验案例,每一个都能做为面试的作品展现。</span><br /> <span style="font-size:14px;">5、本课程已经在线下的培训课程中经过实际检验,老师每次培训结束后,都能帮助同学们运用面试技巧,成功找到更好的工作。</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【超人气讲师】</b></span><br /> <span style="font-size:14px;">胡书敏 | 10年大厂工作经验,8年Java面试官经验,5年线下Java职业培训经验,5年架构师经验</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【报名须知】</b></span><br /> <span style="font-size:14px;">上课模式是什么?</span><br /> <span style="font-size:14px;">课程采取录播模式,课程永久有效,可无限次观看</span><br /> <span style="font-size:14px;">课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化</span><br /> <br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><strong>如何开始学习?</strong></span><br /> <span style="font-size:14px;">PC端:报名成功后可以直接进入课程学习</span><br /> <span style="font-size:14px;">移动端:<span style="font-family:Helvetica;font-size:14px;background-color:#FFFFFF;">CSDN 学院APP(注意不是CSDN APP哦)</span></span> </p>
<p> <span style="color:#337FE5;"><strong>【为什么还需要学习C++?】</strong></span> </p> <p style="margin-left:0cm;"> 你是否接触很多语言,但从来没有了解过编程语言的本质? </p> <p style="margin-left:0cm;text-align:start;"> 你是否想成为一名资深开发人员,想开发别人做不了的高性能程序? </p> <p style="margin-left:0cm;text-align:start;"> 你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? </p> <p style="margin-left:0cm;text-align:start;">   </p> <p style="margin-left:0cm;text-align:start;"> 那么C++就是你个人能力提升,职业之路进阶的不二之选。 </p> <p style="margin-left:0cm;text-align:start;"> <br /> </p> <p style="margin-left:0cm;text-align:start;"> <br /> </p> <p style="margin-left:0cm;"> <strong><span style="color:#337FE5;">【课程特色】</span></strong> </p> <p style="margin-left:0cm;text-align:start;"> 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。 </p> <p style="margin-left:0cm;text-align:start;"> 2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。 </p> <p style="margin-left:0cm;text-align:start;"> 3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。 </p> <p style="margin-left:0cm;text-align:start;"> <br /> </p> <p class="ql-long-24357476"> <span style="color:#337FE5;"><strong>【学完后我将达到什么水平?】</strong></span> </p> <p class="ql-long-24357476"> 1.对C++的各个知识能够熟练配置、开发、部署; </p> <p class="ql-long-24357476"> 2.吊打一切关于C++的笔试面试题; </p> <p class="ql-long-24357476"> 3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。 </p> <p class="MsoNoSpacing" style="margin-left:18pt;"> <br /> </p> <div> <br /> </div> <p> <br /> </p> <p style="margin-left:0cm;text-align:start;"> <span style="color:#337FE5;"><strong>【面向人群】</strong></span> </p> <p style="margin-left:0cm;text-align:start;"> <span style="color:#222226;font-family:PingFangSC-Regular, "font-size:14px;background-color:#FFFFFF;">1.希望一站式快速入门的C++初学者;</span> </p> <p style="margin-left:0cm;text-align:start;"> <span style="color:#222226;font-family:PingFangSC-Regular, "font-size:14px;background-color:#FFFFFF;">2.希望快速学习 C++、掌握编程要义、修炼内功的开发者;</span> </p> <p style="margin-left:0cm;text-align:start;"> <span style="color:#222226;font-family:PingFangSC-Regular, "font-size:14px;background-color:#FFFFFF;">3.有志于挑战更高级的开发项目,成为资深开发的工程师。</span> </p> <p style="margin-left:0cm;text-align:start;"> <br /> </p> <p> <br /> </p> <p> <span style="color:#337FE5;"><strong>【课程设计】</strong></span> </p> <p> 本课程包含3大模块 </p> <p> <strong>基础篇</strong><br /> 本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。 </p> <p> <br /> <strong>进阶篇</strong><br /> 本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。 </p> <p> <br /> <strong>提升篇:</strong><br /> 本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。 </p> <p> <img src="https://img-bss.csdnimg.cn/202007091130239667.png" alt="" /> </p>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页