ApprenticeFoundryRulesAndUnits 8.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ApprenticeFoundryRulesAndUnits --version 8.1.0
                    
NuGet\Install-Package ApprenticeFoundryRulesAndUnits -Version 8.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ApprenticeFoundryRulesAndUnits" Version="8.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApprenticeFoundryRulesAndUnits" Version="8.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ApprenticeFoundryRulesAndUnits" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ApprenticeFoundryRulesAndUnits --version 8.1.0
                    
#r "nuget: ApprenticeFoundryRulesAndUnits, 8.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ApprenticeFoundryRulesAndUnits@8.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ApprenticeFoundryRulesAndUnits&version=8.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ApprenticeFoundryRulesAndUnits&version=8.1.0
                    
Install as a Cake Tool

Foundry Rules and Units

Overview

FoundryRulesAndUnits is a comprehensive, modernized unit system library providing type-safe unit conversions, measurement operations, and full backward compatibility. This library supports 6 complete unit systems (SI, MKS, CGS, FPS, IPS, mmNs) with 39+ unit families and advanced features for engineering and scientific applications.

๐Ÿš€ Key Features

Modern Architecture

  • Unified Unit System: Clean IUnitSystem interface handling all conversions (no singletons!)
  • Type-Safe Conversions: Strongly-typed unit operations with compile-time safety
  • 24+ Unit Types: Complete coverage from Length/Mass to specialized units like Frequency/Resistance
  • Factory Methods: Intuitive construction with Length.FromMeters(5.0), Temperature.FromCelsius(25)
  • Full Operator Support: Natural arithmetic with length1 + length2, force * scalar

Unit Systems Supported

  • SI (International System of Units)
  • MKS (Meter-Kilogram-Second)
  • CGS (Centimeter-Gram-Second)
  • FPS (Foot-Pound-Second)
  • IPS (Inch-Pound-Second)
  • mmNs (Millimeter-Newton-Second)

39+ Unit Families

  • Mechanical: Length, Mass, Force, Speed, Power, Area, Volume
  • Thermal: Temperature, Pressure
  • Electrical: Voltage, Current, Resistance, Capacitance
  • Digital: DataStorage, DataFlow
  • Scientific: Frequency, Time, Duration, Angle
  • Specialized: Quantity, QuantityFlow, Percent, Dimensionless

๐Ÿ“ฆ Installation & Setup

NuGet Package

<PackageReference Include="ApprenticeFoundryRulesAndUnits" Version="8.0.0" />

Basic Setup

using FoundryRulesAndUnits.Units;

// Initialize the global unit system (do this once at app startup)
MeasuredValue.SetGlobalUnitSystem(UnitSystemType.SI); // or MKS, CGS, FPS, IPS, mmNs

// Alternatively, set it with a UnitSystem instance
var unitSystem = new UnitSystem(UnitSystemType.SI);
MeasuredValue.SetGlobalUnitSystem(unitSystem);

๐Ÿ’ก Quick Start Examples

Creating Measurements

// Factory methods (recommended)
var length = Length.FromMeters(5.0);
var temp = Temperature.FromCelsius(25.0);
var force = Force.FromNewtons(100.0);

// Constructor approach
var mass = new Mass(50.0, "kg");
var speed = new Speed(60.0, "mph");

Unit Conversions

var length = Length.FromMeters(5.0);
double feet = length.As("ft");        // Convert to feet
double inches = length.As("in");      // Convert to inches
double pixels = length.AsPixels();    // Special conversion for UI

Arithmetic Operations

var length1 = Length.FromMeters(10);
var length2 = Length.FromFeet(5);

var total = length1 + length2;         // Addition
var difference = length1 - length2;    // Subtraction
var scaled = length1 * 2.0;           // Scalar multiplication
var ratio = length1 / length2;        // Ratio (returns double)

Advanced Operations

// Create flow rates from quantities and time
var quantity = Quantity.FromEach(1000);
var time = Time.FromSeconds(60);
var flow = quantity / time;            // QuantityFlow in "ea/s"

// Compare measurements
if (length1 > length2) {
    Console.WriteLine("Length1 is longer");
}

๐Ÿ—๏ธ Architecture Overview

Modern Unit Classes (24/24 FULLY MODERNIZED) ๐ŸŽ‰

