Home > Software engineering >  How to get parameters using the id of an element
How to get parameters using the id of an element

Time:07-14

Ok, right now I have an app where I click on an element and then a windows forms window shows the element properties. This is how it is now :

Reference reference = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
Element element = doc.GetElement(reference);
Parameter length = element.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);
Parameter height = element.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM);
Parameter area = element.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED);
var form1 = new Form1(
                (length.AsDouble().ToString(),
                height.AsDouble().ToString(),
                area.AsDouble().ToString(),
                doc);

Everything is working well, no problems! BUT, now, what I want is that instead of clicking on the element, I want to type the element id and get the parameters by the id of the element. In other words, I wouldnt be clicking on any element.

I tried commenting the reference (because I dont need it anymore) and passing the Id of the element as parameter for GetElement:

//Reference reference = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element);
Element element = doc.GetElement("358425");

But it doesnt work. The element is null.

CodePudding user response:

Yes. That is expected. Look at the documentation of the GetElement method. It takes three overloads. The one taking a string argument expects a unique id input. You are providing the string representation of the element id instead. That is a different thing. Interpreted as a unique id, it is invalid, so you receive a null element. You need to create an ElementId from your string (or integer number) using the element id constructor taking an int and pass that to the appropriate GetElement overload..

CodePudding user response:

Found the solution! You cant type the element id directy, doesnt matter if its a string or an int. You have to create an Elementid object. Sometimes things are just more complicated than they need to be!

The solution: Element element = doc.GetElement(new ElementId(Int32.Parse(inputText)));

  • Related