得到选择的对象,过滤选择的对象。


start


[Transaction(TransactionMode.Manual)]

[Regeneration(RegenerationOption.Manual)]

public class Document_Selection : IExternalCommand

{

    public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)

    {

        try

        {

            UIDocument uidoc = commandData.Application.ActiveUIDocument;

            //得到选择的对象

            Selection selection = uidoc.Selection;

            ElementSet collection = selection.Elements;

            if (0 == collection.Size)

            {

                TaskDialog.Show("Revit", "You haven't selected any elements");

            }

            else

            {

                string info = "Ids of selected elements in the document are:";

                foreach (Element elem in collection)

                {

                    info += "\n\t" + elem.Id.IntegerValue;

                }

                TaskDialog.Show("Revit", info);

            }


            //得到当前文档中门的ID

            ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));

            ElementCategoryFilter doorsCategoryfilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);

            LogicalAndFilter doorsInstancefilter = new LogicalAndFilter(familyInstanceFilter, doorsCategoryfilter);

            FilteredElementCollector collector = new FilteredElementCollector(uidoc.Document);

            ICollection<ElementId> doors = collector.WherePasses(doorsInstancefilter).ToElementIds();

            string prompt = "the ids of the doors in the current document are:";

            foreach (ElementId id in doors)

            {

                prompt += "\n\t" + id.IntegerValue;

            }

            TaskDialog.Show("Revit", prompt);

        }

        catch (Exception e)

        {

            messages = e.Message;

            return Result.Failed;

        }

        return Result.Succeeded;

    }

}

end