Skip to content

Commit

Permalink
style: dict utils as extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernsteinhagen committed Jan 28, 2025
1 parent 057cbaa commit bb14bb5
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ private void GetMaterialName(string sectionName, Dictionary<string, object?> pro
_settingsStore.Current.SapModel.PropFrame.GetMaterial(sectionName, ref materialName);

// append to General Data of properties dictionary
Dictionary<string, object?> generalData = DictionaryUtils.EnsureNestedDictionary(
properties,
SectionPropertyCategory.GENERAL_DATA
);
Dictionary<string, object?> generalData = properties.EnsureNested(SectionPropertyCategory.GENERAL_DATA);
generalData["Material"] = materialName;
}

Expand Down Expand Up @@ -77,22 +74,21 @@ ref radiusOfGyrationAboutMinorAxis
string modulusUnit = $"{distanceUnit}³"; // // TODO: Formalize this better
string inertiaUnit = $"{distanceUnit}\u2074"; // TODO: Formalize this better

Dictionary<string, object?> mechanicalProperties = DictionaryUtils.EnsureNestedDictionary(
properties,
Dictionary<string, object?> mechanicalProperties = properties.EnsureNested(
SectionPropertyCategory.SECTION_PROPERTIES
);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "Area", crossSectionalArea, areaUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "As2", shearAreaInMajorAxisDirection, areaUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "As3", shearAreaInMinorAxisDirection, areaUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "J", torsionalConstant, inertiaUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "I22", momentOfInertiaAboutMajorAxis, inertiaUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "I33", momentOfInertiaAboutMinorAxis, inertiaUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "S22", sectionModulusAboutMajorAxis, modulusUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "S33", sectionModulusAboutMinorAxis, modulusUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "Z22", plasticModulusAboutMajorAxis, modulusUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "Z33", plasticModulusAboutMinorAxis, modulusUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "R22", radiusOfGyrationAboutMajorAxis, distanceUnit);
DictionaryUtils.AddValueWithUnits(mechanicalProperties, "R33", radiusOfGyrationAboutMinorAxis, distanceUnit);
mechanicalProperties.AddWithUnits("Area", crossSectionalArea, areaUnit);
mechanicalProperties.AddWithUnits("As2", shearAreaInMajorAxisDirection, areaUnit);
mechanicalProperties.AddWithUnits("As3", shearAreaInMinorAxisDirection, areaUnit);
mechanicalProperties.AddWithUnits("J", torsionalConstant, inertiaUnit);
mechanicalProperties.AddWithUnits("I22", momentOfInertiaAboutMajorAxis, inertiaUnit);
mechanicalProperties.AddWithUnits("I33", momentOfInertiaAboutMinorAxis, inertiaUnit);
mechanicalProperties.AddWithUnits("S22", sectionModulusAboutMajorAxis, modulusUnit);
mechanicalProperties.AddWithUnits("S33", sectionModulusAboutMinorAxis, modulusUnit);
mechanicalProperties.AddWithUnits("Z22", plasticModulusAboutMajorAxis, modulusUnit);
mechanicalProperties.AddWithUnits("Z33", plasticModulusAboutMinorAxis, modulusUnit);
mechanicalProperties.AddWithUnits("R22", radiusOfGyrationAboutMajorAxis, distanceUnit);
mechanicalProperties.AddWithUnits("R33", radiusOfGyrationAboutMinorAxis, distanceUnit);
}

private void GetPropertyModifiers(string sectionName, Dictionary<string, object?> properties)
Expand All @@ -113,10 +109,7 @@ private void GetPropertyModifiers(string sectionName, Dictionary<string, object?
["Weight"] = stiffnessModifiersArray[7],
};

Dictionary<string, object?> generalData = DictionaryUtils.EnsureNestedDictionary(
properties,
SectionPropertyCategory.GENERAL_DATA
);
Dictionary<string, object?> generalData = properties.EnsureNested(SectionPropertyCategory.GENERAL_DATA);
generalData["Modifiers"] = modifiers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void GetGeneralProperties(string materialName, Dictionary<string, object
ref materialGuid
);

var generalData = DictionaryUtils.EnsureNestedDictionary(properties, SectionPropertyCategory.GENERAL_DATA);
var generalData = properties.EnsureNested(SectionPropertyCategory.GENERAL_DATA);
generalData["Name"] = materialName;
generalData["Type"] = materialType.ToString();
generalData["Notes"] = materialNotes;
Expand All @@ -76,7 +76,7 @@ private void GetWeightAndMassProperties(string materialName, Dictionary<string,
ref massPerUnitVolume
);

