Revit二次开发关于共享参数、项目参数的创建,删除以及简介。

Revit二次开发关于共享参数、项目参数的创建,删除。
在企业二次开发功能中,很多模型需要添加附加参数,这时候就得使用共享参数或者项目参数来实现,在文档中对于可载入族添加参数时,一般都是为族添加共享参数,因为项目参数仅在当前文档中能够使用,无法脱离文档,而族共享参数则可以跟随族文件载入项目而一同添加进入项目,当族载入项目以后,族绑定的共享参数就自动转换为当前的一个项目参数。
关于创建:
项目参数因为手动操作可以看到是与category绑定的,所以创建的时候需要指定参数绑定的类型,而族共享参数的创建就是打开族文档,然后创建一个共享参数,通过族参数管理器FamilyManager添加参数AddParameter();创建参数的同时还得注意一个问题,新建参数绑定成功以后,如果有要求按照指定顺序排列,还需要在族文件中对Parameter使用ReorderParameters()方法进行重新排序。
关于删除:
共享参数的删除相对简单,获取到族以后,编辑族,然后通过族参数管理器FamilyManager中的RemoveParameter()方法移除参数就可以。
项目参数的删除相对复杂,需要获取到当前文档的所有BindingMap,然后遍历Map找到自己需要的Definition然后从Map中删除即可。
代码如下:

///删除项目参数
public void DeleteProjectPara(Element Elem,List<string> DeleteParams,Document doc)
{
	Transaction RemoveAops = new Transaction(doc, "Delete Para");
    RemoveAops.Start();
    List<Definition> RemoveDef = new List<Definition>();
    BindingMap map = doc.ParameterBindings;
    DefinitionBindingMapIterator dep = map.ForwardIterator();
    while (dep.MoveNext())
    {
        Definition definition = dep.Key;
        string definitionName = definition.Name;
        foreach (var item in DeleteParams)
        {
            if (definitionName == item.Definition.Name)
            {
               ParameterType parameterType = definition.ParameterType;
               InstanceBinding instanceBinding = dep.Current as InstanceBinding;
               if (instanceBinding != null)
               {
                  CategorySet categorySet = instanceBinding.Categories;
                  if (categorySet.Contains(Elem.Category))
                  {
                       RemoveDef.Add(definition);
                  }
               }
          }
      }
   }
   foreach (var item in RemoveDef)
   {
     map.Remove(item);
   }
   RemoveAops.Commit();
}

//删除族共享参数
public void DeleteFamilySharePara(Document doc,Family family,List<string> DeleteParams)
{
	Document FamilyDoc = UiDoc.Document.EditFamily(family);
    Transaction tr = new Transaction(FamilyDoc, "Delete Para");
    tr.Start();
    foreach (var item in DeleteParams)
    {
        if (FamilyDoc.FamilyManager.get_Parameter(item.Definition.Name) != null)
         { 
            FamilyDoc.FamilyManager.MakeType(FamilyDoc.FamilyManager.get_Parameter(item.Definition.Name));  
            FamilyDoc.FamilyManager.RemoveParameter(FamilyDoc.FamilyManager.get_Parameter(item.Definition.Name));
          }
     }
     FamilyDoc.LoadFamily(UiDoc.Document, new ProjectFamLoadOption());
     tr.Commit();
     OverEditFamily.Add(family);
}

