printrun-src/printrun/gui/viz.py

changeset 15
0bbb006204fc
child 46
cce0af6351f0
equal deleted inserted replaced
14:51bf56ba3c10 15:0bbb006204fc
1 # This file is part of the Printrun suite.
2 #
3 # Printrun is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Printrun is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
15
16 import traceback
17 import logging
18
19 import wx
20
21 class NoViz(object):
22
23 showall = False
24
25 def clear(self, *a):
26 pass
27
28 def addfile_perlayer(self, gcode, showall = False):
29 layer_idx = 0
30 while layer_idx < len(gcode.all_layers):
31 yield layer_idx
32 layer_idx += 1
33 yield None
34
35 def addfile(self, *a, **kw):
36 pass
37
38 def addgcode(self, *a, **kw):
39 pass
40
41 def addgcodehighlight(self, *a, **kw):
42 pass
43
44 def Refresh(self, *a):
45 pass
46
47 def setlayer(self, *a):
48 pass
49
50 class NoVizWindow(object):
51
52 def __init__(self):
53 self.p = NoViz()
54
55 def Destroy(self):
56 pass
57
58 class VizPane(wx.BoxSizer):
59
60 def __init__(self, root, parentpanel = None):
61 super(VizPane, self).__init__(wx.VERTICAL)
62 if not parentpanel: parentpanel = root.panel
63 if root.settings.mainviz == "None":
64 root.gviz = NoViz()
65 root.gwindow = NoVizWindow()
66 return
67 use2dview = root.settings.mainviz == "2D"
68 if root.settings.mainviz == "3D":
69 try:
70 import printrun.gcview
71 root.gviz = printrun.gcview.GcodeViewMainWrapper(parentpanel, root.build_dimensions_list, root = root, circular = root.settings.circular_bed, antialias_samples = int(root.settings.antialias3dsamples))
72 root.gviz.clickcb = root.show_viz_window
73 except:
74 use2dview = True
75 logging.error("3D view mode requested, but we failed to initialize it.\n"
76 + "Falling back to 2D view, and here is the backtrace:\n"
77 + traceback.format_exc())
78 if use2dview:
79 from printrun import gviz
80 root.gviz = gviz.Gviz(parentpanel, (300, 300),
81 build_dimensions = root.build_dimensions_list,
82 grid = (root.settings.preview_grid_step1, root.settings.preview_grid_step2),
83 extrusion_width = root.settings.preview_extrusion_width,
84 bgcolor = root.bgcolor)
85 root.gviz.SetToolTip(wx.ToolTip(_("Click to examine / edit\n layers of loaded file")))
86 root.gviz.showall = 1
87 root.gviz.Bind(wx.EVT_LEFT_DOWN, root.show_viz_window)
88 use3dview = root.settings.viz3d
89 if use3dview:
90 try:
91 import printrun.gcview
92 objects = None
93 if isinstance(root.gviz, printrun.gcview.GcodeViewMainWrapper):
94 objects = root.gviz.objects
95 root.gwindow = printrun.gcview.GcodeViewFrame(None, wx.ID_ANY, 'Gcode view, shift to move view, mousewheel to set layer', size = (600, 600), build_dimensions = root.build_dimensions_list, objects = objects, root = root, circular = root.settings.circular_bed, antialias_samples = int(root.settings.antialias3dsamples))
96 except:
97 use3dview = False
98 logging.error("3D view mode requested, but we failed to initialize it.\n"
99 + "Falling back to 2D view, and here is the backtrace:\n"
100 + traceback.format_exc())
101 if not use3dview:
102 from printrun import gviz
103 root.gwindow = gviz.GvizWindow(build_dimensions = root.build_dimensions_list,
104 grid = (root.settings.preview_grid_step1, root.settings.preview_grid_step2),
105 extrusion_width = root.settings.preview_extrusion_width,
106 bgcolor = root.bgcolor)
107 root.gwindow.Bind(wx.EVT_CLOSE, lambda x: root.gwindow.Hide())
108 if not isinstance(root.gviz, NoViz):
109 self.Add(root.gviz.widget, 1, flag = wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL)

mercurial