Skip to content

Commit

Permalink
Add floor creator example
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyNefyodov committed Sep 27, 2024
1 parent 2fdb374 commit 4faf2d8
Show file tree
Hide file tree
Showing 14 changed files with 412 additions and 1 deletion.
30 changes: 29 additions & 1 deletion ManyCommandsApplication.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExtensibleStorageExample",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UpdaterExample", "source\UpdaterExample\UpdaterExample.csproj", "{F47F705C-483D-463F-8120-46205A4BE6D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageExportExample", "source\ImageExportExample\ImageExportExample.csproj", "{75C2EF19-B1AC-42FA-B455-D1146885BC4E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageExportExample", "source\ImageExportExample\ImageExportExample.csproj", "{75C2EF19-B1AC-42FA-B455-D1146885BC4E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FloorCreatorExample", "source\FloorCreatorExample\FloorCreatorExample.csproj", "{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -231,6 +233,32 @@ Global
{75C2EF19-B1AC-42FA-B455-D1146885BC4E}.Release R24|Any CPU.Build.0 = Release R24|Any CPU
{75C2EF19-B1AC-42FA-B455-D1146885BC4E}.Release R25|Any CPU.ActiveCfg = Release R25|Any CPU
{75C2EF19-B1AC-42FA-B455-D1146885BC4E}.Release R25|Any CPU.Build.0 = Release R25|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R20|Any CPU.ActiveCfg = Debug R20|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R20|Any CPU.Build.0 = Debug R20|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R21|Any CPU.ActiveCfg = Debug R21|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R21|Any CPU.Build.0 = Debug R21|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R22|Any CPU.ActiveCfg = Debug R22|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R22|Any CPU.Build.0 = Debug R22|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R23|Any CPU.ActiveCfg = Debug R23|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R23|Any CPU.Build.0 = Debug R23|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R24|Any CPU.ActiveCfg = Debug R24|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R24|Any CPU.Build.0 = Debug R24|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R25|Any CPU.ActiveCfg = Debug R25|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Debug R25|Any CPU.Build.0 = Debug R25|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Installer|Any CPU.ActiveCfg = Debug R20|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Installer|Any CPU.Build.0 = Debug R20|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R20|Any CPU.ActiveCfg = Release R20|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R20|Any CPU.Build.0 = Release R20|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R21|Any CPU.ActiveCfg = Release R21|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R21|Any CPU.Build.0 = Release R21|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R22|Any CPU.ActiveCfg = Release R22|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R22|Any CPU.Build.0 = Release R22|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R23|Any CPU.ActiveCfg = Release R23|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R23|Any CPU.Build.0 = Release R23|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R24|Any CPU.ActiveCfg = Release R24|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R24|Any CPU.Build.0 = Release R24|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R25|Any CPU.ActiveCfg = Release R25|Any CPU
{96E8A9D4-66AC-464B-B2C2-5EF0A5D9C2E5}.Release R25|Any CPU.Build.0 = Release R25|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 5 additions & 0 deletions source/Application/Application.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ExtensibleStorageExample.Commands;
using ExternalCommandOne.Commands;
using FloorCreatorExample.Commands;
using ImageExportExample.Commands;
using Nice3point.Revit.Toolkit.External;
using UpdaterExample.Updaters;
Expand Down Expand Up @@ -33,6 +34,10 @@ private void CreateRibbon()
panel.AddPushButton<ImageExportCommand>("Image example")
.SetImage("/Application;component/Resources/Icons/RibbonIcon16.png")
.SetLargeImage("/Application;component/Resources/Icons/icons8-building-32-96.png");

panel.AddPushButton<FloorCreatorCommand>("Create floors")
.SetImage("/Application;component/Resources/Icons/RibbonIcon16.png")
.SetLargeImage("/Application;component/Resources/Icons/icons8-building-32-96.png");
}

private void RegisterUpdaters()
Expand Down
1 change: 1 addition & 0 deletions source/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FloorCreatorExample\FloorCreatorExample.csproj" />
<ProjectReference Include="..\ImageExportExample\ImageExportExample.csproj" />
<ProjectReference Include="..\ExtensibleStorageExample\ExtensibleStorageExample.csproj" />
<ProjectReference Include="..\ExternalCommandOne\ExternalCommandOne.csproj" />
Expand Down
25 changes: 25 additions & 0 deletions source/FloorCreatorExample/Commands/FloorCreatorCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Autodesk.Revit.Attributes;
using FloorCreatorExample.ViewModels;
using FloorCreatorExample.Views;
using Nice3point.Revit.Toolkit.External;

