1 | import mesh |
---|
2 | from tkSimpleDialog import Dialog,askfloat, askinteger, askstring |
---|
3 | from Tkinter import FALSE,TRUE, Frame,X, LEFT,YES,BOTH,ALL,Widget,CURRENT, Label,W, Entry, E, ACTIVE, NORMAL, StringVar |
---|
4 | from tkMessageBox import showerror |
---|
5 | |
---|
6 | class vAbstract: |
---|
7 | """ |
---|
8 | Define the visualisation of a mesh object |
---|
9 | """ |
---|
10 | def __init__(self, association): |
---|
11 | # The association is the class oncanvas will hold ie mesh.Vertex |
---|
12 | self.association = association |
---|
13 | self.oncanvas = {} |
---|
14 | |
---|
15 | def visualise(self, meshobject, uniqueID, canvas, scale): |
---|
16 | """ |
---|
17 | draws a mesh object and add it the the canvas data structure. |
---|
18 | Note: a guiID attribute is added to each point object thats visualised. |
---|
19 | This is used in zooming. |
---|
20 | |
---|
21 | meshobject is from mesh.py, eg vertex, hole |
---|
22 | """ |
---|
23 | meshobject.guiID = uniqueID |
---|
24 | meshobject.draw(canvas, uniqueID, scale =scale) |
---|
25 | self.oncanvas[uniqueID] = meshobject |
---|
26 | return meshobject |
---|
27 | |
---|
28 | def unvisualise(self, meshobject, canvas): |
---|
29 | """ |
---|
30 | delete a visual mesh object and |
---|
31 | delete it from the canvas mesh data structure |
---|
32 | """ |
---|
33 | canvas.delete(meshobject.guiID) |
---|
34 | del self.oncanvas[meshobject.guiID] |
---|
35 | |
---|
36 | def hasKey(self, key): |
---|
37 | """ |
---|
38 | returns true if the key is in the canvas list |
---|
39 | """ |
---|
40 | if self.oncanvas.has_key(key): |
---|
41 | return True |
---|
42 | else: |
---|
43 | return False |
---|
44 | |
---|
45 | def getMeshObject(self, key): |
---|
46 | """ |
---|
47 | returns the mesh object, |
---|
48 | given it's key. |
---|
49 | |
---|
50 | precon: The key is valid |
---|
51 | """ |
---|
52 | return self.oncanvas[key] |
---|
53 | |
---|
54 | def editWindow(self, canvas, selMeshObject,userMeshChanged): |
---|
55 | print "Not implimented" |
---|
56 | return userMeshChanged |
---|
57 | |
---|
58 | def defaultWindow(self, canvas): |
---|
59 | print "Not implimented" |
---|
60 | |
---|
61 | def draw (self): |
---|
62 | """ |
---|
63 | This method must be overriden |
---|
64 | """ |
---|
65 | pass |
---|
66 | |
---|
67 | def __add__(self, other): |
---|
68 | return VMesh([self,other]) |
---|
69 | |
---|
70 | def __repr__(self): |
---|
71 | return str(self.__class__) |
---|
72 | |
---|
73 | class vPoints(vAbstract): |
---|
74 | """ |
---|
75 | Define the visualisation of a point |
---|
76 | """ |
---|
77 | |
---|
78 | def draw(self,x,y,mesh,uniqueID,scale,canvas,event): |
---|
79 | """ |
---|
80 | draw a point object, plus add it to the mesh data structure |
---|
81 | |
---|
82 | event isn't used |
---|
83 | """ |
---|
84 | point = mesh.addUserPoint(self.association,x/scale,y/scale) |
---|
85 | self.visualise(point, uniqueID, canvas, scale) |
---|
86 | return point |
---|
87 | |
---|
88 | class vRegions(vPoints): |
---|
89 | """ |
---|
90 | Define the visualisation of a region |
---|
91 | """ |
---|
92 | def editWindow(self, canvas, selMeshObject,userMeshChanged): |
---|
93 | dialog = EditRegionDialog (canvas, |
---|
94 | selMeshObject.getTag(), |
---|
95 | selMeshObject.getMaxArea() ) |
---|
96 | if dialog.ValueOk: |
---|
97 | selMeshObject.setTag(dialog.tag) |
---|
98 | if dialog.maxArea != 0.0: #using 0.0 to mean no max area (hacky) |
---|
99 | selMeshObject.setMaxArea(dialog.maxArea) |
---|
100 | else: |
---|
101 | selMeshObject.deleteMaxArea() |
---|
102 | userMeshChanged = True |
---|
103 | return userMeshChanged |
---|
104 | |
---|
105 | class EditRegionDialog(Dialog): |
---|
106 | """ |
---|
107 | Dialog box for editing region info |
---|
108 | """ |
---|
109 | # initial values, hard coded. |
---|
110 | # should be values associated with the current mesh |
---|
111 | |
---|
112 | |
---|
113 | def __init__(self, |
---|
114 | parent, |
---|
115 | tag, |
---|
116 | maxArea): |
---|
117 | |
---|
118 | |
---|
119 | self.tag = tag |
---|
120 | self.maxArea = maxArea |
---|
121 | |
---|
122 | Dialog.__init__(self, parent) |
---|
123 | |
---|
124 | |
---|
125 | def body(self, master): |
---|
126 | """ |
---|
127 | GUI description |
---|
128 | """ |
---|
129 | self.title("Edit Region") |
---|
130 | |
---|
131 | Label(master, |
---|
132 | text='tag:').grid(row=0, sticky=W) |
---|
133 | Label(master, |
---|
134 | text='Maximum Triangle Area').grid(row=1, sticky=W) |
---|
135 | |
---|
136 | |
---|
137 | tagVar = StringVar() |
---|
138 | tagVar.set(self.tag) |
---|
139 | self.tagstr = Entry(master, |
---|
140 | width = 16, |
---|
141 | textvariable = tagVar, |
---|
142 | takefocus=1) |
---|
143 | |
---|
144 | |
---|
145 | maxAreaVar = StringVar() |
---|
146 | if self.maxArea == None: |
---|
147 | self.maxArea = "" |
---|
148 | maxAreaVar.set(self.maxArea) |
---|
149 | self.maxAreastr = Entry(master, |
---|
150 | width = 16, |
---|
151 | textvariable = maxAreaVar, |
---|
152 | takefocus=1) |
---|
153 | |
---|
154 | self.tagstr.grid(row=0, column=1, sticky=W) |
---|
155 | self.maxAreastr.grid(row=1, column=1, sticky=W) |
---|
156 | |
---|
157 | self.tag = -1 |
---|
158 | self.maxArea = -1 |
---|
159 | self.ValueOk = False |
---|
160 | |
---|
161 | |
---|
162 | def apply(self): |
---|
163 | """ |
---|
164 | check entered values |
---|
165 | """ |
---|
166 | self.goodMaxArea = self.goodtag = True |
---|
167 | self.ValueOk = True |
---|
168 | try: |
---|
169 | self.tag = self.tagstr.get() |
---|
170 | except ValueError: |
---|
171 | self.ValueOk = False |
---|
172 | showerror('Bad mesh generation values', |
---|
173 | ' Values are not numbers.') |
---|
174 | |
---|
175 | if not self.maxAreastr.get() == "": |
---|
176 | try: |
---|
177 | self.maxArea = float(self.maxAreastr.get()) |
---|
178 | #MeshGenDialog.lastMaxArea =self.maxArea |
---|
179 | except ValueError: |
---|
180 | self.ValueOk = False |
---|
181 | showerror('Bad mesh generation values', |
---|
182 | ' Values are not numbers.') |
---|
183 | else: |
---|
184 | self.maxArea = 0.0 |
---|
185 | |
---|
186 | try: |
---|
187 | # value checking |
---|
188 | if self.goodMaxArea == True and self.maxArea < 0.0: |
---|
189 | raise IOError |
---|
190 | |
---|
191 | except IOError: |
---|
192 | self.ValueOk = False |
---|
193 | showerror('Bad mesh generation values', |
---|
194 | 'Values are out of range.') |
---|
195 | |
---|
196 | |
---|
197 | class vSegments(vAbstract): |
---|
198 | """ |
---|
199 | Define the visualisation of a segment |
---|
200 | """ |
---|
201 | |
---|
202 | def draw(self,v1,v2,mesh,uniqueID,scale,canvas,event): |
---|
203 | """ |
---|
204 | draw a segment object, plus add it to the mesh data structure |
---|
205 | |
---|
206 | event isn't used |
---|
207 | """ |
---|
208 | segment = mesh.addUserSegment(v1,v2) |
---|
209 | self.visualise(segment, uniqueID, canvas, scale) |
---|
210 | return segment |
---|
211 | |
---|
212 | def editWindow_int(self, canvas, selMeshObject): |
---|
213 | ask_int = askinteger("Edit Segment", "Tag", |
---|
214 | minvalue=0, initialvalue= selMeshObject.tag) |
---|
215 | if ask_int >= 0: |
---|
216 | selMeshObject.tag = ask_int |
---|
217 | |
---|
218 | def editWindow(self, canvas, selMeshObject, userMeshChanged): |
---|
219 | ask_string = askstring("Edit Segment Tag", "Tag", |
---|
220 | initialvalue= str(selMeshObject.tag)) |
---|
221 | if ask_string != None: |
---|
222 | selMeshObject.tag = ask_string |
---|
223 | userMeshChanged = True |
---|
224 | return userMeshChanged |
---|
225 | |
---|
226 | def defaultWindow(self, canvas): |
---|
227 | ask_string = askstring("Edit Default Segment Tag", "Tag", |
---|
228 | initialvalue= str(self.association.get_default_tag())) |
---|
229 | if ask_string != None: |
---|
230 | self.association.set_default_tag(ask_string) |
---|
231 | |
---|
232 | class vTriangles(vAbstract): |
---|
233 | """ |
---|
234 | Define the visualisation of a triangle |
---|
235 | """ |
---|
236 | |
---|
237 | def draw(self,mesh,uniqueID,scale,canvas,event): |
---|
238 | """ |
---|
239 | currently triangles have no draw function |
---|
240 | """ |
---|
241 | raise TypeError |
---|
242 | |
---|
243 | |
---|
244 | class vMesh(vAbstract): |
---|
245 | """ |
---|
246 | Define the visualisation of a collection of mesh object dictionaries (eg vSegments) |
---|
247 | """ |
---|
248 | def __init__(self,MeshObjects): |
---|
249 | self.association = mesh.MeshObject |
---|
250 | self.MeshObjectsList = MeshObjects |
---|
251 | |
---|
252 | def visualise(self, meshobject, uniqueID, canvas, scale): |
---|
253 | """ |
---|
254 | This should find the right vAbstract instance and call visualise |
---|
255 | """ |
---|
256 | raise Exception |
---|
257 | |
---|
258 | def unvisualise(self, MeshObject, canvas): |
---|
259 | """ |
---|
260 | delete a visual object and |
---|
261 | delete it from the canvas mesh data structure |
---|
262 | """ |
---|
263 | for MeshObjects in self.MeshObjectsList: |
---|
264 | if isinstance(MeshObject,MeshObjects.association): |
---|
265 | MeshObjects.unvisualise(MeshObject, canvas) |
---|
266 | break |
---|
267 | |
---|
268 | def hasKey(self, key): |
---|
269 | """ |
---|
270 | returns true if the key is in the canvas list |
---|
271 | """ |
---|
272 | for MeshObjects in self.MeshObjectsList: |
---|
273 | if MeshObjects.oncanvas.has_key(key): |
---|
274 | return True |
---|
275 | return False |
---|
276 | |
---|
277 | def getMeshObject(self, key): |
---|
278 | """ |
---|
279 | returns the mesh object, |
---|
280 | given it's key. |
---|
281 | |
---|
282 | precon: The key is valid |
---|
283 | """ |
---|
284 | for MeshObjects in self.MeshObjectsList: |
---|
285 | try: |
---|
286 | return MeshObjects.oncanvas[key] |
---|
287 | except KeyError: |
---|
288 | pass |
---|
289 | raise KeyError |
---|
290 | |
---|
291 | def editWindow(self, canvas, selMeshObject, userMeshChanged): |
---|
292 | for MeshObjects in self.MeshObjectsList: |
---|
293 | if isinstance(selMeshObject,MeshObjects.association): |
---|
294 | userMeshChanged =MeshObjects.editWindow(canvas, selMeshObject,userMeshChanged) |
---|
295 | break |
---|
296 | return userMeshChanged |
---|
297 | |
---|
298 | def defaultWindow(self, canvas, MeshClass): |
---|
299 | for MeshObjects in self.MeshObjectsList: |
---|
300 | if MeshClass == MeshObjects.association: |
---|
301 | MeshObjects.defaultWindow(canvas) |
---|
302 | break |
---|