Revit二次开发示例:ModelessForm_ExternalEvent

 

#region Namespaces
using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
#endregion

namespace ModelessForm_ExternalEvent
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
    public class App : IExternalApplication
    {
        internal static App thisApp = null;
        private ModelessForm m_MyForm;


        public Result OnStartup(UIControlledApplication a)
        {
            m_MyForm = null;
            thisApp = this;

            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication a)
        {
            if (m_MyForm != null && m_MyForm.Visible)
            {
                m_MyForm.Close();
            }

            return Result.Succeeded;
        }

        public void ShowForm(UIApplication uiapp)
        {
            if (m_MyForm == null || m_MyForm.IsDisposed)
            {
                RequestHandler handler = new RequestHandler();
                ExternalEvent exEvent = ExternalEvent.Create(handler);

                m_MyForm = new ModelessForm(exEvent, handler);
                m_MyForm.Show();
            }
        }

        public void WakeFormUp()
        {
            if (m_MyForm != null)
            {
                m_MyForm.WakeUp();
            }
        }
    }
}

 

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion

namespace ModelessForm_ExternalEvent
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            try
            {
                App.thisApp.ShowForm(commandData.Application);
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }

            
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ModelessForm_ExternalEvent
{
    public enum RequestId : int
    {
        /// <summary>
        /// None
        /// </summary>
        None = 0,
        /// <summary>
        /// "Delete" request
        /// </summary>
        Delete = 1,
        /// <summary>
        /// "FlipLeftRight" request
        /// </summary>
        FlipLeftRight = 2,
        /// <summary>
        /// "FlipInOut" request
        /// </summary>
        FlipInOut = 3,
        /// <summary>
        /// "MakeRight" request
        /// </summary>
        MakeRight = 4,
        /// <summary>
        /// "MakeLeft" request
        /// </summary>
        MakeLeft = 5,
        /// <summary>
        /// "TurnOut" request
        /// </summary>
        TurnOut = 6,
        /// <summary>
        /// "TurnIn" request
        /// </summary>
        TurnIn = 7,
        /// <summary>
        /// "Rotate" request
        /// </summary>
        Rotate = 8
    }

    public class Request
    {
        private int m_request = (int)RequestId.None;

        public RequestId Take()
        {
            return (RequestId)Interlocked.Exchange(ref m_request, (int)RequestId.None);
        }

        public void Make(RequestId request)
        {
            Interlocked.Exchange(ref m_request, (int)request);
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace ModelessForm_ExternalEvent
{
    public class RequestHandler:IExternalEventHandler
    {
        private delegate void DoorOperation(FamilyInstance e);
        private Request m_request = new Request();
        
        public Request Request
        {
            get { return m_request; }
        }

        public string GetName()
        {
            return "R2013 External Event Sample";
        }

        public void Execute(UIApplication uiapp)
        {
            try
            {
                switch (Request.Take())
                {
                    case RequestId.None:
                        {
                            return;
                        }
                    case RequestId.Delete:
                        {
                            ModifySelectedDoors(uiapp, "Delete doors", e => e.Document.Delete(e.Id));
                            break;
                        }
                    case RequestId.FlipLeftRight:
                        {
                            ModifySelectedDoors(uiapp, "Flip door Hand", e => e.flipHand());
                            break;
                        }
                    case RequestId.FlipInOut:
                        {
                            ModifySelectedDoors(uiapp, "Flip door Facing", e => e.flipFacing());
                            break;
                        }
                    case RequestId.MakeLeft:
                        {
                            ModifySelectedDoors(uiapp, "Make door Left", MakeLeft);
                            break;
                        }
                    case RequestId.MakeRight:
                        {
                            ModifySelectedDoors(uiapp, "Make door Right", MakeRight);
                            break;
                        }
                    case RequestId.TurnOut:
                        {
                            ModifySelectedDoors(uiapp, "Place door Out", TurnOut);
                            break;
                        }
                    case RequestId.TurnIn:
                        {
                            ModifySelectedDoors(uiapp, "Place door In", TurnIn);
                            break;
                        }
                    case RequestId.Rotate:
                        {
                            ModifySelectedDoors(uiapp, "Rotate door", FlipHandAndFace);
                            break;
                        }
                    default:
                        {
                            // some kind of a warning here should
                            // notify us about an unexpected request 
                            break;
                        }
                }
            }
            finally
            {

                App.thisApp.WakeFormUp();
            }

            return;
        }


        private void ModifySelectedDoors(UIApplication uiApp, string text, DoorOperation operation)
        {
            UIDocument uidoc = uiApp.ActiveUIDocument;

            if ((uidoc != null) && (uidoc.Selection != null))
            {
                ICollection<ElementId> selElements = uidoc.Selection.GetElementIds();
                if (selElements.Count > 0)
                {
                    FilteredElementCollector collector = new FilteredElementCollector(uidoc.Document, selElements);
                    ICollection<Element> doorset = collector.OfCategory(BuiltInCategory.OST_Doors).ToElements();
                    
                    if (doorset != null)
                    {
                        using (Transaction trans = new Transaction(uidoc.Document))
                        {
                            if (trans.Start(text) == TransactionStatus.Started)
                            {
                                foreach (FamilyInstance door in doorset)
                                {
                                    operation(door);
                                }
                                trans.Commit();
                            }
                        }
                    }
                }
            }
        }

        private void FlipHandAndFace(FamilyInstance e)
        {
            e.flipFacing();
            e.flipHand();
        }

        private static void MakeLeft(FamilyInstance e)
        {
            if (e.FacingFlipped ^ e.HandFlipped) e.flipHand();
        }

        private void MakeRight(FamilyInstance e)
        {
            if (!(e.FacingFlipped ^ e.HandFlipped)) e.flipHand();
        }

        private void TurnIn(FamilyInstance e)
        {
            if (!e.FacingFlipped) e.flipFacing();
        }

        private void TurnOut(FamilyInstance e)
        {
            if (e.FacingFlipped) e.flipFacing();
        }

    }

}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.UI;

namespace ModelessForm_ExternalEvent
{
    public partial class ModelessForm : Form
    {
        private RequestHandler m_Handler;
        private ExternalEvent m_ExEvent;

        public ModelessForm(ExternalEvent exEvent, RequestHandler handler)
        {
            InitializeComponent();

            m_Handler = handler;
            m_ExEvent = exEvent;
        }

        private void ModelessForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_ExEvent.Dispose();
            m_ExEvent = null;
            m_Handler = null;
        }

        private void EnableCommands(bool status)
        {
            foreach (Control ctrl in this.Controls)
            {
                ctrl.Enabled = status;
            }
            if (!status)
            {
                this.btnExit.Enabled = true;
            }
        }

        private void MakeRequest(RequestId request)
        {
            m_Handler.Request.Make(request);
            m_ExEvent.Raise();
            DozeOff();
        }

        private void DozeOff()
        {
            EnableCommands(false);
        }

        public void WakeUp()
        {
            EnableCommands(true);
        }


        private void btnFlipLeftRight_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.FlipLeftRight);
        }

        private void btnFlipInOut_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.FlipInOut);
        }

        private void btnFlipLeft_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.MakeLeft);
        }

        private void btnFlipIn_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.TurnIn);
        }

        private void btnFlipRight_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.MakeRight);
        }

        private void btnFlipOut_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.Rotate);
        }

        private void btnRotate_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.Rotate);
        }

        private void btnDeleted_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.Delete);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        

        

        

        
    }
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.UI;

