Finding and Selecting all instances of an element

Finding and Selecting all instances of an element

Sciender
Observer Observer
736 Views
2 Replies
Message 1 of 3

Finding and Selecting all instances of an element

Sciender
Observer
Observer

     Hi, so I'm new to the Revit API and Revit in general, so sorry if this is in the wrong place or formatted weird.

So, I'm trying to make an add-in that will take a group, find all instances of a specific element, then duplicate the original group on top of the center of the found elements.  In this case, I want to find all the Ceiling-Hosted Air Terminals in my drawing and duplicate the group on top of their center point.  I'm using the My First Revit Plugin lessons as sort of a base, but it's not much help for what I'm trying to do.  The code I have already is copy/pasted over from these lessons, but I'm not entirely sure how it ties together, so I can't use it very effectively.  If the answer is too much to put here, I understand, but it would be a huge help just to be pointed in the right direction to research the solution myself, as that hasn't given me much either.  I'll attach my existing code in a .txt file, but I'm making the program in Visual Studio 2022.  Again, sorry if this is formatted weird or in the wrong spot.

0 Likes
737 Views
2 Replies
Replies (2)
Message 2 of 3

SimonaQQ
Advocate
Advocate

Hi,

According to my understanding:

    [Transaction(TransactionMode.Manual)]
    public class CommandTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var uidoc = commandData.Application.ActiveUIDocument;
            var doc = uidoc.Document;
            var selection = uidoc.Selection;

            //select group
            var group = doc.GetElement(selection.PickObject(ObjectType.Element, new GroupSelectionFilter())) as Group;            
            var groupLocation = (group.Location as LocationPoint).Point;

            //find all the Air Terminals in my drawing,Here I take duct as an example
            var ducts = new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(typeof(Duct)).Cast<Duct>().ToList();

            if (ducts != null && ducts.Count > 0)
            {
                //Copy the group to the desired location,Here I take the end of duct as an example
                using (Transaction tra = new Transaction(doc, "CopyGroups"))
                {
                    tra.Start();

                    ducts.ForEach(t =>
                    {
                        var ductLocationLine = (t.Location as LocationCurve).Curve as Line;

                        var end0 = ductLocationLine.GetEndPoint(0);
                        //the end of duct
                        var end1 = ductLocationLine.GetEndPoint(1);
                        //copy
                        var newGroup = ElementTransformUtils.CopyElement(doc, group.Id, end1.Subtract(groupLocation));
                    });

                    tra.Commit();
                }
            }

            return Result.Succeeded;
        }
    }

    public class GroupSelectionFilter : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            if (elem is Group)
            {
                return true;
            }
            return false;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return true;
        }
    }
0 Likes
Message 3 of 3

Sciender
Observer
Observer

Thank you for the help.

Unfortunately, the point of this plugin is to place hanger groups on ceiling-hosted Air Terminals, not just air terminals in general.  What would help me a lot is just a way to find all of the ceiling-hosted air terminals and their center point, and I can probably go from there.  While it wasn't what I needed, I greatly appreciate the time you took to type out that solution.  I'll go ahead and edit my original post to better represent my issue.

0 Likes