Skip to content

Commit

Permalink
Внес изменения после ревью
Browse files Browse the repository at this point in the history
  • Loading branch information
KGubin committed Oct 30, 2024
1 parent 09de7eb commit b05b75f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 106 deletions.
4 changes: 2 additions & 2 deletions src/RevitRoomViewer/Models/PluginConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

using dosymep.Bim4Everyone;
using dosymep.Bim4Everyone.ProjectConfigs;
Expand All @@ -24,6 +24,6 @@ public static PluginConfig GetPluginConfig() {

internal class RevitSettings : ProjectSettings {
public override string ProjectName { get; set; }
public Dictionary<string, RoomElement> RoomsWithSettings { get; set; }
public ObservableCollection<RoomElement> RoomsWithSettings { get; set; }
}
}
56 changes: 31 additions & 25 deletions src/RevitRoomViewer/Models/RevitRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

using Autodesk.Revit.ApplicationServices;
Expand All @@ -16,46 +16,52 @@ public RevitRepository(UIApplication uiApplication) {

public UIApplication UIApplication { get; }
public UIDocument ActiveUIDocument => UIApplication.ActiveUIDocument;

public Application Application => UIApplication.Application;
public Document Document => ActiveUIDocument.Document;

public List<LevelViewModel> GetLevelsWithRooms(Dictionary<string, RoomElement> roomsWithSettings) {
public ObservableCollection<RoomElement> GetRooms(ObservableCollection<RoomElement> roomsWithSettings) {

Room[] rooms = new FilteredElementCollector(Document, Document.ActiveView.Id)
.WherePasses(new RoomFilter())
.Cast<Room>()
.Where(r => r.Area > 0)
.ToArray();

var roomsWithSettingsCollection = new ObservableCollection<RoomElement>();

foreach(var room in rooms) {
var roomSetting = roomsWithSettings
.FirstOrDefault(r => r.Id == room.Id);

var roomElement = new RoomElement() {
Id = room.Id,
LevelId = room.LevelId,
Name = room.Name,
Description = roomSetting?.Description ?? string.Empty,
NeedMeasuring = roomSetting?.NeedMeasuring ?? false
};
roomsWithSettingsCollection.Add(roomElement);
}
return roomsWithSettingsCollection;
}

public ObservableCollection<LevelViewModel> GetLevels(ObservableCollection<RoomElement> rooms) {
var levels = new FilteredElementCollector(Document)
.OfClass(typeof(Level))
.Cast<Level>()
.ToList();

var rooms = new FilteredElementCollector(Document)
.OfCategory(BuiltInCategory.OST_Rooms)
.WhereElementIsNotElementType()
.Cast<Room>()
.ToList();

var levelViewModels = new List<LevelViewModel>();
var levelViewModels = new ObservableCollection<LevelViewModel>();

foreach(var level in levels) {

var roomsOnLevel = rooms
.Where(room => room.LevelId == level.Id)
.Select(room => {
roomsWithSettings.TryGetValue(room.Id.ToString(), out RoomElement roomSetting);
var roomElement = new RoomElement(room) {
Description = roomSetting?.Description ?? string.Empty,
NeedMeasuring = roomSetting?.NeedMeasuring ?? false
};
return roomElement;
})
.ToList();

var roomsOnLevel = new ObservableCollection<RoomElement>(
rooms.Where(room => room.LevelId == level.Id)
);

var levelViewModel = new LevelViewModel(level.Name, level, roomsOnLevel);
levelViewModels.Add(levelViewModel);
}

return levelViewModels;
}

}
}
14 changes: 4 additions & 10 deletions src/RevitRoomViewer/Models/RoomElement.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB;

