##  fifDim.py Version 1.01 (module macrolib.fifDim)

##  Copyright (c) 2006 Bruce Vaughan, BV Detailing & Design, Inc.

##  NOT FOR SALE. The software is provided "as is" without any warranty.

############################################################################

"""

Functions:

round_off_point(pt, [dec_places])

Return a 'point' object where 'pt.x', 'pt.y', and 'pt.z' are rounded off to

the nearest 'dec_places'. Argument 'dec_places' defaults to 8.

round_off(number, [denom])

Return a float where 'number' is rounded off to the nearest 'denom'.

'denom' defaults to 16.

simplify(n, d)

Return the numerator (n) and denominator (d) of a fraction as a tuple of

integers into its simplest form or lowest terms.

fifDim(d, [a])

Convert a float or integer to string in feet-inches-fraction format using

"'-" as separator.

'd' is the number to be converted.

'a' is the accuracy (8ths, 16ths, 32nds, etc.) and should be an integer. It

defaults to 16 (feet'-inches 16ths).

dim__(s)

Wrapper function for built-in float() to convert xx'-xx xx/xx formatted

string to float.

Works similar to SDS/2 param.dim.

Given a string 's' formatted per fifDim(), returns an equivalent value float.

Version History:

Version 1.01 (02/01/07) -   Adjust function dim__() to allow for ['-] or ['] as

indicator for feet in FI16ths format.

"""

from math import floor, modf

def round_off_point(p, q=8):

return Point(round(p.x, q), round(p.y, q), round(p.z, q))

def round_off(n, r = 16):

return round(float(n) * r) / r

def simplify(n, d):

n, d = map(int, [round(n, 2), d])

if n:

a, b = n, d

while a:

a, b = b%a, a

return n/b, d/b

else:

return n, d

def fifDim(d, a=16):

if d < 0:

sign = '-'

else:

sign = ''

d = abs(float(d)) # if interger, convert to float

rem = round((d % 12.0) * a) / a

feet = int(floor(d / 12.0))

frac, inch = modf(rem)

num, den = simplify(frac*a, a)

if num > 0 and feet > 0:

return "%s%s'-%s %s/%s" % (sign, feet, int(inch), num, den)

elif num > 0 and inch > 0:

return "%s%s %s/%s" % (sign, int(inch), num, den)

elif feet > 0 and num == 0:

return "%s%s'-%s" % (sign, feet, int(inch))

elif inch >= 0 and num == 0:

return "%s%s" % (sign, int(inch))

else:

return "%s%s/%s" % (sign, num, den)

def dim__(s):

try:

if s.startswith('-'):

sign = -1

else:

sign = 1

if "'" in s:

ft, inch = s.split("'")

in_frac = inch.lstrip('-').split()

if len(in_frac) > 1:

n,d = in_frac[1].split("/")

return (abs(float(ft)*12.0) + float(in_frac[0]) + float(n)/float(d)) * sign

else:

return (abs(float(ft)*12.0) + float(inch)) * sign

else:

in_frac = s.split()

if len(in_frac) > 1:

n,d = in_frac[1].split("/")

return (abs(float(in_frac[0])) + float(n)/float(d)) * sign

elif '/' in s:

n,d = s.split("/")

return (abs(float(n)/float(d))) * sign

else:

return (abs(float(s))) * sign

return None

except:

raise TypeError, "The argument string was incorrectly formatted"

def test_script():

print "Test function round_off():"

print round_off(45, 16)

print round_off(45.7845, 16)

print round_off(45.8457, 8)

print round_off(45.7845, 4)

print round_off(45.7245, 2)

print round_off(45.4999, 1)

print "\nTest function fifDim():"

print fifDim(4778.7463, 2)

print fifDim(4.56, 16)

print fifDim(0.43, 16)

print fifDim(47757.3256, 16)

print fifDim(123.4556, 32)

print fifDim(4.15648, 64)

print fifDim(5667, 32)

print fifDim(12.8258, 8)

print fifDim(-4778.7463, 2)

print fifDim(-4.56, 16)

print fifDim(-0.43, 16)

print fifDim(-47757.3256, 16)

print fifDim(-123.4556, 32)

print fifDim(-4.15648, 64)

print fifDim(-5667, 32)

print fifDim(-12.8258, 8)

print fifDim(4778.7463, 2)

print fifDim(4.56, 16)

print fifDim(0.43, 16)

print fifDim(47757.3256, 16)

print fifDim(123.4556, 64)

print fifDim(4.15648, 128)

print fifDim(5667, 64)

print fifDim(12.8258, 64)

print fifDim(-4778.7463, 68)

print fifDim(-4.56, 24)

print fifDim(-0.43, 23)

print fifDim(-47757.3256, 32)

print fifDim(-123.4556, 64)

print fifDim(-4.15648, 256)

print fifDim(-5667, 64)

print fifDim(-12.8258, 16)

print "\nTest function dim__():"

print dim__(fifDim(-123.4556, 32))

print dim__(fifDim(-4.15648, 64))

print dim__(fifDim(-5667, 32))

print dim__(fifDim(-12.8258, 8))

print dim__(fifDim(4778.7463, 2))

print dim__(fifDim(4.56, 16))

print dim__(fifDim(0.43, 16))

print dim__(fifDim(47757.3256, 16))

print dim__("-14456'-0")

print dim__("-14'-0 1/32")

print dim__("0'-0 1/4")

print dim__("-0'-0 1/4")

print dim__("256/257")

print dim__("14'-4 1/4")

print dim__("12'4 1/4")

if __name__ == '__main__':

test_script()

"""

>>> Test function round_off():

45.0

45.8125

45.875

45.75

45.5

45.0

Test function fifDim():

398'-2 1/2

4 9/16

7/16

3979'-9 5/16

10'-3 15/32

4 5/32

472'-3

1'-0 7/8

-398'-2 1/2

-4 9/16

-7/16

-3979'-9 5/16

-10'-3 15/32

-4 5/32

-472'-3

-1'-0 7/8

398'-2 1/2

4 9/16

7/16

3979'-9 5/16

10'-3 29/64

4 5/32

472'-3

1'-0 53/64

-398'-2 3/4

-4 13/24

-10/23

-3979'-9 5/16

-10'-3 29/64

-4 5/32

-472'-3

-1'-0 13/16

Test function dim__():

-123.46875

-4.15625

-5661.0

-12.875

4778.5

4.5625

0.4375

47757.3125

-173472.0

-168.03125

0.25

-0.25

0.996108949416

172.25

148.25

>>>

"""