zoukankan      html  css  js  c++  java
  • Revit创建共享参数和获取项目参数

    Revit除了有内建的参数,还允许用户自定义参数,分别是:共享参数和项目参数。

    对应界面上的菜单分别是"管理->共享参数"和"管理->项目参数"

    共享参数被定义在Revit之外的一个共享参数文件(.txt中)。删掉这个文件,共享参数就会丢失。

    程序入口代码:

     public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                Autodesk.Revit.ApplicationServices.Application revitApp = commandData.Application.ActiveUIDocument.Application.Application;
                Autodesk.Revit.DB.Document revirDoc = commandData.Application.ActiveUIDocument.Document;
                TaskDialog.Show("Revit", GetSharInfo(revitApp));
                return Result.Succeeded;
            }

    1.获取共享参数

            /// <summary>
            /// 获取共享参数信息
            /// </summary>
            /// <param name="commandData"></param>
            /// <returns></returns>
            private string GetSharInfo(Autodesk.Revit.ApplicationServices.Application revitApp)
            {
                StringBuilder str = new StringBuilder();
                DefinitionFile definitionFile = revitApp.OpenSharedParameterFile();
                DefinitionGroups groups = definitionFile.Groups;
                foreach (DefinitionGroup group in groups)
                {
                    foreach (Definition definition in group.Definitions)
                    {
                        string name = definition.Name;
                        ParameterType type = definition.ParameterType;
                        str.AppendLine(string.Format("{0}---{1}", name, type.ToString()));
                    }
                }
                return str.ToString();
            }

    2.创建共享参数

     /// <summary>
            /// 创建共享参数
            /// </summary>
            /// <param name="commandData"></param>
            private void CreateSharInfo(Autodesk.Revit.ApplicationServices.Application revitApp, Autodesk.Revit.DB.Document revitDoc)
            {
                try
                {
                    string sharedParametersFilename = @"C:shared-parameters.txt";
                    string groupName = "MyGroup";
                    string definitionName = "MyDefinition";
                    ParameterType parameterType = ParameterType.Text;
                    CategorySet categorySet = new CategorySet();
                    Category wallCategory = revitDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Walls);
                    categorySet.Insert(wallCategory);
                    bool instanceParameter = true;
                    BuiltInParameterGroup parameterGroup = BuiltInParameterGroup.PG_DATA;
                    if (!System.IO.File.Exists(sharedParametersFilename))
                    {
                        try
                        {
                            System.IO.StreamWriter sw = System.IO.File.CreateText(sharedParametersFilename);
                            sw.Close();
                        }
                        catch (Exception)
                        {
                            throw new Exception(string.Format("Can't create shared parameter file:{0}", sharedParametersFilename));
                        }
                    }
                    revitApp.SharedParametersFilename = sharedParametersFilename;
    
                    DefinitionFile definitionFile = revitApp.OpenSharedParameterFile();
                    if (definitionFile == null)
                    {
                        throw new Exception("Can not open shared parameter file");
                    }
    
                    DefinitionGroups groups = definitionFile.Groups;
                    DefinitionGroup group = groups.get_Item(groupName);
                    if (group == null)
                    {
                        group = groups.Create(groupName);
                    }
                    Definition definition = group.Definitions.get_Item(definitionName);
                    if (definition == null)
                    {
                        definition = group.Definitions.Create(new ExternalDefinitionCreationOptions(definitionName, parameterType));
                    }
                    ElementBinding binding = null;
                    if (instanceParameter)
                    {
                        binding = revitApp.Create.NewInstanceBinding(categorySet);
                    }
                    else
                    {
                        binding = revitApp.Create.NewTypeBinding(categorySet);
                    }
                    bool insertSuccess = revitDoc.ParameterBindings.Insert(definition, binding, parameterGroup);
                    if (!insertSuccess)
                    {
                        throw new Exception("Failed to bind definition to category");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
            }

    3.获取项目参数(API无法创建项目参数的)

     /// <summary>
            /// 获取项目参数(API无法创建项目参数)
            /// </summary>
            /// <param name="revitDoc"></param>
            /// <returns></returns>
            private string GetProjectParam(Autodesk.Revit.DB.Document revitDoc)
            {
                StringBuilder strBuild = new StringBuilder();
                BindingMap map = revitDoc.ParameterBindings;
                DefinitionBindingMapIterator dep = map.ForwardIterator();
                while (dep.MoveNext())
                {
                    Definition definition = dep.Key;
                    string definitionName = definition.Name;
                    ParameterType parameterType = definition.ParameterType;
                    InstanceBinding instanceBinding = dep.Current as InstanceBinding;
                    if (instanceBinding != null)
                    {
                        CategorySet categorySet = instanceBinding.Categories;
                        strBuild.AppendLine(string.Format("Name:{0} Type:{1} CategorySet:{2}", definitionName, parameterType, categorySet.GetType().ToString()));
                    }
                    else
                    {
                        strBuild.AppendLine(string.Format("Name:{0} Type:{1}", definitionName, parameterType));
                    }
                }
                return strBuild.ToString();
            }
  • 相关阅读:
    微人事项目-mybatis-持久层
    通过外键连接多个表
    springioc
    Redis 消息中间件 ServiceStack.Redis 轻量级
    深度数据对接 链接服务器 数据传输
    sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取
    sqlserver 索引优化 CPU占用过高 执行分析 服务器检查
    sql server 远程备份 bak 删除
    冒泡排序
    多线程 异步 beginInvoke EndInvoke 使用
  • 原文地址:https://www.cnblogs.com/hualuohuakaihuamanyuan/p/5345250.html
Copyright © 2011-2022 走看看