namespace FloorCreatorExample.Commands
{
/// <summary>
/// External command entry point invoked from the Revit interface
/// </summary>
[UsedImplicitly]
[Transaction(TransactionMode.Manual)]
public class FloorCreatorCommand : ExternalCommand
{
public override void Execute()
{
var viewModel = new FloorCreatorExampleViewModel();
var view = new FloorCreatorExampleView(viewModel);
viewModel.CloseRequest += (s, e) => view.Close();
viewModel.HideRequest += (s, e) => view.Hide();
viewModel.ShowRequest += (s, e) => view.ShowDialog();
view.ShowDialog();
}
}
}
38 changes: 38 additions & 0 deletions source/FloorCreatorExample/Core/FloorCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Autodesk.Revit.DB.Architecture;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FloorCreatorExample.Core
{
public static class FloorCreator
{
public static void CreateFloors(ElementType floorType, Room[] rooms)
{
var options = new SpatialElementBoundaryOptions()
{
SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish,
StoreFreeBoundaryFaces = true
};
var ids = new List<ElementId>();
using var transaction = new Transaction(floorType.Document);
transaction.Start("Create floors");
foreach (var room in rooms)
{
var segments = room.GetBoundarySegments(options).First();
var loop = new CurveLoop();
foreach (var segment in segments)
{
loop.Append(segment.GetCurve());
}
var list = new List<CurveLoop>() { loop };
var floor = Floor.Create(floorType.Document, list, floorType.Id, room.LevelId);
ids.Add(floor.Id);
}
transaction.Commit();
Context.ActiveUiDocument.Selection.SetElementIds(ids);
}
}
}
23 changes: 23 additions & 0 deletions source/FloorCreatorExample/Core/RoomSelectionFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FloorCreatorExample.Core
{
public class RoomSelectionFilter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
return elem is Room;
}

public bool AllowReference(Reference reference, XYZ position)
{
return true;
}
}
}
50 changes: 50 additions & 0 deletions source/FloorCreatorExample/FloorCreatorExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<UseWPF>true</UseWPF>
<LangVersion>latest</LangVersion>
<PlatformTarget>x64</PlatformTarget>
<ImplicitUsings>true</ImplicitUsings>
<Configurations>Debug R20;Debug R21;Debug R22;Debug R23;Debug R24;Debug R25</Configurations>
<Configurations>$(Configurations);Release R20;Release R21;Release R22;Release R23;Release R24;Release R25</Configurations>
</PropertyGroup>

<PropertyGroup Condition="$(Configuration.Contains('R20'))">
<RevitVersion>2020</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('R21'))">
<RevitVersion>2021</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('R22'))">
<RevitVersion>2022</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('R23'))">
<RevitVersion>2023</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('R24'))">
<RevitVersion>2024</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('R25'))">
<RevitVersion>2025</RevitVersion>
<TargetFramework>net8.0-windows</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nice3point.Revit.Build.Tasks" Version="1.*" />
<PackageReference Include="Nice3point.Revit.Toolkit" Version="$(RevitVersion).*" />
<PackageReference Include="Nice3point.Revit.Extensions" Version="$(RevitVersion).*" />
<PackageReference Include="Nice3point.Revit.Api.RevitAPI" Version="$(RevitVersion).*" />
<PackageReference Include="Nice3point.Revit.Api.RevitAPIUI" Version="$(RevitVersion).*" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.*" />
</ItemGroup>

<ItemGroup>
<Folder Include="Models" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI.Selection;
using FloorCreatorExample.Core;
using System.Collections.ObjectModel;

namespace FloorCreatorExample.ViewModels
{
public sealed partial class FloorCreatorExampleViewModel : ObservableObject
{
[ObservableProperty] private ElementType _selectedFloorType;
[ObservableProperty] private ElementType[] _floorTypes;
[ObservableProperty] private Room[] _rooms = [];

public FloorCreatorExampleViewModel()
{
FloorTypes = Context.ActiveDocument
.EnumerateTypes<FloorType>()
.ToArray();
}

[RelayCommand]
private void SelectRooms()
{
RaiseHideRequest();

var references = Context.ActiveUiDocument.Selection.PickObjects(ObjectType.Element,
new RoomSelectionFilter(),
"Select rooms");

Rooms = references
.Select(reference => (Room)Context.ActiveDocument.GetElement(reference))
.ToArray();

RaiseShowRequest();
}

[RelayCommand]
private void CreateFloors()
{
try
{
FloorCreator.CreateFloors(SelectedFloorType, Rooms);
}
finally
{
RaiseCloseRequest();
}
}


public event EventHandler CloseRequest;

private void RaiseCloseRequest()
{
CloseRequest?.Invoke(this, EventArgs.Empty);
}

public event EventHandler HideRequest;

private void RaiseHideRequest()
{
HideRequest?.Invoke(this, EventArgs.Empty);
}

public event EventHandler ShowRequest;

private void RaiseShowRequest()
{
ShowRequest?.Invoke(this, EventArgs.Empty);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
using Visibility = System.Windows.Visibility;

namespace FloorCreatorExample.Views.Converters
{
public class BoolVisibilityConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value! ? Visibility.Visible : Visibility.Hidden;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (Visibility)value! == Visibility.Visible;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
using Visibility = System.Windows.Visibility;

namespace FloorCreatorExample.Views.Converters
{
public class EnumVisibilityConverter<TEnum> : MarkupExtension, IValueConverter where TEnum : Enum
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is not TEnum valueEnum)
{
throw new ArgumentException($"{nameof(value)} is not type: {typeof(TEnum)}");
}

if (parameter is not TEnum parameterEnum)
{
throw new ArgumentException($"{nameof(parameter)} is not type: {typeof(TEnum)}");
}

return EqualityComparer<TEnum>.Default.Equals(valueEnum, parameterEnum) ? Visibility.Visible : Visibility.Hidden;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace FloorCreatorExample.Views.Converters
{
public class InverseBoolConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value!;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value!;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
Loading

0 comments on commit 4faf2d8

Please sign in to comment.