FreeCAD / FreeCAD/FreeCAD

Expressions: rewrite unit handling so that unit prefixes will be recognized

Open
#5,955 0 comments 0 reactions 0 assignees View on GitHub
Topic: Expressions Topic: Units Type: Feature
Dominant language
C++
Stars
33.6k
Forks
6k
Avg merge
3d 17h
Merged PRs (30d)
196

Description

Issue imported from https://tracker.freecad.org/view.php?id=4037

* **Reporter:** uwestoehr
* **Date submitted:** 6/25/2019
* **FreeCAD version:** 0.18
* **Category:** Feature
* **Status:** new
* **Tags:** expressions, units

# Original report text

Currently we have the case that the expression parser knows units with prefixes, e.g. "mA". For other units like e.g. "j", it does not know any prefix.

To overcome this, the expression parser must be rewritten so that it recognizes these prefixes:
n, mu/µ, m, k, M, G
and connects them automatically to the unit that follows directly on the prefix letter.

So "ncd" will be recognized as "we have 'n' -> nano (10^-9), the next letter is 'c' which could be part of a known unit that begins with 'c', the next letter is 'd' so we know we have the unit "cd" with the prefix 'n'.
However "n A" will be recognized as "we have 'n' -> nano (10^-9), the next letter is ' ' (space), so we don't have a unit connected to the prefix letter 'n' and we issue an error.

This method would lead to
- consistent handling of all units
- support for unit prefixes for all units

# Additional information

Forum thread: https://forum.freecadweb.org/viewtopic.php?f=38&t=37164

The idea was approved as useful by berndhahnebach

# Other bug information

* **Priority:** normal
* **Severity:** feature
* **Category:** Feature
* **Updated:** 2/6/2021

# Discussion from Mantis ticket

### Comment by berndhahnebach 2019-06-26 20:20
https://en.wikipedia.org/wiki/Unit_prefix

---

### Comment by openBrain 2019-06-30 14:15
Be very careful to side effects ! I'd not want that 'min' unit being considered as "milli-inch" instead of the correct "minute". ;)

---

### Comment by uwestoehr 2019-07-01 09:07
> I'd not want that 'min' unit being considered as "milli-inch"

Yes, the procedure should be
1. read the whole given unit and if it is known like "min" then nothing is done and the unit is taken as it is
2.1 if the unit is unknown, check if it begins with a known letter of unit prefixes
2.2 if yes, read the unit beginning with the second character and if this is a known unit, use it multiplied with the factor according to the read unit prefix
3. if 1. and 2. did not lead to a known unit, deny the unit and issue an error message

---

### Comment by ezzieyguywuf 2019-11-29 13:40
I think a better approach is perhaps to hard-code a set of given prefixes to units that they are compatible with.

For example, “milli”, “kilo”, etc, are commonly accepted with metric units.

Not so much for imperial- I’ve never heard of a milli-inch

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.