/usr/share/pythoncad/PythonCAD/Generic/units.py is in pythoncad 0.1.37.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | #
# Copyright (c) 2002, 2003, 2004, 2006 Art Haas
#
# This file is part of PythonCAD.
#
# PythonCAD is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# available units
#
import types
import util
MILLIMETERS = 0
MICROMETERS = 1
METERS = 2
KILOMETERS = 3
INCHES = 4
FEET = 5
YARDS = 6
MILES = 7
def get_all_units():
unitlist = []
unitlist.append(_('Millimeters'))
unitlist.append(_('Micrometers'))
unitlist.append(_('Meters'))
unitlist.append(_('Kilometers'))
unitlist.append(_('Inches'))
unitlist.append(_('Feet'))
unitlist.append(_('Yards'))
unitlist.append(_('Miles'))
return unitlist
class Unit(object):
"""A class defining a unit for linear dimensions.
The Unit class is used to establish what unit to
assign distances between points. The class contains
two methods used to scale distance value to and from
an equivalent distance in millimeters.
A Unit instance has the following methods:
getStringUnit(): Get the instance unit string as a string value.
setStringUnit(): Set the instance unit value based on a string
{set/set}Unit(): Get/Set the instance unit.
toMillimeters(): Convert a distance to millimeters
fromMillimeters(): Convert a distance from millimeters
The Unit class has the following classmethods:
getUnitAsString(): Return a text string for a unit value.
getUnitFromString(): Return a unit value for a given text string.
getUnitStrings(): Return the available unit options as strings.
getUnitValues(): Return the availbable unit values.
"""
MILLIMETERS = 0
MICROMETERS = 1
METERS = 2
KILOMETERS = 3
INCHES = 4
FEET = 5
YARDS = 6
MILES = 7
def __init__(self, unit=None):
_unit = unit
if _unit is None:
_unit = Unit.MILLIMETERS
if (_unit != Unit.MILLIMETERS and
_unit != Unit.MICROMETERS and
_unit != Unit.METERS and
_unit != Unit.KILOMETERS and
_unit != Unit.INCHES and
_unit != Unit.FEET and
_unit != Unit.YARDS and
_unit != Unit.MILES):
raise ValueError, "Invalid unit choice: " + str(unit)
self.__unit = _unit
def getUnitAsString(cls, u):
"""Return a text string for the unit value.
getUnitAsString(u)
This classmethod returns 'millimeters', 'micrometers', 'meters',
'kilometers', 'inches', 'feet', 'yards', or 'miles'. Passing
an invalid unit value will raise a ValueError exception.
"""
if not isinstance(u, int):
raise TypeError, "Invalid argument type: " + `type(u)`
if u == Unit.MILLIMETERS:
_str = 'millimeters'
elif u == Unit.MICROMETERS:
_str = 'micrometers'
elif u == Unit.METERS:
_str = 'meters'
elif u == Unit.KILOMETERS:
_str = 'kilometers'
elif u == Unit.INCHES:
_str = 'inches'
elif u == Unit.FEET:
_str = 'feet'
elif u == Unit.YARDS:
_str = 'yards'
elif u == Unit.MILES:
_str = 'miles'
else:
raise ValueError, "Unexpected unit value: %d" % u
return _str
getUnitAsString = classmethod(getUnitAsString)
def getUnitFromString(cls, s):
"""Return a unit value for a given text string.
getUnitFromString(s)
This classmethod returns a value based on the string argument:
'millimeters' -> Unit.MILLIMETERS
'micrometers' -> Unit.MICROMETERS
'meters' -> Unit.METERS
'kilometers' -> Unit.KILOMETERS
'inches' -> Unit.INCHES
'feet' -> Unit.FEET
'yards' -> 'Unit.YARDS
'miles' -> Unit.MILES
If the string is not listed above a ValueError execption is raised.
"""
if not isinstance(s, str):
raise TypeError, "Invalid argument type: " + `type(s)`
_ls = s.lower()
if _ls == 'millimeters':
_u = Unit.MILLIMETERS
elif _ls == 'micrometers':
_u = Unit.MICROMETERS
elif _ls == 'meters':
_u = Unit.METERS
elif _ls == 'kilometers':
_u = Unit.KILOMETERS
elif _ls == 'inches':
_u = Unit.INCHES
elif _ls == 'feet':
_u = Unit.FEET
elif _ls == 'yards':
_u = Unit.YARDS
elif _ls == 'miles':
_u = Unit.MILES
else:
raise ValueError, "Unexpected unit string: " + s
return _u
getUnitFromString = classmethod(getUnitFromString)
def getUnitStrings(cls):
"""Return the available unit values as strings.
getUnitStrings()
This classmethod returns a list of strings.
"""
return [_('Millimeters'),
_('Micrometers'),
_('Meters'),
_('Kilometers'),
_('Inches'),
_('Feet'),
_('Yards'),
_('Miles')
]
getUnitStrings = classmethod(getUnitStrings)
def getUnitValues(cls):
"""Return the available unit values.
getUnitValues()
This classmethod returns a list of unit values.
"""
return [Unit.MILLIMETERS,
Unit.MICROMETERS,
Unit.METERS,
Unit.KILOMETERS,
Unit.INCHES,
Unit.FEET,
Unit.YARDS,
Unit.MILES
]
getUnitValues = classmethod(getUnitValues)
def getUnit(self):
return self.__unit
def getStringUnit(self):
_u = self.__unit
if _u == Unit.MILLIMETERS:
_str = 'millimeters'
elif _u == Unit.MICROMETERS:
_str = 'micrometers'
elif _u == Unit.METERS:
_str = 'meters'
elif _u == Unit.KILOMETERS:
_str = 'kilometers'
elif _u == Unit.INCHES:
_str = 'inches'
elif _u == Unit.FEET:
_str = 'feet'
elif _u == Unit.YARDS:
_str = 'yards'
elif _u == Unit.MILES:
_str = 'miles'
else:
raise ValueError, "Unexpected unit: %d" % _u
return _str
def setStringUnit(self, unit):
if not isinstance(unit, types.StringTypes):
raise TypeError, "Unexpected unit string: " + str(unit)
_ul = str(unit.lower())
if _ul == 'millimeters':
_unit = MILLIMETERS
elif _ul == 'micrometers':
_unit = MICROMETERS
elif _ul == 'meters':
_unit = METERS
elif _ul == 'kilometers':
_unit = KILOMETERS
elif _ul == 'inches':
_unit = INCHES
elif _ul == 'feet':
_unit = FEET
elif _ul == 'yards':
_unit = YARDS
elif _ul == 'miles':
_unit = MILES
else:
raise ValueError, "Unexpected unit string: %s" % unit
self.__unit = _unit
def setUnit(self, unit):
if (unit != Unit.MILLIMETERS and
unit != Unit.MICROMETERS and
unit != Unit.METERS and
unit != Unit.KILOMETERS and
unit != Unit.INCHES and
unit != Unit.FEET and
unit != Unit.YARDS and
unit != Unit.MILES):
raise ValueError, "Invalid unit choice: " + str(unit)
self.__unit = unit
unit = property(getUnit, setUnit, None, "Basic unit.")
def toMillimeters(self, value):
"""Scale a value to the equivalent distance in millimeters.
toMillimeters(value)
"""
_v = util.get_float(value)
if self.__unit == Unit.MILLIMETERS:
_sv = _v
elif self.__unit == Unit.MICROMETERS:
_sv = _v * 1e-3
elif self.__unit == Unit.METERS:
_sv = _v * 1e3
elif self.__unit == Unit.KILOMETERS:
_sv = _v * 1e6
elif self.__unit == Unit.INCHES:
_sv = _v * 25.4
elif self.__unit == Unit.FEET:
_sv = _v * 304.8
elif self.__unit == Unit.YARDS:
_sv = _v * 914.4
elif self.__unit == Unit.MILES:
_sv = _v * 1609344.4
else:
raise ValueError, "Undefined unit value! " + str(self.__unit)
return _sv
def fromMillimeters(self, value):
"""Scale a value from an equivalent distance in millimeters.
fromMillimeters(value)
"""
_v = util.get_float(value)
if self.__unit == Unit.MILLIMETERS:
_sv = _v
elif self.__unit == Unit.MICROMETERS:
_sv = _v * 1e3
elif self.__unit == Unit.METERS:
_sv = _v * 1e-3
elif self.__unit == Unit.KILOMETERS:
_sv = _v * 1e-6
elif self.__unit == Unit.INCHES:
_sv = _v / 25.4
elif self.__unit == Unit.FEET:
_sv = _v / 304.8
elif self.__unit == Unit.YARDS:
_sv = _v / 914.4
elif self.__unit == Unit.MILES:
_sv = _v / 1609344.4
else:
raise ValueError, "Undefined unit value! " + str(self.__unit)
return _sv
def unit_string(value):
"""Return a text string for the integer unit value.
unit_string(value)
"""
if value == MILLIMETERS:
_str = 'millimeters'
elif value == MICROMETERS:
_str = 'micrometers'
elif value == METERS:
_str = 'meters'
elif value == KILOMETERS:
_str = 'kilometers'
elif value == INCHES:
_str = 'inches'
elif value == FEET:
_str = 'feet'
elif value == YARDS:
_str = 'yards'
elif value == MILES:
_str = 'miles'
else:
raise ValueError, "Unexpected unit: " + str(value)
return _str
|