添加项目文件。
This commit is contained in:
48
DotNet.Revit.ExternalEvent/CmdExternalEvent.cs
Normal file
48
DotNet.Revit.ExternalEvent/CmdExternalEvent.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Autodesk.Revit.Attributes;
|
||||
using DotNet.Revit.ExternalEvent;
|
||||
using System.Windows.Interop;
|
||||
using Autodesk.Windows;
|
||||
|
||||
namespace DotNet.Revit.ExternalEvent
|
||||
{
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
public class CmdExternalEvent : IExternalCommand
|
||||
{
|
||||
private static CmdExternalEvent m_Instance;
|
||||
private ExternalEventHelper m_ExternalEventHelper;
|
||||
|
||||
public static CmdExternalEvent Instance
|
||||
{
|
||||
get { return m_Instance; }
|
||||
}
|
||||
|
||||
public ExternalEventHelper ExternalEventHelper
|
||||
{
|
||||
get { return m_ExternalEventHelper; }
|
||||
}
|
||||
|
||||
|
||||
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
||||
{
|
||||
m_Instance = this;
|
||||
m_ExternalEventHelper = new ExternalEventHelper(commandData.Application);
|
||||
|
||||
|
||||
var main = new MainWinodow();
|
||||
var mainHelper = new WindowInteropHelper(main);
|
||||
mainHelper.Owner = ComponentManager.ApplicationWindow;
|
||||
main.Show();
|
||||
|
||||
return Result.Succeeded;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
24
DotNet.Revit.ExternalEvent/DocumentExtension.cs
Normal file
24
DotNet.Revit.ExternalEvent/DocumentExtension.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DotNet.Revit.ExternalEvent
|
||||
{
|
||||
public static class DocumentExtension
|
||||
{
|
||||
public static void Invoke(this Document doc, Action<Transaction> action, string name = "INVOKE")
|
||||
{
|
||||
using (var tr = new Transaction(doc, name))
|
||||
{
|
||||
tr.Start();
|
||||
action(tr);
|
||||
|
||||
if (tr.GetStatus() == TransactionStatus.Started)
|
||||
tr.Commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
80
DotNet.Revit.ExternalEvent/DotNet.Revit.ExternalEvent.csproj
Normal file
80
DotNet.Revit.ExternalEvent/DotNet.Revit.ExternalEvent.csproj
Normal file
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{7164311A-01C1-4FAB-97DB-B9310C973800}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>DotNet.Revit.ExternalEvent</RootNamespace>
|
||||
<AssemblyName>DotNet.Revit.ExternalEvent</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="AdWindows, Version=6.3.0.21, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\CYBIM\CYCommons\References\Revit\16\AdWindows.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="PresentationCore" />
|
||||
<Reference Include="PresentationFramework" />
|
||||
<Reference Include="RevitAPI">
|
||||
<HintPath>..\..\..\..\CYBIM\CYCommons\References\Revit\16\RevitAPI.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RevitAPIUI">
|
||||
<HintPath>..\..\..\..\CYBIM\CYCommons\References\Revit\16\RevitAPIUI.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xaml" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CmdExternalEvent.cs" />
|
||||
<Compile Include="DocumentExtension.cs" />
|
||||
<Compile Include="ExternalEventHelper.cs" />
|
||||
<Compile Include="MainWinodow.xaml.cs">
|
||||
<DependentUpon>MainWinodow.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Page Include="MainWinodow.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
124
DotNet.Revit.ExternalEvent/ExternalEventHelper.cs
Normal file
124
DotNet.Revit.ExternalEvent/ExternalEventHelper.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using Autodesk.Revit.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DotNet.Revit.ExternalEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// 外部事件的封装.
|
||||
/// </summary>
|
||||
public class ExternalEventHelper
|
||||
{
|
||||
private readonly ExternalEventHandlerCommon externalEventHandlerCommon;
|
||||
|
||||
private readonly Autodesk.Revit.UI.ExternalEvent externalEvent;
|
||||
|
||||
/// <summary>
|
||||
/// 外部事件刚刚开始并且准备执行时触发.
|
||||
/// </summary>
|
||||
public event EventHandler<ExternalEventArg> Started;
|
||||
|
||||
/// <summary>
|
||||
/// 外部事件结束时触发.
|
||||
/// </summary>
|
||||
public event EventHandler<ExternalEventArg> End;
|
||||
|
||||
public ExternalEventHelper(UIApplication uiApp)
|
||||
{
|
||||
this.externalEventHandlerCommon = new ExternalEventHandlerCommon();
|
||||
this.externalEvent = Autodesk.Revit.UI.ExternalEvent.Create(this.externalEventHandlerCommon);
|
||||
|
||||
this.externalEventHandlerCommon.Started += externalEventCommon_Started;
|
||||
this.externalEventHandlerCommon.End += externalEventCommon_End;
|
||||
}
|
||||
|
||||
public ExternalEventHelper(UIControlledApplication uiControlApp)
|
||||
{
|
||||
this.externalEventHandlerCommon = new ExternalEventHandlerCommon();
|
||||
this.externalEvent = Autodesk.Revit.UI.ExternalEvent.Create(this.externalEventHandlerCommon);
|
||||
|
||||
this.externalEventHandlerCommon.Started += externalEventCommon_Started;
|
||||
this.externalEventHandlerCommon.End += externalEventCommon_End;
|
||||
}
|
||||
|
||||
public void Invoke(Action<UIApplication> action, string name = "")
|
||||
{
|
||||
var nf = string.IsNullOrWhiteSpace(name) ? Guid.NewGuid().ToString() : name;
|
||||
this.externalEventHandlerCommon.Actions.Enqueue(new KeyValuePair<string, Action<UIApplication>>(nf, action));
|
||||
this.externalEvent.Raise();
|
||||
}
|
||||
|
||||
private void externalEventCommon_End(object sender, ExternalEventArg e)
|
||||
{
|
||||
if (this.End != null)
|
||||
this.End(this, e);
|
||||
}
|
||||
|
||||
private void externalEventCommon_Started(object sender, ExternalEventArg e)
|
||||
{
|
||||
if (this.Started != null)
|
||||
this.Started(this, e);
|
||||
}
|
||||
|
||||
class ExternalEventHandlerCommon : IExternalEventHandler
|
||||
{
|
||||
internal Queue<KeyValuePair<string, Action<UIApplication>>> Actions { get; set; }
|
||||
|
||||
public event EventHandler<ExternalEventArg> Started;
|
||||
public event EventHandler<ExternalEventArg> End;
|
||||
|
||||
internal ExternalEventHandlerCommon()
|
||||
{
|
||||
this.Actions = new Queue<KeyValuePair<string, Action<UIApplication>>>();
|
||||
}
|
||||
|
||||
public void Execute(UIApplication app)
|
||||
{
|
||||
while (this.Actions.Count > 0)
|
||||
{
|
||||
var first = this.Actions.Dequeue();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(first.Key) || first.Value == null)
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
if (this.Started != null)
|
||||
this.Started(this, new ExternalEventArg(app, first.Key));
|
||||
|
||||
first.Value(app);
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.End(this, new ExternalEventArg(app, first.Key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string GetName()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部事件参数.
|
||||
/// </summary>
|
||||
/// <seealso cref="System.EventArgs" />
|
||||
public class ExternalEventArg : EventArgs
|
||||
{
|
||||
public ExternalEventArg(UIApplication app, string name)
|
||||
{
|
||||
this.App = app;
|
||||
this.Name = name;
|
||||
}
|
||||
|
||||
public UIApplication App { get; private set; }
|
||||
|
||||
public string Name { get; private set; }
|
||||
}
|
||||
}
|
||||
12
DotNet.Revit.ExternalEvent/MainWinodow.xaml
Normal file
12
DotNet.Revit.ExternalEvent/MainWinodow.xaml
Normal file
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="DotNet.Revit.ExternalEvent.MainWinodow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
mc:Ignorable="d"
|
||||
Height="291" Width="462" WindowStartupLocation="CenterOwner">
|
||||
<Grid>
|
||||
<Button Content="Button" HorizontalAlignment="Left" Height="30" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="120" Click="Button_Click"/>
|
||||
<Button Content="Button" HorizontalAlignment="Right" Height="30" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="120" Click="Button_Click_1"/>
|
||||
</Grid>
|
||||
</Window>
|
||||
76
DotNet.Revit.ExternalEvent/MainWinodow.xaml.cs
Normal file
76
DotNet.Revit.ExternalEvent/MainWinodow.xaml.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using DotNet.Revit.ExternalEvent;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
|
||||
namespace DotNet.Revit.ExternalEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWinodow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWinodow : Window
|
||||
{
|
||||
public MainWinodow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// 连续调用外部事件...
|
||||
|
||||
var helper = CmdExternalEvent.Instance.ExternalEventHelper;
|
||||
|
||||
helper.Invoke(m =>
|
||||
{
|
||||
var uiDoc = m.ActiveUIDocument;
|
||||
var doc = uiDoc.Document;
|
||||
|
||||
var ids = uiDoc.Selection.GetElementIds();
|
||||
if (ids.Count > 0)
|
||||
{
|
||||
doc.Invoke(x =>
|
||||
{
|
||||
doc.Delete(ids);
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
helper.Invoke(m =>
|
||||
{
|
||||
var uiDoc = m.ActiveUIDocument;
|
||||
var doc = uiDoc.Document;
|
||||
try
|
||||
{
|
||||
var refer = uiDoc.Selection.PickObject(ObjectType.Element);
|
||||
doc.Invoke(x =>
|
||||
{
|
||||
doc.Delete(refer.ElementId);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void Button_Click_1(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
36
DotNet.Revit.ExternalEvent/Properties/AssemblyInfo.cs
Normal file
36
DotNet.Revit.ExternalEvent/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("DotNet.Revit.ExternalEvent")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("DotNet.Revit.ExternalEvent")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("912969b4-7586-4f87-9d7d-69f0e3b38c83")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user