Select element in PickObject (or make the selected element graphically visible)

Select element in PickObject (or make the selected element graphically visible)

jens.slofstra!
Enthusiast Enthusiast
2,571 Views
3 Replies
Message 1 of 4

Select element in PickObject (or make the selected element graphically visible)

jens.slofstra!
Enthusiast
Enthusiast

Hi everyone,

I am making a selection tool but ran into a problem with visually indicating the selected element.

If possible I would like to avoid using PickObjects.

 

The current code follows these steps:

- PickObject in a while loop

- After PickObject I would like to select the element but PickObject clears this selection.

- New solution override element graphics but this doesn't quite work (I don't know why).

 

Below you will find the code I currently have:

private Document _doc;
private UIDocument _uidoc;

using(var subtx = new SubTransaction(_doc))
                {
                    subtx.Start();

                    var overrideHighlight = new OverrideGraphicSettings();
                    overrideHighlight.SetProjectionLineColor(new Color(0, 0, 255));

                    var overrideClear = new OverrideGraphicSettings();

                    var elementList = new List<ElementId>();
                    var exitSelection = false;
                    while (!exitSelection)
                    {
                        try
                        {
                            var element = _doc.GetElement(
                                                _uidoc.Selection.PickObject(
                                                    Autodesk.Revit.UI.Selection.ObjectType.Element,
                                                    new SelectionFilterByFamily("FamilyName")));

                            if (!elementList.Contains(element.Id))
                            {
                                elementList.Add(element.Id);
                                _doc.ActiveView.SetElementOverrides(element.Id, overrideHighlight);
                                _doc.Regenerate();
                            }
                            else
                            {
                                elementList.Remove(element.Id);
                                _doc.ActiveView.SetElementOverrides(element.Id, overrideClear);
                                _doc.Regenerate();
                            }
                        }
                        catch (Autodesk.Revit.Exceptions.OperationCanceledException)
                        {
                            exitSelection = true;
                        }
                    }

                    subtx.Commit();
                }

If possible I would like one of two solutions:

- Have the PickObject method not clear the selection.

- Override graphics but display them properly.

0 Likes
Accepted solutions (1)
2,572 Views
3 Replies
Replies (3)
Message 2 of 4

miguelmachadoecosta
Advocate
Advocate

@jens.slofstra! wrote:

If possible I would like one of two solutions:

- Have the PickObject method not clear the selection.


Doesn't reselecting the elements solve your problem? Adding the following line after the if statement:

_uidoc.Selection.SetElementIds(elementList);

 

0 Likes
Message 3 of 4

jens.slofstra!
Enthusiast
Enthusiast

@miguelmachadoecosta,

No, this is sadly not possible. Because when I call the next pickobject methode the selection will be cleared.

0 Likes
Message 4 of 4

miguelmachadoecosta
Advocate
Advocate
Accepted solution

It seems that in order to retain the selection you have to use this method, which might make you want to ditch the loop: https://www.revitapidocs.com/2022/eac2b67c-28ae-f057-501e-bff28ea4dcfe.htm

 

Try this:

 

List<ElementId> elementList = _uidoc.Selection.GetElementIds();
List<Reference> referenceList = elementList.Select(x => new Reference(_doc.GetElement(x))).ToList;
try
{
    referenceList = _uidoc.Selection.PickObjects(Selection.ObjectType.Element, new SelectionFilter(), "Select any elements", referenceList);
    elementList = referenceList.Select(x => x.ElementId).ToList;
}
catch  Autodesk.Revit.Exceptions.OperationCanceledException)
{
}
_uidoc.Selection.SetElementIds(elementList);

 

With this selection filter, for example:

 

public class SelectionFilter : Selection.ISelectionFilter
{
    public bool AllowElement(Element element)
    {
        return true;
    }

    public bool AllowReference(Reference refer, XYZ point)
    {
        return false;
    }
}

 

P.S.- I converted this code from VB.NET to C#, so not sure if the syntax is 100% correct.

0 Likes