Skip to main content

ConvertArea

Short summary

This function can be used to convert areas from one physical unit into another. The value will change corresponding to the conversion. Example: 1 square meter = 1E-6 square kilometer = 1550 square inch = 10.7639 square foot = 1.19599 square yard.

Return: CNM_RecipeHandling.Area: a copy of the given area converted into the desired unit

Parameters

NameTypeCommentKind
areaToConvertAreathe area that should be convertedinout
convertToAreaUnitthe physical unit it should be converted toinput
resultPrefixMetricPrefixesthe metrical prefix (for SI units)input

Code

Declaration

FUNCTION ConvertArea : Area
VAR_IN_OUT CONSTANT
(* the area that should be converted *)
areaToConvert :Area;
END_VAR
VAR_INPUT
(* the physical unit it should be converted to *)
convertTo :AreaUnit := AreaUnit.SQUARE_METER;
(* the metrical prefix (for SI units) *)
resultPrefix :MetricPrefixes := MetricPrefixes.NONE;
END_VAR
VAR
factor :LREAL;
exponent :INT := 1;
END_VAR
VAR CONSTANT
(* area convertions related to squaremeter: square inch, square foot, square yard, acre *)
AREAS : ARRAY[AreaUnit.SQUARE_METER..AreaUnit.HECTARE] OF LREAL := [
1.0, // in square meter
0.00064516, // in square inch
0.0929034, // in square foot
0.83612736, // in square yard
4046.873, // in us acre,
10000.0 // in hectare
];
(* mapping that returns the correct exponentiation factor for sqared or linear units *)
UNIT_SPECIFIC_FACTOR :ARRAY[AreaUnit.SQUARE_METER..AreaUnit.HECTARE] OF USINT := [
2, // in square meter
2, // in square inch
2, // in square foot
2, // in square yard
1, // in us acre,
1 // in hectare
];
END_VAR

Implementation

factor := AREAS[areaToConvert.unit] / AREAS[convertTo];
exponent := ((UNIT_SPECIFIC_FACTOR[areaToConvert.unit] * areaToConvert.prefix)
- ( UNIT_SPECIFIC_FACTOR[convertTo] * resultPrefix));
ConvertArea.prefix := resultPrefix;
ConvertArea.unit := convertTo;
IF CheckValueIsANumber(areaToConvert.value) THEN
ConvertArea.value := areaToConvert.value * factor * FastPow10(exponent);
ELSE
ConvertArea.value := areaToConvert.value;
END_IF