Search elements for null value in shared parameter and change to 0

Search elements for null value in shared parameter and change to 0

Anonymous
Not applicable
502 Views
3 Replies
Message 1 of 4

Search elements for null value in shared parameter and change to 0

Anonymous
Not applicable

Like the title says, I am looking through all the elements and finding the shared parameter "AE Opmeting" in them. When they are null or blank I want to add a 0 to the parameter value.

 

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var app = commandData.Application;
            var doc = app.ActiveUIDocument.Document;

            var sortedElements
                = new Dictionary<string, List<Element>>();

            // Iterate over all elements, both symbols and 
            // model elements, and them in the dictionary.

            ElementFilter f = new LogicalOrFilter(
                new ElementIsElementTypeFilter(false),
                new ElementIsElementTypeFilter(true));

            var collector
                = new FilteredElementCollector(doc)
                    .WherePasses(f);

            string name;

            foreach (var e in collector)
            {
                var parameter = e.LookupParameter("AE Opmeting");
                // var Opmeting = parameter.AsInteger();

                if (parameter == null) parameter.Set(0);
            }

            return Result.Succeeded;
        }

 

I get the null exception, which sort of is expected.

https://www.revitapidocs.com/2022/fe10010f-e127-7248-1f17-8c1ee0d41ea0.htm

While reading about this, I do not know how to implement this.

If this is even the right change that has to be made.

 

 

Any help will be greatly appreciated.

 

0 Likes
Accepted solutions (2)
503 Views
3 Replies
Replies (3)
Message 2 of 4

bhprest
Advocate
Advocate
Accepted solution

So the big issue here is that you're checking whether the parameter is null, and not the user-visible parameter value.

 

If the parameter is null, this actually indicates that the element does NOT have the parameter on it.

 

What you should be doing is something like the following:

 

if (null != parameter)
{
	if (string.IsNullOrEmpty(parameter.AsValueString()))
	{
		parameter.Set("0")
	}
}

 

The exact code will actually depend on what type of parameter it is. If it's a number, then you can check whether the parameter.AsDouble() == 0. But if it is a number, I think it should default to 0 anyway, which makes me think you're dealing with text.

 

0 Likes
Message 3 of 4

Anonymous
Not applicable

The parameter is a number, and name is unique in the project.

parameter propertiesparameter properties

But I might understand the code wrong, does the:

 

null != parameter

 

check if it's not null, so when something is present in the value. Because i want to fill in the values where it is null.

 

            foreach (var e in collector)
            {
                var parameter = e.LookupParameter("AE Opmeting");


                if (null == parameter) parameter.Set(0);
            }

In my mind it should be something along the lines of this. This does throw an error.

 

errorNullParameter.PNG

0 Likes
Message 4 of 4

Anonymous
Not applicable
Accepted solution

It got it working.

 

 

 

 using (var transaction = new Transaction(doc))
            {
                if (transaction.Start("Replace null with zero") == TransactionStatus.Started)
                {
 // the code
}

transaction.Commit();

 

 

 

Forgot to add this to the code.

 

Thanks for helping me out!

0 Likes