Execute code while pipe command is running

Execute code while pipe command is running

Gilles.Lagrilliere
Advocate Advocate
1,455 Views
13 Replies
Message 1 of 14

Execute code while pipe command is running

Gilles.Lagrilliere
Advocate
Advocate

Hello everyone

 

Is it possible to run code while running the pipe command in Revit?

for example:

when the pipe command is running I want to automatically specify the system type based on the pipe type name.

 

 

0 Likes
1,456 Views
13 Replies
Replies (13)
Message 2 of 14

ameer.mansourWAK8Y
Advocate
Advocate
0 Likes
Message 3 of 14

Gilles.Lagrilliere
Advocate
Advocate

Hello Ameer

 

How do I use AddInCommandBinding?

I looked in the sdk sample but I still don't understand it.

0 Likes
Message 4 of 14

ameer.mansourWAK8Y
Advocate
Advocate
_pipeCommandId = RevitCommandId.LookupPostableCommandId(PostableCommand.Pipe); 

AddInCommandBinding _bindedCommandId = _uiApp.CreateAddInCommandBinding(_pipeCommandId); 

_bindedCommandId.BeforeExecuted += ImportBinding_PipeCommand_BeforeExecuted; 

_bindedCommandId.Executed += ImportBinding_PipeCommand_Executed;


//then you create your method of executed and before executed and set //whatever you want to set



private void ImportBinding_PipeCommand_BeforeExecuted(object sender, Autodesk.Revit.UI.Events.BeforeExecutedEventArgs e)
 {
 //change any properties 
} 

private void _bindedCommandId_Executed(object sender, Autodesk.Revit.UI.Events.ExecutedEventArgs e)
 {
 //get created element and change any properties 
}
0 Likes
Message 5 of 14

ameer.mansourWAK8Y
Advocate
Advocate

or you can use Iupdater 


    class CustomUpdater: IUpdater
    {

        #region Fields

        AddInId _appId;
        UpdaterId _updaterId;

        // constructor takes the AddInId for the add-in associated with this updater
        DrawPipeFittingPaletteUpdater(AddInId id)
        {
            _appId = id;
            _updaterId = new UpdaterId(_appId, new Guid("addGuidHere"));
        }

        #endregion

        #region Properties



        #endregion

        #region Constructors



        #endregion

        #region Methods

        public void Execute(UpdaterData data)
        {
            var doc = data.GetDocument();
            
            foreach (ElementId elemId in data.GetAddedElementIds())
            {
                var selectedElement = doc.GetElement(elemId);
               
                
            }
        }

        public string GetAdditionalInformation()
        {
            return "Updater to update.";
        }

        public ChangePriority GetChangePriority()
        {
            return ChangePriority.MEPAccessoriesFittingsSegmentsWires;
        }

        public UpdaterId GetUpdaterId()
        {
            return _updaterId;
        }

        public string GetUpdaterName()
        {
            return "Custom Updater";
        }


        #endregion


        /// <summary>
        /// To be called in Iexternal Command onStartup Event.
        /// </summary>
        /// <param name="id">application.ActiveAddInId</param>
        internal static void RegisterUpdater(AddInId AddinId)
        {
            // Register wall updater with Revit
            CustomUpdater updater = new CustomUpdater(AddinId);
            UpdaterRegistry.RegisterUpdater(updater, true);

            // Change Scope = any Pipe element
            ElementCategoryFilter pipeFitingFilter = new ElementCategoryFilter(BuiltInCategory.OST_PipeFitting);

            // Change type = element addition
            UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), pipeFitingFilter,
                                        Element.GetChangeTypeElementAddition());
        }

        /// <summary>
        /// To be called in Iexternal Command onShutDown Event.
        /// </summary>
        /// <param name="id">application.ActiveAddInId</param>
        internal static void UnregisterUpdater(AddInId AddinId)
        {
            CustomUpdaterupdater = new CustomUpdater(AddinId);
            if (UpdaterRegistry.IsUpdaterRegistered(updater.GetUpdaterId()))
            {
                UpdaterRegistry.UnregisterUpdater(updater.GetUpdaterId());
            }
        }
    }
0 Likes
Message 6 of 14

MarryTookMyCoffe
Collaborator
Collaborator

omg, so much code. You can do it in more simple way, there is a event before button is execute. I thing you have to add UIFramework.dll to project to get to it but I used in past to change symbol of item that i what to input on project.

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes
Message 7 of 14

Gilles.Lagrilliere
Advocate
Advocate

I'm gonna try these solutions and I hope one of them will work.

Right now I don't have time to try these solutions. I have time in the holidays. 

So I will update this as soon as I get to it.

0 Likes
Message 8 of 14

ameer.mansourWAK8Y
Advocate
Advocate

really?

can you provide a sample?

0 Likes
Message 9 of 14

Gilles.Lagrilliere
Advocate
Advocate

Hey

 

I tested this solution but it isn't working for me.

 

what I want is when I use the pipe command I can choose a pipe type that I want to draw.

Then I want the System Type parameter to change accordingly.

 

If I use this

 private void Importbinding_PipeCommand_Executed(object sender, ExecutedEventArgs e)
{
TaskDialog.Show("test", "test");
} 

 

The command gets overridden and doesn't want to draw pipes anymore. It only shows the TaskDialog.

 

0 Likes
Message 10 of 14

Gilles.Lagrilliere
Advocate
Advocate

Hello @ameer.mansourWAK8Y  and @MarryTookMyCoffe 

 

Does anyone has an answer for this?

I don't want to override the command. 

I want to change some parameters while the command is running.

 

0 Likes
Message 11 of 14

ameer.mansourWAK8Y
Advocate
Advocate

did you try Iupdater?
Iupdater doesn't override command but runs after pipe command excuted.

0 Likes
Message 12 of 14

Gilles.Lagrilliere
Advocate
Advocate

Hey @ameer.mansourWAK8Y 

 

I tried to use the IUpdater but I didn't know how to use it. So I tried to play around with the code and hope it will succeed somehow 😄

 

Here is my code:

namespace Test
{
    class Test : IExternalApplication
    {
        public Result OnShutdown(UIControlledApplication application)
        {
            CustomUpdater updater = new CustomUpdater(application.ActiveAddInId);
                UpdaterRegistry.UnregisterUpdater(updater.GetUpdaterId());

            return Result.Succeeded; ;
        }

        public Result OnStartup(UIControlledApplication application)
        {
            // Register Pipe updater with Revit
            CustomUpdater updater = new CustomUpdater(application.ActiveAddInId);
            UpdaterRegistry.RegisterUpdater(updater);

            // Change Scope = any Pipe element
            ElementCategoryFilter pipeFilter = new ElementCategoryFilter(BuiltInCategory.OST_PipeCurves);

            // Change type = element addition
            UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), pipeFilter,
                                        Element.GetChangeTypeElementAddition());

            return Result.Succeeded;
        }
    }

    class CustomUpdater : IUpdater
    {
        AddInId _appId;
        UpdaterId _updaterId;

        // constructor takes the AddInId for the add-in associated with this updater
        public CustomUpdater(AddInId addinId)
        {
            _appId = addinId;
            _updaterId = new UpdaterId(_appId, new Guid("FBFBF6B2-4C06-42d4-97C1-D1B4EB593EFF"));
        }

        public void Execute(UpdaterData data)
        {
            var doc = data.GetDocument();

            foreach (ElementId elemId in data.GetAddedElementIds())
            {
                var selectedElement = doc.GetElement(elemId);

                if (selectedElement.GetType() == typeof(Pipe) )
                {
                    Pipe p = selectedElement as Pipe;

                    string pipeTypeName = p.Name;

                    var pst = new FilteredElementCollector(doc)
                        .OfClass(typeof(PipingSystemType));

                    ElementId systemTypeId = new ElementId(0);

                    foreach (PipingSystemType i in pst)
                    {
                        if (i.Name == pipeTypeName)
                        {
                            systemTypeId = i.Id;
                            break;
                        }
                    }

                    Transaction t = new Transaction(doc);

                    t.Start("AutoChange PipeSystemType");

                    p.SetSystemType(systemTypeId);

                    t.Commit();
                }
                
            }
        }

        public string GetAdditionalInformation()
        {
            return "Updater to update.";
        }

        public ChangePriority GetChangePriority()
        {
            return ChangePriority.MEPAccessoriesFittingsSegmentsWires;
        }

        public UpdaterId GetUpdaterId()
        {
            return _updaterId;
        }

        public string GetUpdaterName()
        {
            return "Custom Updater";
        }
    }
}

  

0 Likes
Message 13 of 14

ameer.mansourWAK8Y
Advocate
Advocate

you should not open transactions in executed function.

0 Likes
Message 14 of 14

Gilles.Lagrilliere
Advocate
Advocate

Hello

 

It still doesn't do anything I'm not even getting an error message or something like that.

I'm still trying to understand how this IUpdater works with the help of the SDK samples but so far I still don't get it.

0 Likes