Skip to content

Commit

Permalink
Revit fallback to Element Converter (#3512)
Browse files Browse the repository at this point in the history
* add root converter with fallback and tests

* proper references

* Converters work

* fmt

* added test!

* fmt
  • Loading branch information
adamhathcock authored Jun 19, 2024
1 parent 8405135 commit 77829a5
Show file tree
Hide file tree
Showing 22 changed files with 887 additions and 53 deletions.
19 changes: 19 additions & 0 deletions All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{B1324D25
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Speckle.Converters.Revit2023.Tests", "DUI3-DX\Converters\Revit\Speckle.Converters.Revit2023.Tests\Speckle.Converters.Revit2023.Tests.csproj", "{AEC26A0B-25F3-4544-A9D6-A427BFF79250}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Speckle.Converters.Common.Tests", "DUI3-DX\Sdk\Speckle.Converters.Common.Tests\Speckle.Converters.Common.Tests.csproj", "{95E23A97-E5EA-4506-A52C-D3DA9012DA02}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug Mac|Any CPU = Debug Mac|Any CPU
Expand Down Expand Up @@ -2807,6 +2809,22 @@ Global
{AEC26A0B-25F3-4544-A9D6-A427BFF79250}.Release|Any CPU.Build.0 = Release|Any CPU
{AEC26A0B-25F3-4544-A9D6-A427BFF79250}.Release|x64.ActiveCfg = Release|Any CPU
{AEC26A0B-25F3-4544-A9D6-A427BFF79250}.Release|x64.Build.0 = Release|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Debug Mac|Any CPU.ActiveCfg = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Debug Mac|Any CPU.Build.0 = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Debug Mac|x64.ActiveCfg = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Debug Mac|x64.Build.0 = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Debug|x64.ActiveCfg = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Debug|x64.Build.0 = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Release Mac|Any CPU.ActiveCfg = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Release Mac|Any CPU.Build.0 = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Release Mac|x64.ActiveCfg = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Release Mac|x64.Build.0 = Debug|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Release|Any CPU.Build.0 = Release|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Release|x64.ActiveCfg = Release|Any CPU
{95E23A97-E5EA-4506-A52C-D3DA9012DA02}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -3019,6 +3037,7 @@ Global
{F06E4C37-4076-4272-9CA6-FB505E02CD31} = {BE521908-7944-46F3-98BF-B47D34509934}
{E1C43415-3200-45F4-8BF9-A4DD7D7F2ED6} = {D92751C8-1039-4005-90B2-913E55E0B8BD}
{AEC26A0B-25F3-4544-A9D6-A427BFF79250} = {D92751C8-1039-4005-90B2-913E55E0B8BD}
{95E23A97-E5EA-4506-A52C-D3DA9012DA02} = {2E00592E-558D-492D-88F9-3ECEE4C0C7DA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1D43D91B-4F01-4A78-8250-CC6F9BD93A14}
Expand Down
1 change: 1 addition & 0 deletions DUI3-DX.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"DUI3-DX\\Sdk\\Speckle.Autofac\\Speckle.Autofac.csproj",
"DUI3-DX\\Sdk\\Speckle.Connectors.Utils\\Speckle.Connectors.Utils.csproj",
"DUI3-DX\\Sdk\\Speckle.Converters.Common.DependencyInjection\\Speckle.Converters.Common.DependencyInjection.csproj",
"DUI3-DX\\Sdk\\Speckle.Converters.Common.Tests\\Speckle.Converters.Common.Tests.csproj",
"DUI3-DX\\Sdk\\Speckle.Converters.Common\\Speckle.Converters.Common.csproj",
"Objects\\Objects\\Objects.csproj",
"Objects\\Tests\\Objects.Tests.Unit\\Objects.Tests.Unit.csproj"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ public RootObjectBuilderResult Build(
var countProgress = 0; // because for(int i = 0; ...) loops are so last year
var cacheHitCount = 0;
List<SendConversionResult> results = new(revitElements.Count);
var path = new string[2];
foreach (Element revitElement in revitElements)
{
ct.ThrowIfCancellationRequested();

var cat = revitElement.Category.Name;
var path = new[] { doc.GetElement(revitElement.LevelId) is not Level level ? "No level" : level.Name, cat };
path[0] = doc.GetElement(revitElement.LevelId) is not Level level ? "No level" : level.Name;
path[1] = cat;
var collection = GetAndCreateObjectHostCollection(path);

var applicationId = revitElement.Id.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class RevitConverterModule : ISpeckleModule
public void Load(SpeckleContainerBuilder builder)
{
builder.AddConverterCommon<RootToSpeckleConverter, RevitToSpeckleUnitConverter, IRevitForgeTypeId>();
builder.ScanAssemblyOfType<RootConvertManager>();

// POC: do we need ToSpeckleScalingService as is, do we need to interface it out?
builder.AddScoped<IScalingServiceToSpeckle, ScalingServiceToSpeckle>();
Expand All @@ -28,5 +29,6 @@ public void Load(SpeckleContainerBuilder builder)
builder.AddScoped<IHostedElementConversionToSpeckle, HostedElementConversionToSpeckle>();
builder.AddScoped<IParameterObjectAssigner, ParameterObjectAssigner>();
builder.AddScoped<ISlopeArrowExtractor, SlopeArrowExtractor>();
builder.AddScoped<IRootElementProvider, RevitRootElementProvider>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Speckle.Revit2023.Api" Version="0.1.1-preview.0.23" />
<PackageReference Include="Speckle.Revit2023.Api" Version="0.1.1-preview.0.24" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Sdk\Speckle.Converters.Common.DependencyInjection\Speckle.Converters.Common.DependencyInjection.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0"/>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="Speckle.Revit2023.Interfaces" Version="0.1.1-preview.0.23" />
<PackageReference Include="Speckle.Revit2023.Interfaces" Version="0.1.1-preview.0.24" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using FluentAssertions;
using Moq;
using NUnit.Framework;
using Objects;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Converters.RevitShared.Services;
using Speckle.Converters.RevitShared.ToSpeckle;
using Speckle.Revit.Interfaces;

namespace Speckle.Converters.Revit2023.Tests;

public class XyzConversionToPointTests
{
private readonly MockRepository _repository = new(MockBehavior.Strict);

private readonly Mock<IConversionContextStack<IRevitDocument, IRevitForgeTypeId>> _revitConversionContextStack;
private readonly Mock<IScalingServiceToSpeckle> _scalingServiceToSpeckle;

public XyzConversionToPointTests()
{
_revitConversionContextStack = _repository.Create<IConversionContextStack<IRevitDocument, IRevitForgeTypeId>>();
_scalingServiceToSpeckle = _repository.Create<IScalingServiceToSpeckle>();
}

[TearDown]
public void Verify() => _repository.VerifyAll();

[Test]
public void Convert_Point()
{
var x = 3.1;
var y = 3.2;
var z = 3.3;
var xScaled = 4.1;
var yScaled = 4.2;
var zScaled = 4.3;
var xyz = _repository.Create<IRevitXYZ>();
xyz.Setup(x => x.X).Returns(x);
xyz.Setup(x => x.Y).Returns(y);
xyz.Setup(x => x.Z).Returns(z);

var units = "units";
var conversionContext = _repository.Create<IConversionContext<IRevitDocument>>();
conversionContext.Setup(x => x.SpeckleUnits).Returns(units);

_scalingServiceToSpeckle.Setup(a => a.ScaleLength(x)).Returns(xScaled);
_scalingServiceToSpeckle.Setup(a => a.ScaleLength(y)).Returns(yScaled);
_scalingServiceToSpeckle.Setup(a => a.ScaleLength(z)).Returns(zScaled);

_revitConversionContextStack.Setup(x => x.Current).Returns(conversionContext.Object);

var converter = new XyzConversionToPoint(_scalingServiceToSpeckle.Object, _revitConversionContextStack.Object);
var point = converter.Convert(xyz.Object);

point.x.Should().Be(xScaled);
point.y.Should().Be(yScaled);
point.z.Should().Be(zScaled);
point.units.Should().Be(units);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
},
"Speckle.Revit2023.Interfaces": {
"type": "Direct",
"requested": "[0.1.1-preview.0.23, )",
"resolved": "0.1.1-preview.0.23",
"contentHash": "H66I9JRUGt1l1YS8aOdniRPDOixRPqua9puGrhGnTEKJ26kVlgkM3FpKfdAMFea4hf03hdqhnFVmNEwgA6mPHA=="
"requested": "[0.1.1-preview.0.24, )",
"resolved": "0.1.1-preview.0.24",
"contentHash": "BSVpOUJc9g6ISrw8GxvtkglTlITpHEDYNOhxv1ZPbckBsI0yO36JiphhQV4q57ERqD9PpCozUJkVhlCaxWeS6A=="
},
"Castle.Core": {
"type": "Transitive",
Expand Down Expand Up @@ -491,7 +491,7 @@
"dependencies": {
"Speckle.Autofac": "[2.0.999-local, )",
"Speckle.Objects": "[2.0.999-local, )",
"Speckle.Revit2023.Interfaces": "[0.1.1-preview.0.23, )"
"Speckle.Revit2023.Interfaces": "[0.1.1-preview.0.24, )"
}
},
"speckle.converters.common.dependencyinjection": {
Expand All @@ -505,7 +505,7 @@
"type": "Project",
"dependencies": {
"Speckle.Converters.Common": "[2.0.999-local, )",
"Speckle.Revit2023.Interfaces": "[0.1.1-preview.0.23, )"
"Speckle.Revit2023.Interfaces": "[0.1.1-preview.0.24, )"
}
},
"Speckle.Core": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Import Project="..\Speckle.Converters.RevitShared\Speckle.Converters.RevitShared.projitems" Label="Shared" />

<ItemGroup>
<PackageReference Include="Speckle.Revit2023.Interfaces" Version="0.1.1-preview.0.23" />
<PackageReference Include="Speckle.Revit2023.Interfaces" Version="0.1.1-preview.0.24" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ List<IRevitSolid> solids
//options = ViewSpecificOptions ?? options ?? new Options() { DetailLevel = DetailLevelSetting };
options ??= _revitOptionsFactory.Create(RevitViewDetailLevel.Fine);

IRevitGeometryElement geom;
IRevitGeometryElement? geom;
try
{
geom = element.GetGeometry(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public interface IParameterValueExtractor
{
object? GetValue(IRevitParameter parameter);
double GetValueAsDouble(IRevitElement element, RevitBuiltInParameter builtInParameter);
bool TryGetValueAsDouble(IRevitElement element, RevitBuiltInParameter builtInParameter, out double? value);
bool TryGetValueAsDouble(
IRevitElement element,
RevitBuiltInParameter builtInParameter,
[NotNullWhen(true)] out double? value
);
int GetValueAsInt(IRevitElement element, RevitBuiltInParameter builtInParameter);
bool? GetValueAsBool(IRevitElement element, RevitBuiltInParameter builtInParameter);
string? GetValueAsString(IRevitElement element, RevitBuiltInParameter builtInParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public SOBR.RevitColumn Convert(IRevitFamilyInstance target)
SOBR.RevitColumn speckleColumn =
new() { family = symbol.FamilyName, type = target.Document.GetElement(target.GetTypeId()).NotNull().Name };

//should these all be try?
if (
_parameterValueExtractor.TryGetValueAsRevitLevel(
target,
Expand All @@ -52,20 +53,41 @@ out var level
{
speckleColumn.level = _levelConverter.Convert(level);
}
//should these all be try?
if (
_parameterValueExtractor.TryGetValueAsRevitLevel(
target,
RevitBuiltInParameter.FAMILY_TOP_LEVEL_PARAM,
out var topLevel
)
)
{
speckleColumn.topLevel = _levelConverter.Convert(topLevel);
}

var topLevel = _parameterValueExtractor.GetValueAsRevitLevel(target, RevitBuiltInParameter.FAMILY_TOP_LEVEL_PARAM);

speckleColumn.topLevel = _levelConverter.Convert(topLevel);
speckleColumn.baseOffset = _parameterValueExtractor.GetValueAsDouble(
target,
RevitBuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM
);

speckleColumn.topOffset = _parameterValueExtractor.GetValueAsDouble(
target,
RevitBuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM
);
//should these all be try?
if (
_parameterValueExtractor.TryGetValueAsDouble(
target,
RevitBuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM,
out var baseOffset
)
)
{
speckleColumn.baseOffset = baseOffset.Value;
}

//should these all be try?
if (
_parameterValueExtractor.TryGetValueAsDouble(
target,
RevitBuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM,
out var topOffset
)
)
{
speckleColumn.topOffset = topOffset.Value;
}
speckleColumn.facingFlipped = target.FacingFlipped;
speckleColumn.handFlipped = target.HandFlipped;
speckleColumn.isSlanted = target.IsSlantedColumn;
Expand All @@ -77,7 +99,7 @@ out var level
}

speckleColumn.baseLine =
GetBaseCurve(target, speckleColumn.topLevel.elevation, speckleColumn.topOffset)
GetBaseCurve(target, speckleColumn.topLevel?.elevation, speckleColumn.topOffset)
?? throw new SpeckleConversionException("Unable to find a valid baseCurve for column");

speckleColumn.displayValue = _displayValueExtractor.GetDisplayValue(target);
Expand All @@ -87,7 +109,7 @@ out var level
return speckleColumn;
}

private ICurve? GetBaseCurve(IRevitFamilyInstance target, double topLevelElevation, double topLevelOffset)
private ICurve? GetBaseCurve(IRevitFamilyInstance target, double? topLevelElevation, double topLevelOffset)
{
Base baseGeometry = _locationConverter.Convert(target.Location);
ICurve? baseCurve = baseGeometry as ICurve;
Expand All @@ -110,7 +132,12 @@ out var level

return new SOG.Line(
basePoint,
new SOG.Point(basePoint.x, basePoint.y, topLevelElevation + topLevelOffset, _contextStack.Current.SpeckleUnits),
new SOG.Point(
basePoint.x,
basePoint.y,
topLevelElevation ?? 0 + topLevelOffset,
_contextStack.Current.SpeckleUnits
),
_contextStack.Current.SpeckleUnits
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SOG.Pointcloud Convert(IRevitPointCloudInstance target)
{
var minPlane = _revitPlaneUtils.CreateByNormalAndOrigin(
_revitxyzUtils.BasisZ,
transform.OfPoint(boundingBox.Min)
transform.OfPoint(boundingBox.NotNull().Min)
);
var filter = _revitFilterFactory.CreateMultiPlaneFilter(minPlane);
var points = target.GetPoints(filter, 0.0001, 999999); // max limit is 1 mil but 1000000 throws error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Speckle.Converters.Common;
using Speckle.Revit.Interfaces;

namespace Speckle.Converters.RevitShared;

public class RevitRootElementProvider : IRootElementProvider
{
private static readonly Type s_wrappedElementType = typeof(IRevitElement);

public Type GetRootType() => s_wrappedElementType;
}
Loading

0 comments on commit 77829a5

Please sign in to comment.