Revit shared parameters are different from built-in or native parameters (BuiltInParameter). Shared parameters can be user created and actually all of them are supposed to be defined or created by users or third parties. BuiltInParameter instances however are hard coded and maintained by Revit itself.
Revit Parameter Organizer can organize various Revit parameters such as Shared Parameters, Family Parameters, Project Parameters and Built-in Parameter in many good ways.
In this post, let’s see how to create shared parameters programmatically with Revit API.
The following help methods can help achieve the goal:
public static List<T> RawConvertSetToList<T>(IEnumerable set)
{
List<T> list = (from T p in set select p).ToList<T>();
return list;
}
public static Definition RawCreateSharedParameter(RvtApplication app, string name, string group, ParameterType type, bool visible)
{
DefinitionFile defFile = app.OpenSharedParameterFile();
if (defFile == null) throw new Exception("No SharedParameter File!");
DefinitionGroup dg = RawConvertSetToList<DefinitionGroup>(defFile.Groups).FirstOrDefault(g => g.Name == group);
if (dg == null) dg = defFile.Groups.Create(group);
Definition def = RawConvertSetToList<Definition>(dg.Definitions).FirstOrDefault(d => d.Name == name);
if (def != null) return def; //dg.Definitions.Erase(def); //ReadOnly Exception!!
def = dg.Definitions.Create(name, type, visible);
return def;
}
As the code indicates, the first step is to get the DefinitionFile object through calling the method OpenSharedParameterFile of the Revit Application, the second step is to find the DefinitionGroup of interest or create one when necessary, the last is to create the Definition (ExternalDefinition actually) into the Definitions collection from the given parameter name, type, and visibility.
Also a side finding as commented in the code: please do not try to erase an existing shared parameter represented by a Definition (actually ExternalDefinition) object with the straightforward DefinitionGroup.Definitions.Erase() method. Otherwise, an unexpected exception would just occur. We will talk about this in a bit more detail later and see if there is a way to work around the issue.
The following test code can be used to exercise the help methods:
…
RawCreateSharedParameter(CachedApp, "Volume11", "Group1", ParameterType.Volume, true);
RawCreateSharedParameter(CachedApp, "Volume41", "Group4", ParameterType.Volume, true);
RawCreateSharedParameter(CachedApp, "Volume42", "Group4", ParameterType.Volume, false);
RawCreateSharedParameter(CachedApp, "LengthTest11", "Group1", ParameterType.Text, false);
…
If the external shared parameter definition file looks like the following before the test code is run:
# This is a Revit shared parameter file.
# Do not edit manually.
*META VERSION MINVERSION
META 2 1
*GROUP ID NAME
GROUP 1 Group1
GROUP 2 Group2
GROUP 3 Group3
*PARAM GUID NAME DATATYPE DATACATEGORY GROUP VISIBLE
PARAM 5d4dcb0d-49db-4ac3-a618-98431e8e5f39 Area11 AREA 1 1
PARAM 18daaa5a-1a42-45b0-8330-14eccaa3283c LengthTest11 LENGTH 1 1
PARAM cec3106d-5a45-4191-91e1-f18f3f8c58dd AngleTest21 FORCE 2 1
PARAM a2143977-eecc-4e87-9953-6afaf1bcbddf ForceTest21 FORCE 2 1
PARAM 23e79d90-7f17-4f02-bcdd-aee0fa41f22e Force11 FORCE 1 1
PARAM b3bbc191-68d6-4992-b9d8-c9b81006711b ColorTemprature11 COLOR_TEMPERATURE 1 1
PARAM d29fac95-7a0e-439f-97bd-124a2100ba62 WattageTest21 ELECTRICAL_WATTAGE 2 1
PARAM 7e689e9f-d2ac-46bc-a04f-3741518586f1 YesNoTest31 YESNO 3 1
PARAM e08272bb-d2e1-4be8-8955-50e27f560649 TextTest31 LENGTH 3 1
PARAM a5a433d4-11bd-48c3-8fa0-a59af5281c8a MaterialTest21 MATERIAL 2 1
PARAM 966d79df-9baa-45ec-9334-f49e395ca903 URLTest31 URL 3 1
The final shared parameter file will look like this:
# This is a Revit shared parameter file.
# Do not edit manually.
*META VERSION MINVERSION
META 2 1
*GROUP ID NAME
GROUP 1 Group1
GROUP 2 Group2
GROUP 3 Group3
GROUP 4 Group4
*PARAM GUID NAME DATATYPE DATACATEGORY GROUP VISIBLE
PARAM 5d4dcb0d-49db-4ac3-a618-98431e8e5f39 Area11 AREA 1 1
PARAM 83c7b23d-c7dc-4894-a4a7-ad3f60d37477 Volume42 VOLUME 4 0
PARAM 18daaa5a-1a42-45b0-8330-14eccaa3283c LengthTest11 LENGTH 1 1
PARAM cec3106d-5a45-4191-91e1-f18f3f8c58dd AngleTest21 FORCE 2 1
PARAM a2143977-eecc-4e87-9953-6afaf1bcbddf ForceTest21 FORCE 2 1
PARAM 8f2c078c-81ff-462c-a581-df071f2e470b Volume11 VOLUME 1 1
PARAM 23e79d90-7f17-4f02-bcdd-aee0fa41f22e Force11 FORCE 1 1
PARAM b3bbc191-68d6-4992-b9d8-c9b81006711b ColorTemprature11 COLOR_TEMPERATURE 1 1
PARAM d29fac95-7a0e-439f-97bd-124a2100ba62 WattageTest21 ELECTRICAL_WATTAGE 2 1
PARAM 7e689e9f-d2ac-46bc-a04f-3741518586f1 YesNoTest31 YESNO 3 1
PARAM d57fb9b5-42cd-4c72-b6f1-df97fcef1d71 Volume41 VOLUME 4 1
PARAM e08272bb-d2e1-4be8-8955-50e27f560649 TextTest31 LENGTH 3 1
PARAM a5a433d4-11bd-48c3-8fa0-a59af5281c8a MaterialTest21 MATERIAL 2 1
PARAM 966d79df-9baa-45ec-9334-f49e395ca903 URLTest31 URL 3 1
SharedParameter Creator of RevitAddinWizard can help create the code automatically in a configurable and flexible way in a second.
By the use of text file i am able to create SharedParameters Successfully Programmatically.
but without use of text file i want to create SharedParameter Programmatically.is it posible ... directly hard coding or i can fetch from dataset.
i tried using ExternalDefinition but still no Success. i am getting it as null.
Regards
Kailash Kute
Posted by: Account Deleted | 03/28/2012 at 07:10 AM
You are right using ExternalDefinition to create project parameters is not possible. It's discussed in detail in another post. Searching the keyword will get you there. We have to use a text file to create a shared parameter first and create a project parameter from it next.
It's an imposition by the Revit API though it seems Revit product itself can create project parameters directly on the fly. Though we cannot do much about it, we can work around the limitation through using some temporary text file and switching the existing shared parameter text file back and forth if necessary.
This technique has been demonstrated in another post. I’ve appended its title and link below for your convenience:
Parameter of Revit API – 31: Create Project Parameter
http://spiderinnet.typepad.com/blog/2011/05/parameter-of-revit-api-31-create-project-parameter.html
The RawCreateProjectParameter sample method may be of your interest in this case particularly.
Posted by: Spiderinnet | 03/28/2012 at 03:19 PM
hi Everybody,
warm greetings for the day,
well i have a one family instance (Elbow or Coupling),now i want to know whether this family instance is of which template,
Imperial template or Metric template,rather than using its naming convention M_ for metric.
are there ways we come to know this Family Instance is of Imperial or Metric.
Regards
Kailash Kute
Posted by: Account Deleted | 04/11/2012 at 03:48 AM
Sorry for the late response.
There is definitely a reliable way to detect the unit system of the family instance or the family symbol it is created base on.
Calling the Document.DisplayUnitSystem property against the Family document is the way to do. It will tell you if the Family is created in the Imperial or Metric unit system.
Posted by: Spiderinnet | 04/13/2012 at 06:33 PM
How to create a Parameter with the FamilyType type? Is there any hidden tricks?
Posted by: Israel Rodriguez | 09/24/2015 at 08:10 PM
Israel, you raised a very good question. The fact is that you can set the ParameterType (DATATYPE as in the shared parameter file) as FamilyType in the API class ExternalDefinition, but there is no way to set the DATACATEGORY as indicated by the shared parameter file with the API so far. So the hidden trick is that you have to set it with your own way into the file on disk instead of the definition object in memory.
Here are two such shared parameters (one is for Window FamilyType and the other for Door):
PARAM c8b24a90-c55c-47dc-be95-e599535a9980 window FAMILYTYPE -2000014 1 1 desc 1
PARAM 73c8af90-c407-4a12-afe3-bcae5af31845 FamilyTypes FAMILYTYPE -2000023 1 1 Doors family type 1
It follows the new shared parameter file format:
*PARAM GUID NAME DATATYPE DATACATEGORY GROUP VISIBLE DESCRIPTION USERMODIFIABLE
Posted by: Spiderinnet | 09/25/2015 at 04:48 AM