matdxf.py :  » Business-Application » ThanCad » thancad-0.0.9 » p_gtecqm » 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 » p_gtecqm » matdxf.py
from itertools import islice
from math import fabs
from dxfinter import *
#from Tkinter import *

class Struct: pass

def replot(evt=None):
    "Replot the diagrams each time the user changes window dimensions."
    c.update()
    b = c.winfo_width()
    h = c.winfo_height()
    c.delete(ALL)

    axis = (g.xmin, 0), (g.xmax,0)

    scalex = b/g.sx; dy = h*0.5; scaleQ = dy/g.sQ
    xy = [ ( (x-g.xmin)*scalex, h-((y-g.Qmin)*scaleQ+dy) ) for (x,y) in g.Q]
    c.create_line(xy, fill= "orange")
    for x,y in islice(g.Q, 0, None, 20):
        xy = [ ( (x-g.xmin)*scalex, h-((y-g.Qmin)*scaleQ+dy) ) for (x,y) in ((x,0),(x,y))]
        c.create_line(xy, fill= "orange")
    xy = [ ( (x-g.xmin)*scalex, h-((y-g.Qmin)*scaleQ+dy) ) for (x,y) in axis]
    c.create_line(xy, fill= "yellow")

    scaleM = dy/g.sM
    xy = [ ( (x-g.xmin)*scalex, h-((-y-g.Mmin)*scaleM+0) ) for (x,y) in g.M]
    c.create_line(xy, fill= "orange")
    for x,y in islice(g.M, 0, None, 20):
        xy = [ ( (x-g.xmin)*scalex, h-((-y-g.Mmin)*scaleM+0) ) for (x,y) in ((x,0),(x,y))]
        c.create_line(xy, fill= "orange")
    xy = [ ( (x-g.xmin)*scalex, h-((-y-g.Mmin)*scaleM+0) ) for (x,y) in axis]
    c.create_line(xy, fill= "yellow")

#---Draw min, max Q values

    for i in xrange(1, len(g.Q)-1):
        Q1, Q2, Q3 = [Q[1] for Q in g.Q[i-1:i+2]]
        if DQ(Q2-Q1) * DQ(Q3-Q2) > 0.0: continue
        if DQ(Q2-Q1) == 0.0 == DQ(Q3-Q2): continue
        x, y = g.Q[i]
        x, y = (x-g.xmin)*scalex, h-((y-g.Qmin)*scaleQ+dy)
        print "Qmax=", Q2
        if Q2 > 0: dyp = -10
        else     : dyp =  10
        c.create_text(x, y+dyp, fill="pink", text="%.2f" % Q2)
        c.create_rectangle(x-1, y-1, x+1, y+1, fill="pink", outline="pink")

#---Draw min, max M values

    xprev = -1000.0
    for i in xrange(1, len(g.Q)-1):
        Q1, Q2, Q3 = [Q[1] for Q in g.M[i-1:i+2]]
        if DM(Q2-Q1) * DM(Q3-Q2) > 0.0: continue
        if DM(Q2-Q1) == 0.0 == DM(Q3-Q2): continue
        x, y = g.M[i]
        x, y = (x-g.xmin)*scalex, h-((-y-g.Mmin)*scaleM+0)
        if x-xprev < 10: continue
        xprev = x
        print "Mmax=", Q2
        if -Q2 > 0: dyp = -10
        else      : dyp =  10
        c.create_text(x, y+dyp, fill="pink", text="%.2f" % Q2)
        c.create_rectangle(x-1, y-1, x+1, y+1, fill="pink", outline="pink")

        x1 = x = g.M[i][0]; y = 0.0
        x, y = (x-g.xmin)*scalex, h-((-y-g.Mmin)*scaleM+0)
#        if x-xprev < 10: continue
#        xprev = x
        c.create_text(x, y-dyp, fill="green", text="%.2f" % x1)
        c.create_rectangle(x-1, y-1, x+1, y+1, fill="pink", outline="green")

def DQ(dq):
    if fabs(dq) < g.Qmax*1e-6: return 0.0
    return dq
def DM(dq):
    if fabs(dq) < g.Mmax*1e-6: return 0.0
    return dq


def diag(Q, M, tit):
    "Initialise diagram."
    global c, g
    g = Struct()
    xmin = Q[0][0]; xmax = Q[-1][0]
    Qmax = max([fabs(y) for (x,y) in Q]); Qmin = -Qmax
    Mmax = max([fabs(y) for (x,y) in M]); Mmin = -Mmax
    g.sx, g.xmin, g.xmax = margins(xmin, xmax)
    g.sQ, g.Qmin, g.Qmax = margins(Qmin, Qmax)
    g.sM, g.Mmin, g.Mmax = margins(Mmin, Mmax)
    g.Q, g.M = Q, M

    root = Tk()
    c = Canvas(root, background= "black", width=400, height=600)
    c.grid(sticky="wesn")
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    replot()
    c.bind("<Configure>", replot)
    root.title(tit)
    root.mainloop()

def margins(xmin, xmax):
    "Extend xmin, xmax so that we have some margins."
    dx = (xmax - xmin)*0.2
    if dx == 0.0: dx = 1.0
    return xmax-xmin+dx, xmin-dx*0.5, xmax+dx*0.5
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.