thancomtool.py :  » Business-Application » ThanCad » thancad-0.0.9 » thancom » 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 » thancom » thancomtool.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.

Package which processes commands entered by the user.
This module provides for the commands of the tool menu.
"""

from math import atan2,hypot
import p_ggen
from p_ggeom import area
from p_gtri import hull
import thandr, thantkdia, thanlayer
from thanvar import Canc,lineSimplify
from thanopt import thancadconf
from thantrans import T



def thanToolSimplif(proj):
    "Simplifies a line."
    from selutil import thanSel1line
    from thancommod import thanModEnd,thanModCanc
    lin1 = thanSel1line(proj, T["Select the line to simplify:\n"], strict=False)
    if lin1 == Canc: return thanModCanc(proj)    # Simplification cancelled
#    cp = lineSimplify(lin1.cp)
    cp = lineSimplify(lin1.cp, ermeanmax=0.15*10, erabsmax=0.20*10, zerabsmax=0.10*10)
    elem = thandr.ThanLine()
    elem.thanSet(cp)
    proj[1].thanElementAdd(elem)
    elem.thanTkDraw(proj[2].than)
    thanModEnd(proj)


def thanToolHull(proj):
    "Finds the convex hull of a line."
    from selutil import thanSelMultlines
    from thancommod import thanModEnd,thanModCanc
    lins = thanSelMultlines(proj, 2, T["Select lines to find their convex hull:\n"])
    if lins == Canc: return thanModCanc(proj)    # Hull cancelled
    cp = []
    for lin1 in lins: cp.extend(lin1.cp)
    u = hull(cp)
    if u == None: return thanModCanc(proj, T["Degenerate convex hull."])
    elem = thandr.ThanLine()
    elem.thanSet(u)
    proj[1].thanElementAdd(elem)
    elem.thanTkDraw(proj[2].than)
    thanModEnd(proj)


def thanToolCen(proj):
    "Finds the centroid lines."
    from selutil import thanSelMultlines
    from thancommod import thanModEnd,thanModCanc
    lins = thanSelMultlines(proj, 2, T["Select lines to find their centroid:\n"])
    if lins == Canc: return thanModCanc(proj)    # centroid cancelled
    n = proj[1].thanVar["dimensionality"]
    xr = xrange(n)
    xr3 = xrange(2, n)
    iterby2 = p_ggen.iterby2
    sum = [0.0]*n
    s =  0.0
    for lin1 in lins:
        for a,b in iterby2(lin1.cp):
            cc = [(a[i]+b[i])*0.5 for i in xr]
            dd = hypot(b[0]-a[0], b[1]-a[1])
            for i in xr3: dd += hypot(dd, b[i]-a[i])
            for i in xr: sum[i] += cc[i]*dd
            s += dd
    if s == 0.0: return thanModEnd(proj, T["No centroid for zero length lines."])
    for i in xr: sum[i] /= s
    elem = thandr.ThanPoint()
    elem.thanSet(sum)
    proj[1].thanElementAdd(elem)
    elem.thanTkDraw(proj[2].than)
    thanModEnd(proj)


def thanToolDist(proj):
    "Computes the distance between 2 points shown by the user."
    c1 = proj[2].thanGudGetPoint(T["First point: "])
    if c1 == Canc: return proj[2].thanGudCommandCan()    # Distance cancelled
    c2 = proj[2].thanGudGetLine(c1, T["Next point: "])
    if c2 == Canc: return proj[2].thanGudCommandCan()    # Distance cancelled
    dx = c2[0]-c1[0]
    dy = c2[1]-c1[1]
    strdis = proj[1].thanUnits.strdis
    strang = proj[1].thanUnits.strang
    mes = "Dx=%s   Dy=%s   theta=%s\nD=%s" % (strdis(dx), strdis(dy),
        strang(atan2(dy, dx)), strdis(hypot(dx,dy)))
    proj[2].thanGudCommandEnd(mes, "info")


def thanToolArea(proj):
    "Computes the area of a polygon defined by the user."
    from thancomdraw import getpol
    cs = getpol(proj)
    if cs == Canc: return proj[2].thanGudCommandCan()    # Area cancelled
    a = area(cs)
    strdis = proj[1].thanUnits.strdis
    mes = "Area=%s" % strdis(a)
    proj[2].thanGudCommandEnd(mes, "info")


def thanToolAngle(proj):
    "Computes the angle among 3 points; the second points is the top of the angle."
    than = proj[2].than
    g2l = than.ct.global2Local

    cc = proj[2].thanGudGetPoint(T["First point (corner): "])
    if cc == Canc: return proj[2].thanGudCommandCan()      # Angle cancelled

    c1 = proj[2].thanGudGetLine(cc, T["First side: "])
    if c1 == Canc: return proj[2].thanGudCommandCan()      # Angle cancelled
    theta1 = atan2(c1[1]-cc[1], c1[0]-cc[0])
    r = hypot(c1[1]-cc[1], c1[0]-cc[0]) * 0.5
    than.dc.create_line(g2l(cc[0], cc[1]), g2l(c1[0], c1[1]), fill="blue", tags=("e0",))

    theta2 = proj[2].thanGudGetArc(cc, r, theta1, T["Second side: "])
    if theta2 == Canc:
        than.dc.delete("e0")
        return proj[2].thanGudCommandCan()  # Angle cancelled

    strang = proj[1].thanUnits.strang
    mes = "theta=%s" % strang(theta2-theta1)
    than.dc.delete("e0")
    proj[2].thanGudCommandEnd(mes, "info")


def thanToolOsnap(proj):
    "Displays a dialog for the drafting settings."
    d = thantkdia.ThanTkOsnap(proj[2], thancadconf.thanOsnapModes, proj[2].BOSN, title="Drafting Settings")
    if d.result == None:
        proj[2].thanGudCommandCan()
    else:
        thancadconf.thanOsnapModes.clear()
        thancadconf.thanOsnapModes.update(d.result)
        proj[2].thanGudCommandEnd()


def thanToolId(proj):
    "Shows the coordinates of a point chosen by the user."
    c1 = proj[2].thanGudGetPoint(T["Specify point: "])
    if c1 == Canc: return proj[2].thanGudCommandCan()           # Id command was cancelled
    strcoo = proj[1].thanUnits.strcoo
    proj[2].thanGudCommandEnd("%s: %s" % (T["World xyz"], strcoo(c1)), "info")


def thanToolTextfind(proj):
    "Searches for text."
    st = proj[2].thanGudGetText(T["Enter text to search for: "])
    if st == Canc: return proj[2].thanGudCommandCan()    # find was cancelled
    elems = []
    i = 0
    it = __gettext(proj, st)
    while True:
        i, stat = __getnext(proj, elems, i, st, it)           # Next ThanText to show
  if i == -1: return                                    # No elements found
  res = __getTextfindOpts(proj, elems[i], i, stat)
        if res == Canc: return proj[2].thanGudCommandCan()    # find was cancelled
  if res == "n": i+=1; continue
  if res == "p": i-=1; continue
  x, y, s = elems[i].c1[0], elems[i].c1[1], elems[i].size*10
  proj[2].thanGudZoomWin((x-s, y-s, x+s, y+s))
        proj[2].thanAutoRegen(regenImages=True)
  if res == "": return proj[2].thanGudCommandEnd()

def __getnext(proj, elems, i, st, it):
    "Decide what ThanText to show to the user."
    strcoo = proj[1].thanUnits.strcoo
    if i < len(elems):               # Just return saved ThanText element
        stat = "'%s' at %s\n" % (st, strcoo(elems[i].c1))
  return i, stat
    for elem in it:                  # Try to locate another ThanText Element:
  elems.append(elem)           # ..Element found
  i = len(elems) - 1
        stat = "'%s' at %s\n" % (st, strcoo(elems[i].c1))
  return i, stat
    if elems:                        # ..No more elements found
        stat = "'%s': %s.\n" % (st, T["No more occurrences were found"])
  i -= 1
  return i, stat
    else:                            # ..No more elements; in fact no elements have been found
        stat = "'%s': %s." % (st, T["not found"])
        proj[2].thanGudCommandEnd(stat, "can")
  return -1, None

def __gettext(proj, st):
    "Text element in active layers; gnerator."
    dilay = proj[1].thanLayerTree.dilay
    TT = thandr.ThanText
    for lay in dilay.itervalues():
        for elem in lay.thanQuad:
      if isinstance(elem, TT) and st in elem.text: yield elem

def __getTextfindOpts(proj, elem, i, statonce):
    "Gets text find options."
    if i > 0:
        stat1 = T["Press enter for zoom and finish (zoomto/next/previous) <enter>: "]
    else:
        stat1 = T["Press enter for zoom and finish (zoomto/next) <enter>: "]
    stat = statonce+stat1
    while True:
        res = proj[2].thanGudGetText(stat)
        if res == Canc: return Canc                       # find text was cancelled
        res = res.strip().lower()
        if res == "": return res
        n = len(res)
        if res == "zoomto"[:n]: return "z"
        if res == "next"[:n]:   return "n"
        if i > 0 and res == "previous"[:n]: return "p"
        stat = "Invalid option. Try again.\n" + stat1
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.