Revit secondary development - modeless window

A non-modal window has the advantage that it can stay in front of the program and continue to complete the operation. However, in the secondary development of Revit, there are also several considerations for non-modal windows.

1. It is necessary to close the non-modal window when the document is closed, otherwise the document will be closed, and the window is still such a strange bug.

2. Events of non-modal windows need to be registered in IExternalCommand.

3. Each operation must be performed in an external event.

The following code pays attention to the last two considerations, the first one can be solved by using the Document event.

First register the event in IExternalCommand.

public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
				ExecuteEventHandler executeEventHandler = new ExecuteEventHandler("Creat Model Line");
				ExternalEvent externalEvent = ExternalEvent.Create(executeEventHandler);
				// show UI
				ModelessView modelessView = new ModelessView(executeEventHandler, externalEvent);              

                //The window is always displayed before the main program
                System.Windows.Interop.WindowInteropHelper mainUI = new System.Windows.Interop.WindowInteropHelper(modelessView);
                mainUI.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
                modelessView.Show();

                return Autodesk.Revit.UI.Result.Succeeded;
            }
            catch (Exception e)
            {
                message = e.Message;
                return Autodesk.Revit.UI.Result.Failed;
            }
        }
    }

Then write a generic external event.

public class ExecuteEventHandler : IExternalEventHandler
    {
        public string Name { get;private set; }

        public Action<UIApplication> ExecuteAction { get; set; }

        public ExecuteEventHandler(string name)
        {
            Name = name;
        }

        public void Execute(UIApplication app)
        {            
            if(ExecuteAction!=null)
            {
                try
                {
                    ExecuteAction(app);
                }
                catch
                { }
            }
        }

        public string GetName()
        {
            return Name;
        }
    }

Next, create components through controls.

public partial class ModelessView : Window
    {        
                
        ExecuteEventHandler _executeEventHandler= null;
        ExternalEvent _externalEvent = null;

        public ModelessView(ExecuteEventHandler executeEventHandler,ExternalEvent externalEvent)
        {
            InitializeComponent();
            _executeEventHandler = executeEventHandler;
            _externalEvent = externalEvent;            
        }

        private void creatLine_Click(object sender, RoutedEventArgs e)
        {
            if(_externalEvent!=null)
            {
                _executeEventHandler.ExecuteAction = new Action<UIApplication>((app) =>
                  {
                      if (app.ActiveUIDocument == null || app.ActiveUIDocument.Document == null)
                          return;

                      Document revitDoc = app.ActiveUIDocument.Document;
                      using (Transaction transaction=new Transaction(revitDoc,"Creat Line1"))
                      {
                          transaction.Start();
                          Autodesk.Revit.DB.Line line = Autodesk.Revit.DB.Line.CreateBound(new XYZ(0, 0, 0), new XYZ(100, 0, 0));
                          SketchPlane sketchPlane = SketchPlane.Create(revitDoc, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero));
                          revitDoc.Create.NewModelCurve(line as Curve, sketchPlane);
                          transaction.Commit();
                      }                      
                  });
                _externalEvent.Raise();
            }
        }

        private void creatLine2_Click(object sender, RoutedEventArgs e)
        {
            if (_externalEvent != null)
            {
                _executeEventHandler.ExecuteAction = new Action<UIApplication>((app) =>
                {
                    if (app.ActiveUIDocument == null || app.ActiveUIDocument.Document == null)
                        return;

                    Document revitDoc = app.ActiveUIDocument.Document;
                    using (Transaction transaction = new Transaction(revitDoc, "Creat Line2"))
                    {
                        transaction.Start();
                        Autodesk.Revit.DB.Line line = Autodesk.Revit.DB.Line.CreateBound(new XYZ(0, 100, 0), new XYZ(100, 100, 0));
                        SketchPlane sketchPlane = SketchPlane.Create(revitDoc, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero));
                        revitDoc.Create.NewModelCurve(line as Curve, sketchPlane);
                        transaction.Commit();
                    }
                });
                _externalEvent.Raise();
            }
        }
    }
This is how a simple modeless window is implemented.
 
 

If you are interested in Revit secondary development and Dyanmo programming, please join the QQ group to communicate: 660319009

For personal inquiries, please add qq: 254033230, I see money, don't bother! ! !


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325852862&siteId=291194637