Revit的AddinManager在2014以后就不提供安装包了,而是要用户自己手动去配置,对于有Revit开发经验的人来说是比较
简单的,但是新手总是会遇到这样或那样的问题,在我的博客前面已经有讲过怎么手动配置,
这篇博客主要是探索一下,能不能也做一个像2014一样的安装包呢?
想一下,大概应该包括下面几个过程
1.获取当前安装了的Revit版本
这个可以通过
RevitProductUtility.GetAllInstalledRevitProducts();
来获取
2.获取放置addin文件的位置
这个可以通过
RevitProduct.AllUsersAddInFolder
来获取,也可以在程序中写死
3.复制addin文件到放置addin文件的位置
4.替换dll路径
下面是代码编译的安装包下载
//从资源文件加载RevitAddInUtility.dll
public partial class App : Application
{
public App()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string dllName = args.Name.Contains(",") ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
dllName = dllName.Replace(".", "_");
if (dllName.EndsWith("_resources")) return null;
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(GetType().Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
byte[] bytes = (byte[])rm.GetObject(dllName);
return System.Reflection.Assembly.Load(bytes);
}
}
//主界面
<Window x:Class="AddinInstall.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Convertor="clr-namespace:AddinInstall"
Title="Addin Manager Install" SizeToContent="WidthAndHeight">
<Window.Resources>
<Convertor:BtnIsEnabledConvert x:Key="Convertor" />
<Style TargetType="Label">
<Setter Property="Margin" Value="5"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="Margin" Value=" 5"/>
<Setter Property="Height" Value="23"/>
<Setter Property="Width" Value="200"/>
</Style>
<Style TargetType="Button">
<Setter Property="Margin" Value="5"/>
<Setter Property="Height" Value="23"/>
<Setter Property="Width" Value="75"/>
</Style>
<Style TargetType="StackPanel">
<Setter Property="Orientation" Value="Horizontal"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="Width" Value="120"/>
<Setter Property="Height" Value="23"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="选择Revit版本" Grid.Column="0" Grid.Row="0"/>
<ComboBox Grid.Column="1" Grid.Row="0" ItemsSource="{Binding Path=RvtProducts, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" SelectedValue="{Binding Path= RvtProduct, UpdateSourceTrigger=PropertyChanged}"/>
<Label Content="安装路径" Grid.Column="0" Grid.Row="1"/>
<StackPanel Grid.Column="1" Grid.Row="1">
<TextBox Text="{Binding Path=Path, UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="选择路径" Command="{Binding Path=PathCommand}" />
</StackPanel>
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2">
<Button Content="确定" Command="{Binding Path=OK_Command}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor , AncestorType={x:Type Window}}}">
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource Convertor}">
<Binding Path="Path"/>
<Binding Path="RvtProduct"/>
</MultiBinding>
</Button.IsEnabled>
</Button>
<Button Content="取消" Command="{Binding Path=Cancel_Command}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
</StackPanel>
</Grid>
</Window>
public partial class MainWindow : Window
{
public ViewModel ViewModel = new ViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = ViewModel;
}
}
// ViewModele
public class ViewModel:INotifyPropertyChanged
{
private string path = string.Empty;
public string Path
{
get
{
return path;
}
set
{
path = value;
NotifyPropertyChanged("Path");
}
}
private RevitProduct rvtProduct = null;
public RevitProduct RvtProduct
{
get { return rvtProduct; }
set { rvtProduct = value; NotifyPropertyChanged("RvtProduct"); }
}
private List<RevitProduct> rvtProducts = new List<RevitProduct>();
public List<RevitProduct> RvtProducts
{
get
{
return rvtProducts;
}
}
public BtnIsEnabledConvert Convertor
{
get { return new BtnIsEnabledConvert(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if(PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public ViewModel()
{
rvtProducts = RevitProductUtility.GetAllInstalledRevitProducts();
}
public ICommand OK_Command
{
get
{
return new OK_Command(this);
}
}
public ICommand Cancel_Command
{
get
{
return new Cancel_Command();
}
}
public ICommand PathCommand
{
get
{
return new PathCommand(this);
}
}
}
public class OK_Command : ICommand
{
ViewModel viewModel = null;
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
string fullPath = viewModel.Path + "\\AddInManager.dll";
MainWindow myWin = parameter as MainWindow;
myWin.Close();
Install(viewModel.RvtProduct, fullPath);
}
private void Install(RevitProduct rvtProduct,string path)
{
byte[] dll = null;
switch(rvtProduct.Version)
{
case RevitVersion.Revit2015:
dll = Res.AddInManager2015;
break;
case RevitVersion.Revit2016:
dll = Res.AddInManager2016;
break;
case RevitVersion.Revit2017:
dll = Res.AddInManager2017;
break;
}
using (FileStream fs = File.Create(path))
{
fs.Write(dll, 0, dll.Length);
fs.Close();
}
string addinPath = rvtProduct.AllUsersAddInFolder+"\\Autodesk_AddInManager.addin";
using(FileStream fs = File.Create(addinPath))
{
byte[] addin = Res.Autodesk_AddInManager;
fs.Write(addin, 0, addin.Length);
fs.Close();
}
string str = File.ReadAllText(addinPath, Encoding.UTF8);
str = str.Replace("[TARGETDIR]AddInManager.dll", path);
File.WriteAllText(addinPath, str, Encoding.UTF8);
}
public OK_Command(ViewModel vm)
{
this.viewModel = vm;
}
}
public class Cancel_Command : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MainWindow myWin = parameter as MainWindow;
//myWin.DialogResult = false;
myWin.Close();
}
}
public class PathCommand : ICommand
{
ViewModel viewModel = null;
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
DialogResult dr = dialog.ShowDialog();
if(dr==DialogResult.OK)
{
viewModel.Path = dialog.SelectedPath;
}
}
public PathCommand(ViewModel vm)
{
this.viewModel = vm;
}
}
public class BtnIsEnabledConvert : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
foreach (object value in values)
{
if (value == null)
return false;
else
{
if(value is string)
{
if (value as string == string.Empty)
return false;
}
}
}
return true;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
转载请注明来源本文地址:https://www.tuituisoft/blog/3478.html