//创建项目参数
public bool AddNewParameter(Autodesk.Revit.UI.UIApplication _revit, List<string> ParamNameLst, Element CurCategoryElem, BuiltInCategory BuildInCategory)
        {
            bool boundResult = false;
            try
            {
                DefinitionFile informationFile = AccessOrCreateExternalSharedParameterFile(_revit);

                if (null == informationFile)
                {
                    return false;

                }

                DefinitionGroups informationCollections = informationFile.Groups;
                DefinitionGroup informationCollection = null;

                informationCollection = informationCollections.get_Item("Name");

                //revit不允许自定义新增组名
                if (null == informationCollection)//创建组
                {
                    informationCollections.Create("Name");
                    informationCollection = informationCollections.get_Item("Name");
                }

                string zuNameTag = "";

                List<string> CurParams = new List<string>();
                ParameterSet Params = CurCategoryElem.Parameters;
                IEnumerator ParaEnu = Params.GetEnumerator();
                while (ParaEnu.MoveNext())
                {
                    if ((ParaEnu.Current as Parameter).IsShared)
                        CurParams.Add((ParaEnu.Current as Parameter).Definition.Name);
                }

                foreach (string paraNameT in ParamNameLst)
                {

                    string paraName = paraNameT;
                    if (paraNameT.Contains("\n"))
                    {
                        paraName = paraName.Replace("\n", "");
                    }

                    if (CurParams.Contains(paraName)) continue;
                    Definition information = informationCollection.Definitions.get_Item(paraNameT);
                    if (null == information)
                    {

                        ExternalDefinitionCreationOptions externalDefinitionCreationOptions = new ExternalDefinitionCreationOptions(paraName, Autodesk.Revit.DB.ParameterType.Text);
                        if (paraName == "用来分隔的参数,不设置值" )
                        {
                            externalDefinitionCreationOptions.UserModifiable = false;
                            zuNameTag = paraName;
                        }
                        externalDefinitionCreationOptions.Description = zuNameTag;
                        informationCollection.Definitions.Create(externalDefinitionCreationOptions);
                        information = informationCollection.Definitions.get_Item(paraName);

                    }

                    CategorySet categories = _revit.Application.Create.NewCategorySet();

                    //指定类型才添加共享参数名称
                    List<Parameter> ls = FindParameterByDefNameLs(CurCategoryElem.Parameters, paraName);
                    bool bAdd = true;
                    if (ls != null && ls.Count > 0)
                    {
                        if (paraName == "实例" || paraName == "实例编码")
                        {
                            bAdd = false;
                        }
                        else
                        {
                            foreach (var pa in ls)//判断多个相同的参数名名称 1.
                            {
                                if (pa.Definition.ParameterGroup == BuiltInParameterGroup.PG_DATA && pa.IsShared == true)
                                {
                                    bAdd = false;
                                    break;
                                }
                            }
                        }
                    }


                    if (bAdd)//  没有此参数 才为此类型添加参数  不重复添加     判断同一组中只允许一个,允许不同组中有相同 2.是共享参数才不加  3.而且是在数据Group下面 4. 不是当前组名 就添加
                    {
                        Category category = null;
                        category = _revit.ActiveUIDocument.Document.Settings.Categories.get_Item(BuildInCategory);
                        if (category.AllowsBoundParameters)
                            categories.Insert(category);
                    }
                    if (categories.Size > 0)
                    {
                        InstanceBinding caseTying = _revit.Application.Create.NewInstanceBinding(categories);
                        boundResult = _revit.ActiveUIDocument.Document.ParameterBindings.Insert(information, caseTying, BuiltInParameterGroup.PG_DATA);//BuiltInParameterGroup写入哪个组中,不可以自定义新增
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                Logger.Instance.Error(ex);
            }
            return boundResult;
        }
        
//创建族共享参数
        public void AddFamilySharedParam(Autodesk.Revit.UI.UIApplication _revit, Family family, Dictionary<string, List<string>> TypeParaPairs)
        {
            try
            {
                DefinitionFile informationFile = AccessOrCreateExternalSharedParameterFile(_revit);
                Document doc = _revit.ActiveUIDocument.Document.EditFamily(family);
                FamilyManager manager = doc.FamilyManager;
                FamilyTypeSet types = manager.Types;
                Transaction tr = new Transaction(doc, "SetFamilyType");
                tr.Start();
                if (null == informationFile)
                {
                    return;
                }
                DefinitionGroups informationCollections = informationFile.Groups;
                DefinitionGroup informationCollection = null;
                informationCollection = informationCollections.get_Item("NAME");
                //revit不允许自定义新增组名
                if (null == informationCollection)//创建组
                {
                    informationCollections.Create("NAME");
                    informationCollection = informationCollections.get_Item("NAME");
                }
                string zuNameTag = "";
                foreach (var Type in TypeParaPairs)
                {
                    System.Collections.IEnumerator enumerator = types.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        if ((enumerator.Current as FamilyType).Name.Contains(Type.Key))
                        {
                            manager.CurrentType = enumerator.Current as FamilyType;
                        }
                    }
                    var ParaEnu = manager.Parameters.GetEnumerator();
                    IList<FamilyParameter> SortPara = manager.GetParameters();
                    foreach (var item in Type.Value)
                    {
                        ExternalDefinition parameterDef = informationCollection.Definitions.get_Item(item) as ExternalDefinition;
                        if (null == parameterDef)
                        {
                            ExternalDefinitionCreationOptions externalDefinitionCreationOptions = new ExternalDefinitionCreationOptions(item, Autodesk.Revit.DB.ParameterType.Text);
                            if (item == "用来分隔的参数,不设置值" )
                            {
                                externalDefinitionCreationOptions.UserModifiable = false;
                                zuNameTag = item;
                                externalDefinitionCreationOptions.Description = zuNameTag;
                                informationCollection.Definitions.Create(externalDefinitionCreationOptions);
                                parameterDef = informationCollection.Definitions.get_Item(item) as ExternalDefinition;
                            }
                            else
                            {
                                parameterDef = informationCollection.Definitions.Create(new ExternalDefinitionCreationOptions(item, ParameterType.Text)) as ExternalDefinition;
                            }
                        }
                        // 创建共享族参数
                        FamilyParameter newParameter = manager.AddParameter(parameterDef, BuiltInParameterGroup.PG_DATA, true);
                        SortPara.Add(newParameter);
                    }
                    //参数排序
                    manager.ReorderParameters(SortPara);
                }
                tr.Commit();
                doc.LoadFamily(_revit.ActiveUIDocument.Document, new ProjectFamLoadOption());
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                Logger.Instance.Error(ex);
            }
        }

以上是我开发中使用的创建删除方法,才疏学浅,望各位多指点,多交流。QQ:464905795 谢谢大家浏览。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gaga.boom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值