Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create View filters for project parameter

5 REPLIES 5
Reply
Message 1 of 6
will.wydock
845 Views, 5 Replies

Create View filters for project parameter

Hi,

Occasionally we have to make view filters multiple view filters in a model based on a project parameter. I am trying to put together a macro that I can modify when needed to help me create these filters. However, I am struggling with being able to select any parameters that are not built in to create filters. How can I find the parameter Id when i only have the name?

 

Thank you in advance.

 

public void viewFilter()
		{
		    Document doc = this.ActiveUIDocument.Document;
		    srviewfilter(doc,"PU");
		
		}
		public void srviewfilter(Document doc,string typeabbreviation)
		{
			Element firstmech = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_MechanicalEquipment).FirstElement();
			
			
			
			string prefix="Mech_Eq-";
			IList<ElementId> catIds = new List<ElementId>();
		    catIds.Add(doc.Settings.Categories.get_Item("Mechanical Equipment").Id);
		    catIds.Add(doc.Settings.Categories.get_Item("Duct Accessories").Id);
		    
		    string filtername=string.Concat(prefix,typeabbreviation);
		    
		    IList<FilterRule> rules = new List<FilterRule>();
		    ElementId paramID = new ElementId(firstmech.LookupParameter("TAG_MECH").Id);
		    rules.Add(ParameterFilterRuleFactory.CreateEqualsRule(paramID,typeabbreviation, true));
		    using (Transaction t = new Transaction(doc,"View Filter"))
		    {
		        t.Start();
		            		        		        
		        ParameterFilterElement filter = ParameterFilterElement.Create(doc, filtername, catIds, rules);
		        
		        t.Commit();
		    }
		}
5 REPLIES 5
Message 2 of 6
a-carpentier
in reply to: will.wydock

One way that could work but that may be computationally heavy is to declare two lists (one of parameter id, one of parameter names) and to associate the two of them.

So you would loop through all the parameters, and for each parameter you add its id to one list and its name to the other list.

 

Then you could easily find the id of a parameter based on its name, as long as all parameters in the list have unique names.

 

Dim ParamIdList as new List(Of ElementId)
Dim ParamNameList as new List(Of String)

Dim ParamId as ElementId = Nothing
Dim ParamName as String = "Name of the parameter I want the ElementId of"

-------
Put all the parameters info into the lists
-------

For i=0 to ParamNameList.count-1
If ParamNameList(i) = ParamName then
ParamId = ParamIdList(i)
Exit For
End If
Next

(I code in VB.Net)

 

This may not be the fastest solution or the prettiest one, but I think it could work.

 

Antoine

Message 3 of 6
FAIR59
in reply to: will.wydock

Project parameters are "document-based" parameters, and are stored in a (Shared)ParameterElement.

Given a name, this is how you find the Id:

string name2Find = "test";
ParameterElement pElem = new FilteredElementCollector(doc)
				.OfClass(typeof(ParameterElement))
				.Cast<ParameterElement>()
				.Where(e=> e.Name == name2Find)
				.FirstOrDefault();
			
ElementId paramID  = pElem.Id; 
Message 4 of 6
jeremytammik
in reply to: FAIR59

Thank you for the answer!

 

In a previous thread, you compared the name of the Definition instead of the ParameterElement itself:

 

https://forums.autodesk.com/t5/revit-api-forum/deleting-a-non-shared-project-parameter/td-p/5975020

 

Do both of these works equally?

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 6
FAIR59
in reply to: jeremytammik

@jeremytammik 

I haven't  run into an error yet, using the Name of the ParameterElement. Before I was delving into Parameters and FamilyParameters, and with those you can only use the Definition.Name, which probably is the "official" place to look for the name.

Of course only the develpment team can give you a definitive answer. 

Message 6 of 6
jeremytammik
in reply to: FAIR59

Thank you! Mostly you seem to know more than they do  🙂

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community