ALL unit classes now follow the clean, modern pattern:

โœ… Physical & Mechanical Units:

  • Length, Mass, Temperature, Volume, Force
  • Speed, Power, Area, Time, Duration
  • Distance, Frequency

โœ… Electrical Units:

  • Voltage, Resistance, Current, Capacitance

โœ… Digital & Computing Units:

  • DataStorage, DataFlow

โœ… Specialized Units:

  • Quantity, QuantityFlow, Percent, Heading, Dimensionless

๐Ÿš€ ALL CLASSES FEATURE:

  • Factory methods (FromMeters(), FromVolts(), etc.)
  • Enhanced operators (+, -, *, /, >, <, etc.)
  • JSON serialization support
  • Full backward compatibility

Unit System Management

// Global unit system manages all conversions (clean, no singletons!)
var globalSystem = MeasuredValue.GlobalSystem;

// Switch unit systems at runtime
globalSystem.Apply(UnitSystemType.FPS);  // Switch to Imperial
var converted = globalSystem.Convert(100, "m", "ft"); // 328.084 feet

// Or change globally for all measurements
MeasuredValue.SetGlobalUnitSystem(UnitSystemType.FPS);

// Validation
bool isValid = globalSystem.IsValidUnit("mph");  // true
var units = globalSystem.GetUnitsForFamily(UnitFamilyName.Length); // ["m", "cm", "km", ...]

๐Ÿ”„ Backward Compatibility

Legacy Support Classes

For existing codebases, we provide full compatibility:

// Legacy UnitSystem class (now contains all functionality directly)
var unitSystem = new UnitSystem(UnitSystemType.MKS);
unitSystem.Apply(UnitSystemType.SI);
var categories = unitSystem.Categories();

// Legacy extension methods
var family = UnitCategoryExtensions.GetUnitFamily("kg");  // UnitFamilyName.Mass
bool known = UnitCategoryExtensions.IsKnownUnit("mph");   // true

// Legacy Length methods
var length = new Length(100, "m");
var category = Length.Category();        // UnitCategory (marked obsolete)
double pixels = length.AsPixels();       // UI conversion

๐Ÿš€ Migration Guide

From Legacy to Modern

Old Pattern:

// Legacy approach
var length = new Length(5.0, "m");
var convertedValue = length.As("ft");

New Pattern:

// Modern approach
var length = Length.FromMeters(5.0);    // Factory method
var convertedValue = length.As("ft");   // Same conversion API

Key Benefits of Modern Classes

  1. Factory Methods: Length.FromMeters() vs new Length(value, "m")
  2. Enhanced Operators: Full arithmetic support including scalar operations
  3. Better Performance: Direct integration with global unit system
  4. JSON Serialization: Built-in JSON converter support
  5. Cleaner Code: No singleton dependencies, clean architecture!

๐Ÿงช Testing & Validation

Build Status

  • โœ… FoundryRulesAndUnits: Builds successfully (3 warnings)
  • โœ… FoundryMentorModeler: Builds successfully (3 warnings)
  • โœ… All Tests Passing: Legacy compatibility maintained

Validation Examples

// Unit system validation
var globalSystem = MeasuredValue.GlobalSystem;
Debug.Assert(globalSystem.IsValidUnit("kg"));
Debug.Assert(globalSystem.Convert(1000, "g", "kg") == 1.0);

// Measurement validation  
var length = Length.FromKilometers(1.0);
Debug.Assert(Math.Abs(length.As("m") - 1000.0) < 0.001);

๐Ÿ“ Project Structure

