C#C
C#4y ago
Curious

❔ ✅ remove from list (RevitAPI)

I have started learning C# today. Please bare with me 🙂
I am trying to remove an element from an list if its existing, otherwise add it.
pickedRef = sel.PickObject(ObjectType.PointOnElement, elmFilter, "Pick the elements to be updated.");

// create list and add/remove depending on action with element
List<ElementId> selectId = new List<ElementId>();
Color overrideColor = new Color(255, 182, 193);
ElementId elmId = pickedRef.ElementId;

if (selectId.Contains(elmId))
{
  OverrideColour(uiApp, elmId, overrideColor, true);
  selectId.Remove(elmId);
  TaskDialog.Show("AA", "T");
}
else
{
  OverrideColour(uiApp, elmId, overrideColor, false);
  selectId.Add(elmId);
  TaskDialog.Show("AA", "F");
}

I seem too only be able to run whats in the else, and never if the list contains the element.

The logic works, as I have it in python already:
ref = uidoc.Selection.PickObject(ObjectType.Element, 'Pick elements in the desired order (re-select to Remove), hit ESC to stop picking.')
e_id = ref.ElementId
if e_id not in selectId:
    overridecolor(e_id)
    selectId.append(e_id)
else:
    overridecolor(e_id, True)
    selectId.pop(selectId.index(e_id))
Was this page helpful?