Get DB.Parameter object via its Name

Get DB.Parameter object via its Name

george1985
Collaborator Collaborator
262 Views
3 Replies
Message 1 of 4

Get DB.Parameter object via its Name

george1985
Collaborator
Collaborator

Dear Revit Experts,

 

Is it possible to get Shared Autodesk.Revit.DB.Parameter object, based on its Name?

 

I found LookupParameter method, which does this, but it requires to be called on an instance of an object from the document. For example, to access the 'ProductionStatus' Parameter of a Wall, I need to provide one of the Wall instances.

 

Another way I found is via parameter guid:

Isn't there no third way?
Because I don't have the Wall instance, nor I know the guid of my parameter.

 

I would be very grateful if someone could help me with this issue.

0 Likes
263 Views
3 Replies
Replies (3)
Message 2 of 4

Mohamed_Arshad
Advisor
Advisor

Hi @george1985 

 

I don't think without element we can't able to get the parameter, you can use the FilterElementCollector to get the wall instance. Check below link for additional information.

URL: The Building Coder: Parameter Filter

 

But by using category you can able to get the parameters, Can you please check the below implementation

Reference Code

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Document doc = uiDoc.Document;

            ElementId wallId = new ElementId(BuiltInCategory.OST_Walls);
            ElementId floorId = new ElementId(BuiltInCategory.OST_Floors);

            List<string> parameters = new List<string>();


            List<ElementId> catsids = new List<ElementId>() { wallId, floorId };

            foreach (var item in catsids)
            {
                parameters.AddRange(GetParameters(doc, item));
            }

            parameters = parameters.Distinct().ToList();

            parameters.Sort();


            string param = string.Empty;

            foreach(var item in parameters)
            {
                param += item + "\n";   
            }

            TaskDialog.Show("Parameter Info", param);

            return Result.Succeeded;
        }

 public List<string> GetParameters(Document doc, ElementId elementId)
        {
            List<ElementId> catsids = new List<ElementId>() { elementId };
            ICollection<ElementId> parametersIds = ParameterFilterUtilities.GetFilterableParametersInCommon(doc, catsids);
            List<string> parameters = new List<string>();

            if (parametersIds != null)
            {
                foreach (ElementId paramId in parametersIds)
                {
                    ParameterElement paramEl = doc.GetElement(paramId) as ParameterElement;

                    if (paramEl != null)
                    {
                        var namepar = paramEl.Name;
                        parameters.Add(namepar);
                    }
                    else
                    {
                        BuiltInParameter bp = (BuiltInParameter)paramId.Value;
                        var namepar = LabelUtils.GetLabelFor(bp);
                        parameters.Add(namepar);

                    }
                }
            }

            return parameters;
        }

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 4

mmaso64U7Y
Enthusiast
Enthusiast

Hi @george1985 , recently I had a prorblem similar with yours. It helped me to understand the difference between DB.Parameter and DB.ParameterElement. The first one is owned by an Element and gathers the value. The second one is owned by the Document and it contains the Definition.

 

As @Mohamed_Arshad says, it makes no sense to retrieve a Parameter from no Element. However, you can use the method GetFilterableParametersInCommon to ask the Document for the parameters of a given class.

 

Hope it helps

0 Likes
Message 4 of 4

GaryOrrMBI
Collaborator
Collaborator

In addition to the other comments, in the case of looking for a non built-in parameter definition that is added to a host category (walls, floors, etc.), they can only be added via Binding within a document, thus <Document>.ParameterBindings would return the document's parameter BindingMap that you could then step through to find the one that you're after.

 

		Dim paraBind As RDB.BindingMap = thisDoc.ParameterBindings
		Dim paraIter As RDB.DefinitionBindingMapIterator = paraBind.ForwardIterator()
		paraIter.Reset()
		While paraIter.MoveNext = True
			Dim thisDef As RDB.InternalDefinition = paraIter.[Key]
			'Do Something
			Dim sharedParam As RDB.SharedParameterElement = TryCast(thisDoc.GetElement(thisDef.Id), RDB.SharedParameterElement)
			If sharedParam IsNot Nothing Then
				'Do Something
			Else
				Dim localParam As RDB.ParameterElement = TryCast(thisDoc.GetElement(thisDef.Id), RDB.ParameterElement)
				'Do Something
			End If
			If thisDef.GetGroupTypeId() <> Nothing Then
				'Do Something
			Else
				'Do Something
			End If

			Dim typeBound As RDB.TypeBinding = TryCast(paraIter.Current, RDB.TypeBinding)
			If typeBound IsNot Nothing Then
				'Do Something
				Dim Cats As RDB.CategorySet = typeBound.Categories
				For Each cat As RDB.Category In Cats
					'Do Something
				Next
			Else
				Dim instBound As RDB.InstanceBinding = TryCast(paraIter.Current, RDB.InstanceBinding)
				If instBound IsNot Nothing Then
					'Do Something
					Dim Cats As RDB.CategorySet = instBound.Categories
					For Each cat As RDB.Category In Cats
						'Do Something
					Next
				Else
					'Do Something
				End If
			End If
		End While

 

-G

Gary J. Orr
GaryOrrMBI (MBI Companies 2014-Current)
aka (past user names):
Gary_J_Orr (GOMO Stuff 2008-2014);
OrrG (Forum Studio 2005-2008);
Gary J. Orr (LHB Inc 2002-2005);
Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes