Skip to content

Commit

Permalink
Remember current settings (saves additional file in the project folder)
Browse files Browse the repository at this point in the history
  • Loading branch information
MitjaNemec committed Aug 6, 2023
1 parent 69db95c commit b2cfe6b
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 48 deletions.
274 changes: 227 additions & 47 deletions action_place_footprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from .error_dialog_GUI import ErrorDialogGUI
from .place_footprints import Placer
import re
import configparser


def fp_set_highlight(fp):
Expand Down Expand Up @@ -72,19 +73,26 @@ def SetSizeHints(self, sz1, sz2):
pass

def __init__(self, parent, placer, ref_fp, user_units):

super(PlaceBySheetDialog, self).__init__(parent)

self.placer = placer
self.user_units = user_units
self.ref_fp = ref_fp
self.ref_list = []
self.list_sheetsChoices = None
self.config_filename = os.path.join(self.placer.project_folder, 'place_footprints.ini')
self.logger = logging.getLogger(__name__)

footprints = self.placer.get_footprints_on_sheet(self.ref_fp.sheet_id)
self.height, self.width = self.placer.get_footprints_bounding_box_size(footprints)

self.list_levels.Clear()
self.list_levels.AppendItems(self.ref_fp.filename)
# by default select all items
self.logger.info("Selecting: " + repr(self.list_levels.GetCount()))
for i in range(self.list_levels.GetCount()):
self.list_levels.SetSelection(i)

if user_units == 'mm':
self.lbl_x_mag.SetLabelText(u"step x (mm):")
Expand All @@ -95,12 +103,92 @@ def __init__(self, parent, placer, ref_fp, user_units):
self.lbl_columns_rad_step.Disable()
self.val_columns_rad_step.Disable()

def __del__(self):
# load config file if it exists
if os.path.exists(self.config_filename):
config = configparser.ConfigParser()
config.read(self.config_filename)
# if there is by sheet config
if 'sheet' in config.sections():
# get the arrangement
arrangement = config['sheet']['arrangement']
if arrangement == 'Linear':
self.com_arr.SetSelection(0)
self.modify_dialog_for_linear()
self.val_x_mag.SetValue(config['sheet.linear']['step_x'])
self.val_y_angle.SetValue(config['sheet.linear']['step_y'])
self.val_nth.SetValue(config['sheet.linear']['nth_rotate'])
self.val_rotate.SetValue(config['sheet.linear']['nth_rotate_angle'])
if arrangement == 'Circular':
self.com_arr.SetSelection(2)
self.modify_dialog_for_circular()
self.val_y_angle.SetValue(config['sheet.circular']['angle'])
self.val_columns_rad_step.SetValue(config['sheet.circular']['radial_step'])
self.val_x_mag.SetValue(config['sheet.circular']['radius'])
self.val_nth.SetValue(config['sheet.circular']['nth_rotate'])
self.val_rotate.SetValue(config['sheet.circular']['nth_rotate_angle'])
if arrangement == 'Matrix':
self.com_arr.SetSelection(1)
self.modify_dialog_for_matrix()
self.val_x_mag.SetValue(config['sheet.matrix']['step_x'])
self.val_y_angle.SetValue(config['sheet.matrix']['step_y'])
self.val_columns_rad_step.SetValue(config['sheet.matrix']['columns'])
self.val_nth.SetValue(config['sheet.matrix']['nth_rotate'])
self.val_rotate.SetValue(config['sheet.matrix']['nth_rotate_angle'])

def on_ok(self, event):
# test if all entries have numbers, if they don't then don't do anything
if self.val_x_mag.GetValue() == '':
return
if self.val_y_angle.GetValue() == '':
return
if self.val_nth.GetValue() == '':
return
if self.val_rotate.GetValue() == '':
return
if self.com_arr.GetStringSelection() != 'Linear' and self.val_columns_rad_step.GetValue() == '':
return

# clear highlights
for ref in self.ref_list:
fp = self.placer.get_fp_by_ref(ref).fp
fp_clear_highlight(fp)

self.logger.info("Saving config sheet")