FoundryRulesAndUnits/
โ”œโ”€โ”€ UnitSystem/
โ”‚   โ”œโ”€โ”€ MeasuredValue.cs              # Base class with global unit system
โ”‚   โ”œโ”€โ”€ UnitSystem.cs                 # Complete IUnitSystem implementation  
โ”‚   โ”œโ”€โ”€ UnitCategory.cs               # Legacy compatibility (+ enum)
โ”‚   โ”œโ”€โ”€ Specifications/               # Unit system definitions
โ”‚   โ”‚   โ”œโ”€โ”€ IUnitSystemSpecification.cs
โ”‚   โ”‚   โ”œโ”€โ”€ SISpecification.cs        # SI unit definitions
โ”‚   โ”‚   โ”œโ”€โ”€ MKSSpecification.cs       # MKS unit definitions
โ”‚   โ”‚   โ””โ”€โ”€ [Other system specs...]
โ”‚   โ””โ”€โ”€ UnitTypes/                    # Individual unit classes
โ”‚       โ”œโ”€โ”€ Length.cs                 # โœ… Modernized
โ”‚       โ”œโ”€โ”€ Mass.cs                   # โœ… Modernized  
โ”‚       โ”œโ”€โ”€ Temperature.cs            # โœ… Modernized
โ”‚       โ”œโ”€โ”€ Force.cs                  # โœ… Modernized
โ”‚       โ””โ”€โ”€ [24+ other unit types...]
โ”œโ”€โ”€ Extensions/
โ”‚   โ”œโ”€โ”€ UnitCategoryExtensions.cs     # Legacy compatibility helpers
โ”‚   โ””โ”€โ”€ [Other utility extensions...]
โ””โ”€โ”€ DataModels/                       # Supporting data structures

๐Ÿค Contributing

Adding New Unit Types

Follow the modernized pattern:

[System.Serializable]
[JsonConverter(typeof(MyUnitJsonConverter))]
public class MyUnit : MeasuredValue
{
    public MyUnit() : base(UnitFamilyName.MyFamily) { }
    
    public MyUnit(double value, string? units = null) : base(UnitFamilyName.MyFamily)
    {
        Init(value, units);
    }
    
    // Factory methods
    public static MyUnit FromBaseUnit(double value) => new(value, "base");
    
    // Operators
    public static MyUnit operator +(MyUnit left, MyUnit right) => 
        new(left.Value() + right.Value(), left.Internal());
}

Extending Unit Systems

Add new specifications implementing IUnitSystemSpecification:

public class MyCustomSpecification : IUnitSystemSpecification
{
    public string SystemName => "MySystem";
    // Implement interface methods...
}

๐Ÿ“„ License

MIT License - See LICENSE file for details.

๐Ÿข Authors


  • FoundryMentorModeler: Advanced modeling toolkit using this unit system
  • TRISoC Dashboard: Digital twin dashboard with unit system integration

Version: 8.0.0 | Target: .NET 9.0 | Updated: September 2025

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on ApprenticeFoundryRulesAndUnits:

Package Downloads
ApprenticeFoundryBlazor

A comprehensive C# / Blazor diagramming library that combines 2D and 3D visualization capabilities. Supports .NET 9, includes advanced layout algorithms, glued connections, multi-page diagrams, and seamless 2D/3D integration.

ApprenticeFoundryBlazorThreeJS

3D graphics for blazor applications

ApprenticeFoundryMessageLibrary

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.0.0 0 9/23/2025
8.1.0 46 9/22/2025
7.3.0 162 8/11/2025
7.2.0 144 8/10/2025
7.1.0 98 8/9/2025
7.0.0 126 8/9/2025
6.1.0 103 7/11/2025
5.3.0 581 2/7/2025
5.2.0 218 1/28/2025
5.1.0 220 1/20/2025
5.0.2 120 1/11/2025
5.0.1 146 1/1/2025
5.0.0 143 1/1/2025
4.4.0 249 11/17/2024
4.3.0 709 9/11/2024
4.2.0 155 9/11/2024
4.1.0 146 9/10/2024
4.0.0 375 8/27/2024
3.7.0 392 8/20/2024
3.6.0 221 8/13/2024
3.5.0 175 8/10/2024
3.4.0 447 7/18/2024
3.3.0 145 7/16/2024
3.2.0 395 6/8/2024
3.1.0 594 5/6/2024
3.0.0 323 3/12/2024
2.0.2 250 1/25/2024
2.0.1 461 12/11/2023
2.0.0 460 11/26/2023
1.3.4 1,302 11/17/2023
1.3.3 154 11/16/2023
1.3.2 189 11/16/2023
1.3.1 215 11/12/2023
1.3.0 156 11/12/2023
1.2.1 188 11/12/2023
1.2.0 156 11/12/2023
1.1.0 148 11/4/2023
1.0.9 979 10/31/2023
1.0.8 199 10/30/2023
1.0.7 156 10/29/2023
1.0.6 201 10/21/2023
1.0.5 583 10/1/2023