Skip to content

Custom units

MadsKirkFoged edited this page Jan 22, 2024 · 1 revision

Your options for creating units not already in EngineeringUnits:

  1. Create a new Issue and ask for your new unit to be added.
  2. Create a Pull request where you already have added the units yourself
  3. Add the units in your own project

Adding a new unit into EngineeringUnits

Fork a new version of EngineeringUnits and download a clone of it. Go to the CodeGen project

image

Insert the name of your new unit into GetListOfCombinedUnits():

image

Run the CodeGen Project. Your new unit has been added to the folder EngineeringUnits-> CombinedUnits -> 'NameOfYourUnit'

image

In the newly created folder open the file named YourUnitEnum.cs

Add the quantities of your unit. Here you can get inspiration from other units already added.

example the EnergyCostUnit:

public static readonly EnergyCostUnit SI = new(CostUnit.SI, EnergyUnit.SI);
public static readonly EnergyCostUnit DollarPerJoule = new(CostUnit.USDollar, EnergyUnit.Joule);
public static readonly EnergyCostUnit DollarPerKilojoule = new(CostUnit.USDollar, EnergyUnit.Kilojoule);
public static readonly EnergyCostUnit DollarPerMegajoule = new(CostUnit.USDollar, EnergyUnit.Megajoule);
public static readonly EnergyCostUnit DollarPerGigajoule = new(CostUnit.USDollar, EnergyUnit.Gigajoule);
public static readonly EnergyCostUnit DollarPerKilowattHour = new(CostUnit.USDollar, EnergyUnit.KilowattHour);
public static readonly EnergyCostUnit DollarPerMegawattHour = new(CostUnit.USDollar, EnergyUnit.MegawattHour);

public EnergyCostUnit(CostUnit cost, EnergyUnit energy)
{
    var localUnit = cost / energy;
    var localSymbol = $"{cost}/{energy}";

    Unit = new UnitSystem(localUnit, localSymbol);
}

It can be created by combining EnergyUnit and CostUnit Now you have to work out how to combine other units into your new unit.

If you would like the unit to be a part of the official EngineeringUnits nuget package, you can create a pull request. If you just want to have your unit as part of your own project, you can copy the newly created folder into your own project.

Adding more quantity to a unit in your own project

Exemple: looking to extent LengthCost.

Locate the folder EngineeringUnits-> CombinedUnits -> LengthCost and open LengthCostEnum.cs

image

Look at the structure of the available constructors. In this case:

image

In your own project create a static class with a static property:

image

Call your new unit like this:

image

Clone this wiki locally