Reputation: 85
I'm having trouble writing a script that lets med select the first element in my selection. This is useful for me because i select my correct Air Terminal from a schedule (where I can see the similar air-flow which I want to use) and use the command Create Similar from the selection. The trouble is that this command does not work when multiple elements are selected. Therefore, I want the first object from the list. This is the code which I'm trying:
from Autodesk.Revit.UI.Selection.Selection import SetElementIds
from System.Collections.Generic import List
uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
selection = [ doc.GetElement( elId ) for elId in __revit__.ActiveUIDocument.Selection.GetElementIds() ]
sel=[]
for i in selection:
sel.append(i.Id)
uidoc.Selection.SetElementIds(List[ElementId](sel[0]))
That will return the following error message:
Exception : Microsoft.Scripting.ArgumentTypeException: expected int, got ElementId OK, then I'll try to replace
uidoc.Selection.SetElementIds(List[ElementId](sel[0]))
with
uidoc.Selection.SetElementIds(List[ElementId](sel[0].IntegerValue))
This seems to work, but selection is not modified
I am just starting to write RPS-scripts, but I'm hoping someone will show me what am I doing wrong here even if it is very obvious.
Thank you. Kyrre
EDIT: Thank you Jeremy, for solving this for me! The trick was to generate a List, not a python list. .Add method is what I did not get.
Final code if someone is interested:
from Autodesk.Revit.UI.Selection.Selection import SetElementIds
from System.Collections.Generic import List
from Autodesk.Revit.DB import ElementId
uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
selection = [ doc.GetElement( elId ) for elId in __revit__.ActiveUIDocument.Selection.GetElementIds() ]
sel=[]
for i in selection:
sel.append(i.Id)
ids=List[ElementId](1)
ids.Add(sel[0])
uidoc.Selection.SetElementIds(ids)
Upvotes: 2
Views: 2976
Reputation: 8294
SetElementIds
takes a .NET ICollection<ElementId>
argument, as you can see from the Revit API documentation.
Your statement calls the .NET List
constructor that expects an integrer argument specifying the number N
of elements to allovate space for: List[ElementId](N)
.
sel[0]
is an ElementId
, not an integer, which causes the first error.
sel[0].IntegerValue
is a (very large and semi-arbitrary) integer number, so that causes no error. However, you are still leaving the List
empty, unpopulated.
You should initialise the List
for one single element and add that:
ids = List[ElementId](1)
ids.Add(sel[0])
Upvotes: 1