-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat_current.py
286 lines (234 loc) · 10.7 KB
/
float_current.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# much of this is coming from sample app of wx floatcanvas.
import cStringIO
try:
import numpy as N
haveNumpy = True
#print "Using numpy, version:", N.__version__
except ImportError:
# numpy isn't there
haveNumpy = False
errorText = (
"The FloatCanvas requires the numpy module, version 1.* \n\n"
"You can get info about it at:\n"
"http://numpy.scipy.org/\n\n"
)
#---------------------------------------------------------------------------
def _ColorFromElevation(elevation):
if elevation == -1:
return "Red"
return "Blue"
def BuildDrawFrame(): # this gets called when needed, rather than on import
try:
from floatcanvas import NavCanvas, FloatCanvas, Resources
except ImportError: # if it's not there locally, try the wxPython lib.
from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
import wx.lib.colourdb
import time, random
class DrawFrame(wx.Frame):
def __init__(self,parent, id,title,position,size):
wx.Frame.__init__(self,parent, id,title,position, size)
MenuBar = wx.MenuBar()
file_menu = wx.Menu()
item = file_menu.Append(-1, "&Close","Close")
self.Bind(wx.EVT_MENU, self.OnQuit, item)
item = file_menu.Append(-1, "&Save File","Save the updated dxf")
self.Bind(wx.EVT_MENU, self.OnSaveFILE, item)
item = file_menu.Append(-1, "&Open File","Open vector file")
self.Bind(wx.EVT_MENU, self.OnOpenFILE, item)
MenuBar.Append(file_menu, "&File")
draw_menu = wx.Menu()
view_menu = wx.Menu()
item = view_menu.Append(-1, "Zoom to &Fit","Zoom to fit the window")
self.Bind(wx.EVT_MENU, self.ZoomToFit, item)
MenuBar.Append(view_menu, "&View")
help_menu = wx.Menu()
item = help_menu.Append(-1, "&About",
"More information About this program")
self.Bind(wx.EVT_MENU, self.OnAbout, item)
MenuBar.Append(help_menu, "&Help")
self.SetMenuBar(MenuBar)
self.CreateStatusBar()
# Add the Canvas
NC = NavCanvas.NavCanvas(self,
Debug = 0,
BackgroundColor = "DARK SLATE BLUE")
self.Canvas = NC.Canvas # reference the contained FloatCanvas
self.ElevationWindow = wx.TextCtrl(self, wx.ID_ANY,
"?",
style = (wx.TE_MULTILINE |
wx.SUNKEN_BORDER)
)
self.ElevationWindow.Bind(wx.EVT_TEXT, self.OnMsgUpdate)
self.ElevationWindow.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
##Create a sizer to manage the Canvas and message window
MainSizer = wx.BoxSizer(wx.VERTICAL)
MainSizer.Add(NC, 1000, wx.EXPAND)
MainSizer.Add(self.ElevationWindow, 1, wx.EXPAND | wx.ALL, 5)
self.SetSizer(MainSizer)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.EventsAreBound = False
## getting all the colors for random objects
wx.lib.colourdb.updateColourDB()
self.colors = wx.lib.colourdb.getColourList()
self.lastline = None
return None
def OnKillFocus(self, object):
"""
we always want to be on
focuse for the elevation window
if we move away and we move to our floatcanvas, then
we put it back in elevation window
there might be a better way to do so, but I have
not found one
"""
if type(object.GetWindow()) is wx.lib.floatcanvas.FloatCanvas.FloatCanvas:
self.ElevationWindow.SetFocus()
self.ElevationWindow.SelectAll()
def OnMsgUpdate(self, object):
if self.lastline != None:
try:
self.elevations[self.lastline.line_idx] = float(self.ElevationWindow.GetValue())
except ValueError:
self.elevations[self.lastline.line_idx]
def UnBindAllMouseEvents(self):
## Here is how you unbind FloatCanvas mouse events
self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DOWN)
self.Canvas.Unbind(FloatCanvas.EVT_LEFT_UP)
self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DCLICK)
self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_DOWN)
self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_UP)
self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_DCLICK)
self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_DOWN)
self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_UP)
self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_DCLICK)
self.EventsAreBound = False
def DrawLines(self):
for i in range(len(self.lines)):
L = self.Canvas.AddLine(self.lines[i], LineWidth = 2, LineColor = _ColorFromElevation(self.elevations[i]))
L.line_idx = i
L.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.LineGotHit)
self.Canvas.Draw(Force = True)
def DrawDxf(self, path):
from dxf_reader import DXFReader
dxf = DXFReader(path)
self.lines = []
self.elevations = []
for entity in dxf.lines():
line = []
el = -1
for point in entity.points():
line.append(self.scaler.locate((point[0], point[1])))
lastpoint = point
if len(point) > 2:
el = point[2]
self.lines.append(line)
self.elevations.append(el)
self.DrawLines()
def DrawElv(self, path):
lines = file(path, "rb").readlines();
self.lines = []
self.elevations = []
for line in lines:
splits = [float(i) for i in line.split(",")]
self.elevations.append(splits[0])
self.lines.append([])
for i in range(1, len(splits), 2):
self.lines[-1].append((splits[i],splits[i+1]))
self.DrawLines()
def OnOpenFILE(self, event=None):
import os
dlg = wx.FileDialog(
self, message="Open file ...", defaultDir=os.getcwd(),
defaultFile="", wildcard="*.dxf;*.elv", style=wx.FD_OPEN
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
self.vector_file = path
if len(path)>4 and path[-4:] == ".dxf":
self.DrawDxf(path)
else:
self.DrawElv(path)
# make elv model
def WriteElv(self, path, lines, elevations):
f = file(path, "wt")
for i in range(len(lines)):
s = str(elevations[i])+","+",".join([str(point[0])+","+str(point[1]) for point in lines[i]])
f.write(s+"\n")
f.close()
def OnSaveFILE(self, event=None):
import os
import dxf_writer
dlg = wx.FileDialog(
self, message="Save file as ...", defaultDir=os.getcwd(),
defaultFile="output.elv", wildcard="*.dxf;*.elv", style=wx.SAVE
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
if path[-4:].lower() == ".dxf": #todo: make "isDxfFile"
dxf_writer.WriteDXF(path, self.lines, self.elevations, self.scaler)
if path[-4:].lower() == ".elv":
self.WriteElv(path, self.lines, self.elevations)
def OnAbout(self, event):
dlg = wx.MessageDialog(self,
"This is a small program to attach elevation\n"
"to lines traced over contours\n",
"About Me",
wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def ZoomToFit(self,event):
self.Canvas.ZoomToBB()
def OnQuit(self,event):
self.Close(True)
def OnCloseWindow(self, event):
self.Destroy()
def ShowAll(self, event=None):
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
imageFile = 'kopejais.tif' # this was back.tif
data = open(imageFile, "rb").read()
# convert to a data stream
stream = cStringIO.StringIO(data)
# this allows us to disable logging from tif reader module
noLog = wx.LogNull()
image = wx.ImageFromStream( stream )
# this enables logging back
del noLog
# would this become fast if we did not have a scaled bitmap?
ORIGINAL_Y = 200
BitMap = Canvas.AddScaledBitmap(image, (0, ORIGINAL_Y), Height=ORIGINAL_Y)
from tiff_size import GetTiffSize
sizing = GetTiffSize(imageFile)
from scaler import Scaler
self.scaler = Scaler(sizing[4]-sizing[2], sizing[5]-sizing[3], sizing[2], sizing[3])
self.scaler.set_scale_y(ORIGINAL_Y)
self.Canvas.ZoomToBB()
def LineGotHit(self, Object):
if self.lastline!=None:
self.lastline.SetLineColor(_ColorFromElevation(self.elevations[self.lastline.line_idx]))
self.lastline = Object
self.ElevationWindow.ChangeValue(str(self.elevations[Object.line_idx]))
self.ElevationWindow.SetFocus()
Object.SetLineColor("Green")
self.Canvas.Draw(Force=True)
return DrawFrame
#---------------------------------------------------------------------------
if __name__ == "__main__":
import wx
if not haveNumpy:
raise ImportError(errorText)
StartUpDemo = "hit" # the default
class ElevatorApp(wx.App):
def __init__(self, *args, **kwargs):
wx.App.__init__(self, *args, **kwargs)
def OnInit(self):
DrawFrame = BuildDrawFrame()
frame = DrawFrame(None, -1, "Elevator: atttaching elevation to contours",wx.DefaultPosition,(700,700))
self.SetTopWindow(frame)
frame.Show()
frame.ShowAll()
return True
app = ElevatorApp(False)# put in True if you want output to go to it's own window.
app.MainLoop()