Loop trough elements and set parameter

Loop trough elements and set parameter

Anonymous
Not applicable
2,354 Views
6 Replies
Message 1 of 7

Loop trough elements and set parameter

Anonymous
Not applicable
Hello,

I wanna loop through elements of a chosen familie in a project.
I have this code, but i can't set the parameter. What am i doing wrong? Please help. Thnx in advanced.

//Get application and document objects
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;

//Create collector with the filter Family
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector = collector.OfClass(typeof(Family));

//Get ID from te family with linq-code
var query = from element in collector where element.Name.StartsWith("300_32_ISR_wandopening_houten_binnenkozijn_60WBDBO") select element;
List<Element> fams = query.ToList<Element>();
ElementId famId = fams[0].Id;

//Create FamilySymbolFilter by FamilyID
FamilySymbolFilter filter = new FamilySymbolFilter(famId);

//Create collector filtered by FamilyID and put familysymbols in ICollection : dus alle familytypen in collector plaatsen.
collector = new FilteredElementCollector(doc);
ICollection<Element> Familysymbols = collector.WherePasses(filter).ToElements();

//Create string for testing results
string prompt = "alle deurtypes van familiy 300_32_ISR_wandopening_houten_binnenkozijn_60WBDBO: ";

//Create list to put in FamilySymbol-ID's : dus lijst creeren om alle ID's van de familytypen erin te plaatsen.
List<ElementId> lstFamilySymbolIDs = new List<ElementId>();


//---> Get all FamilySymbol-ID's and put them in a list. : dus alle ID's in de lijst stoppen.
foreach (Element el in Familysymbols)
{
prompt += "\n";
prompt += el.Name + "\n";
prompt += el.Id + "\n";
lstFamilySymbolIDs.Add(el.Id);

Parameter p = el.LookupParameter("Finish");
p.Set("test-waarde");
//el.LookupParameter("MyParam").Set("TestValu​e");
el.get_Parameter("Finish").Set("test-waarde");
0 Likes
Accepted solutions (1)
2,355 Views
6 Replies
Replies (6)
Message 2 of 7

ollikat
Collaborator
Collaborator
Hi.

Despite that you provided the code you are struggling with, I'm afraid it's not easy to help you yet.

A couple of things...

- What do you exactly mean by "I can't set parameter"? Is an exception thrown? Or does the parameter set just fail silently?
- Just to make sure...do you have transaction open while trying to set the parameter?
0 Likes
Message 3 of 7

Anonymous
Not applicable

Hello,

 

i get an null-reference exception. See included attachment.

 

In my opinion i don't have a transaction open, i use a public execute-command ("public Result Execute(")

 

The taskdialogbox shows the element-id's, so the list is populated withe elements.

 

I checked that the existence of parameter "Finish" exists in the selected families.

 

Hope you can help. Thnx in advanced.

0 Likes
Message 4 of 7

ollikat
Collaborator
Collaborator
Accepted solution

Hi

 

Well...still can't say exactly what's the problem with the code since I don't know what is the corresponding line (57) in the code you provided. But that's the place where null reference has been encountered!

 

There are several potential (null reference) failure places in your code.

 

ElementId famId = fams[0].Id;

This one can easily cause null reference exception, as it's not quaranteed you to get any results (if you have misspelled the family name etc). Not sure though if this would throw some kind of out of range exception before nullreferenceexception.

 

foreach (Element el in Familysymbols)

Also this one may fail, as you don't check Familysymbols for the null reference before using in the foreach

 

Parameter p = el.LookupParameter("Finish");
p.Set("test-waarde");

"p" can be also null, if corresponding parameter not found

 

el.get_Parameter("Finish").Set("test-waarde"); 

Same thing applies here.

 

 

I suggest that you should try to run your add-in with the debugger. It will allow you to determine the exact failure place (although the exception message tells you that already). Furthermore you should test variables for null more often. Getting parameters is a good example, which can be considered a scenario where it's not ALWAYS quaranteed to get the intended parameter object.

And what comes to transaction, eventually your add-in will fail, if you haven't either opened a transaction or used automatic transaction mode.

Message 5 of 7

Anonymous
Not applicable

Hi, thnx for the quick responce.

 

When i skip the line for setting the parameter the taskdialog shows the id of the familiytype. So the codelines you suggested shouldn't throw a null-reference?.

How can i debug?. Now i build the solution and copy the *.dll to the application folder of revit.

 

THnx!!

0 Likes
Message 6 of 7

Anonymous
Not applicable

Hi Ollikat,

 

I succeeded in debugging! Seems indeed that the parameter "Finish" doesn 't exist.

 

Actually it exists, but it is a type-parameter and not a instance parameter.

 

How can i set an instance-parameter instead of a type parameter?

 

Thnx in advanced?

Message 7 of 7

ollikat
Collaborator
Collaborator
Hi.

Glad to hear you have proceeded. It always gives energy to strive forward :-).

An answer to your question is actually pretty simple. You need to have an instance of an type in order to get/set instance parameter. In other words it's quite clear the division in Revit between the parameters. Instance elements only have instance parameters, and type/symbols only have type parameters.

So...now your code only needs to known, which instances to fetch from the project and then get them and set the new parameter values. I believe that's peace of cake to you as you already have used the filters very well to get the symbols.
0 Likes