C
C#2y 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");
}
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))
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))
6 Replies
Curious
CuriousOP2y ago
yes Ah right, so how would I go about doing it in C#?
Curious
CuriousOP2y ago
Curious
CuriousOP2y ago
in ref to this I think this will help
Curious
CuriousOP2y ago
Curious
CuriousOP2y ago
I actually thought that was defining the type so That might be it I thought you had to tell it the type of thing thats going to be in the list like for example a string, you'd put string so I'd put int as its IDs? So each time an element is selected on the page, it adds to a list, however if its clicked again, I want to remove it from the list. The way I made works in python and am trying to convert to C#, but also learning C# while I do it haha. the first time it'd have nothing in it sure, but I want to basically do the same thing I did in python, pop it if its exists, otherwise add it to the list in C yeah? just making sure im confused if you're talking about C or py https://pastebin.com/uNvgKT2e my comments are all over the place im yet to "sort" them Ahhhhhhhhh Silly mistake, but thank you for spotting! Make sense tbf! Thank you for the help, seem to be okay atm, probs because this is an API so just need to do minor adjustments 😄 nice for me to learn C# though! Take care!
Accord
Accord2y ago
Closed! Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server