var weightAndMass = DictionaryUtils.EnsureNestedDictionary(properties, "Weight and Mass");
var weightAndMass = properties.EnsureNested("Weight and Mass");
weightAndMass["Weight per Unit Volume"] = weightPerUnitVolume;
weightAndMass["Mass per Unit Volume"] = massPerUnitVolume;
}
Expand All @@ -101,7 +101,7 @@ ref materialDirectionalSymmetryKey
_ => throw new ArgumentException($"Unknown symmetry type: {materialDirectionalSymmetryKey}")
};

var mechanicalProperties = DictionaryUtils.EnsureNestedDictionary(properties, "Mechanical Properties");
var mechanicalProperties = properties.EnsureNested("Mechanical Properties");
mechanicalProperties["Directional Symmetry Type"] = materialDirectionalSymmetryValue.ToString();

GetMechanicalPropertiesByType(materialName, materialDirectionalSymmetryValue, mechanicalProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ private void GetPropertyType(string sectionName, Dictionary<string, object?> pro
_ => throw new ArgumentException($"Unknown property type: {propertyTypeKey}"),
};

var generalData = DictionaryUtils.EnsureNestedDictionary(properties, SectionPropertyCategory.GENERAL_DATA);
generalData["Property Type"] = propertyTypeValue;
var generalData = properties.EnsureNested(SectionPropertyCategory.GENERAL_DATA);
generalData["Property Type"] = propertyTypeValue.ToString();
}

private void GetPropertyModifiers(string sectionName, Dictionary<string, object?> properties)
Expand All @@ -62,7 +62,7 @@ private void GetPropertyModifiers(string sectionName, Dictionary<string, object?
["Weight"] = stiffnessModifiersArray[8]
};

