##############################################################################
# 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 processes draw commands.
"""
from math import cos,sin
from p_gmath import thanNear2
import thandr
from thanvar import Canc
from thantrans import T
from thancomsel import thanSelect1
from selutil import thanSel1line
from thancommod import thanModCanc,thanModEnd
def thanTkDrawElem(proj, elemClass):
"Draws a element with the help of a GUI and stores it to database."
elem = elemClass()
if elem.thanTkGet(proj) == Canc: return proj[2].thanGudCommandCan()
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
proj[2].thanGudCommandEnd()
def thanTkDrawRect(proj):
"Draws a closed line in the shape of a rectangle."
c1 = proj[2].thanGudGetPoint(T["First point: "])
if c1 == Canc: return # Rectangle cancelled
c2 = proj[2].thanGudGetRect(c1, T["Second point: "])
if c2 == Canc: return # Rectangle cancelled
x1, y1 = c1[:2]
x2, y2 = c2[:2]
if x2 > x1: x1, x2 = x2, x1
if y2 > y1: y1, y2 = y2, y1
elem = thandr.ThanLine()
c1[:2] = x1, y1
c2 = list(c1); c2[:2] = x2, y1
c3 = list(c1); c3[:2] = x2, y2
c4 = list(c1); c4[:2] = x1, y2
elem.thanSet([c1, c2, c3, c4, c1])
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
proj[2].thanGudCommandEnd()
def thanTkDrawPoint(proj):
"Draws multiple points with the help of a GUI and stores them to database."
res = proj[2].thanGudGetPoint(T["Specify a point (m=multiple): "], options=("multiple",))
if res == Canc: return proj[2].thanGudCommandCan() # Point cancelled
if res != "m":
elem = thandr.ThanPoint()
elem.thanSet(res)
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
proj[2].thanGudCommandEnd()
return # Point OK
while True: # Get multiple points
res = proj[2].thanGudGetPoint(T["Specify a point (<enter>): "], options=("",))
if res == Canc: return proj[2].thanGudCommandCan() # Point cancelled
if res == "": return proj[2].thanGudCommandEnd() # No more points
elem = thandr.ThanPoint()
elem.thanSet(res)
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
def thanTkDrawPolygon(proj):
"Gets and draws a closed polyline (which is a polygon)."
cp = getpol(proj)
if cp == Canc: return proj[2].thanGudCommandCan() # Polygon cancelled
if not thanNear2(cp[0], cp[-1]): cp.append(list(cp[0]))
elem = thandr.ThanLine()
elem.thanSet(cp)
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
proj[2].thanGudCommandEnd()
def thanTkDrawSolid(proj):
"Gets and draws a closed polyline (which is a polygon)."
def prt(s): proj[2].thanCom.thanAppend("%s\n" % s, "info1")
prt("ThanCad hint: The combination of polygons (which are closed polylines) with")
prt("the 'fill' attribute of layers is superior to the 'solid' command.")
lay = proj[1].thanLayerTree.thanCur
ia = lay.thanAtts["fill"]
if not ia.thanVal:
res = proj[2].thanGudGetYesno(T["Set Fill mode ON for current layers (recommended)? [yes/no] <yes>:"], default="yes")
if res == Canc: return proj[2].thanGudCommandCan() # Polygon cancelled
cp = getsolid(proj)
if cp == Canc: return proj[2].thanGudCommandCan() # Polygon cancelled
if not thanNear2(cp[0], cp[-1]): cp.append(list(cp[0]))
elem = thandr.ThanLineFilled()
elem.thanSet(cp)
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
if res:
from thanlayer import THANNAME
from thansupport import thanLayerCurrent
thanLayerCurrent(proj, parent=lay.thanParent, name=lay.thanAtts[THANNAME].thanVal, current=True, fill=res)
proj[2].thanGudCommandEnd()
def getpol(proj, nmax=-1):
"Get a (non)convex polygon from user of at most nmax corners."
than = proj[2].than
g2l = than.ct.global2Local
c1 = proj[2].thanGudGetPoint(T["First polygon corner: "])
if c1 == Canc: return Canc # Grid cancelled
while True:
c2 = proj[2].thanGudGetLine(c1, T["Second polygon corner: "])
if c2 == Canc: return Canc # Grid cancelled
temp = than.dc.create_line(g2l(c1[0], c1[1]), g2l(c2[0], c2[1]),
fill="blue", tags=("e0",))
item = [temp]
cs = [c1, c2]
while True:
c3 = proj[2].thanGudGetLine2(cs[0], cs[-1], T["Next polygon corner (Undo): "],
options=("undo",""))
if c3 == Canc: than.dc.delete("e0"); return Canc
if c3 == "u":
if len(cs) == 2: than.dc.delete("e0"); break
else: than.dc.delete(item[-1]); del item[-1]; del cs[-1]; continue
if c3 == "": break
cs.append(c3)
temp = than.dc.create_line(g2l(cs[-2][0], cs[-2][1]), g2l(c3[0], c3[1]),
fill="blue", tags=("e0",))
item.append(temp)
if nmax != -1 and len(cs) >= nmax: break
if c3 != "u": break
temp = than.dc.create_line(g2l(cs[-1][0], cs[-1][1]), g2l(cs[0][0], cs[0][1]),
fill="blue", tags=("e0",))
than.dc.delete("e0")
return cs
def getsolid(proj, nmax=-1):
"""Get a triangle or quadrilateral in the style of thAtCAD.
The solid is supposed to be filled with colour. In ThanCad, the 'fill'
attribute of the cirrent layer must be ON, in order to fill the solid.
Solid is just a closed polyline."""
than = proj[2].than
g2l = than.ct.global2Local
c1 = proj[2].thanGudGetPoint(T["First solid corner: "])
if c1 == Canc: return Canc # Grid cancelled
while True:
c2 = proj[2].thanGudGetLine(c1, T["Second solid corner: "])
if c2 == Canc: return Canc # Grid cancelled
temp = than.dc.create_line(g2l(c1[0], c1[1]), g2l(c2[0], c2[1]),
fill="blue", tags=("e0",))
item = [temp]
cs = [c1, c2]
while True:
c3 = proj[2].thanGudGetLine2(cs[0], cs[-1], T["Third polygon corner (Undo): "],
options=("undo",))
if c3 == Canc: than.dc.delete("e0"); return Canc
if c3 == "u": than.dc.delete("e0"); break
cs.append(c3)
temp = than.dc.create_line(g2l(cs[0][0], cs[0][1]), g2l(c3[0], c3[1]),
fill="blue", tags=("e0",))
item.append(temp)
while True:
c3 = proj[2].thanGudGetLine2(cs[-1], cs[-2], T["Fourth solid corner (Undo): "],
options=("undo",""))
if c3 == Canc: than.dc.delete("e0"); return Canc
if c3 == "u": than.dc.delete(item[-1]); del item[-1]; del cs[-1]; break
if c3 == "": # Solid triangle
than.dc.delete("e0")
return cs
cs.append(c3)
temp = than.dc.create_line(g2l(cs[-2][0], cs[-2][1]), g2l(c3[0], c3[1]),
fill="blue", tags=("e0",))
item.append(temp)
temp = than.dc.create_line(g2l(cs[-1][0], cs[-1][1]), g2l(c3[0], c3[1]),
fill="blue", tags=("e0",))
item.append(temp)
than.dc.delete("e0") # Solid quadrilateral
cs[2], cs[3] = cs[3], cs[2] # Imitate thAtCAD's sequence of ccordinates
return cs
def thanTkDrawText(proj):
"Draws multiple texts with the help of a GUI and stores them to database."
un = proj[1].thanUnits
size = proj[2].thanGudGetPosFloat(T["Text Size: "], 10.00)
if size == Canc: return proj[2].thanGudCommandCan() # Text cancelled
st = "%s(%s): " % (T["Rotation angle"], un.anglunit)
theta = proj[2].thanGudGetFloat(st, 0.00)
if theta == Canc: return proj[2].thanGudCommandCan() # Text cancelled
c1 = proj[2].thanGudGetPoint(T["Text location: "])
if c1 == Canc: return proj[2].thanGudCommandCan() # Text cancelled
theta = un.unit2rad(theta)
tt = cos(theta), sin(theta)
nn = -tt[1], tt[0]
while True:
text = proj[2].thanGudGetText(T["Text: "], "")
if text == Canc: return proj[2].thanGudCommandCan() # Text cancelled
if text.strip() == "": return proj[2].thanGudCommandEnd() # No more texts
elem = thandr.ThanText()
elem.thanSet(text, c1, size, theta)
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
c1[:2] = c1[0]-1.2*size*nn[0], c1[1]-1.2*size*nn[1]
def thanTkDrawPointNamed(proj):
"Draws multiple points with the help of a GUI and stores them to database."
res = proj[2].thanGudGetPoint(T["Specify a point (m=multiple): "], options=("multiple",))
if res == Canc: return proj[2].thanGudCommandCan() # Point cancelled
if res != "m":
name = proj[2].thanGudGetText0(T["Point name: "])
if name == Canc: return proj[2].thanGudCommandCan() # Point cancelled
elem = thandr.ThanPointNamed()
elem.thanSet(res, name)
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
proj[2].thanGudCommandEnd()
return # Point OK
while True: # Get multiple points
res = proj[2].thanGudGetPoint(T["Specify a point (<enter>): "], options=("",))
if res == Canc: return proj[2].thanGudCommandCan() # Point cancelled
if res == "": return proj[2].thanGudCommandEnd() # No more points
name = proj[2].thanGudGetText(T["Point name: "])
if name == Canc: return proj[2].thanGudCommandCan() # Point cancelled
elem = thandr.ThanPointNamed()
elem.thanSet(res, name)
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
def thanPointNamedReplace(proj):
"Gets names points as point, name, height and deletes the original objects."
elpnt = thanSelect1(proj, T["Select an unnamed point: "], filter=lambda e: isinstance(e, thandr.ThanPoint))
if elpnt == Canc: return thanModCanc(proj) # Point cancelled
todel = [elpnt]
elnam = thanSelect1(proj, T["Select a text element for point name (t=type name): "],
filter=lambda e: isinstance(e, thandr.ThanText), options=("text", ))
if elnam == Canc: return thanModCanc(proj) # Point cancelled
if elnam == "t":
nam = proj[2].thanGudGetText(T["Type point name: "], default="")
if nam == Canc: return thanModCanc(proj) # Point cancelled
else:
nam = elnam.text
todel.append(elnam)
while True:
elh = thanSelect1(proj, T["Select a text element for point height (t=type height): "],
filter=lambda e:isinstance(e, thandr.ThanText), options=("text", ))
if elh == Canc: return thanModCanc(proj) # Point cancelled
if elh == "t": break
try: h = float(elh.text.replace(",", "."))
except ValueError: pass
else: break
proj[2].thanCom.thanAppend(T["Not a float number. Try Again.\n"], "can")
if elh == "t":
h = proj[2].thanGudGetFloat(T["Type point height: "], default=0.0)
if h == Canc: return thanModCanc(proj) # Point cancelled
else:
todel.append(elh)
proj[1].thanElementDelete(todel, proj) # Delete original elements
elem = thandr.ThanPointNamed()
elem.thanSet(elpnt.cp, "%s/%s" % (nam, h))
proj[1].thanElementAdd(elem)
elem.thanTkDraw(proj[2].than)
proj[2].thanGudCommandEnd()
def thanCurveMake(proj):
"Transforms a polyline to a curve (which has very similar properties)."
lin = thanSel1line(proj, T["Select a line to transform to curve: "])
if lin == Canc: return thanModCanc(proj) # Curve cancelled
lin.__class__ = thandr.ThanCurve
thanModEnd(proj, T["Line was succesfully transformed to curve."])
|