FreeCADCmd is exited on quaitity without unit

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

FreeCADCmd is exited on quaitity without unit

Post by bernd »

Code: Select all

from FreeCAD import Units
myquantity = Units.Quantity('500')
Units.Unit(myquantity)
I know it is wrong but FreeCADCmd exits !

Code: Select all

hugo@Ahorn:~/build$ ./bin/FreeCADCmd 
FreeCAD 0.19, Libs: 0.19R21276 (Git)
(c) Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-2020
FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.
FreeCAD wouldn't be possible without FreeCAD community.
  #####                 ####  ###   ####  
  #                    #      # #   #   # 
  #     ##  #### ####  #     #   #  #   # 
  ####  # # #  # #  #  #     #####  #   # 
  #     #   #### ####  #    #     # #   # 
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

[FreeCAD Console mode <Use Ctrl-D (i.e. EOF) to exit.>]
>>> 
>>> from FreeCAD import Units
myquantity = Units.Quantity('500')
Units.Unit(myquantity)>>> >>> 
<Exception> Unit underflow in unit
hugo@Ahorn:~/build$ 
Is this correct bahaviour ?


Code: Select all

OS: Debian GNU/Linux 10 (buster) (KDE//usr/share/xsessions/plasma)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21276 (Git)
Build type: Unknown
Branch: femtmp
Hash: bed6f9f61cbfbb330b8ea6f68439bb267da53b40
Python version: 3.7.3
Qt version: 5.11.3
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Switzerland (de_CH)
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCADCmd is exited on quaitity without unit

Post by wmayer »

User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCADCmd is exited on quaitity without unit

Post by bernd »

great :D ... found another strange one ...

Code: Select all

from FreeCAD import Units
Units.Unit(Units.Quantity('500'))
Units.Unit(Units.Quantity('5'))
Units.Unit(Units.Quantity('-5'))

Code: Select all

>>> 
>>> from FreeCAD import Units
>>> 
>>> Units.Unit(Units.Quantity('500'))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
OverflowError: Unit underflow in unit
>>> 
>>> Units.Unit(Units.Quantity('5'))
Unit: mm^5 (5,0,0,0,0,0,0,0)
>>> 
>>> Units.Unit(Units.Quantity('-5'))
Unit: 1/mm^5 (-5,0,0,0,0,0,0,0)
>>> 
>>> 
I would expect the first one for all number without unit, but this does not apply for floats from -7.9999 to 7.9999
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCADCmd is exited on quaitity without unit

Post by wmayer »

bernd wrote: Wed May 27, 2020 6:09 am found another strange one ...
Sorry, but what exactly is strange?
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCADCmd is exited on quaitity without unit

Post by bernd »

from Python Base.Unit docsting

Code: Select all

Unit
defines a unit type, calculate and compare.
The following constructors are supported:
Unit()                        -- empty constructor
Unit(i1,i2,i3,i4,i5,i6,i7,i8) -- unit signature
Unit(Quantity)                -- copy unit from Quantity
Unit(Unit)                    -- copy constructor
Unit(string)                  -- parse the string for units
I only tested Unit(Quantity) and Unit(string)

Code: Select all

q1 = Units.Quantity('500 MPa')
q1.Unit
Units.Unit(q1)

Code: Select all

>>> 
>>> from FreeCAD import Units
>>> 
>>> q1 = Units.Quantity('500 MPa')
>>> q1.Unit
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> Units.Unit(q1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
OverflowError: Unit overflow in unit
>>> 
IMHO the last line should return a Base.Unit.Pressure


Code: Select all

q2 = Units.Quantity('500')
q2.Unit
Units.Unit(q2)

Code: Select all

>>> 
>>> q2 = Units.Quantity('500')
>>> q2.Unit
Unit:  (0,0,0,0,0,0,0,0)
>>> Units.Unit(q2)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
OverflowError: Unit underflow in unit
>>> 
IMO the last line should return a empty Base.Unit (like second line did)

Code: Select all

q3 = Units.Quantity('5')
q3.Unit
Units.Unit(q3)

Code: Select all

>>> 
>>> q3 = Units.Quantity('5')
>>> q3.Unit
Unit:  (0,0,0,0,0,0,0,0)
>>> Units.Unit(q3)
Unit: mm^5 (5,0,0,0,0,0,0,0)
>>> 
>>> 
IMO the last line should return a empty Base.Unit (like second line did). Why does numbers from -7.999 to 7.999 return legth unit with the order of the first caracter of the provided string? 7 instead of 5 returns m^7, see next example ...

Code: Select all

q4 = Units.Quantity('7')
q4.Unit
Units.Unit(q4)

Code: Select all

>>> 
>>> q4 = Units.Quantity('7')
>>> q4.Unit
Unit:  (0,0,0,0,0,0,0,0)
>>> Units.Unit(q4)
Unit: mm^7 (7,0,0,0,0,0,0,0)
>>> 
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCADCmd is exited on quaitity without unit

Post by bernd »

all together for simple copy paste ...

Code: Select all

from FreeCAD import Units

q1 = Units.Quantity('500 MPa')
q1.Unit
Units.Unit(q1)

q2 = Units.Quantity('500')
q2.Unit
Units.Unit(q2)

q3 = Units.Quantity('5')
q3.Unit
Units.Unit(q3)

q4 = Units.Quantity('7')
q4.Unit
Units.Unit(q4)
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCADCmd is exited on quaitity without unit

Post by wmayer »

git commit 24bb210a0

The problem was the order of checked types in UnitPy::PyInit. Since the QuantityPy supports the number protocol this line

Code: Select all

    if (PyArg_ParseTuple(args, "|iiiiiiii", &i1,&i2,&i3,&i4,&i5,&i6,&i7,&i8)) {
handled the first identifier i1 which got its value from the quantity. That's why the length dimension always was so high.
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCADCmd is exited on quaitity without unit

Post by bernd »

:D even understood what was going wrong ...

Code: Select all

>>> from FreeCAD import Units
>>> 
>>> q1 = Units.Quantity('500 MPa')
>>> q1.Unit
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> Units.Unit(q1)
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> 
>>> q2 = Units.Quantity('500')
>>> q2.Unit
Unit:  (0,0,0,0,0,0,0,0)
>>> Units.Unit(q2)
Unit:  (0,0,0,0,0,0,0,0)
>>> 
>>> q3 = Units.Quantity('5')
>>> q3.Unit
Unit:  (0,0,0,0,0,0,0,0)
>>> Units.Unit(q3)
Unit:  (0,0,0,0,0,0,0,0)
>>> 
>>> q4 = Units.Quantity('7')
>>> q4.Unit
Unit:  (0,0,0,0,0,0,0,0)
>>> Units.Unit(q4)
Unit:  (0,0,0,0,0,0,0,0)
>>> 
Post Reply