var generalData = DictionaryUtils.EnsureNestedDictionary(properties, SectionPropertyCategory.GENERAL_DATA);
var generalData = properties.EnsureNested(SectionPropertyCategory.GENERAL_DATA);
generalData["Modifiers"] = modifiers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,19 @@ ref area
if (sectionIndex != -1)
{
// General Data
var generalData = DictionaryUtils.EnsureNestedDictionary(properties, SectionPropertyCategory.GENERAL_DATA);
var generalData = properties.EnsureNested(SectionPropertyCategory.GENERAL_DATA);
generalData["Section Shape"] = propTypes[sectionIndex].ToString();

// Section Dimensions
string unit = _settingsStore.Current.SpeckleUnits;
var sectionDimensions = DictionaryUtils.EnsureNestedDictionary(
properties,
SectionPropertyCategory.SECTION_DIMENSIONS
);
DictionaryUtils.AddValueWithUnits(sectionDimensions, "t3", t3[sectionIndex], unit);
DictionaryUtils.AddValueWithUnits(sectionDimensions, "t2", t2[sectionIndex], unit);
DictionaryUtils.AddValueWithUnits(sectionDimensions, "tf", tf[sectionIndex], unit);
DictionaryUtils.AddValueWithUnits(sectionDimensions, "tw", tw[sectionIndex], unit);
DictionaryUtils.AddValueWithUnits(sectionDimensions, "t2b", t2b[sectionIndex], unit);
DictionaryUtils.AddValueWithUnits(sectionDimensions, "tfb", tfb[sectionIndex], unit);
DictionaryUtils.AddValueWithUnits(sectionDimensions, "Area", area[sectionIndex], $"{unit}²");
var sectionDimensions = properties.EnsureNested(SectionPropertyCategory.SECTION_DIMENSIONS);
sectionDimensions.AddWithUnits("t3", t3[sectionIndex], unit);
sectionDimensions.AddWithUnits("t2", t2[sectionIndex], unit);
sectionDimensions.AddWithUnits("tf", tf[sectionIndex], unit);
sectionDimensions.AddWithUnits("tw", tw[sectionIndex], unit);
sectionDimensions.AddWithUnits("t2b", t2b[sectionIndex], unit);
sectionDimensions.AddWithUnits("tfb", tfb[sectionIndex], unit);
sectionDimensions.AddWithUnits("Area", area[sectionIndex], $"{unit}²");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void ExtractProperties(string sectionName, Dictionary<string, object?> pr
continue;
}

var nestedProperties = DictionaryUtils.EnsureNestedDictionary(properties, nestedDictionary.Key);
var nestedProperties = properties.EnsureNested(nestedDictionary.Key);
foreach (var kvp in nestedValues)
{
nestedProperties[kvp.Key] = kvp.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ref guid

Dictionary<string, object?> propertyData = [];
propertyData["Type"] = "Wall";
DictionaryUtils.AddValueWithUnits(propertyData, "Thickness", thickness, settingsStore.Current.SpeckleUnits);
propertyData.AddWithUnits("Thickness", thickness, settingsStore.Current.SpeckleUnits);

Dictionary<string, object?> properties = [];
properties[SectionPropertyCategory.GENERAL_DATA] = generalData;
Expand Down Expand Up @@ -129,7 +129,7 @@ ref guid

Dictionary<string, object?> propertyData = [];
propertyData["Type"] = slabType.ToString();
DictionaryUtils.AddValueWithUnits(propertyData, "Thickness", thickness, settingsStore.Current.SpeckleUnits);
propertyData.AddWithUnits("Thickness", thickness, settingsStore.Current.SpeckleUnits);

Dictionary<string, object?> properties = [];
properties[SectionPropertyCategory.GENERAL_DATA] = generalData;
Expand Down Expand Up @@ -171,7 +171,7 @@ ref guid
generalData["Notes"] = notes;

Dictionary<string, object?> propertyData = [];
DictionaryUtils.AddValueWithUnits(propertyData, "Thickness", thickness, settingsStore.Current.SpeckleUnits);
propertyData.AddWithUnits("Thickness", thickness, settingsStore.Current.SpeckleUnits);

Dictionary<string, object?> properties = [];
properties[SectionPropertyCategory.GENERAL_DATA] = generalData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Helpers\IApplicationPropertiesExtractor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\TopLevel\CsiObjectToSpeckleConverterBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Constants.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\DictionaryUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\DictionaryExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Enums.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public void ExtractProperties(CsiFrameWrapper frame, PropertyExtractionResult fr
{
frameData.ApplicationId = frame.GetSpeckleApplicationId(_settingsStore.Current.SapModel);

var geometry = DictionaryUtils.EnsureNestedDictionary(frameData.Properties, ObjectPropertyCategory.GEOMETRY);
var geometry = frameData.Properties.EnsureNested(ObjectPropertyCategory.GEOMETRY);
(geometry["I-End Joint"], geometry["J-End Joint"]) = GetEndPointNames(frame);

var assignments = DictionaryUtils.EnsureNestedDictionary(frameData.Properties, ObjectPropertyCategory.ASSIGNMENTS);
var assignments = frameData.Properties.EnsureNested(ObjectPropertyCategory.ASSIGNMENTS);
assignments["Groups"] = GetGroupAssigns(frame);
assignments["Material Overwrite"] = GetMaterialOverwrite(frame);
assignments["Local Axis 2 Angle"] = GetLocalAxes(frame);
Expand Down Expand Up @@ -110,7 +110,7 @@ private string[] GetGroupAssigns(CsiFrameWrapper frame)
_ = _settingsStore.Current.SapModel.FrameObj.GetLocalAxes(frame.Name, ref angle, ref advanced);

Dictionary<string, object?> resultsDictionary = [];
DictionaryUtils.AddValueWithUnits(resultsDictionary, "Angle", angle, "Degrees");
resultsDictionary.AddWithUnits("Angle", angle, "Degrees");
resultsDictionary["Advanced"] = advanced.ToString();

return resultsDictionary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void ExtractProperties(CsiJointWrapper joint, PropertyExtractionResult jo
{
jointData.ApplicationId = joint.GetSpeckleApplicationId(_settingsStore.Current.SapModel);

var assignments = DictionaryUtils.EnsureNestedDictionary(jointData.Properties, ObjectPropertyCategory.ASSIGNMENTS);
var assignments = jointData.Properties.EnsureNested(ObjectPropertyCategory.ASSIGNMENTS);
assignments["groups"] = new List<string>(GetGroupAssigns(joint));
assignments["restraints"] = GetRestraints(joint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public void ExtractProperties(CsiShellWrapper shell, PropertyExtractionResult sh
{
shellData.ApplicationId = shell.GetSpeckleApplicationId(_settingsStore.Current.SapModel);

var geometry = DictionaryUtils.EnsureNestedDictionary(shellData.Properties, ObjectPropertyCategory.GEOMETRY);
var geometry = shellData.Properties.EnsureNested(ObjectPropertyCategory.GEOMETRY);
geometry["Joints"] = GetPointNames(shell); // TODO: 🪲 Viewer shows 4 but only displays 3

var assignments = DictionaryUtils.EnsureNestedDictionary(shellData.Properties, ObjectPropertyCategory.ASSIGNMENTS);
var assignments = shellData.Properties.EnsureNested(ObjectPropertyCategory.ASSIGNMENTS);
assignments["Groups"] = GetGroupAssigns(shell);
assignments["Local Axis 2 Angle"] = GetLocalAxes(shell);
assignments["Material Overwrite"] = GetMaterialOverwrite(shell);
Expand All @@ -56,7 +56,7 @@ private string[] GetGroupAssigns(CsiShellWrapper shell)
_ = _settingsStore.Current.SapModel.AreaObj.GetLocalAxes(shell.Name, ref angle, ref advanced);

Dictionary<string, object?> resultsDictionary = [];
DictionaryUtils.AddValueWithUnits(resultsDictionary, "Angle", angle, "Degrees");
resultsDictionary.AddWithUnits("Angle", angle, "Degrees");
resultsDictionary["Advanced"] = advanced.ToString();

return resultsDictionary;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Speckle.Converters.CSiShared.Utils;

/// <summary>
/// Provides utility methods for dictionary operations common across the CSI converter.
/// Provides extension methods for dictionary operations common across the CSI converter.
/// </summary>
public static class DictionaryUtils
public static class DictionaryExtensions
{
/// <summary>
/// Ensures a nested dictionary exists at the specified key, creating it if necessary.
Expand All @@ -12,7 +12,7 @@ public static class DictionaryUtils
/// <remarks>
/// This pattern is used throughout property extractors to maintain consistent property organization.
/// </remarks>
public static Dictionary<string, object?> EnsureNestedDictionary(Dictionary<string, object?> dictionary, string key)
public static Dictionary<string, object?> EnsureNested(this Dictionary<string, object?> dictionary, string key)
{
if (!dictionary.TryGetValue(key, out var obj) || obj is not Dictionary<string, object?> nestedDictionary)
{
Expand All @@ -27,13 +27,13 @@ public static class DictionaryUtils
/// Adds a value with its associated units to a parent dictionary using a standardized format.
/// Creates a nested dictionary with 'name', 'value', and 'units' keys.
/// </summary>
public static void AddValueWithUnits(
Dictionary<string, object?> parentDictionary,
public static void AddWithUnits(
this Dictionary<string, object?> dictionary,
string key,
object value,
string? units = null
) =>
parentDictionary[key] = new Dictionary<string, object?>
dictionary[key] = new Dictionary<string, object?>
{
["name"] = key,
["value"] = value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public EtabsFramePropertiesExtractor(IConverterSettingsStore<CsiConversionSettin

public void ExtractProperties(CsiFrameWrapper frame, Dictionary<string, object?> properties)
{
var objectId = DictionaryUtils.EnsureNestedDictionary(properties, ObjectPropertyCategory.OBJECT_ID);
var objectId = properties.EnsureNested(ObjectPropertyCategory.OBJECT_ID);
objectId["Design Orientation"] = GetDesignOrientation(frame);
(objectId["Label"], objectId["Level"]) = GetLabelAndLevel(frame);

var assignments = DictionaryUtils.EnsureNestedDictionary(properties, ObjectPropertyCategory.ASSIGNMENTS);
var assignments = properties.EnsureNested(ObjectPropertyCategory.ASSIGNMENTS);
assignments["Spring Assignment"] = GetSpringAssignmentName(frame);

var design = DictionaryUtils.EnsureNestedDictionary(properties, ObjectPropertyCategory.DESIGN);
var design = properties.EnsureNested(ObjectPropertyCategory.DESIGN);
design["Design Procedure"] = GetDesignProcedure(frame);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public EtabsJointPropertiesExtractor(IConverterSettingsStore<CsiConversionSettin

public void ExtractProperties(CsiJointWrapper joint, Dictionary<string, object?> properties)
{
var objectId = DictionaryUtils.EnsureNestedDictionary(properties, ObjectPropertyCategory.OBJECT_ID);
var objectId = properties.EnsureNested(ObjectPropertyCategory.OBJECT_ID);
(objectId["label"], objectId["level"]) = GetLabelAndLevel(joint);

var assignments = DictionaryUtils.EnsureNestedDictionary(properties, ObjectPropertyCategory.ASSIGNMENTS);
var assignments = properties.EnsureNested(ObjectPropertyCategory.ASSIGNMENTS);
(assignments["diaphragmOption"], assignments["diaphragmName"]) = GetAssignedDiaphragm(joint);
assignments["springAssignment"] = GetSpringAssignmentName(joint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ IConverterSettingsStore<CsiConversionSettings> settingsStore

public void ExtractProperties(CsiShellWrapper shell, Dictionary<string, object?> properties)
{
var objectId = DictionaryUtils.EnsureNestedDictionary(properties, ObjectPropertyCategory.OBJECT_ID);
var objectId = properties.EnsureNested(ObjectPropertyCategory.OBJECT_ID);
objectId["Design Orientation"] = GetDesignOrientation(shell);
(objectId["Label"], objectId["Level"]) = GetLabelAndLevel(shell);

var assignments = DictionaryUtils.EnsureNestedDictionary(properties, ObjectPropertyCategory.ASSIGNMENTS);
var assignments = properties.EnsureNested(ObjectPropertyCategory.ASSIGNMENTS);
assignments["Diaphragm"] = GetAssignedDiaphragmName(shell);
assignments["Opening"] = IsOpening(shell);
assignments["Pier"] = GetPierAssignmentName(shell);
Expand Down

0 comments on commit bb14bb5

Please sign in to comment.