Jump to content

Unit qualified numbers


Recommended Posts

In Vectorscript I have shape drawing functions that have a unit embedded so they work in any current units ie 10.7mm

This doesn't work in Python. I have two examples that work - see attached files "convertnumbers 1" and "convertnumbers 2"

What I want to do with "convertnumbers 2" is overload the standard Python number or float so I can type for example 10.7.mm or (10.7).mm if the double period causes problems.

Any thoughts

Thanks

PS Couldn't find a way for an inline format 10.7.mm but have found a way with format vwu.mm(10.7) see attached file "convertnumbers 3"

Edited by BillW
Link to comment

This is already in dlibrary, a helper library for python plug-ins: https://bitbucket.org/dieterdworks/vw-dlibrary/src/42443e6a43ea0c54b49d17e71a9b1071a6a0e908/dlibrary/document.py?at=master&fileviewer=file-view-default

check out the Units class from the document module. It's full with helper methods for this kind of thing.

Also, in dlibrary, many other functions will let you enter both a number or a string with the number and unit for their parameters, which is super helpful.

Everyone is free to contribute to it.

Especially this function:

@staticmethod
def resolve_length_units(dimension):
   """Will return the same, but length unit strings will be transformed to floats.
   For a float or str dimension, this will return a float.
   For a tuple, which would be a point, this will return a tuple of floats.

   :type dimension: float | str | tuple
   :rtype: float | tuple
   """
   if isinstance(dimension, str):
       return Units.__validate_length_str_to_length_units(dimension)
   elif isinstance(dimension, tuple):
       return tuple((Units.__validate_length_str_to_length_units(item) if isinstance(item, str) else item)
                    for item in dimension)
   else:
       return dimension

Edited by Dieter @ DWorks
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...