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 ...
随机推荐
- 用 VeraCrypt 加密闪存盘
导读 很多安全专家偏好像 VeraCrypt 这类能够用来加密闪存盘的开源软件,是因为可以获取到它的源代码.要是你需要在 Windows 系统,苹果的 OS X 系统或者 Linux 系统上加密以及访 ...
- Ubuntu上如何安装Java,Eclipse,Pydev,Python(自带,不用装),BeautifulSoup
如何安装Java,如果出于编程的需要安装Java,需要安装的是JDK,而不仅仅是JRE,下面说说如何在Ubuntu下如何安装JDK:只有两步,1.下载并解压,2.配置环境变量1.下载并解压:下载地址: ...
- PHP读取csv文件
<?php //取数据 $f_d = file_get_contents('tpl_import_info.csv'); $f_d = iconv('gbk', 'utf-8', $f_d); ...
- Eclipse在已创建的project中导入其他文件
Eclipse在已创建的project中导入其他文件 前两天被同事问到,如何通过不拷贝源文件的方式,在之前已经创建好的project中直接导入其他目录下的文件, 整理了一下,将目前所知道的eclips ...
- volatile关键字
[本文链接] http://www.cnblogs.com/hellogiser/p/volatile.html [分析] volatile 关键字是一种类型修饰符,用它声明的类型变量表示可以被某些编 ...
- 关于https和数字证书的一些必须知识
下面这篇文章叫做 HTTPS连接的前几毫秒发生了什么 ,是一篇译文,写得不错,十分有助于理解https. http://blog.jobbole.com/48369/ 下面的链接是百度文库的 数字证 ...
- 转MYSQL学习(三) 函数
这一节主要介绍MYSQL里的函数,MYSQL里的函数很多,我这里主要介绍MYSQL里有而SQLSERVER没有的函数 数学函数 1.求余函数MOD(X,Y) MOD(X,Y)返回x被y除后的余数,MO ...
- Java for LeetCode 054 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】clone-graph
写在前面的话: 看了看自己的博客,从一月底开始就没怎么更新过,我也确实将近5个月没怎么写代码了.今天突然觉得有些心慌,感觉手都已经生疏了.果然,随便找了道题就卡住了.隐约感觉要用map但又不太记得用法 ...
- HDU 5651 xiaoxin juju needs help (组合数)
xiaoxin juju needs helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...