Event handler respond with OK after called Purge button

Event handler respond with OK after called Purge button

Shai.Nguyen
Advocate Advocate
1,319 Views
8 Replies
Message 1 of 9

Event handler respond with OK after called Purge button

Shai.Nguyen
Advocate
Advocate

My idea is same as: https://forums.autodesk.com/t5/revit-api-forum/purge-unused/m-p/7170536/highlight/true#M23707

I want to clean up the revit file before sent it to client. One of those is purge at least 3 times. How can I write an Event that respond with OK?
Thank you in advanced, your solution is appriciated.

purge.PNG

0 Likes
Accepted solutions (2)
1,320 Views
8 Replies
Replies (8)
Message 2 of 9

jeremytammik
Autodesk
Autodesk

You can look at the JtClicker windows form clicker app:

 

http://thebuildingcoder.typepad.com/blog/2009/10/dismiss-dialogue-using-windows-api.html

 

https://github.com/jeremytammik/JtClicker

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 9

Shai.Nguyen
Advocate
Advocate

Thanks Jeremy, I will take a look

0 Likes
Message 4 of 9

RPTHOMAS108
Mentor
Mentor

Basic example:

 

Imports Autodesk.Revit.UI
Imports Autodesk.Revit.DB

Public Class MultiPurgeExampleClass
    Private Shared RunCount As Integer = 0
    Private IntApp As Autodesk.Revit.UI.UIApplication = Nothing
    Private IntDoc As Document = Nothing

    Public Sub Run()
        If IntDoc Is Nothing Then Exit Sub Else 
        'Code upto this point your choice (part of IExternalCommand)
        Try
            AddHandler IntApp.DialogBoxShowing, AddressOf ReturnOKResult
        Catch ex As Exception
        End Try
        RunCount = 0
        ReRun()
    End Sub
    Public Sub New(ByVal CMD As Autodesk.Revit.UI.ExternalCommandData, _
                           ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet)
        IntApp = CMD.Application
        If IntApp.ActiveUIDocument Is Nothing Then Exit Sub Else 
        IntDoc = IntApp.ActiveUIDocument.Document
    End Sub

    Private Sub ReRun()
        RunCount += 1
        Dim CDMID As Autodesk.Revit.UI.RevitCommandId = _
        Autodesk.Revit.UI.RevitCommandId.LookupPostableCommandId(PostableCommand.PurgeUnused)
        IntApp.PostCommand(CDMID)
    End Sub
    Private Sub ReturnOKResult(s As Object, e As Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs)
        e.OverrideResult(MsgBoxResult.Ok)
        If RunCount < 5 Then
            ReRun()
        Else
            RunCount = 0
            Try
                RemoveHandler IntApp.DialogBoxShowing, AddressOf ReturnOKResult
            Catch ex As Exception
            End Try
            MsgBox("Multi purge complete.", Title:="Information")
        End If
    End Sub
End Class
Message 5 of 9

Shai.Nguyen
Advocate
Advocate
Hi Thomas, I'm using C#. I wondering if you can translate this code to C# language. I'll very appriciate
0 Likes
Message 6 of 9

RPTHOMAS108
Mentor
Mentor
Accepted solution

Create a new instance of MultiPurgeExample and then start it with Run.

 

C#

 

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;

public class MultiPurgeExampleClass
{
    private static int RunCount = 0;
    private Autodesk.Revit.UI.UIApplication IntApp = null;

    private Document IntDoc = null;
    public void Run()
    {
        if (IntDoc == null)
            return;
        //Code upto this point your choice (part of IExternalCommand)
        try
        {
            IntApp.DialogBoxShowing += ReturnOKResult;
        }
        catch (Exception ex)
        {
        }
        RunCount = 0;
        ReRun();
    }
    public MultiPurgeExampleClass(Autodesk.Revit.UI.ExternalCommandData CMD, ref string message, Autodesk.Revit.DB.ElementSet elements)
    {
        IntApp = CMD.Application;
        if (IntApp.ActiveUIDocument == null)
            return;
        IntDoc = IntApp.ActiveUIDocument.Document;
    }

    private void ReRun()
    {
        RunCount += 1;
        Autodesk.Revit.UI.RevitCommandId CDMID = Autodesk.Revit.UI.RevitCommandId.LookupPostableCommandId(PostableCommand.PurgeUnused);
        IntApp.PostCommand(CDMID);
    }
    private void ReturnOKResult(object s, Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs e)
    {
        e.OverrideResult(MsgBoxResult.Ok);
        if (RunCount < 5)
        {
            ReRun();
        }
        else
        {
            RunCount = 0;
            try
            {
                IntApp.DialogBoxShowing -= ReturnOKResult;
            }
            catch (Exception ex)
            {
            }
            Interaction.MsgBox("Multi purge complete.", Title: "Information");
        }
    }
}
Message 7 of 9

Shai.Nguyen
Advocate
Advocate

Hi Thomas, why I have an error here?

loi.PNG

0 Likes
Message 8 of 9

RPTHOMAS108
Mentor
Mentor
Accepted solution

It is a visual basic namespace that is missing but you don't need it, bad conversion from VB on my part:

 

Use instead TaskDialogResult.OK for the first error (it is just a value of 1)

 

Use instead a TaskDialog.Show for the second error (if you care about being told the task is complete)

0 Likes
Message 9 of 9

Shai.Nguyen
Advocate
Advocate

Thanks again. You are fast like a thunder storm

0 Likes