简体   繁体   中英

How to add parameters to a revit family element from revit document in revit-api c#

I'm working on a project where I need to load pipe fittings (bends) revit families on a revit document and update its shared parameters as type parameters.

I was able to achieve this task since I needed to access the bend family as family instance.

The second part of the requirement is to update the coupling family parameters of the bend as well. The problem is that I'm unable to access the coupling parameter from the revit document.

If I try it manually, the coupling parameter is accessible only when I double click on the bend family, this opens the revit family file. I then can access both the coupling on either side of the bend. How can I achieve the above task of adding parameters to a revit family element from revit document programmatically.

Please guide me.

Thank You in advance.

My Code:

 foreach (var item in MainModel.listElement)//List<Element>
 {
    FamilyInstance elFamInst = item as FamilyInstance;
    if (elFamInst.Symbol.FamilyName == "MainFamilyName")//Bend Family
    {
       ElementId id = item.GetTypeId();
       ElementType et = doc.GetElement(id) as ElementType;

       Family fam = elFamInst.Symbol.Family;

       if (elFamInst.SuperComponent == null)
       {
          var subElements = elFamInst.GetSubComponentIds();
          if (subElements.Count != 0)
          {
             foreach (var aSubElemId in subElements)
             {
                var aSubElem = doc.GetElement(aSubElemId);
                if (aSubElem is FamilyInstance)
                {
                   FamilyInstance subEl = aSubElem as FamilyInstance;
                   Family fm = subEl.Symbol.Family;
                   if(subEl.Symbol.FamilyName == "subFamilyName")//coupling family
                   {
                       Document docfamily = doc.EditFamily(fam);
                       if (null != docfamily && docfamily.IsFamilyDocument == true)
                       {
                          FamilyManager familyManager = docfamily.FamilyManager;
                          using (Transaction trans = new Transaction(docfamily, "param"))
                          {
                              trans.Start();
                              FamilyParameter fp = familyManager.AddParameter("Namess",BuiltInParameterGroup.PG_IDENTITY_DATA,ParameterType.Text, false);
                              familyManager.Set(fp, "JP");
                              trans.Commit();
                          }
                        }

                    }

                  }
               }
             }
          }
       }
    }

I am unable to achieve the result, also I want the parameter "JP" to be set only on the coupling family ie the parameter with the name "subFamilyName".

Yes, you can achieve the same programmatically as well via the API.

Just as you need to open the family file in the manual approach, you can call the EditFamily method to do so in the API:

https://apidocs.co/apps/revit/2019/56e636ee-5008-0ee5-9d6c-5f622dedfbcb.htm

Check out various posts by The Building Coder mentioning this method, eg, Modifying, Saving and Reloading Families .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM