457 lines
15 KiB
C#
457 lines
15 KiB
C#
using System.ComponentModel;
|
||
using System.Diagnostics;
|
||
using System.Windows;
|
||
using System.Windows.Data;
|
||
|
||
using Autodesk.Revit.DB;
|
||
using Autodesk.Revit.UI;
|
||
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
|
||
using Nice3point.Revit.Toolkit.External.Handlers;
|
||
|
||
|
||
|
||
namespace ShrlAlgo.RvKits.RvView;
|
||
|
||
public partial class ViewManagerViewModel : ObservableObject
|
||
{
|
||
[ObservableProperty]
|
||
[NotifyCanExecuteChangedFor(nameof(CopyAsDependentCommand))]
|
||
private bool canCopyAsDependent;
|
||
|
||
[ObservableProperty]
|
||
[NotifyCanExecuteChangedFor(nameof(CopyOnlyCommand))]
|
||
private bool canCopyOnly;
|
||
|
||
[ObservableProperty]
|
||
[NotifyCanExecuteChangedFor(nameof(CopyWithDetailCommand))]
|
||
private bool canCopyWidthDetail;
|
||
|
||
[ObservableProperty]
|
||
ICollectionView filteredList;
|
||
|
||
private readonly ActionEventHandler handler;
|
||
|
||
[ObservableProperty]
|
||
string searchText;
|
||
|
||
private List<ViewManagerModel> selectedItems;
|
||
|
||
[ObservableProperty]
|
||
private List<View> viewTemplates;
|
||
|
||
public ViewManagerViewModel(UIApplication uiapp)
|
||
{
|
||
//IsActive = true;
|
||
ViewCollection = uiapp.ActiveUIDocument.Document
|
||
.OfClass<View>()
|
||
.Cast<View>()
|
||
.Where(
|
||
v =>
|
||
v.GetTypeId() != ElementId.InvalidElementId
|
||
&& v is not ViewSchedule { IsTitleblockRevisionSchedule: true }
|
||
)
|
||
.OrderBy(v => v.ViewType)
|
||
.ThenBy(v => v.Name)
|
||
.Select(v => new ViewManagerModel(v))
|
||
.ToList();
|
||
//ViewTemplates = doc.OfCollector<View>().Cast<View>().Where(v => v.IsTemplate).ToList();
|
||
//ViewTemplates.Insert(0,View);
|
||
//var models = (from v in views let model = new ViewManagerModel(v) select model).ToList();
|
||
|
||
FilteredList = CollectionViewSource.GetDefaultView(ViewCollection);
|
||
handler = new ActionEventHandler();
|
||
//ViewCollection.ListChanged += ViewCollection_ListChanged;
|
||
}
|
||
|
||
//[RelayCommand]
|
||
//private void ActiveView(System.Collections.IList items)
|
||
//{
|
||
// if (items == null || items.Count == 0)
|
||
// {
|
||
// return;
|
||
// }
|
||
// var UiDocument = uiapp.ActiveUIDocument;
|
||
// var list = items.Cast<ViewManagerModel>().ToList();
|
||
// try
|
||
// {
|
||
// foreach (var model in list)
|
||
// {
|
||
// UiDocument.ActiveView = model.View;
|
||
// }
|
||
// }
|
||
// catch (Exception e)
|
||
// {
|
||
// Console.WriteLine(e);
|
||
// throw;
|
||
// }
|
||
//}
|
||
[RelayCommand]
|
||
private void Confirm(System.Collections.IList items)
|
||
{
|
||
if (items == null || items.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
var list = items.Cast<ViewManagerModel>().ToList();
|
||
handler.Raise(uiapp =>
|
||
{
|
||
var doc = uiapp.ActiveUIDocument.Document;
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var model in list)
|
||
{
|
||
var view = model.View;
|
||
if (view == null)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
try
|
||
{
|
||
view.Name = model.Name;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
MessageBox.Show("名称未修改\n\r修改的视图名称不能包含:\n\r\\:{}[]|;<>?`~\n\r及其他不能打印的字符或与其他视图重名", "提示");
|
||
}
|
||
|
||
view.ViewTemplateId = ElementId.InvalidElementId;
|
||
if (model.IsDisplayStyleEditable)
|
||
{
|
||
view.DisplayStyle = (Autodesk.Revit.DB.DisplayStyle)model.DisplayStyle;
|
||
}
|
||
|
||
if (view.HasViewDiscipline())
|
||
{
|
||
Debug.Assert(model.Discipline != null, "model.Discipline != null");
|
||
view.Discipline = (Autodesk.Revit.DB.ViewDiscipline)model.Discipline;
|
||
}
|
||
|
||
if (model.IsScaleEditable)
|
||
{
|
||
view.Scale = model.Scale;
|
||
}
|
||
|
||
if (model.IsDetailLevelEditable)
|
||
{
|
||
Debug.Assert(model.DetailLevel != null, "model.DetailLevel != null");
|
||
view.DetailLevel = (Autodesk.Revit.DB.ViewDetailLevel)model.DetailLevel;
|
||
}
|
||
|
||
if (model.ViewTemplate != null && view.IsValidViewTemplate(model.ViewTemplate.Id))
|
||
{
|
||
view.ViewTemplateId = model.ViewTemplate.Id;
|
||
}
|
||
|
||
if (model.IsSheetNameEditable)
|
||
{
|
||
view.get_Parameter(BuiltInParameter.VIEW_DESCRIPTION).Set(model.SheetName);
|
||
}
|
||
|
||
if (model.IsSheetNumberEditable && view is ViewSheet sheet)
|
||
{
|
||
sheet.SheetNumber = model.SheetNumber;
|
||
}
|
||
}
|
||
},
|
||
"修改视图属性"
|
||
);
|
||
});
|
||
}
|
||
|
||
[RelayCommand(CanExecute = nameof(CanCopyAsDependent))]
|
||
private void CopyAsDependent(System.Collections.IList items)
|
||
{
|
||
if (items == null || items.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
var list = items.Cast<ViewManagerModel>().ToList();
|
||
handler.Raise(uiapp =>
|
||
{
|
||
var doc = uiapp.ActiveUIDocument.Document;
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
try
|
||
{
|
||
foreach (var model in list)
|
||
{
|
||
var view = model.View;
|
||
var viewCopy = view.CopyWithOption(ViewDuplicateOption.AsDependent);
|
||
ViewCollection.Insert(ViewCollection.IndexOf(model) + 1, new ViewManagerModel(viewCopy));
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.ToLog(e.Message);
|
||
}
|
||
},
|
||
"复制视图"
|
||
);
|
||
});
|
||
}
|
||
|
||
[RelayCommand(CanExecute = nameof(CanCopyOnly))]
|
||
private void CopyOnly(System.Collections.IList items)
|
||
{
|
||
if (items == null || items.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
var list = items.Cast<ViewManagerModel>().ToList();
|
||
handler.Raise(uiapp =>
|
||
{
|
||
var doc = uiapp.ActiveUIDocument.Document;
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
try
|
||
{
|
||
foreach (var model in list)
|
||
{
|
||
var view = model.View;
|
||
var viewCopy = view.CopyWithOption(ViewDuplicateOption.Duplicate);
|
||
ViewCollection.Insert(ViewCollection.IndexOf(model) + 1, new ViewManagerModel(viewCopy));
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.ToLog(e.Message);
|
||
}
|
||
},
|
||
"复制视图"
|
||
);
|
||
});
|
||
}
|
||
|
||
[RelayCommand(CanExecute = nameof(CanCopyWidthDetail))]
|
||
private void CopyWithDetail(System.Collections.IList items)
|
||
{
|
||
if (items == null || items.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
var list = items.Cast<ViewManagerModel>().ToList();
|
||
handler.Raise(uiapp =>
|
||
{
|
||
var doc = uiapp.ActiveUIDocument.Document;
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
try
|
||
{
|
||
foreach (var model in list)
|
||
{
|
||
var view = model.View;
|
||
var viewCopy = view.CopyWithOption(ViewDuplicateOption.WithDetailing);
|
||
ViewCollection.Insert(ViewCollection.IndexOf(model) + 1, new ViewManagerModel(viewCopy));
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
LogHelper.ToLog(e.Message);
|
||
}
|
||
},
|
||
"复制视图"
|
||
);
|
||
});
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void Delete(System.Collections.IList items)
|
||
{
|
||
if (items == null || items.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
var list = items.Cast<ViewManagerModel>().ToList();
|
||
//ViewCollection.RemoveAll(v => list.Contains(v));
|
||
handler.Raise(uiapp =>
|
||
{
|
||
var doc = uiapp.ActiveUIDocument.Document;
|
||
doc.Invoke(
|
||
__ =>
|
||
{
|
||
foreach (var model in list)
|
||
{
|
||
try
|
||
{
|
||
//dg.Items.Remove(model);
|
||
var opened = uiapp.ActiveUIDocument
|
||
.GetOpenUIViews()
|
||
.FirstOrDefault(ui => ui.ViewId == model.View.Id);
|
||
if (opened == null)
|
||
{
|
||
ViewCollection.Remove(model);
|
||
doc.Delete(model.View.Id);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
MessageBox.Show("视图不可删除");
|
||
e.Message.ToLog();
|
||
}
|
||
}
|
||
},
|
||
"删除视图"
|
||
);
|
||
//dg.ItemsSource = null;
|
||
//dg.ItemsSource = ViewCollection;
|
||
});
|
||
}
|
||
|
||
[RelayCommand]
|
||
void Search()
|
||
{
|
||
FilteredList.Filter = o =>
|
||
{
|
||
if (string.IsNullOrEmpty(SearchText))
|
||
{
|
||
return true;
|
||
}
|
||
var item = o as ViewManagerModel;
|
||
return item.Name.Contains(SearchText) || item.UserViewType.GetAttribute<DescriptionAttribute>().Description.Contains(SearchText);
|
||
};
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void RemoveTemplate(System.Collections.IList items)
|
||
{
|
||
if (items != null || items.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
handler.Raise(uiapp =>
|
||
{
|
||
var list = items.Cast<ViewManagerModel>().ToList();
|
||
foreach (var model in list)
|
||
{
|
||
model.ViewTemplate = null;
|
||
}
|
||
});
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void UpdateExecute(System.Collections.IList items)
|
||
{
|
||
if (items == null || items.Count == 0)
|
||
{
|
||
return;
|
||
}
|
||
selectedItems = items.Cast<ViewManagerModel>().ToList();
|
||
//更新可用性字段
|
||
CanCopyOnly = selectedItems.All(m => m.View.CanViewBeDuplicated(ViewDuplicateOption.Duplicate));
|
||
CanCopyWidthDetail = selectedItems.All(m => m.View.CanViewBeDuplicated(ViewDuplicateOption.WithDetailing));
|
||
CanCopyAsDependent = selectedItems.All(m => m.View.CanViewBeDuplicated(ViewDuplicateOption.AsDependent));
|
||
//CommandManager.InvalidateRequerySuggested();
|
||
//通知命令按钮可用性更新
|
||
//CopyOnly.NotifyCanExecuteChanged();
|
||
//CopyWithDetail.NotifyCanExecuteChanged();
|
||
//CopyAsDependent.NotifyCanExecuteChanged();
|
||
}
|
||
|
||
private void ViewCollection_ListChanged(object sender, ListChangedEventArgs e)
|
||
{
|
||
if (e.ListChangedType == ListChangedType.ItemChanged)
|
||
{
|
||
var changedItem = ViewCollection[e.NewIndex];
|
||
//var changedItem1 = ViewCollection[e.OldIndex];
|
||
var x = e.PropertyDescriptor;
|
||
foreach (var item in selectedItems)
|
||
{
|
||
var prop = item.GetType().GetProperty(x.Name, x.PropertyType);
|
||
var v = x.GetValue(changedItem);
|
||
prop.SetValue(item, v);
|
||
}
|
||
handler.Raise(uiapp =>
|
||
{
|
||
var doc = uiapp.ActiveUIDocument.Document;
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var model in selectedItems)
|
||
{
|
||
var view = model.View;
|
||
if (view == null)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
try
|
||
{
|
||
view.Name = model.Name;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
MessageBox.Show("名称未修改\n\r修改的视图名称不能包含:\n\r\\:{}[]|;<>?`~\n\r及其他不能打印的字符", "提示");
|
||
}
|
||
|
||
view.ViewTemplateId = ElementId.InvalidElementId;
|
||
if (model.IsDisplayStyleEditable)
|
||
{
|
||
view.DisplayStyle = (Autodesk.Revit.DB.DisplayStyle)model.DisplayStyle;
|
||
}
|
||
|
||
if (view.HasViewDiscipline())
|
||
{
|
||
Debug.Assert(model.Discipline != null, "model.Discipline != null");
|
||
view.Discipline = (Autodesk.Revit.DB.ViewDiscipline)model.Discipline;
|
||
}
|
||
|
||
if (model.IsScaleEditable)
|
||
{
|
||
view.Scale = model.Scale;
|
||
}
|
||
|
||
if (model.IsDetailLevelEditable)
|
||
{
|
||
Debug.Assert(model.DetailLevel != null, "model.DetailLevel != null");
|
||
view.DetailLevel = (Autodesk.Revit.DB.ViewDetailLevel)model.DetailLevel;
|
||
}
|
||
|
||
if (model.ViewTemplate != null && view.IsValidViewTemplate(model.ViewTemplate.Id))
|
||
{
|
||
view.ViewTemplateId = model.ViewTemplate.Id;
|
||
}
|
||
|
||
if (model.IsSheetNameEditable)
|
||
{
|
||
view.get_Parameter(BuiltInParameter.VIEW_DESCRIPTION).Set(model.SheetName);
|
||
}
|
||
|
||
if (model.IsSheetNumberEditable && view is ViewSheet sheet)
|
||
{
|
||
sheet.SheetNumber = model.SheetNumber;
|
||
}
|
||
}
|
||
},
|
||
"修改视图属性"
|
||
);
|
||
});
|
||
}
|
||
}
|
||
|
||
//public void Receive(ViewMessage message)
|
||
//{
|
||
// if (selectedItems==null||!selectedItems.Any())
|
||
// {
|
||
// return;
|
||
// }
|
||
//}
|
||
|
||
//[ObservableProperty]
|
||
//ViewManagerModel modelPropertyChanged;
|
||
|
||
//public void Receive(PropertyChangedMessage<ViewManagerModel> message)
|
||
//{
|
||
// //ModelPropertyChanged = message.NewValue;
|
||
//}
|
||
|
||
public List<ViewManagerModel> ViewCollection { get; set; }
|
||
}
|