# save the config
# if existing, append
if os.path.exists(self.config_filename):
config = configparser.ConfigParser()
config.read(self.config_filename)
else:
config = configparser.ConfigParser()
config['sheet'] = {'arrangement': self.com_arr.GetStringSelection()}
arrangement = config['sheet']['arrangement']
if arrangement == 'Linear':
self.logger.info("Saving config sheet linear")
config['sheet.linear'] = {'step_x': self.val_x_mag.GetValue(),
'step_y': self.val_y_angle.GetValue(),
'nth_rotate': self.val_nth.GetValue(),
'nth_rotate_angle': self.val_rotate.GetValue()}
if arrangement == 'Circular':
self.logger.info("Saving config sheet circular")
config['sheet.circular'] = {'angle': self.val_y_angle.GetValue(),
'radial_step': self.val_columns_rad_step.GetValue(),
'radius': self.val_x_mag.GetValue(),
'nth_rotate': self.val_nth.GetValue(),
'nth_rotate_angle': self.val_rotate.GetValue()}
if arrangement == 'Matrix':
self.logger.info("Saving config sheet matrix")
config['sheet.matrix'] = {'step_x': self.val_x_mag.GetValue(),
'step_y': self.val_y_angle.GetValue(),
'columns': self.val_columns_rad_step.GetValue(),
'nth_rotate': self.val_nth.GetValue(),
'nth_rotate_angle': self.val_rotate.GetValue()}
with open(self.config_filename, 'w') as configfile:
self.logger.info("Saving config saving file")
config.write(configfile)
event.Skip()

def modify_dialog_for_linear(self):
if self.user_units == 'mm':
self.lbl_x_mag.SetLabelText(u"step x (mm):")
Expand Down Expand Up @@ -136,6 +224,7 @@ def modify_dialog_for_matrix(self):

def modify_dialog_for_circular(self):
number_of_all_sheets = len(self.list_sheets.GetSelections())
self.logger.info("Selection: " + repr(self.list_sheets.GetSelections()))
circumference = number_of_all_sheets * self.width
radius = circumference / (2 * math.pi)
angle = 360.0 / number_of_all_sheets
Expand Down Expand Up @@ -234,6 +323,8 @@ def __init__(self, parent, placer, ref_fp, user_units):

self.placer = placer
self.user_units = user_units
self.config_filename = os.path.join(self.placer.project_folder, 'place_footprints.ini')
self.logger = logging.getLogger(__name__)

# grab footprint data
self.ref_fp = ref_fp
Expand All @@ -249,61 +340,105 @@ def __init__(self, parent, placer, ref_fp, user_units):
self.lbl_columns_rad_step.Disable()
self.val_columns_rad_step.Disable()

# load config file if it
def on_show( self, event ):
if os.path.exists(self.config_filename):
config = configparser.ConfigParser()
config.read(self.config_filename)
# if there is by sheet config
if 'reference' in config.sections():
# get the arrangement
arrangement = config['reference']['arrangement']
if arrangement == 'Linear':
self.com_arr.SetSelection(0)
self.modify_dialog_for_linear()
self.val_x_mag.SetValue(config['reference.linear']['step_x'])
self.val_y_angle.SetValue(config['reference.linear']['step_y'])
self.val_nth.SetValue(config['reference.linear']['nth_rotate'])
self.val_rotate.SetValue(config['reference.linear']['nth_rotate_angle'])
if arrangement == 'Circular':
self.com_arr.SetSelection(2)
self.modify_dialog_for_circular()
self.val_y_angle.SetValue(config['reference.circular']['angle'])
self.val_columns_rad_step.SetValue(config['reference.circular']['radial_step'])
self.val_x_mag.SetValue(config['reference.circular']['radius'])
self.val_nth.SetValue(config['reference.circular']['nth_rotate'])
self.val_rotate.SetValue(config['reference.circular']['nth_rotate_angle'])
if arrangement == 'Matrix':
self.com_arr.SetSelection(1)
self.modify_dialog_for_matrix()
self.val_x_mag.SetValue(config['reference.matrix']['step_x'])
self.val_y_angle.SetValue(config['reference.matrix']['step_y'])
self.val_columns_rad_step.SetValue(config['reference.matrix']['columns'])
self.val_nth.SetValue(config['reference.matrix']['nth_rotate'])
self.val_rotate.SetValue(config['reference.matrix']['nth_rotate_angle'])
event.Skip()

