BillW Posted March 16, 2016 Share Posted March 16, 2016 (edited) 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 March 17, 2016 by BillW Quote Link to comment
Dieter @ DWorks Posted March 17, 2016 Share Posted March 17, 2016 (edited) 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 March 17, 2016 by Dieter @ DWorks Quote Link to comment
Recommended Posts
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.