206 lines
5.2 KiB
C#
206 lines
5.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Globalization;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Text.RegularExpressions;
|
|||
|
|
|
|||
|
|
using Autodesk.Revit;
|
|||
|
|
using Autodesk.Revit.DB;
|
|||
|
|
using Autodesk.RevitAddIns;
|
|||
|
|
|
|||
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|||
|
|
using CommunityToolkit.Mvvm.Input;
|
|||
|
|
|
|||
|
|
using Microsoft.Win32;
|
|||
|
|
|
|||
|
|
namespace RevitLess;
|
|||
|
|
|
|||
|
|
public partial class MainWindowViewModel : ObservableObject
|
|||
|
|
{
|
|||
|
|
private static RevitVersion RevitVersion = RevitVersion.Unknown;
|
|||
|
|
|
|||
|
|
private static Document doc;
|
|||
|
|
private static RevitContext RevitApp => RevitContext.Instance;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取Revit文件的版本
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="path">文件路径</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static RevitVersion GetRevitVersion(string path)
|
|||
|
|
{
|
|||
|
|
string revitVersion = null;
|
|||
|
|
var stream = new FileStream(path, FileMode.Open);
|
|||
|
|
|
|||
|
|
var size = 1024 * 1024;
|
|||
|
|
var bytes = new byte[size];
|
|||
|
|
|
|||
|
|
while (stream.Read(bytes, 0, size) > 0)
|
|||
|
|
{
|
|||
|
|
var str = Encoding.Unicode.GetString(bytes);
|
|||
|
|
|
|||
|
|
const string pattern = @"Autodesk Revit \d{4}";
|
|||
|
|
var match = Regex.Match(str, pattern);
|
|||
|
|
if (match.Success)
|
|||
|
|
{
|
|||
|
|
revitVersion = "Revit" + match.Value.Substring(match.Length - 4, 4);
|
|||
|
|
|
|||
|
|
//File.WriteAllText(@"D:\abc.txt", str);
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach (RevitVersion ver in Enum.GetValues(typeof(RevitVersion)))
|
|||
|
|
{
|
|||
|
|
if (Enum.GetName(typeof(RevitVersion), ver) == revitVersion)
|
|||
|
|
{
|
|||
|
|
return ver;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return RevitVersion.Unknown;
|
|||
|
|
//var s = Enum.GetName(typeof(RevitVersion), revitVersion);
|
|||
|
|
//return revitVersion;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 根据Revit文件获取Revit安装路径
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private string GetRevitLocation()
|
|||
|
|
{
|
|||
|
|
var file = new FileInfo(SourceFilePath);
|
|||
|
|
|
|||
|
|
if (file.Exists)
|
|||
|
|
{
|
|||
|
|
//文件级获取Revit版本,不打开Revit软件
|
|||
|
|
RevitVersion = GetRevitVersion(SourceFilePath);
|
|||
|
|
//根据文件版本获取Revit产品安装位置
|
|||
|
|
return RevitProductUtility.GetAllInstalledRevitProducts().Find(x => x.Version == RevitVersion)?.InstallLocation;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//已安装的默认Revit产品安装位置
|
|||
|
|
return RevitProductUtility.GetAllInstalledRevitProducts().Find(x => x.Version >= RevitVersion)?.InstallLocation;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static void SaveFile(Document document)
|
|||
|
|
{
|
|||
|
|
var workPath = Path.GetDirectoryName(typeof(App).Assembly.Location);
|
|||
|
|
var i = 1;
|
|||
|
|
var saveFilePath = Path.Combine(workPath, $"项目 {i}.rvt");
|
|||
|
|
var saveFile = new FileInfo(saveFilePath);
|
|||
|
|
if (saveFile.Exists)
|
|||
|
|
{
|
|||
|
|
i++;
|
|||
|
|
saveFilePath = $"项目 {i}";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
document.SaveAs(saveFilePath);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private bool CanOperation => !(string.IsNullOrEmpty(SourceFilePath) || string.IsNullOrEmpty(TargetFilePath));
|
|||
|
|
|
|||
|
|
private void InitFinished(object sender, Product revitProduct)
|
|||
|
|
{
|
|||
|
|
doc = RevitApp.OpenFile(SourceFilePath);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[RelayCommand(CanExecute = nameof(CanOperation))]
|
|||
|
|
private void Execute()
|
|||
|
|
{
|
|||
|
|
Log = "Revit程序启动中...\n";
|
|||
|
|
RevitApp.Run();
|
|||
|
|
RevitApp.InitFinished += InitFinished; //订阅
|
|||
|
|
RevitApp.InitRevitAppAndOpenFile(); //执行订阅事件
|
|||
|
|
ExecuteOperation();
|
|||
|
|
RevitApp.Exit();
|
|||
|
|
var time = Convert.ToString(DateTime.Now, CultureInfo.InvariantCulture);
|
|||
|
|
Log = $"{time} 处理完毕,结束进程。";
|
|||
|
|
//Console.ReadKey();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void ExecuteOperation()
|
|||
|
|
{
|
|||
|
|
//document.SaveAs(saveFilePath, new SaveAsOptions() { OverwriteExistingFile = true });
|
|||
|
|
//View3D view = revit.GetView3D(document);
|
|||
|
|
//if (view != null)
|
|||
|
|
//{
|
|||
|
|
// Console.WriteLine(view.Name);
|
|||
|
|
|
|||
|
|
// //var elements = revit.GetElementsWithView(view);
|
|||
|
|
// foreach (var element in elements)
|
|||
|
|
// {
|
|||
|
|
// Console.WriteLine(element.Name);
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
var view = RevitHelper.GetView3D(doc);
|
|||
|
|
using (var ts = new Transaction(doc, "修改"))
|
|||
|
|
{
|
|||
|
|
ts.Start();
|
|||
|
|
doc.Create.NewModelCurve(Line.CreateBound(new XYZ(0, 0, 0), new XYZ(0, 10, 0)), view.SketchPlane);
|
|||
|
|
ts.Commit();
|
|||
|
|
}
|
|||
|
|
//doc.SaveAs(,new SaveAsOptions { PreviewViewId =view.Id});
|
|||
|
|
doc.SaveAs(TargetFilePath);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[ObservableProperty]
|
|||
|
|
[NotifyCanExecuteChangedFor(nameof(ExecuteCommand))]
|
|||
|
|
private string sourceFilePath;
|
|||
|
|
|
|||
|
|
[ObservableProperty]
|
|||
|
|
[NotifyCanExecuteChangedFor(nameof(ExecuteCommand))]
|
|||
|
|
private string targetFilePath;
|
|||
|
|
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private string log;
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void SelectFile()
|
|||
|
|
{
|
|||
|
|
var dialog = new OpenFileDialog
|
|||
|
|
{
|
|||
|
|
CheckFileExists = true,
|
|||
|
|
CheckPathExists = true,
|
|||
|
|
DereferenceLinks = false,
|
|||
|
|
Filter = "所有Revit文件 (*.rvt, *.rfa, *.rte, *.rft)|*.rvt;*.rfa;*.rte;*.rft",
|
|||
|
|
FilterIndex = 0,
|
|||
|
|
Title = "打开文件",
|
|||
|
|
ValidateNames = true,
|
|||
|
|
Multiselect = true,
|
|||
|
|
ReadOnlyChecked = true,
|
|||
|
|
ShowReadOnly = true
|
|||
|
|
};
|
|||
|
|
if (dialog.ShowDialog() == true)
|
|||
|
|
{
|
|||
|
|
SourceFilePath = dialog.FileName;
|
|||
|
|
var revitPath = GetRevitLocation();
|
|||
|
|
//动态加载程序集
|
|||
|
|
RevitContext.OnAssemblyResolve(revitPath);
|
|||
|
|
Log = $"当前使用的Revit文件版本为:{RevitVersion}\n";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void SaveFile()
|
|||
|
|
{
|
|||
|
|
SaveFileDialog dialog =
|
|||
|
|
new()
|
|||
|
|
{
|
|||
|
|
CheckPathExists = true,
|
|||
|
|
DereferenceLinks = false,
|
|||
|
|
AddExtension = true,
|
|||
|
|
Filter = "所有Revit文件 (*.rvt, *.rfa, *.rte, *.rft)|*.rvt;*.rfa;*.rte;*.rft",
|
|||
|
|
FilterIndex = 0,
|
|||
|
|
Title = "保存文件",
|
|||
|
|
ValidateNames = true,
|
|||
|
|
};
|
|||
|
|
if (dialog.ShowDialog() == true)
|
|||
|
|
{
|
|||
|
|
TargetFilePath = dialog.FileName;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|