Skip to content

Commit

Permalink
Merge branch 'dui3/alpha' into add-host-app-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock authored Jul 3, 2024
2 parents 23232eb + 5fddfd9 commit 96a5bda
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Autodesk.Revit.DB;
using Speckle.Autofac.DependencyInjection;
using Speckle.Converters.Common;
using Speckle.Converters.Common.DependencyInjection;
using Speckle.Converters.RevitShared;
using Speckle.Converters.RevitShared.Helpers;
Expand All @@ -18,6 +19,7 @@ public void Load(SpeckleContainerBuilder builder)
// register all application converters
builder.AddApplicationConverters<RevitToSpeckleUnitConverter, ForgeTypeId>();

builder.AddScoped<IRootToHostConverter, RevitRootToHostConverter>();
builder.AddSingleton(new RevitContext());

// POC: do we need ToSpeckleScalingService as is, do we need to interface it out?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Core.Models;

namespace Speckle.Converters.RevitShared;

public class RevitRootToHostConverter : IRootToHostConverter
{
private readonly IConverterResolver<IToHostTopLevelConverter> _converterResolver;

public RevitRootToHostConverter(IConverterResolver<IToHostTopLevelConverter> converterResolver)
{
_converterResolver = converterResolver;
}

public object Convert(Base target)
{
var objectConverter = _converterResolver.GetConversionForType(target.GetType());

if (objectConverter == null)
{
throw new SpeckleConversionException($"No conversion found for {target.GetType().Name}");
}

return objectConverter.Convert(target)
?? throw new SpeckleConversionException($"Conversion of object with type {target.GetType()} returned null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="$(MSBuildThisFileDirectory)IRevitVersionConversionHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ReferencePointConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RevitConversionSettings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RevitRootToHostConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\ScalingServiceToHost.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\ScalingServiceToSpeckle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToHost\Raw\Geometry\ArcConverterToHost.cs" />
Expand All @@ -36,6 +37,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ToHost\Raw\Geometry\PolylineConverterToHost.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToHost\Raw\Geometry\VectorConverterToHost.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToHost\TopLevel\BaseTopLevelConverterToHost.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToHost\TopLevel\LevelToHostTopLevelConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToHost\TopLevel\GridlineToHostTopLevelConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToHost\TopLevel\ModelCurveToSpeckleTopLevelConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\BeamConversionToSpeckle.cs" />
Expand All @@ -60,7 +62,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\Geometry\PolylineToSpeckleConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\Geometry\VectorToSpeckleConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\Geometry\XyzConversionToPoint.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\LevelConversionToSpeckle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\TopLevel\LevelTopLevelConverterToSpeckle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\LocationConversionToSpeckle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\MaterialConversionToSpeckle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\ModelCurveArrArrayToSpeckleConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Speckle.Converters.Common;
using Speckle.Converters.RevitShared.Helpers;
using Speckle.Converters.RevitShared.ToSpeckle;
using Speckle.Converters.RevitShared.Services;

namespace Speckle.Converters.RevitShared.ToHost.ToLevel;

[NameAndRankValue(nameof(SOBE.Level), 0)]
public class LevelToHostTopLevelConverter : BaseTopLevelConverterToHost<SOBE.Level, DB.Level>
{
private readonly IRevitConversionContextStack _contextStack;
private readonly ScalingServiceToHost _scalingService;

public LevelToHostTopLevelConverter(IRevitConversionContextStack contextStack, ScalingServiceToHost scalingService)
{
_contextStack = contextStack;
_scalingService = scalingService;
}

public override DB.Level Convert(SOBE.Level target)
{
using var documentLevelCollector = new DB.FilteredElementCollector(_contextStack.Current.Document);
var docLevels = documentLevelCollector.OfClass(typeof(DB.Level)).ToElements().Cast<DB.Level>();

// POC : I'm not really understanding the linked use case for this. Do we want to bring this over?

//bool elevationMatch = true;
////level by name component
//if (target is RevitLevel speckleRevitLevel && speckleRevitLevel.referenceOnly)
//{
// //see: https://speckle.community/t/revit-connector-levels-and-spaces/2824/5
// elevationMatch = false;
// if (GetExistingLevelByName(docLevels, target.name) is DB.Level existingLevelWithSameName)
// {
// return existingLevelWithSameName;
// }
//}

DB.Level revitLevel;
var targetElevation = _scalingService.ScaleToNative(target.elevation, target.units);

if (GetExistingLevelByElevation(docLevels, targetElevation) is DB.Level existingLevel)
{
revitLevel = existingLevel;
}
else
{
revitLevel = DB.Level.Create(_contextStack.Current.Document, targetElevation);
revitLevel.Name = target.name;

if (target is SOBR.RevitLevel rl && rl.createView)
{
using var viewPlan = CreateViewPlan(target.name, revitLevel.Id);
}
}

return revitLevel;
}

private static DB.Level GetExistingLevelByElevation(IEnumerable<DB.Level> docLevels, double elevation)
{
return docLevels.FirstOrDefault(l => Math.Abs(l.Elevation - elevation) < RevitConversionContextStack.TOLERANCE);

Check failure on line 62 in DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/ToHost/TopLevel/LevelToHostTopLevelConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check failure on line 62 in DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/ToHost/TopLevel/LevelToHostTopLevelConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

private DB.ViewPlan CreateViewPlan(string name, DB.ElementId levelId)
{
using var collector = new DB.FilteredElementCollector(_contextStack.Current.Document);
var vt = collector
.OfClass(typeof(DB.ViewFamilyType))
.First(el => ((DB.ViewFamilyType)el).ViewFamily == DB.ViewFamily.FloorPlan);

var view = DB.ViewPlan.Create(_contextStack.Current.Document, vt.Id, levelId);
view.Name = name;

return view;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common;
using Speckle.Converters.RevitShared.Services;

namespace Speckle.Converters.RevitShared.ToSpeckle;

public class LevelConversionToSpeckle : ITypedConverter<DB.Level, SOBR.RevitLevel>
[NameAndRankValue(nameof(DB.Level), 0)]
public class LevelConversionToSpeckle : BaseTopLevelConverterToSpeckle<DB.Level, SOBR.RevitLevel>
{
private readonly ScalingServiceToSpeckle _scalingService;

Expand All @@ -12,7 +13,7 @@ public LevelConversionToSpeckle(ScalingServiceToSpeckle scalingService)
_scalingService = scalingService;
}

public SOBR.RevitLevel Convert(DB.Level target)
public override SOBR.RevitLevel Convert(DB.Level target)
{
SOBR.RevitLevel level =
new()
Expand Down

0 comments on commit 96a5bda

Please sign in to comment.