• Revit二次开发示例:ModelessForm_ExternalEvent


    使用Idling事件处理插件任务。

    #region Namespaces
    using System;
    using System.Collections.Generic;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Events;
    #endregion
    
    namespace ModelessForm_IdlingEvent
    {
        [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
        [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
        [Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
        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.IsDisposed)
                {
                    m_MyForm.Dispose();
                    m_MyForm = null;
    
                    a.Idling -= IdlingHandler;
                }
    
                return Result.Succeeded;
            }
    
            public void ShowForm(UIApplication uiapp)
            {
                if (m_MyForm == null || m_MyForm.IsDisposed)
                {
                    m_MyForm = new ModelessForm();
                    m_MyForm.Show();
    
                    uiapp.Idling += IdlingHandler;
                }
            }
    
            public void IdlingHandler(object sender, IdlingEventArgs args)
            {
                UIApplication uiapp = sender as UIApplication;
    
                if (m_MyForm.IsDisposed)
                {
                    uiapp.Idling -= IdlingHandler;
                    return;
                }
                else
                {
                    RequestId request = m_MyForm.Request.Take();
    
                    if (request != RequestId.None)
                    {
                        try
                        {
                            RequestHandler.Execute(uiapp, request);
                        }
                        finally
                        {
    
                            m_MyForm.WakeUp();
                        }
                    }
                }
    
                return;
            }
        }
    }

     

    #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_IdlingEvent
    {
        [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 Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    
    namespace ModelessForm_IdlingEvent
    {
        public static class RequestHandler
        {
            private delegate void DoorOperation(FamilyInstance e);
    
            public static void Execute(UIApplication uiapp, RequestId request)
            {
                switch (request)
                {
                    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:
                        {
                            break;
                        }
                }
                return;
            }
    
            private static 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 static void FlipHandAndFace(FamilyInstance e)
            {
                e.flipFacing();
                e.flipHand();
            }
    
            private static void MakeLeft(FamilyInstance e)
            {
                if (e.FacingFlipped ^ e.HandFlipped) e.flipHand();
            }
    
            private static void MakeRight(FamilyInstance e)
            {
                if (!(e.FacingFlipped ^ e.HandFlipped)) e.flipHand();
            }
    
            private static void TurnIn(FamilyInstance e)
            {
                if (!e.FacingFlipped) e.flipFacing();
            }
    
            private static void TurnOut(FamilyInstance e)
            {
                if (e.FacingFlipped) e.flipFacing();
            }
    
    
        }
    }

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace ModelessForm_IdlingEvent
    {
        public enum RequestId:int
        {
            None = 0,
            Delete=1,
            FlipLeftRight=2,
            FlipInOut=3,
            MakeRight=4,
            MakeLeft=5,
            TurnOut=6,
            TurnIn=7,
            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.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ModelessForm_IdlingEvent
    {
        public partial class ModelessForm : Form
        {
            private Request m_request;
    
            public Request Request
            {
                get
                {
                    return m_request;
                }
                private set
                {
                    m_request = value;
                }
            }
    
            public ModelessForm()
            {
                InitializeComponent();
                Request = new Request();
            }
    
            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)
            {
                Request.Make(request);
                DozeOff();
            }
    
            private void DozeOff()
            {
                EnableCommands(false);
            }
    
            public void WakeUp()
            {
                EnableCommands(true);
            }
    
            private void btnFlipLeftRight_Click(object sender, EventArgs e)
            {
                MakeRequest(RequestId.FlipLeftRight);
            }
    
            private void btnFlipUpDown_Click(object sender, EventArgs e)
            {
                MakeRequest(RequestId.FlipInOut);
            }
    
            private void btnFlipLeft_Click(object sender, EventArgs e)
            {
                MakeRequest(RequestId.MakeLeft);
            }
    
            private void btnFlipUp_Click(object sender, EventArgs e)
            {
                MakeRequest(RequestId.TurnIn);
            }
    
            private void btnFlipRight_Click(object sender, EventArgs e)
            {
                MakeRequest(RequestId.MakeRight);
            }
    
            private void btnFlipDown_Click(object sender, EventArgs e)
            {
                MakeRequest(RequestId.TurnOut);
            }
    
            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();
            }
        }
    }
  • 相关阅读:
    css实现垂直居中
    js验证输入框
    textarea统计字数
    ajax提交form表单
    JS中的正则表达式
    《遮蔽层的隐藏块》
    2016各大互联网公司前端面试题汇总
    JQ 添加节点和插入节点的方法总结
    [原]CentOS7部署osm2pgsql
    [原]CentOS7部署PostGis
  • 原文地址:https://www.cnblogs.com/xpvincent/p/3620389.html
Copyright © 2020-2023  润新知