namespace RevitRoomViewer.Models {
internal class RoomElement {

private readonly Room _room;

public RoomElement(Room room) {
_room = room;
}
public string Id => _room.Id.ToString();
public string LevelId => _room.LevelId.ToString();
public string Name => _room.Name;
public ElementId Id { get; set; }
public ElementId LevelId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool NeedMeasuring { get; set; }
}
Expand Down
8 changes: 4 additions & 4 deletions src/RevitRoomViewer/ViewModels/LevelViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Autodesk.Revit.DB;

Expand All @@ -10,13 +10,13 @@ namespace RevitRoomViewer.ViewModels {
internal class LevelViewModel : BaseViewModel {

private readonly Level _level;
private readonly List<RoomElement> _rooms;
private readonly ObservableCollection<RoomElement> _rooms;

public Level Element { get => _level; }
public List<RoomElement> Rooms { get => _rooms; }
public ObservableCollection<RoomElement> Rooms { get => _rooms; }
public string Name { get; set; }

public LevelViewModel(string name, Level level, List<RoomElement> rooms) {
public LevelViewModel(string name, Level level, ObservableCollection<RoomElement> rooms) {
Name = name;
_level = level;
_rooms = rooms;
Expand Down
72 changes: 36 additions & 36 deletions src/RevitRoomViewer/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
Expand All @@ -16,32 +16,10 @@ internal class MainViewModel : BaseViewModel {
private string _revitVersion;
private string _errorText;

private List<LevelViewModel> _levels;
private LevelViewModel _selectedLevel;
public Dictionary<string, RoomElement> RoomsWithSettings { get; set; } = new Dictionary<string, RoomElement>();

public MainViewModel(
PluginConfig pluginConfig,
RevitRepository revitRepository) {

_pluginConfig = pluginConfig;
_revitRepository = revitRepository;

#if REVIT_2023_OR_LESS
RevitVersion = "Revit 2023 или меньше :) ";
#else
RevitVersion = "Revit 2024";
#endif


LoadViewCommand = RelayCommand.Create(LoadView);
AcceptViewCommand = RelayCommand.Create(AcceptView, CanAcceptView);

}

public ICommand LoadViewCommand { get; }
public ICommand AcceptViewCommand { get; }
private ObservableCollection<LevelViewModel> _levels;

public ObservableCollection<RoomElement> RoomsWithSettings;
public string ErrorText {
get => _errorText;
set => this.RaiseAndSetIfChanged(ref _errorText, value);
Expand All @@ -50,7 +28,7 @@ public string RevitVersion {
get => _revitVersion;
set => this.RaiseAndSetIfChanged(ref _revitVersion, value);
}
public List<LevelViewModel> Levels {
public ObservableCollection<LevelViewModel> Levels {
get => _levels;
set => this.RaiseAndSetIfChanged(ref _levels, value);
}
Expand All @@ -59,9 +37,30 @@ public LevelViewModel SelectedLevel {
set => this.RaiseAndSetIfChanged(ref _selectedLevel, value);
}

public ICommand LoadViewCommand { get; }
public ICommand AcceptViewCommand { get; }

public MainViewModel(
PluginConfig pluginConfig,
RevitRepository revitRepository) {

_pluginConfig = pluginConfig;
_revitRepository = revitRepository;

#if REVIT_2023_OR_LESS
RevitVersion = "Revit 2023 или меньше :) ";
#else
RevitVersion = "Revit 2024";
#endif
LoadViewCommand = RelayCommand.Create(LoadView);
AcceptViewCommand = RelayCommand.Create(AcceptView, CanAcceptView);
}

private void LoadView() {
LoadConfig();
Levels = _revitRepository.GetLevelsWithRooms(RoomsWithSettings);

var rooms = _revitRepository.GetRooms(RoomsWithSettings);
Levels = _revitRepository.GetLevels(rooms);
SelectedLevel = Levels.FirstOrDefault();
}

Expand All @@ -76,7 +75,14 @@ private void AcceptView() {
private void UpdateRoomSettings(LevelViewModel levelViewModel) {
var rooms = levelViewModel.Rooms;
foreach(var room in rooms) {
RoomsWithSettings[room.Id.ToString()] = room;
var existingRoom = RoomsWithSettings.FirstOrDefault(r => r.Id == room.Id);

if(existingRoom != null) {
existingRoom.Description = room.Description;
existingRoom.NeedMeasuring = room.NeedMeasuring;
} else {
RoomsWithSettings.Add(room);
}
}
}

Expand All @@ -95,28 +101,22 @@ private bool CanAcceptView() {
ErrorText = "Ни у одной комнаты нет описания";
return false;
}


}

ErrorText = null;
return true;

}

private void LoadConfig() {
RevitSettings setting = _pluginConfig.GetSettings(_revitRepository.Document);
if(setting != null && setting.RoomsWithSettings != null)
RoomsWithSettings = setting.RoomsWithSettings;
RoomsWithSettings = setting?.RoomsWithSettings ?? new ObservableCollection<RoomElement>();
}

private void SaveConfig() {
RevitSettings setting = _pluginConfig.GetSettings(_revitRepository.Document)
?? _pluginConfig.AddSettings(_revitRepository.Document);
?? _pluginConfig.AddSettings(_revitRepository.Document);

setting.RoomsWithSettings = RoomsWithSettings;
_pluginConfig.SaveProjectConfig();
}
}
}

82 changes: 53 additions & 29 deletions src/RevitRoomViewer/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,92 @@
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"

xmlns:base="clr-namespace:dosymep.WPF.Views"
xmlns:local="clr-namespace:RevitRoomViewer.Views"
xmlns:vms="clr-namespace:RevitRoomViewer.ViewModels"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"

mc:Ignorable="d"
WindowStartupLocation="CenterOwner"

Title="{DynamicResource MainWindow.Title}"
Height="450" Width="800"

Height="450"
Width="800"
d:DataContext="{d:DesignInstance vms:MainViewModel, IsDesignTimeCreatable=False}">

<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded">
<b:EventTrigger
EventName="Loaded">
<b:InvokeCommandAction
Command="{Binding LoadViewCommand}" />
Command="{Binding LoadViewCommand}" />
</b:EventTrigger>
</b:Interaction.Triggers>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition
Height="*" />
<RowDefinition
Height="Auto" />
</Grid.RowDefinitions>

<TabControl ItemsSource="{Binding Levels}"
<TabControl
ItemsSource="{Binding Levels}"
SelectedItem="{Binding SelectedLevel}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
<TextBlock
Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>

<TabControl.ContentTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Rooms}"
AutoGenerateColumns="False"
SelectionMode="Single"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">

<DataGrid
AutoGenerateColumns="False"
SelectionMode="Single"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding Rooms}">
<DataGrid.Columns>
<DataGridTextColumn Header="Имя"
Binding="{Binding Name}"
IsReadOnly="True" />
<DataGridTextColumn Header="Описание"
Binding="{Binding Description}" />
<DataGridCheckBoxColumn Header="Нужен замер"
Binding="{Binding NeedMeasuring}" />
<DataGridTextColumn
Header="Имя"
IsReadOnly="True"
Binding="{Binding Name}" />
<DataGridTextColumn
Header="Описание"
Binding="{Binding Description, UpdateSourceTrigger=PropertyChanged}" />
<DataGridCheckBoxColumn
Header="Нужен замер"
Binding="{Binding NeedMeasuring}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>

<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Margin="10" Foreground="Orange" VerticalAlignment="Center" Text="{Binding ErrorText}" />
<TextBlock Margin="30,0,0,0" Foreground="Black" VerticalAlignment="Center" Text="{Binding RevitVersion}" />
<Button Margin="10" Width="80" Content="Сохранить" Command="{Binding AcceptViewCommand}" />
<Button Margin="10" Width="80" Content="Отменить" IsCancel="True" />
<StackPanel
Grid.Row="1"
Orientation="Horizontal"
HorizontalAlignment="Right">
<TextBlock
Margin="10"
Foreground="Orange"
VerticalAlignment="Center"
Text="{Binding ErrorText}" />
<TextBlock
Margin="30,0,0,0"
Foreground="Black"
VerticalAlignment="Center"
Text="{Binding RevitVersion}" />
<Button
Margin="10"
Width="80"
Content="Сохранить"
Command="{Binding AcceptViewCommand}" />
<Button
Margin="10"
Width="80"
Content="Отменить"
IsCancel="True" />
</StackPanel>
</Grid>
</base:PlatformWindow>

0 comments on commit b05b75f

Please sign in to comment.