-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaction_save_restore_layout.py
334 lines (283 loc) · 13.2 KB
/
action_save_restore_layout.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# -*- coding: utf-8 -*-
# action_save_restore_layout.py
#
# Copyright (C) 2019 Mitja Nemec
#
# 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.
#
# 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., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import wx
import pcbnew
import os
import logging
import sys
from .save_layout_dialog_GUI import SaveLayoutDialogGUI
from .restore_layout_dialog_GUI import RestoreLayoutDialogGUI
from .initial_dialog_GUI import InitialDialogGUI
from .error_dialog_GUI import ErrorDialogGUI
from .save_restore_layout import SaveLayout
from .save_restore_layout import RestoreLayout
class ErrorDialog(ErrorDialogGUI):
def SetSizeHints(self, sz1, sz2):
# DO NOTHING
pass
def __init__(self, parent):
super(ErrorDialog, self).__init__(parent)
class InitialDialog(InitialDialogGUI):
SAVE = 1025
RESTORE = 1026
# hack for new wxFormBuilder generating code incompatible with old wxPython
# noinspection PyMethodOverriding
def SetSizeHints(self, sz1, sz2):
# DO NOTHING
pass
def __init__(self, parent):
super(InitialDialog, self).__init__(parent)
def on_save(self, event):
event.Skip()
self.EndModal(InitialDialog.SAVE)
def on_restore(self, event):
event.Skip()
self.EndModal(InitialDialog.RESTORE)
class RestoreDialog(RestoreLayoutDialogGUI):
def SetSizeHints(self, sz1, sz2):
# DO NOTHING
pass
def __init__(self, parent, logger):
self.logger = logger
self.logger.info("Initialization start")
super(RestoreDialog, self).__init__(parent)
self.logger.info(f"Initialization done {self.m_staticText3.GetLabelText()}")
class SaveDialog(SaveLayoutDialogGUI):
def SetSizeHints(self, sz1, sz2):
# DO NOTHING
pass
def __init__(self, parent, layout_saver, logger):
super(SaveDialog, self).__init__(parent)
self.logger = logger
self.save_layout = layout_saver
self.list_levels.Clear()
self.list_levels.AppendItems(layout_saver.src_anchor_fp.filename)
self.hl_fps = []
self.hl_items = []
def level_changed(self, event):
# clear highlight on all footprints on selected level
self.save_layout.highlight_clear_level(self.hl_fps, self.hl_items)
self.hl_fps = []
self.hl_items = []
pcbnew.Refresh()
# highlight all footprints on selected level
(self.hl_fps, self.hl_items) = self.save_layout.highlight_set_level(self.save_layout.src_anchor_fp.sheet_id[0:self.list_levels.GetSelection() + 1],
self.cb_tracks.GetValue(),
self.cb_zones.GetValue(),
self.cb_text.GetValue(),
self.cb_drawings.GetValue(),
self.cb_intersecting.GetValue())
pcbnew.Refresh()
event.Skip()
def __del__(self):
# clear highlight on all footprints on selected level
self.save_layout.highlight_clear_level(self.hl_fps, self.hl_items)
self.hl_fps = []
self.hl_items = []
pcbnew.Refresh()
class SaveRestoreLayout(pcbnew.ActionPlugin):
def __init__(self):
super(SaveRestoreLayout, self).__init__()
self.frame = None
self.name = "Save/Restore Layout"
self.category = "Save Restore Layout"
self.description = "The plugin can save and restore partial layout of footprints from one hierarchical sheet."
self.icon_file_name = os.path.join(
os.path.dirname(__file__), 'save_restore_layout_light.png')
self.dark_icon_file_name = os.path.join(
os.path.dirname(__file__), 'save_restore_layout_dark.png')
self.debug_level = logging.INFO
# plugin paths
self.plugin_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)))
self.version_file_path = os.path.join(self.plugin_folder, 'version.txt')
# load the plugin version
with open(self.version_file_path) as fp:
self.version = fp.readline()
def defaults(self):
pass
def Run(self):
# grab PCB editor frame
self.frame = wx.FindWindowByName("PcbFrame")
# load board
board = pcbnew.GetBoard()
pass
# go to the project folder - so that log will be in proper place
os.chdir(os.path.dirname(os.path.abspath(board.GetFileName())))
# Remove all handlers associated with the root logger object.
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
file_handler = logging.FileHandler(filename='save_restore_layout.log', mode='w')
handlers = [file_handler]
# set up logger
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)s %(lineno)d:%(message)s',
datefmt='%m-%d %H:%M:%S',
handlers=handlers)
logger = logging.getLogger(__name__)
logger.info("Plugin executed on: " + repr(sys.platform))
logger.info("Plugin executed with python version: " + repr(sys.version))
logger.info("KiCad build version: " + str(pcbnew.GetBuildVersion()))
logger.info("Plugin version: " + self.version)
logger.info("Frame repr: " + repr(self.frame))
# check if there is exactly one footprints selected
selected_footprints = [x.GetReference() for x in board.GetFootprints() if x.IsSelected()]
# if more or less than one show only a message box
if len(selected_footprints) != 1:
logger.info("Plugin failed to run as more or less then 1 footprint selected")
caption = 'Save/Restore Layout'
message = "More or less than 1 footprint selected. Please select exactly one footprint " \
"and run the plugin again"
dlg = wx.MessageDialog(self.frame, message, caption, wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
return
# this is the source anchor footprint reference
anchor_fp_ref = selected_footprints[0]
logger.info("Anchor footprint reference is " + repr(anchor_fp_ref))
# show initial dialog
dlg = InitialDialog(self.frame)
res = dlg.ShowModal()
dlg.Destroy()
if res == InitialDialog.SAVE:
src_anchor_fp_ref = anchor_fp_ref
logger.info("Save layout chosen")
# prepare the layout to save
try:
save_layout = SaveLayout(board, src_anchor_fp_ref)
except Exception:
logger.exception("Fatal error when creating an instance of SaveLayout")
e_dlg = ErrorDialog(self.frame)
e_dlg.ShowModal()
e_dlg.Destroy()
logging.shutdown()
return
# show the level GUI
main_dlg = SaveDialog(self.frame, save_layout, logger)
main_dlg.CenterOnParent()
action = main_dlg.ShowModal()
if action == wx.ID_OK:
# get the selected level
selected_level = main_dlg.list_levels.GetSelection()
# if user did not select any level available cancel
if selected_level < 0:
logger.info("User failed to select hierarchy level to save")
caption = 'Save/Restore Layout'
message = "One hierarchical level has to be chosen"
dlg = wx.MessageDialog(self.frame, message, caption, wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
logging.shutdown()
main_dlg.Destroy()
return
# Ask the user top specify file
wildcard = "Saved Layout Files (*.pckl)|*.pckl"
dlg = wx.FileDialog(self.frame, "Select a file", os.getcwd(),
save_layout.src_anchor_fp.filename[selected_level].strip(".kicad_sch"), wildcard,
wx.FD_SAVE)
res = dlg.ShowModal()
if res != wx.ID_OK:
logger.info("No filename given. User canceled the plugin during file save selection")
logging.shutdown()
dlg.Destroy()
main_dlg.Destroy()
return
data_file = dlg.GetPath()
dlg.Destroy()
# run the plugin
logger.info("Saving the layout in " + repr(data_file) + " for level " + repr(selected_level))
try:
save_layout.save_layout(save_layout.src_anchor_fp.sheet_id[0:selected_level + 1], data_file,
main_dlg.cb_tracks.GetValue(),
main_dlg.cb_zones.GetValue(),
main_dlg.cb_text.GetValue(),
main_dlg.cb_drawings.GetValue(),
main_dlg.cb_intersecting.GetValue())
except Exception:
logger.exception("Fatal error running SaveLayout")
e_dlg = ErrorDialog(self.frame)
e_dlg.ShowModal()
e_dlg.Destroy()
logging.shutdown()
return
pass
main_dlg.Destroy()
logging.shutdown()
return
if res == InitialDialog.RESTORE:
dst_anchor_fp_ref = anchor_fp_ref
logger.info("Restore layout chosen")
# ask the user to find the layout information file
wildcard = "Saved Layout Files (*.pckl)|*.pckl"
dlg = wx.FileDialog(self.frame, "Choose a file", os.getcwd(), "", wildcard, wx.FD_OPEN)
res = dlg.ShowModal()
if res != wx.ID_OK:
logging.shutdown()
return
layout_file = dlg.GetPath()
dlg.Destroy()
# ask the user to optionally specify whathere the target layout elements should be put in the group
main_dlg = RestoreDialog(self.frame, logger)
main_dlg.CenterOnParent()
action = main_dlg.ShowModal()
if action == wx.ID_OK:
group_name = main_dlg.lbl_group_name.GetValue()
if group_name == "":
group_name = None
# create an instance
try:
restore_layout = RestoreLayout(board, dst_anchor_fp_ref, group_name)
except Exception:
logger.exception("Fatal error when creating an instance of RestoreLayout")
caption = 'Save/Restore Layout'
message = "Fatal error when creating an instance of RestoreLayout.\n" \
+ "You can raise an issue on GiHub page.\n" \
+ "Please attach the save_restore_layout.log which you should find in the project folder."
dlg = wx.MessageDialog(self.frame, message, caption, wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
logging.shutdown()
return
# run the main backend
try:
restore_layout.restore_layout(layout_file)
except (ValueError, LookupError) as error:
caption = 'Save/Restore Layout'
message = str(error)
dlg = wx.MessageDialog(self.frame, message, caption, wx.OK | wx.ICON_EXCLAMATION)
dlg.ShowModal()
dlg.Destroy()
logger.exception("Error when restoring layout")
logging.shutdown()
return
except Exception:
logger.exception("Fatal error when restoring layout")
caption = 'Save/Restore Layout'
message = "Fatal error when restoring layout.\n" \
+ "You can raise an issue on GiHub page.\n" \
+ "Please attach the save_restore_layout.log which you should find in the project folder."
dlg = wx.MessageDialog(self.frame, message, caption, wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
logging.shutdown()
return
logging.shutdown()
return