How to add new parameters to elements

How to add new parameters to elements

igor.barcelosC54YD
Enthusiast Enthusiast
3,437 Views
14 Replies
Message 1 of 15

How to add new parameters to elements

igor.barcelosC54YD
Enthusiast
Enthusiast

Hello, 

 

I need to add a new parameter to an element (an family instance in this case). First of all, is it possible to add the parameter only to the element and not for the whole family ? 

 

I have tried to add a new parameter to a family called "family" using the below code but it does not work. I get "Parameter type is not accessible..."

igorbarcelosC54YD_0-1671552938640.png

Document famdoc = Doc.EditFamily(family);

using (Transaction t = new Transaction(famdoc, "CreateParameters"))
{
t.Start();


FamilyParameter FamParam = famdoc.FamilyManager.AddParameter("My Parameter", BuiltInParameterGroup.PG_DATA, ParameterType.Text, true);

t.Commit();
}

 

Any ideas why this approach does not work ? 

 

 

 

0 Likes
Accepted solutions (1)
3,438 Views
14 Replies
Replies (14)
Message 2 of 15

RPTHOMAS108
Mentor
Mentor

If added in the Family they are applicable to the entire family i.e. either as Instance or Type parameters. So in the project all elements of that family will have those parameters.

In the project Parameters can be bound to categories instead and as such all elements of a certain category will have those parameters either as Instance or Type parameters.

 

ParameterType was removed in 2023, so is now not accessible from outside the assembly. The message indicates it still exists but is not accessible.

You need to use the method that uses ForgeTypeIds.

e.g.

FamilyManager.AddParameter("Param Name", GroupTypeId.Data, SpecTypeId.String.Text, true)

Message 3 of 15

igor.barcelosC54YD
Enthusiast
Enthusiast

Hello, thanks a lot for your response. 

 

I have another question. Let's say that a I need to add a new parameter to "Donnes d'intification". How can I get the ForgeTypeId for this case ? 

 

igorbarcelosC54YD_0-1671612535534.png

 

 

0 Likes
Message 4 of 15

RPTHOMAS108
Mentor
Mentor

Presumably that is GroupTypeId.IdentityData they are all listed in the RevitAPI.chm in English under GroupTypeId.

 

If you wanted them in another language you could build a list for your reference via LabelUtils.GetLabelForGroup with Revit set to that language.

 

ParameterUtils.GetAllBuiltInGroups

Message 5 of 15

igor.barcelosC54YD
Enthusiast
Enthusiast

Hi, thanks for your response. So I have tried the below code but I does not work, the new parameter "teste" is not added, no errors messages. Maybe because of the language  or its missing something in the code ?  

 

Document famdoc = Doc.EditFamily(family);
FamilyManager familyMgr = famdoc.FamilyManager;
try
{
using (Transaction t = new Transaction(famdoc, "CreateParameters"))
{
MessageBox.Show("ADDING NEW PARAMETER" + family.Name);
t.Start();
FamilyParameter FamParam = familyMgr.AddParameter("teste", GroupTypeId.IdentityData, SpecTypeId.String.Text, true);
t.Commit();
}

}
catch(Exception error)
{
MessageBox.Show("My eror is " + error.Message);
}

Message 6 of 15

RPTHOMAS108
Mentor
Mentor

Have you set the CurrentType in the FamilyManager? Unlike in the UI you need a default type in the API to define family parameters (even instance parameters).

0 Likes
Message 7 of 15

igor.barcelosC54YD
Enthusiast
Enthusiast

Hi, 

 

No, I don't think I did. Could you walk me trough the necessary code ? 

 

Message 8 of 15

RPTHOMAS108
Mentor
Mentor

FamilyManager.CurrentType is a property that can be assigned a FamilyType.

 

If there are no FamilyType objects in the Document (FamilyManager.Types) then you have to create one (FamilyManager.NewType). If you use NewType then it will make that new type created the current type.

 

If you refer to RevitAPI.chm for FamilyManager.NewType then that has an example in C#. In that example it is assumed "2X2" doesn't already exist, in reality you should first check if a named type you are adding already exists. As it is the example is one of those: it'll work first time but perhaps not the second time code examples.

Message 9 of 15

igor.barcelosC54YD
Enthusiast
Enthusiast

I have tried  the below code from this post : https://forums.autodesk.com/t5/revit-api-forum/how-to-use-forgetypeid-to-add-family-parameters-to-a-...

 

Still does not work.

 

using (Transaction t = new Transaction(famdoc, "CreateParameters"))
{
MessageBox.Show("ADDING NEW PARAMETER" + family.Name);
t.Start();

FamilyParameter CedWorksetParameter = familyMgr.AddParameter("CedWorkset", GroupTypeId.Data, SpecTypeId.String.Text, true); //An instance parameter
FamilyParameter CedFamilyTypeParameter = familyMgr.AddParameter("CedFamilyType", GroupTypeId.Data, SpecTypeId.String.Text, false); //Not an instance parameter

FamilyTypeSet familyTypes = familyMgr.Types;
FamilyTypeSetIterator familyTypesItor = familyTypes.ForwardIterator();
familyTypesItor.Reset();
while (familyTypesItor.MoveNext())
{
FamilyType familyType = familyTypesItor.Current as FamilyType;
familyMgr.CurrentType = familyType;
familyMgr.Set(CedWorksetParameter, "Refrigeration");
familyMgr.Set(CedFamilyTypeParameter, "Refrigeration");
}

t.Commit();
}

 

0 Likes
Message 10 of 15

RPTHOMAS108
Mentor
Mentor

The order is wrong, you have to set FamilyManager.CurrentType first before anything else (even for adding modifying instance parameters).

 

In that example they may have had types already defined. Hence they may find it doesn't work in some scenarios, true understanding is more than just copying code. The limitation of the FamilyManager is that the CurrentType needs to be set to something before you can do anything with it (sometimes it may already have a value set whilst at other times it will not e.g. when there are no types defined in the family document).

 

This is a limitation with the API object that doesn't exist in the UI, it is just the way they made it unfortunately.

 

The code in that post was probably copied from elsewhere without them understanding the mechanism involved with the FamilyManager at the time and now it is being copied a second time. Yes it fixed their issue but they went away still not understanding how it works sadly (otherwise they would have ensured the CurrentType was set first). In fairness it didn't occur to me at the time either that they may be lacking that insight.

0 Likes
Message 11 of 15

igor.barcelosC54YD
Enthusiast
Enthusiast

Hi, sorry if I get this wrong but I have only one type in my familly (called "OA-O" see below image). 

I set the only type as CurrentType inside the While and then I set the parameters. Don't know why this does not work. 

 

 

igorbarcelosC54YD_0-1671628629166.pngigorbarcelosC54YD_1-1671628677610.png

 

 

Message 12 of 15

guillaume.cassin2GQKV
Explorer
Explorer

 

 

0 Likes
Message 13 of 15

igor.barcelosC54YD
Enthusiast
Enthusiast

I'm quitte new to Revit so i have tried to add a new parameter manullay and I dont even see option to do it, like "New Parameter". 

 

Any ideias why ?

 

igorbarcelosC54YD_0-1671639144512.png

 

0 Likes
Message 14 of 15

RPTHOMAS108
Mentor
Mentor
Accepted solution

To use FamilyManager you need to be in a Family document (Document.IsFamilyDocument = true).

 

If you want to add parameters in the project then that is a separate process via category bindings.

0 Likes
Message 15 of 15

igor.barcelosC54YD
Enthusiast
Enthusiast

Ok, problem solved. Thanks a lot for your help !! 

0 Likes