下載app免費(fèi)領(lǐng)取會員
關(guān)于Revit開發(fā)其實是可以使用多線程的,但是是有限制的,目前發(fā)現(xiàn)只要在其他線程里啟用Transaction,基本Revit就崩潰了,
但是在其他線程里不啟用Transaction還是可以使用的,比如說我們要在Revit里檢索一些東西,但這些東西又很多,需要的時間
比較長,這種情況我們就可以把檢索的任務(wù)給其他線程,然后用戶先可以先進(jìn)行其他操作,
下面說一個簡單的例子,在Task里檢索建筑柱的數(shù)量,然后顯示到Window里,但是檢索數(shù)量的時候,用戶可以在Window里進(jìn)行
其他數(shù)據(jù)的輸入:
namespace MultiThreading
{
[Transaction(TransactionMode.Manual)]
public class Class1:IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
ViewModel vm = new ViewModel(doc);
if (vm.ShowWindow() ?? false)
{
}
return Result.Succeeded;
}
}
public class ViewModel:ViewModelBase
{
public MainWindow win = null;
public ViewModel(Document doc)
{
Task task = new Task(() =>
{
Thread.Sleep(10000);//由于檢索太快,所以讓Task等待10秒
FilteredElementCollector temc = new FilteredElementCollector(doc);
temc.OfCategory(BuiltInCategory.OST_Columns).OfClass(typeof(FamilyInstance));
I = temc.Count();
CanExecute = true;
});
task.Start();
win = new MainWindow();
win.DataContext = this;
}
private bool canExecute = false;
public bool CanExecute
{
get
{
return canExecute;
}
set
{
canExecute = value;
base.RaisePropertyChanged(() => CanExecute);
base.RaisePropertyChanged(() => OK_Command);
}
}
private int? i = null;
public int? I
{
get
{
return i;
}
set
{
i = value;
base.RaisePropertyChanged(() => I);
}
}
public ICommand OK_Command
{
get
{
return new RelayCommand(() => {
win.DialogResult = true;
win.Close();
},()=>CanExecute);
}
}
public ICommand Cancel_Command
{
get
{
return new RelayCommand(() =>
{
win.DialogResult = false;
win.Close();
});
}
}
public bool? ShowWindow()
{
return win.ShowDialog();
}
}
}
<Window x:Class="MultiThreading.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Grid>
<Label Content="柱子的數(shù)量為:" HorizontalAlignment="Left" Margin="86,97,0,0" VerticalAlignment="Top"/>
<Button Content="確定" Command="{Binding Path=OK_Command}" HorizontalAlignment="Left" Margin="76,229,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="取消" Command="{Binding Path=Cancel_Command}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="191,229,0,0"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="180,101,-8,0" IsReadOnly="True" TextWrapping="Wrap" Text="{Binding Path=I,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="156,169,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<Label Content="其他輸入:" HorizontalAlignment="Left" Margin="86,165,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。
下一篇:二次開發(fā)教程:Revit開發(fā)將WPF的Ower設(shè)置為Revit窗體
推薦專題