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();
}
}
}
Revit二次开发示例:ModelessForm_ExternalEvent的更多相关文章
- Revit二次开发示例:HelloRevit
本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...
- Revit二次开发示例:EventsMonitor
在该示例中,插件在Revit启动时弹出事件监控选择界面,供用户设置,也可在添加的Ribbon界面完成设置.当Revit进行相应操作时,弹出窗体会记录事件时间和名称. #region Namespace ...
- Revit二次开发示例:ErrorHandling
本示例介绍了Revit的错误处理. #region Namespaces using System; using System.Collections.Generic; using Autodes ...
- Revit二次开发示例:ChangesMonitor
在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息. #region Namespaces using System; using System.Collections.Ge ...
- Revit二次开发示例:AutoStamp
该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印. #region Namespaces using System; using System.Collect ...
- Revit二次开发示例:Journaling
关于Revit Journal读写的例子. #region Namespaces using System; using System.Collections.Generic; using Sys ...
- Revit二次开发示例:DisableCommand
Revit API 不支持调用Revit内部命令,但可以用RevitCommandId重写它们(包含任意选项卡,菜单和右键命令).使用RevitCommandId.LookupCommandId()可 ...
- Revit二次开发示例:DesignOptions
本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...
- Revit二次开发示例:DeleteObject
在本例中,通过命令可以删除选中的元素. 需要注意的是要在代码中加入Transaction,否则的话会出现Modifying is forbidden because the document has ...
随机推荐
- HTML快速入门2
三.版面风格控制 1. 字体控制 A. 字体大小 用 <font Size=#> 和 </font> 表示,#为字号: 1 - 7 ,缺省为 3 ,可用 <basefon ...
- LInux 安全测试
[CVE-2013-2094]Linux PREF_EVENTS Local Root 2.6.37-3.8.10 x86_64 踩(0)http://zone.wooyun.org/content/ ...
- div设置边框黑框显示
style="width:756px; height:68px; border:1px solid #000000;"
- 编译安装0bda 8179无线网卡
CentOS下安装USB无线网卡(Obda:8179) 参考:http://blog.163.com/thinki_cao/blog/static/83944875201311593529913/ c ...
- iOS category中的所谓属性 和 从xib初始化对象的方法 以及类扩展
今天在编码时遇到以下代码 @interface UITextField (TCCustomFont) @property (nonatomic, copy) NSString* fontName; @ ...
- php中常用魔术方法的举例
魔术方法是php面向对象特有的功能,并且有时候能实现意想不到的效果,包括前面提到的构造函数.析构函数.还有__clone函数,另外再简单的介绍几个: 1.__toSring和__invoke clas ...
- NEFU 1146 又见A+B
又见a+b Problem:1146 Time Limit:1000ms Memory Limit:65535K Description 给定两个非负整数A,B,求他们的和. Input 多组输入,每 ...
- IE的浏览器模式和文档模式
只有IE浏览器中才会有“浏览器模式”和“文档模式”,兼容性视图涉及两个重要的功能 便是“浏览器模式[browser mode]”和“文档模式[document mode]”,在IE8/IE9中按F12 ...
- 纯css3 加载loading动画特效
最近项目中要实现当页面还没有加载完给用户提示正在加载的loading,本来是想做个图片提示的,但是图片如果放大电脑的分辨率就会感觉到很虚,体验效果很不好.于是就采用css3+js实现这个loading ...
- RecyclerView拖拽排序和滑动删除实现
效果图 如何实现 那么是如何实现的呢?主要就要使用到ItemTouchHelper ,ItemTouchHelper 一个帮助开发人员处理拖拽和滑动删除的实现类,它能够让你非常容易实现侧滑删除.拖拽的 ...