ApprenticeFoundryRulesAndUnits 9.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ApprenticeFoundryRulesAndUnits --version 9.0.0
                    
NuGet\Install-Package ApprenticeFoundryRulesAndUnits -Version 9.0.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="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApprenticeFoundryRulesAndUnits" Version="9.0.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 9.0.0
                    
#r "nuget: ApprenticeFoundryRulesAndUnits, 9.0.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@9.0.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=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ApprenticeFoundryRulesAndUnits&version=9.0.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 (5)

Showing the top 5 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

ApprenticeFoundryMentorModeler

Package Description

ApprenticeFoundryWorldsAndDrawings

Unified 2D and 3D drawing and graphics library for Blazor applications

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.10.0 133 12/28/2025
10.9.0 211 12/21/2025
10.8.2 289 12/4/2025
10.8.1 142 11/29/2025
10.7.0 197 11/23/2025
10.6.0 272 11/16/2025
10.5.0 188 11/15/2025
10.4.1 340 11/12/2025
10.4.0 230 11/6/2025
10.3.0 267 11/2/2025
10.2.0 212 11/2/2025
10.1.0 236 10/30/2025
10.0.0 232 10/29/2025
9.3.0 147 10/25/2025
9.2.0 243 10/22/2025
9.1.0 273 10/14/2025
9.0.0 341 9/23/2025
8.1.0 355 9/22/2025
7.3.0 244 8/11/2025
7.2.0 334 8/10/2025
Loading failed