BuildingCoder.CmdViewsShowingElements.Execute C# (CSharp) Method

Execute() public method

public Execute ( ExternalCommandData revit, string &message, ElementSet elements ) : System.Result
revit ExternalCommandData
message string
elements ElementSet
return System.Result
        public Result Execute(
            ExternalCommandData revit,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = revit.Application;
              UIDocument uidoc = uiapp.ActiveUIDocument;
              Document doc = uidoc.Document;

              // Retrieve pre-selected elements.

              ICollection<ElementId> ids
            = uidoc.Selection.GetElementIds();

              if( 0 == ids.Count )
              {
            message = "Please pre-select some elements "
              + "before launching this command to list "
              + "the views displaying them.";

            return Result.Failed;
              }

              // Determine views displaying them.

              IEnumerable<Element> targets
            = from id in ids select doc.GetElement( id );

              IEnumerable<View> views = targets
            .FindAllViewsWhereAllElementsVisible();

              // Report results.

              string names = string.Join( ", ",
            ( from v in views select v.Name ) );

              int nElems = targets.Count<Element>();

              int nViews = names.Count<char>(
            c => ',' == c ) + 1;

              TaskDialog dlg = new TaskDialog( string.Format(
            "{0} element{1} are visible in {2} view{3}",
            nElems, Util.PluralSuffix( nElems ),
            nViews, Util.PluralSuffix( nViews ) ) );

              dlg.MainInstruction = names;

              dlg.Show();

              return Result.Succeeded;
        }
CmdViewsShowingElements