I've been struggling with this weird problem for a few hours now 😞
I have some elements that has a parameter called PANEL ID. sometimes this parameter is in readonly mode.
When I'm accessing the element directly, like in this example, I can get the parameter value perfectly fine:
Element element = doc.GetElement(new ElementId(3697816));
Definition parameterDefinition = element1.LookupParameter("PANEL ID")?.Definition;
Parameter par = element1.get_Parameter(parameterDefinition);
string parValue = par.AsString();
Here above, the ID number is the number of such element that has a Panel ID value which is readonly. This code works fine.
Here, when accessing the same kinds of elements as part of any collection, parValue is empty (""):
foreach (Element element1 in elementsList)
{
Definition parameterDefinition = element1.LookupParameter("PANEL ID")?.Definition;
if(parameterDefinition == null)
continue;
Parameter par = element1.get_Parameter(parameterDefinition);
string parValue = par.AsString();
}
In here above, parValue is empty for elements that their PANEL ID parameter is readonly. It works fine when the PANEL ID parameter is not readonly.
Thanks in advance!!
Solved! Go to Solution.
Solved by tomerFUNPJ. Go to Solution.
Where does the collection of Elements come from and what is their IsValidObject property?
Sometimes you'll get elements but due to document state they don't have readable values e.g. when you hold onto out of context objects.
LookupParameter is not an great way of getting parameters, should only be used with non-shared family and project parameters i.e. those without GUID or BuiltInParameter value to use instead.
You can also get the parameter directly without the definition (check returned parameter is null instead of checking the definition is null).
So this comes directly before your previous code block?
What type of parameter is it? The below are some of the reasons for parameters being read only. Some can be ruled out because they are always read only.
Is on element nested in other instance: always read only.
Is built-in parameter that doesn't allow setting via API: always read only.
Is built-in parameter not applicable due to other values: sometimes read only
Is read only due to document state.
So why is the parameter you are trying to get a value of read only what is the current state of the document at the time?
You code looks weird to me.
In both your samples, you are asking the element for its parameter, then the parameter for the definition, then the definition for the corresponding parameter value on the specific element:
You can skip two of those steps, and just ask the element for its parameter and value directly:
That would confuse me a lot less.
Maybe it will confuse Revit and yourself less as well.
Can you show me with Revitlookup this parameter?
Is it a built-in parameter, shared, non-shared? You say it is has a string value, are you getting that from it's StorageType?
Probably you have invalid objects or are not getting the exact parameter you think you are, that's all I can say from what you have told me.
Thanks. Now I am closer to the problem origin. The problem now only happens on a list which is filtered by a BoundingBoxIntersectsFilter:
Outline outline = new Outline(bb.Min, bb.Max);
BoundingBoxIntersectsFilter bbfilter = new BoundingBoxIntersectsFilter(outline); //filters if element falls within BBox
FilteredElementCollector collector = new FilteredElementCollector(doc); //create collector, on current document
ElementMulticategoryFilter filter1 = new ElementMulticategoryFilter(ElementsCategories); //create filter using categories list
ICollection<ElementId> AllElements = collector.WherePasses(filter1).WhereElementIsNotElementType().ToElementIds(); //add filters to collector and get elements
if (AllElements.Count == 0)
return new List<Element>();
List<Element> res = new List<Element>(new FilteredElementCollector(doc, AllElements).WherePasses(bbfilter).ToElements());
foreach (Element element1 in res)
{
Parameter par = element1.LookupParameter("PANEL ID");
if (par == null)
continue;
int id = element1.Id.IntegerValue;
string parValue = par.AsString();
}
Here I create the list where the problem occurs.
Thanks in advance.
Since the parameter in question is a shared parameter do you get the same result when you access the parameter via Element.get_Parameter(GUID)?
The value appears accessible on RevitLookup in it's read only state. If you get to the same RevitLookup information page via 'Snoop DB' is the value still available or not i.e. when coming from a collection there.
Is there more to this picture such as having the same parameter on host and secondary nested elements etc?
Seems like my problem was not using doc.Regenerate();
My readonly params were updated from other elements, that that parameters were changed. But I couldn't see it in the API until I used doc.Regenerate();
Thanks everyone!
Another interesting context that could be added to the list of situations requiring regen:
https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.33
Good day,
I am new to the Revit API and I think the issue I am experiencing is similar to the above though I cannot get it to work.
I just want to read the value of a parameter as a sting.
For a particular element, as I understand it, the below should work?
element.LookupParameter()metod.AsSting()
I am sure I am missing a basic thing, but I really got stuck here.
Would appreciate the help.
Thanks
Reading a parameter as a sting sounds painful!
In your screen snapshot, you display an error trying to call a method by the name LookupParameters.
The Revit API does not define any such method. Do you mean LookupParameter?
https://www.revitapidocs.com/2023/4400b9f8-3787-0947-5113-2522ff5e5de2.htm
Lesson 1 in computer programming: unfortunately, it will do (exactly) what you tell it to do, not what you think, or hope.
Hi Jeremy,
Thank you for the response. Apologies I made a mistake in the code when I took the screenshot. The real error I get is in the attached.
I have also tried the ToString() method.
Regards
Can't find what you're looking for? Ask the community or share your knowledge.