Addin be an element selector, have elements selected when addin terminates

Addin be an element selector, have elements selected when addin terminates

Anonymous
Not applicable
711 Views
5 Replies
Message 1 of 6

Addin be an element selector, have elements selected when addin terminates

Anonymous
Not applicable

I assume an addin can terminate with elements selected because I've seen addins do that. How is this accomplished? I have not seen any examples for this. User selection and automatic selection tasks within the running addin are easily accomplished. What is needed is to have the addin perform the selection task and then exit leaving the selected items still selected so that builtin commands that can operate on a preselection be invoked by the user.

0 Likes
Accepted solutions (1)
712 Views
5 Replies
Replies (5)
Message 2 of 6

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

This example will select all walls in the model and leave it selected. Adjust it to the type you want by replacing the Wall class with the desired one.

Please if this solution satisfies your need don't forget to mark the reply as an answer:

 

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace BIMOasis
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtCmd : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                //SelectionFilterElement sf = new SelectionFilterElement();

                FilteredElementCollector collector = new FilteredElementCollector(cmdData.Application.ActiveUIDocument.Document).OfClass(typeof(Wall));
                List<Element> elements = collector.Cast<Element>().ToList();
                List<ElementId> Ids = new List<ElementId>();

                foreach (Element e in elements)
                {
                    elemSet.Insert(e);
                    Ids.Add(e.Id);
                }

                cmdData.Application.ActiveUIDocument.Selection.SetElementIds(Ids);
                return Result.Succeeded;

            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        #endregion
    }
}

 


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 3 of 6

Anonymous
Not applicable

I had tried Selection.SetElementIds(Ids) without any luck before posting the question. I was using Ids as an

ICollection<ElementId>

within a transaction.   I'll give it another try to see if I can make it work.  Thanks again.

 

0 Likes
Message 4 of 6

Mustafa.Salaheldin
Collaborator
Collaborator

Just try my code and tell me it didn't work. My solution is tested and verified. Smiley Wink


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 5 of 6

Anonymous
Not applicable

Thank you Mustafa.  I think what is needed is to invoke the Selection.SetElementIds at the top level IExternalCommand level. I was able to get this working only by passing the Id list back up to the top as shown in this code. I guess there is a basic concept I am missing that I might know in the future.

 

[Transaction(TransactionMode.Manual)]
    class CmdAimManyLights : IExternalCommand {
        public Result Execute(ExternalCommandData commandData,
                             ref string message,
                             ElementSet elements) {

            UIApplication uiapp = commandData.Application;
            UIDocument _uidoc = uiapp.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application _app = uiapp.Application;
            Autodesk.Revit.DB.Document _doc = _uidoc.Document;

            PlunkOClass plunkThis = new PlunkOClass(commandData.Application);
            BuiltInCategory _bicItemBeingRot = BuiltInCategory.OST_LightingFixtures;

            List<ElementId> _selIds;
            plunkThis.TwoPickAimRotateMany(_bicItemBeingRot, out _selIds);
            _uidoc.Selection.SetElementIds(_selIds);
            return Result.Succeeded;
        }
    }
0 Likes
Message 6 of 6

Mustafa.Salaheldin
Collaborator
Collaborator

OK I'm happy that could help and please don't forget to mark your reply as an answer too.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes