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: 

Unable to get parameter.AsString() value when the parameter is readonly

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
tomerFUNPJ
1561 Views, 13 Replies

Unable to get parameter.AsString() value when the parameter is readonly

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!!

13 REPLIES 13
Message 2 of 14
RPTHOMAS108
in reply to: tomerFUNPJ

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).

Message 3 of 14
tomerFUNPJ
in reply to: RPTHOMAS108

From what I experienced it can be in any kind of collection. For example
List<Element> elemetsList = GetSelection();

public static List<Element> GetSelection()
{
ICollection<ElementId> ids = RevitDBUtils.uidoc.Selection.GetElementIds();
if (!ids.Any())
return new List<Element>();
return ids.Select(x => x.ToElement()).ToList();
}
Message 4 of 14
RPTHOMAS108
in reply to: tomerFUNPJ

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?

Message 5 of 14
tomerFUNPJ
in reply to: RPTHOMAS108

It's a parameter type and it's value is a string. The problem is not that some of them are randomly readonly. Some of them are readonly from the model and that's ok (That's how they are in the document as you said). The problem is that I'm unable to read them to parValue if they are readonly. I'm able to read parameters that are not readonly this way.
Thank you.
Message 6 of 14
jeremy_tammik
in reply to: tomerFUNPJ

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:

 

  • Element > Parameter > Definition > Parameter > AsString

 

You can skip two of those steps, and just ask the element for its parameter and value directly:

 

  • Element > Parameter > AsString

 

That would confuse me a lot less.

 

Maybe it will confuse Revit and yourself less as well.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 7 of 14
RPTHOMAS108
in reply to: tomerFUNPJ

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.

 

 

Message 8 of 14
tomerFUNPJ
in reply to: jeremy_tammik

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. 

Message 9 of 14
RPTHOMAS108
in reply to: tomerFUNPJ

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?

 

Message 10 of 14
tomerFUNPJ
in reply to: tomerFUNPJ

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!

Message 11 of 14
jeremy_tammik
in reply to: tomerFUNPJ

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

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 12 of 14
wagnerhNA49Z
in reply to: tomerFUNPJ

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

Message 13 of 14

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.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 14 of 14

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.

Post to forums  

Autodesk Design & Make Report