Skip to content

Commit

Permalink
chore: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
didimitrie committed Oct 9, 2024
1 parent c7eee14 commit e153eeb
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ CancellationToken cancellationToken
)
{
var baseGroupName = $"Project {projectName}: Model {modelName}"; // TODO: unify this across connectors!

onOperationProgressed.Report(new("Converting", null));
using var activity = _activityFactory.Start("Build");

// 0 - Clean then Rock n Roll! 🎸
{
_activityFactory.Start("Pre receive clean");
Expand All @@ -101,7 +101,7 @@ CancellationToken cancellationToken
{
_logger.LogError(ex, "Failed to clean up before receive in Revit");
}

_transactionManager.CommitTransaction();
}

Expand All @@ -111,7 +111,7 @@ CancellationToken cancellationToken
unpackedRoot.DefinitionProxies,
unpackedRoot.ObjectsToConvert.ToList()
);

// 2 - Bake materials
if (unpackedRoot.RenderMaterialProxies != null)
{
Expand All @@ -121,41 +121,45 @@ CancellationToken cancellationToken
foreach (var kvp in map)
{
_revitToHostCacheSingleton.MaterialsByObjectId.Add(kvp.Key, kvp.Value);
}
}
_transactionManager.CommitTransaction();
}

// 3 - Bake objects
(HostObjectBuilderResult builderResult, List<(DirectShape res, string applicationId)> postBakePaintTargets)
conversionResults;
(
HostObjectBuilderResult builderResult,
List<(DirectShape res, string applicationId)> postBakePaintTargets
) conversionResults;
{
_activityFactory.Start("Baking objects");
_transactionManager.StartTransaction(true, "Baking objects");
conversionResults = BakeObjects(localToGlobalMaps, onOperationProgressed, cancellationToken);
_transactionManager.CommitTransaction();
}

// 4 - Paint solids
{
_activityFactory.Start("Painting solids");
_transactionManager.StartTransaction(true, "Painting solids");
PostBakePaint(conversionResults.postBakePaintTargets);
_transactionManager.CommitTransaction();
}

// 5 - Create group
{
_activityFactory.Start("Grouping");
_transactionManager.StartTransaction(true, "Grouping");
_groupBaker.BakeGroupForTopLevel(baseGroupName);
_transactionManager.CommitTransaction();
_transactionManager.CommitTransaction();
}



return conversionResults.builderResult;
}

private (HostObjectBuilderResult builderResult, List<(DirectShape res, string applicationId)> postBakePaintTargets) BakeObjects(
private (
HostObjectBuilderResult builderResult,
List<(DirectShape res, string applicationId)> postBakePaintTargets
) BakeObjects(
List<LocalToGlobalMap> localToGlobalMaps,
IProgress<CardProgress> onOperationProgressed,
CancellationToken cancellationToken
Expand All @@ -177,7 +181,11 @@ CancellationToken cancellationToken

// POC hack of the ages: try to pre transform curves before baking
// we need to bypass the local to global converter as there we don't have access to what we want. that service will/should stop existing.
if (localToGlobalMap.AtomicObject is ITransformable transformable and ICurve && localToGlobalMap.Matrix.Count > 0 && localToGlobalMap.AtomicObject["units"] is string units)
if (
localToGlobalMap.AtomicObject is ITransformable transformable and ICurve
&& localToGlobalMap.Matrix.Count > 0
&& localToGlobalMap.AtomicObject["units"] is string units
)
{
ITransformable? newTransformable = null;
foreach (var mat in localToGlobalMap.Matrix)
Expand All @@ -188,7 +196,7 @@ CancellationToken cancellationToken
localToGlobalMap.AtomicObject = (newTransformable as Base)!;
localToGlobalMap.Matrix = new(); // flush out the list, as we've applied the transforms already
}

// actual conversion happens here!
var result = _converter.Convert(localToGlobalMap.AtomicObject);
onOperationProgressed.Report(new("Converting", (double)++count / localToGlobalMaps.Count));
Expand All @@ -201,19 +209,21 @@ CancellationToken cancellationToken

bakedObjectIds.Add(directShapes.UniqueId);
_groupBaker.AddToTopLevelGroup(directShapes);

if (localToGlobalMap.AtomicObject is IRawEncodedObject and Base myBase)
{
postBakePaintTargets.Add((directShapes, myBase.applicationId ?? myBase.id));
}

conversionResults.Add(
new(Status.SUCCESS, localToGlobalMap.AtomicObject, directShapes.UniqueId, "Direct Shape")
);
}
else
{
throw new SpeckleConversionException($"Failed to cast {result.GetType()} to direct shape definition wrapper.");
throw new SpeckleConversionException(
$"Failed to cast {result.GetType()} to direct shape definition wrapper."
);
}
}
catch (Exception ex) when (!ex.IsFatal())
Expand Down Expand Up @@ -244,7 +254,7 @@ private void PostBakePaint(List<(DirectShape res, string applicationId)> paintTa
{
continue;
}

// NOTE: some geometries fail to convert as solids, and the api defaults back to meshes (from the shape importer). These cannot be painted, so don't bother.
foreach (var geo in elGeometry)
{
Expand All @@ -258,12 +268,11 @@ private void PostBakePaint(List<(DirectShape res, string applicationId)> paintTa
}
}
}

