How to get parameter value of selected element

How to get parameter value of selected element

kellyxsy99
Explorer Explorer
12,487 Views
5 Replies
Message 1 of 6

How to get parameter value of selected element

kellyxsy99
Explorer
Explorer

I want to get parameter values of selected element such as height, lenght, etc...

but, the return was "Autodesk.Revit.DB". I can't find which caused a problem.

 

Here is my code

 

Reference reference = uidoc.Selection.PickObject(ObjectType.Element);
			Element element = uidoc.Document.GetElement(reference);

			Parameter foundParameter = null;
			foreach (Parameter parameter in element.Parameters)
            {
				if (parameter.Definition.ParameterType == ParameterType.Length)
                {
					foundParameter = parameter;
					

					using (Transaction tx = new Transaction(doc))
					{
						tx.Start("transaction");
						TaskDialog.Show("title 🙂 ", parameter.ToString());
						tx.Commit();
					}
				}
            }
			
			return Result.Succeeded;
0 Likes
Accepted solutions (1)
12,488 Views
5 Replies
Replies (5)
Message 2 of 6

franciscopossetto
Advocate
Advocate
Accepted solution

Hey, 

 

You can do it on several ways. Your code is not working because you are not reading the value after get the Parameter. You are just converting an object into a string.

 

Instead of do this:

 

parameter.ToString()

 

 

Do this:

 

parameter.AsDouble().ToString();

 

 

On this link you can see all methods that can be applied on parameters, such us AsDouble(), AsString(), AsInteger(), etc... this is depending on the type of value your parameter stores. I used AsDouble() because dimensions stores doubles.

https://www.revitapidocs.com/2015/0b04b80d-b318-986e-48cc-835d0dda76e5.htm

 

To get parameter values, you take a look on the methods LookupParameter() and get_Parameter().

In addition, you do not need to initiate a transaction to get a value.

 

I hope it helps,

Kind regards.

Github:
https://github.com/franpossetto
0 Likes
Message 3 of 6

kellyxsy99
Explorer
Explorer

Thanks for your reply.

Following your solution, I changed my codes like this.

 

Parameter parameter1 = element.LookupParameter("Code");
parameter1.AsValueString();

Parameter parameter2 = element.LookupParameter("Length");
parameter2.AsDouble().ToString();

 

 (element is selected from model. The value type of "Code" is string and "Length" is Double.)

However, Results are still "Autodesk.Revit.DB.Parameter".

 

How do I solve this problem?

0 Likes
Message 4 of 6

kellyxsy99
Explorer
Explorer

oh, I solved the problem! thanks a lot.

0 Likes
Message 5 of 6

a7med_ma7moud
Contributor
Contributor
Hope if you can share the solution, I am facing the same issue
0 Likes
Message 6 of 6

RPTHOMAS108
Mentor
Mentor

Don't use AsValueString unless the storage type is a value type such as double (was originally for parameters with units). I think it does work for certain other storage types (ElementId) that point to elements but it doesn't work for storage types of string (for such AsString should be used).

 

Practically I never use AsValueString, the one thing it is good for is formatting the values with units as they are in the UI, that is about it.

 

So above it could have been used for Length but not Code (which I assume is perhaps a string representing Uniclass etc).