namespace ModelessForm_ExternalEvent
{
    public partial class ModelessForm : Form
    {
        private RequestHandler m_Handler;
        private ExternalEvent m_ExEvent;

        public ModelessForm(ExternalEvent exEvent, RequestHandler handler)
        {
            InitializeComponent();

            m_Handler = handler;
            m_ExEvent = exEvent;
        }

        private void ModelessForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_ExEvent.Dispose();
            m_ExEvent = null;
            m_Handler = null;
        }

        private void EnableCommands(bool status)
        {
            foreach (Control ctrl in this.Controls)
            {
                ctrl.Enabled = status;
            }
            if (!status)
            {
                this.btnExit.Enabled = true;
            }
        }

        private void MakeRequest(RequestId request)
        {
            m_Handler.Request.Make(request);
            m_ExEvent.Raise();
            DozeOff();
        }

        private void DozeOff()
        {
            EnableCommands(false);
        }

        public void WakeUp()
        {
            EnableCommands(true);
        }


        private void btnFlipLeftRight_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.FlipLeftRight);
        }

        private void btnFlipInOut_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.FlipInOut);
        }

        private void btnFlipLeft_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.MakeLeft);
        }

        private void btnFlipIn_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.TurnIn);
        }

        private void btnFlipRight_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.MakeRight);
        }

        private void btnFlipOut_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.Rotate);
        }

        private void btnRotate_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.Rotate);
        }

        private void btnDeleted_Click(object sender, EventArgs e)
        {
            MakeRequest(RequestId.Delete);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        

        

        

        
    }
}

你可能感兴趣的:(extern)