private void PreReceiveDeepClean(string baseGroupName)
{
DirectShapeLibrary
.GetDirectShapeLibrary(_converterSettings.Current.Document).Reset(); // Note: this needs to be cleared, as it is being used in the converter

DirectShapeLibrary.GetDirectShapeLibrary(_converterSettings.Current.Document).Reset(); // Note: this needs to be cleared, as it is being used in the converter

_revitToHostCacheSingleton.MaterialsByObjectId.Clear(); // Massive hack!
_groupBaker.PurgeGroups(baseGroupName);
_materialBaker.PurgeMaterials(baseGroupName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ CancellationToken cancellationToken
{
var guid = BakeObject(geometryBase, obj, layerIndex);
conversionIds.Add(guid.ToString());
}
}
else if (result is List<GeometryBase> geometryBases) // one to many raw encoding case
{
// NOTE: I'm unhappy about this case (dim). It's needed as the raw encoder approach can hypothetically return
// multiple "geometry bases" - but this is not a fallback conversion.
// EXTRA NOTE: Oguzhan says i shouldn't be unhappy about this - it's a legitimate case
// EXTRA EXTRA NOTE: TY Ogu, i am no longer than unhappy about it. It's legit "mess".
// EXTRA EXTRA NOTE: TY Ogu, i am no longer than unhappy about it. It's legit "mess".
foreach (var gb in geometryBases)
{
var guid = BakeObject(gb, obj, layerIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,38 @@ RevitToHostCacheSingleton revitToHostCacheSingleton

DB.ElementId materialId = DB.ElementId.InvalidElementId;
if (
_revitToHostCacheSingleton.MaterialsByObjectId.TryGetValue(targetAsBase.applicationId ?? targetAsBase.id, out var mappedElementId)
_revitToHostCacheSingleton.MaterialsByObjectId.TryGetValue(
targetAsBase.applicationId ?? targetAsBase.id,
out var mappedElementId
)
)
{
materialId = mappedElementId;
}

if (materialId == DB.ElementId.InvalidElementId)
{
return shapeImportResult.ToList(); // exit fast if there's no material id associated with this object
}

// check whether the results have any meshes inside - if yes, it means the shape importer produced a subpar result.
// as we cannot paint meshes later (as you can solid faces), we need to create them now.
// we'll default to using the display value of the original object as it's a better fallback.
// note: if you're tempted to try and re-mesh the shape importer's meshes, don't - they are garbage.
// check whether the results have any meshes inside - if yes, it means the shape importer produced a subpar result.
// as we cannot paint meshes later (as you can solid faces), we need to create them now.
// we'll default to using the display value of the original object as it's a better fallback.
// note: if you're tempted to try and re-mesh the shape importer's meshes, don't - they are garbage.
var hasMesh = shapeImportResult.Any(o => o is DB.Mesh);
if (!hasMesh)
{
return shapeImportResult.ToList();
}

var displayValue = targetAsBase.TryGetDisplayValue<SOG.Mesh>().NotNull();
var returnList = new List<DB.GeometryObject>();
foreach (var mesh in displayValue)
{
mesh.applicationId = targetAsBase.applicationId ?? targetAsBase.id; // to properly map materials
returnList.AddRange(_meshConverter.Convert(mesh));
}

return returnList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class MeshConverterToHost : ITypedConverter<SOG.Mesh, List<DB.GeometryObj
private readonly ScalingServiceToHost _scalingServiceToHost;

public MeshConverterToHost(
RevitToHostCacheSingleton revitToHostCacheSingleton, ScalingServiceToHost scalingServiceToHost
RevitToHostCacheSingleton revitToHostCacheSingleton,
ScalingServiceToHost scalingServiceToHost
)
{
_revitToHostCacheSingleton = revitToHostCacheSingleton;
Expand All @@ -31,7 +32,7 @@ public MeshConverterToHost(
Target = target,
GraphicsStyleId = ElementId.InvalidElementId
};

tsb.OpenConnectedFaceSet(false);
var vertices = ArrayToPoints(mesh.vertices, mesh.units);

Expand Down Expand Up @@ -121,13 +122,14 @@ private XYZ[] ArrayToPoints(IList<double> arr, string units)

XYZ[] points = new XYZ[arr.Count / 3];
var fTypeId = _scalingServiceToHost.UnitsToNative(units) ?? UnitTypeId.Meters;

for (int i = 2, k = 0; i < arr.Count; i += 3)
{
points[k++] = new XYZ(
_scalingServiceToHost.ScaleToNative(arr[i - 2], fTypeId),
_scalingServiceToHost.ScaleToNative(arr[i - 2], fTypeId),
_scalingServiceToHost.ScaleToNative(arr[i - 1], fTypeId),
_scalingServiceToHost.ScaleToNative(arr[i], fTypeId));
_scalingServiceToHost.ScaleToNative(arr[i], fTypeId)
);
}

return points;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ public DB.DirectShape Convert((Base atomicObject, List<Matrix4x4> matrix) target

// 2 - init DirectShape
var result = DB.DirectShape.CreateElement(_converterSettings.Current.Document, new DB.ElementId(dsCategory));

// If there is no transforms to be applied, use the simple way of creating direct shapes
if (target.matrix.Count == 0)
{
var def = DB.DirectShapeLibrary.GetDirectShapeLibrary(_converterSettings.Current.Document)
var def = DB
.DirectShapeLibrary.GetDirectShapeLibrary(_converterSettings.Current.Document)
.FindDefinition(target.atomicObject.applicationId ?? target.atomicObject.id);
result.SetShape(def);
return result; // note fast exit here
}

// 3 - Transform the geometries
DB.Transform combinedTransform = DB.Transform.Identity;

Expand All @@ -68,7 +69,7 @@ public DB.DirectShape Convert((Base atomicObject, List<Matrix4x4> matrix) target
combinedTransform = combinedTransform.Multiply(revitTransform);
}
}

var transformedGeometries = DB.DirectShape.CreateGeometryInstance(
_converterSettings.Current.Document,
target.atomicObject.applicationId ?? target.atomicObject.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@

namespace Speckle.Converters.Rhino.ToHost.Raw;

public class BrepXToHostConverter: ITypedConverter<SOG.BrepX, List<RG.GeometryBase>>
public class BrepXToHostConverter : ITypedConverter<SOG.BrepX, List<RG.GeometryBase>>
{
public List<RG.GeometryBase> Convert(SOG.BrepX target) => RawEncodingToHost.Convert(target);
}

public class SubDXToHostConverter: ITypedConverter<SOG.SubDX, List<RG.GeometryBase>>
public class SubDXToHostConverter : ITypedConverter<SOG.SubDX, List<RG.GeometryBase>>
{
public List<RG.GeometryBase> Convert(SOG.SubDX target) => RawEncodingToHost.Convert(target);
}

public class ExtrusionElonMuskXToHostConverter: ITypedConverter<SOG.ExtrusionX, List<RG.GeometryBase>>
public class ExtrusionElonMuskXToHostConverter : ITypedConverter<SOG.ExtrusionX, List<RG.GeometryBase>>
{
public List<RG.GeometryBase> Convert(SOG.ExtrusionX target) => RawEncodingToHost.Convert(target);
}



public static class RawEncodingToHost
{
public static List<RG.GeometryBase> Convert(SOG.IRawEncodedObject target)
{
// note: I am not sure that we're going to have other encoding formats, but who knows.
// note: I am not sure that we're going to have other encoding formats, but who knows.
switch (target.encodedValue.format)
{
case SO.RawEncodingFormats.RHINO_3DM:
Expand All @@ -34,7 +32,7 @@ public static class RawEncodingToHost
throw new SpeckleConversionException($"Unsupported brep encoding format: {target.encodedValue.format}");
}
}

private static List<RG.GeometryBase> Handle3dm(SOG.IRawEncodedObject target)
{
var bytes = System.Convert.FromBase64String(target.encodedValue.contents);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;

namespace Speckle.Converters.Rhino.ToHost.TopLevel;

[NameAndRankValue(nameof(SOG.BrepX), NameAndRankValueAttribute.SPECKLE_DEFAULT_RANK)]
Expand All @@ -23,12 +24,12 @@ public SubDXToHostTopLevelConverter(
}

[NameAndRankValue(nameof(SOG.ExtrusionX), NameAndRankValueAttribute.SPECKLE_DEFAULT_RANK)]
public class ExtrusionXToHostTopLevelConverter : SpeckleToHostGeometryBaseTopLevelConverter<SOG.ExtrusionX, List<RG.GeometryBase>>
public class ExtrusionXToHostTopLevelConverter
: SpeckleToHostGeometryBaseTopLevelConverter<SOG.ExtrusionX, List<RG.GeometryBase>>
{
public ExtrusionXToHostTopLevelConverter(
IConverterSettingsStore<RhinoConversionSettings> settingsStore,
ITypedConverter<SOG.ExtrusionX, List<RG.GeometryBase>> geometryBaseConverter
)
: base(settingsStore, geometryBaseConverter) { }
}

Loading

0 comments on commit e153eeb

Please sign in to comment.