Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bjorn/cnx-835-add-converter-projects-and-top-level-converter #429

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
units framework
  • Loading branch information
bjoernsteinhagen committed Dec 1, 2024
commit 173451f6da21f6f1c6ba6f6e6d84662f4a3ec08e
Original file line number Diff line number Diff line change
@@ -5,12 +5,12 @@ namespace Speckle.Converters.CSiShared;

[GenerateAutoInterface]
public class CSiConversionSettingsFactory(
IHostToSpeckleUnitConverter<string> unitsConverter,
IHostToSpeckleUnitConverter<eUnits> unitsConverter,
IConverterSettingsStore<CSiConversionSettings> settingsStore
) : ICSiConversionSettingsFactory
{
public CSiConversionSettings Current => settingsStore.Current;

// TODO: Units currently hard-coded
public CSiConversionSettings Create(cSapModel document) => new(document, unitsConverter.ConvertOrThrow("mm"));
public CSiConversionSettings Create(cSapModel document) =>
new(document, unitsConverter.ConvertOrThrow(document.GetPresentUnits()));
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
using Speckle.Converters.Common;
using Speckle.Sdk.Common;
using Speckle.Sdk.Common.Exceptions;

namespace Speckle.Converters.CSiShared;

// TODO: Unit conversions! This is all gross, I know!
public class CSiToSpeckleUnitConverter : IHostToSpeckleUnitConverter<string>
/// <summary>
/// Convert CSi eUnits enumeration to Speckle units.
/// </summary>
/// <remarks>
/// CSi GetPresentUnits() valid for both SAP 2000 and ETABS.
/// Represents units transmitted through API calls and not necessarily those displayed in GUI.
/// </remarks>
public class CSiToSpeckleUnitConverter : IHostToSpeckleUnitConverter<eUnits>
{
#pragma warning disable IDE0060
public string ToSpeckle(string value)
#pragma warning restore IDE0060
{
// CSi default is meters
return "m";
}
private readonly Dictionary<eUnits, string> _unitMapping = new Dictionary<eUnits, string>();

#pragma warning disable IDE0060
public string FromSpeckle(string value)
#pragma warning restore IDE0060
public CSiToSpeckleUnitConverter()
{
return "m";
_unitMapping[eUnits.lb_in_F] = Units.Inches;
_unitMapping[eUnits.lb_ft_F] = Units.Feet;
_unitMapping[eUnits.kip_in_F] = Units.Inches;
_unitMapping[eUnits.kip_ft_F] = Units.Feet;
_unitMapping[eUnits.kN_mm_C] = Units.Millimeters;
_unitMapping[eUnits.kN_m_C] = Units.Meters;
_unitMapping[eUnits.kgf_mm_C] = Units.Millimeters;
_unitMapping[eUnits.kgf_m_C] = Units.Meters;
_unitMapping[eUnits.N_mm_C] = Units.Millimeters;
_unitMapping[eUnits.N_m_C] = Units.Meters;
_unitMapping[eUnits.Ton_mm_C] = Units.Millimeters;
_unitMapping[eUnits.Ton_m_C] = Units.Meters;
_unitMapping[eUnits.kN_cm_C] = Units.Centimeters;
_unitMapping[eUnits.kgf_cm_C] = Units.Centimeters;
_unitMapping[eUnits.N_cm_C] = Units.Centimeters;
_unitMapping[eUnits.Ton_cm_C] = Units.Centimeters;
}

public string ConvertOrThrow(string hostUnit)
{
return hostUnit ?? throw new ArgumentNullException(nameof(hostUnit), "Host unit cannot be null");
}
public string ConvertOrThrow(eUnits hostUnit) =>
_unitMapping.TryGetValue(hostUnit, out string? value)
? value
: throw new UnitNotSupportedException($"The Unit System \"{hostUnit}\" is unsupported.");
}
Original file line number Diff line number Diff line change
@@ -16,10 +16,11 @@ public static IServiceCollection AddCSiConverters(this IServiceCollection servic

serviceCollection.AddTransient<CSiObjectToSpeckleConverter>();
serviceCollection.AddScoped<DisplayValueExtractor>();

// TODO: Property extractor

serviceCollection.AddRootCommon<CSiRootToSpeckleConverter>(converterAssembly);
serviceCollection.AddApplicationConverters<CSiToSpeckleUnitConverter, string>(converterAssembly);
serviceCollection.AddApplicationConverters<CSiToSpeckleUnitConverter, eUnits>(converterAssembly);
serviceCollection.AddScoped<
IConverterSettingsStore<CSiConversionSettings>,
ConverterSettingsStore<CSiConversionSettings>
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ public JointToSpeckleConverter(IConverterSettingsStore<CSiConversionSettings> se
_settingStore = settingStore;
}

public Point Convert(CSiJointWrapper target) // NOTE: This is just a tempoarary POC
public Point Convert(CSiJointWrapper target) // NOTE: This is just a temporary POC
{
string applicationId = "";