def arr_changed(self, event):
# linear layout
if self.com_arr.GetStringSelection() == u"Linear":
if self.user_units == 'mm':
self.lbl_x_mag.SetLabelText(u"step x (mm):")
self.lbl_y_angle.SetLabelText(u"step y (mm):")
self.val_x_mag.SetValue("%.3f" % self.width)
self.val_y_angle.SetValue("%.3f" % self.height)
else:
self.lbl_x_mag.SetLabelText(u"step x (mils):")
self.lbl_y_angle.SetLabelText(u"step y (mils):")
self.val_x_mag.SetValue("%.3f" % (self.width / 25.4))
self.val_y_angle.SetValue("%.3f" % (self.height / 25.4))
self.lbl_columns_rad_step.Disable()
self.val_columns_rad_step.Disable()
self.modify_dialog_for_linear()
# Matrix
if self.com_arr.GetStringSelection() == u"Matrix":
if self.user_units == 'mm':
self.lbl_x_mag.SetLabelText(u"step x (mm):")
self.lbl_y_angle.SetLabelText(u"step y (mm):")
self.val_x_mag.SetValue("%.3f" % self.width)
self.val_y_angle.SetValue("%.3f" % self.height)
else:
self.lbl_x_mag.SetLabelText(u"step x (mils):")
self.lbl_y_angle.SetLabelText(u"step y (mils):")
self.val_x_mag.SetValue("%.3f" % (self.width / 25.4))
self.val_y_angle.SetValue("%.3f" % (self.height / 25.4))
self.lbl_columns_rad_step.SetLabelText(u"Nr.columns:")
self.lbl_columns_rad_step.Enable()
self.val_columns_rad_step.Enable()
self.val_columns_rad_step.Clear()
self.val_columns_rad_step.SetValue(str(int(round(math.sqrt(len(self.list_footprints.GetSelections()))))))
self.modify_dialog_for_matrix()
# circular layout
if self.com_arr.GetStringSelection() == u"Circular":
number_of_all_footprints = len(self.list_footprints.GetSelections())
circumference = number_of_all_footprints * self.width
radius = circumference / (2 * math.pi)
angle = 360.0 / number_of_all_footprints
if self.user_units == 'mm':
self.lbl_x_mag.SetLabelText(u"radius (mm):")
self.val_x_mag.SetValue("%.3f" % radius)
else:
self.lbl_x_mag.SetLabelText(u"radius (mils):")
self.val_x_mag.SetValue("%.3f" % (radius / 25.4))
self.lbl_y_angle.SetLabelText(u"angle (deg):")
self.val_y_angle.SetValue("%.3f" % angle)
if self.user_units == 'mm':
self.lbl_columns_rad_step.SetLabelText(u"radial step (mm):")
else:
self.lbl_columns_rad_step.SetLabelText(u"radial step (mils):")
self.lbl_columns_rad_step.Enable()
self.val_columns_rad_step.SetValue("0.0")
self.val_columns_rad_step.Enable()
self.modify_dialog_for_circular()

event.Skip()

def modify_dialog_for_linear(self):
if self.user_units == 'mm':
self.lbl_x_mag.SetLabelText(u"step x (mm):")
self.lbl_y_angle.SetLabelText(u"step y (mm):")
self.val_x_mag.SetValue("%.3f" % self.width)
self.val_y_angle.SetValue("%.3f" % self.height)
else:
self.lbl_x_mag.SetLabelText(u"step x (mils):")
self.lbl_y_angle.SetLabelText(u"step y (mils):")
self.val_x_mag.SetValue("%.3f" % (self.width / 25.4))
self.val_y_angle.SetValue("%.3f" % (self.height / 25.4))
self.lbl_columns_rad_step.Disable()
self.val_columns_rad_step.Disable()

def modify_dialog_for_circular(self):
number_of_all_footprints = len(self.list_footprints.GetSelections())
circumference = number_of_all_footprints * self.width
radius = circumference / (2 * math.pi)
angle = 360.0 / number_of_all_footprints
if self.user_units == 'mm':
self.lbl_x_mag.SetLabelText(u"radius (mm):")
self.val_x_mag.SetValue("%.3f" % radius)
else:
self.lbl_x_mag.SetLabelText(u"radius (mils):")
self.val_x_mag.SetValue("%.3f" % (radius / 25.4))
self.lbl_y_angle.SetLabelText(u"angle (deg):")
self.val_y_angle.SetValue("%.3f" % angle)
if self.user_units == 'mm':
self.lbl_columns_rad_step.SetLabelText(u"radial step (mm):")
else:
self.lbl_columns_rad_step.SetLabelText(u"radial step (mils):")
self.lbl_columns_rad_step.Enable()
self.val_columns_rad_step.SetValue("0.0")
self.val_columns_rad_step.Enable()

