thanunits.py :  » Business-Application » ThanCad » thancad-0.0.9 » thandefs » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » ThanCad 
ThanCad » thancad 0.0.9 » thandefs » thanunits.py
##############################################################################
# ThanCad 0.0.9 "DoesSomething": 2dimensional CAD with raster support for engineers.
# 
# Copyright (c) 2001-2009 Thanasis Stamos,  August 23, 2009
# URL:     http://thancad.sourceforge.net
# e-mail:  cyberthanasis@excite.com
# 
# This program 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.
# 
# This program 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 (www.gnu.org/licenses/gpl.html).
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##############################################################################

"""\
ThanCad 0.0.9 "DoesSomething": 2dimensional CAD with raster support for engineers.

This module provides for ThanCad drawings units customisation.
"""

from math import pi

pi2 = 2*pi

class ThanUnits:
    angltran = dict(deg=180/pi, grad=200/pi, rad=1.0)

    def __init__(self):
        self.distunit = "m"                 # Unit of distance measurements
        self.distdigs = 2                   # Number of digits to display for distance values
        self.anglunit = "deg"               # Unit of angular measurements
        self.angldigs = 4                   # Number of digits to display for angular values
        self.angldire = 1                   # Anti-clockswise angles are positive
        self.anglzero = 0.0                 # Zero is at zero radians angle from the x-axis in the anticlockwise direction
  self.thanRecreate()

    def __getstate__(self):
        odict = self.__dict__.copy()
  del odict["rad2unit"], odict["unit2rad"], odict["strang"], \
      odict["strdis"]                 # Do not save functions
  return odict

    def __setstate__(self, odict):
        self.__dict__.update(odict)
  self.thanRecreate()                 # Recreate funstions

    def thanRecreate(self):
        "Recreates the transformation functions."

        self.rad2unit = lambda th, st=self.anglzero, si=self.angldire,\
                  co=self.angltran[self.anglunit] : (((th-st)*si) % pi2) * co
#        (th-st)*si * co = x => (th-st)*si = x/co => th-st = x/co / si => th = x/co / si + st =>
#   th = x/co * si + st => th = x*si/co+st
        self.unit2rad = lambda th, st=self.anglzero, si=self.angldire,\
                  co=self.angltran[self.anglunit] : (th*si/co + st) % pi2

  if self.angldigs > 0:
            self.strang = lambda th, form="%%.%df%s" % (self.angldigs, self.anglunit), rad2unit=self.rad2unit: form % rad2unit(th)
  else:
            self.strang = lambda th, form="%%d%s"    % self.anglunit,                  rad2unit=self.rad2unit: form % int(rad2unit(th))

  if self.distdigs > 0:
            self.strdis = lambda th, form="%%.%df" % self.distdigs: form % th
  else:
            self.strdis = lambda th: str(int(th))


    def strcoo(self, cc):
        "Prints the n coordinates of a point."
  t = [self.strdis(c1) for c1 in cc]
  return " ".join(t)

    def thanConfig(self, distunit=None, distdigs=None, anglunit=None, angldigs=None, angldire=None, anglzero=None):
        "Checks and sets various options."
  if distunit != None: self.distunit = distunit
        if distdigs != None: self.distdigs = int(distdigs)
        if anglunit != None:
      if anglunit not in self.angltran: raise ValueError, "Valid angle units: "+", ".join(self.angltran.keys())
      self.anglunit = anglunit
        if angldigs != None: self.angldigs = int(angldigs)
        if angldire != None:
      if angldire not in (1, -1): raise ValueError, "Angle direction should be 1 or -1."
            self.angldire = angldire
        if anglzero != None:
      if anglzero not in (3,12,9,6): raise ValueError, "Angle zero should be 3, 12, 9 or 6 (o'clock)."
      self.anglzero = ((3-anglzero)*pi/6) % pi2
        self.thanRecreate()

def test():
    "Tests the ThanOpt class."
    import sys; sys.path.append("/x/binwi/libs")
    from p_ggen import inpFloat,inpText
    op = ThanUnits()
    op.thanConfig(anglzero=12, angldire=-1)
    while True:
        th = inpFloat("Give angle in radians: ", ("",))
  if th == "": break
  for un in op.angltran:
      op.thanConfig(anglunit=un)
      th1 = op.rad2unit(th)
      print "in", un, ":", th1, op.strang(th1)

    while True:
        th = inpText("Give angle and unit: ", ("",))
  if th == "": break
  th, un = th.split()
  th = float(th)
  op.thanConfig(anglunit=un)
  th1 = op.unit2rad(th)
  print "in rad :", th1, op.strang(th1)

    a = 120.498
    print "meters:", op.strdis(120.498)
    op.thanConfig(distdigs=3)
    print "meters:", op.strdis(120.498)
    op.thanConfig(distdigs=-1)
    print "meters:", op.strdis(120.498)

if __name__ == "__main__": test()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.