带有进度条和“取消”按钮的Revit外部命令

问题描述 投票:0回答:1

我正在为Revit开发新的外部命令。它需要一个进度条+一个按钮以随时取消其执行。为了得到它,我实施了一个外部事件。

用要由命令执行的代码来实现外部事件处理程序。

public class GestorDeEventoExterno : IExternalEventHandler
{
    public bool CancellationRequested { get; set; }
    private VentanaDeProgreso progressAndcancelWindow;
    private EventWaitHandle eventWait;

    public void Execute(UIApplication aplicacionDeLaIU)
    {
        using (this.eventWait = new AutoResetEvent(false))
        {
            // New thread for the progress bar.
            Thread progressBarThread = new Thread(new ThreadStart(() =>
            {
                // Populating the progress bar window.
                this.progressAndcancelWindow = new VentanaDeProgreso(this);
                progressAndcancelWindow.Show();

                // Chenge the state of the wait event.
                this.eventWait.Set();
                Dispatcher.Run();
            }));

            progressBarThread.SetApartmentState(ApartmentState.STA);
            progressBarThread.IsBackground = true;
            progressBarThread.Start();
            this.eventWait.WaitOne();
        }

        // Get the current revit document.
        Document documentoActivo = aplicacionDeLaIU.ActiveUIDocument.Document;


        // Code to simulate the revit command operation.
        for (int i = 0;
            i <= 100;
            i++)
        {
            // Code to be executed if a cancellation has been requested.
            if (this.CancellationRequested)
            {
                TaskDialog.Show("Test", "Cancel");
                this.progressAndcancelWindow.Dispatcher.Invoke(new Action(this.progressAndcancelWindow.Close));
                return;
            }

            this.progressAndcancelWindow.ActualizarProgreso($"loop number: {i}", i, 100);

            Thread.Sleep(100);
        }

        this.progressAndcancelWindow.Dispatcher.Invoke(new Action(this.progressAndcancelWindow.Close));
        TaskDialog.Show("Test", "END");

    }

    public string GetName()
    {
        return "test";
    }
}

执行外部命令以注册外部事件并填充主窗口

public class Class1 : IExternalCommand
{
    public Result Execute(
        ExternalCommandData externalCommandData,
        ref string message,
        ElementSet elements)
    {
        // Registering the external event.
        GestorDeEventoExterno externalEventHandler = new GestorDeEventoExterno();
        ExternalEvent externalEvent = ExternalEvent.Create(externalEventHandler);

        // Populating the main window.
        VentanaPrincipal mainWindow = new VentanaPrincipal(
            externalEvent);
        mainWindow.Show();

        return Result.Succeeded;
    }
}

最后,进度条窗口后面的代码

public partial class VentanaDeProgreso : Window
{
    private GestorDeEventoExterno externalEventHandler;

    public void ActualizarProgreso(
        string texto,
        int valorActual,
        int valortotal = 100)
    {
        this.Dispatcher.Invoke(
            new Action<string, int, int>(
                delegate (string txt, int vActual, int vTotal)
                {
                    this.IndicadorDeProgreso.Value = valorActual;
                    this.IndicadorDeProgreso.Maximum = vTotal;
                    this.Texto.Text = txt;
                }),
            System.Windows.Threading.DispatcherPriority.Background,
            texto,
            valorActual,
            valortotal);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Asignación de valor verdadero a la propiedad de cancelación solicitada del evento externo.
        this.externalEventHandler.CancellationRequested = true;
    }

    public VentanaDeProgreso(GestorDeEventoExterno externalEventHandler)
    {
        InitializeComponent();
        this.externalEventHandler= externalEventHandler;
    }
}

正如您所看到的,进度窗口具有外部事件处理程序作为属性,而取消按钮单击事件将属性设置为'CancellationRequested'。

我的问题是:我该如何改善?

api revit
1个回答
0
投票

您不需要外部事件即可取消命令。

您只需要一个外部事件就可以取消提交从不可用的上下文中执行Revit API功能的请求。

您的取消不需要Revit API功能,只需您自己的东西,因此不需要任何外部事件。

因此,您可以更简单地重组整个解决方案。 Kiss!

© www.soinside.com 2019 - 2024. All rights reserved.