def modify_dialog_for_matrix(self):
if self.user_units == 'mm':
self.lbl_x_mag.SetLabelText(u"step x (mm):")
self.lbl_y_angle.SetLabelText(u"step y (mm):")
self.val_x_mag.SetValue("%.3f" % self.width)
self.val_y_angle.SetValue("%.3f" % self.height)
else:
self.lbl_x_mag.SetLabelText(u"step x (mils):")
self.lbl_y_angle.SetLabelText(u"step y (mils):")
self.val_x_mag.SetValue("%.3f" % (self.width / 25.4))
self.val_y_angle.SetValue("%.3f" % (self.height / 25.4))
self.lbl_columns_rad_step.SetLabelText(u"Nr.columns:")
self.lbl_columns_rad_step.Enable()
self.val_columns_rad_step.Enable()
self.val_columns_rad_step.Clear()
self.val_columns_rad_step.SetValue(str(int(round(math.sqrt(len(self.list_footprints.GetSelections()))))))

def on_selected(self, event):
# go through the list and set/clear highlight accordingly
nr_items = self.list_footprints.GetCount()
Expand All @@ -317,6 +452,51 @@ def on_selected(self, event):
fp_clear_highlight(footprint)
pcbnew.Refresh()

def on_ok(self, event):
if self.val_x_mag.GetValue() == '':
return
if self.val_y_angle.GetValue() == '':
return
if self.val_nth.GetValue() == '':
return
if self.val_rotate.GetValue() == '':
return
if self.com_arr.GetStringSelection() != 'Linear' and self.val_columns_rad_step.GetValue() == '':
return

# clear highlights
# TODO
# save the config
# if existing, append
if os.path.exists(self.config_filename):
config = configparser.ConfigParser()
config.read(self.config_filename)
else:
config = configparser.ConfigParser()
config['reference'] = {'arrangement': self.com_arr.GetStringSelection()}
arrangement = config['reference']['arrangement']
if arrangement == 'Linear':
config['reference.linear'] = {'step_x': self.val_x_mag.GetValue(),
'step_y': self.val_y_angle.GetValue(),
'nth_rotate': self.val_nth.GetValue(),
'nth_rotate_angle': self.val_rotate.GetValue()}
if arrangement == 'Circular':
config['reference.circular'] = {'angle': self.val_y_angle.GetValue(),
'radial_step': self.val_columns_rad_step.GetValue(),
'radius': self.val_x_mag.GetValue(),
'nth_rotate': self.val_nth.GetValue(),
'nth_rotate_angle': self.val_rotate.GetValue()}
if arrangement == 'Matrix':
config['reference.matrix'] = {'step_x': self.val_x_mag.GetValue(),
'step_y': self.val_y_angle.GetValue(),
'columns': self.val_columns_rad_step.GetValue(),
'nth_rotate': self.val_nth.GetValue(),
'nth_rotate_angle': self.val_rotate.GetValue()}
with open(self.config_filename, 'w') as configfile:
config.write(configfile)

event.Skip()


class InitialDialog(InitialDialogGUI):
BY_REFERENCE = 1025
Expand Down
2 changes: 2 additions & 0 deletions place_by_reference_GUI.fbp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<event name="OnShow">on_show</event>
<object class="wxBoxSizer" expanded="1">
<property name="minimum_size"></property>
<property name="name">bSizer3</property>
Expand Down Expand Up @@ -1477,6 +1478,7 @@
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<event name="OnButtonClick">on_ok</event>
</object>
</object>
</object>
Expand Down
1 change: 1 addition & 0 deletions place_by_sheet_GUI.fbp
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,7 @@
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<event name="OnButtonClick">on_ok</event>
</object>
</object>
</object>
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.3
2.1.0

0 comments on commit b2cfe6b

Please sign in to comment.