- major change: from now on the only dimensional units available are those from ISO METRIC system

This commit is contained in:
Marius Stanciu
2023-04-08 12:29:45 +03:00
parent b446ded9ab
commit faf41d7bca
7 changed files with 8 additions and 357 deletions

View File

@@ -619,11 +619,7 @@ class App(QtCore.QObject):
self.app_units = self.options["units"]
self.default_units = self.defaults["units"]
if self.app_units == 'MM':
self.decimals = int(self.options['decimals_metric'])
else:
self.decimals = int(self.options['decimals_inch'])
self.decimals = int(self.options['decimals_metric'])
if self.options["global_theme"] == 'default':
self.resource_location = 'assets/resources'
@@ -803,8 +799,6 @@ class App(QtCore.QObject):
self.ui = MainGUI(self)
# ########################
# set FlatCAM units in the Status bar
self.set_screen_units(self.app_units)
# decide if to show or hide the Notebook side of the screen at startup
if self.options["global_project_at_startup"] is True:
@@ -1911,7 +1905,6 @@ class App(QtCore.QObject):
self.ui.menueditjump.triggered.connect(self.on_jump_to)
self.ui.menueditlocate.triggered.connect(lambda: self.on_locate(obj=self.collection.get_active()))
self.ui.menuedittoggleunits.triggered.connect(self.on_toggle_units_click)
self.ui.menueditselectall.triggered.connect(self.on_selectall)
self.ui.menueditpreferences.triggered.connect(self.on_preferences)
@@ -4214,310 +4207,6 @@ class App(QtCore.QObject):
"""
self.preferencesUiManager.defaults_write_form_field(field=field)
if field == "units":
self.set_screen_units(self.app_units)
def set_screen_units(self, units):
"""
Set the FlatCAM units on the status bar.
:param units: the new measuring units to be displayed in FlatCAM's status bar.
:return: None
"""
self.ui.units_label.setText("[" + units.lower() + "]")
def on_toggle_units_click(self):
try:
new_units, factor = self.on_toggle_units()
except TypeError:
# hen the self.on_toggle_units() return only one value (maybe None) it means something went wrong,
# it will end up in this exception, and we should return
return
if self.ui.plot_tab_area.currentWidget().objectName() == "preferences_tab":
if factor != 1: # means we had a unit change in the rest of the app
if self.app_units != new_units:
pref_factor = 25.4 if new_units == 'MM' else 1 / 25.4
self.scale_preferences(pref_factor, new_units)
self.options["units"] = new_units
# update th new units in the preferences storage
self.app_units = new_units
def scale_preferences(self, sfactor, new_units):
self.preferencesUiManager.defaults_read_form()
# update the defaults from form, some may assume that the conversion is enough, and it's not
self.on_defaults2options()
# Keys in self.options for which to scale their values
dimensions = [
# Global
'global_gridx', 'global_gridy', 'global_snap_max', "global_tolerance",
'global_tpdf_bmargin', 'global_tpdf_tmargin', 'global_tpdf_rmargin', 'global_tpdf_lmargin',
# Gerber Object
'gerber_noncoppermargin', 'gerber_bboxmargin',
# Gerber Editor
"gerber_editor_newsize", "gerber_editor_lin_pitch", "gerber_editor_buff_f",
"gerber_editor_newdim", "gerber_editor_ma_low", "gerber_editor_ma_high",
# Excellon Object
'excellon_drill_tooldia', 'excellon_slot_tooldia',
# Excellon Editor
"excellon_editor_newdia", "excellon_editor_lin_pitch", "excellon_editor_slot_lin_pitch",
"excellon_editor_slot_length",
# Geometry Object
'tools_mill_cutz', "tools_mill_depthperpass", 'tools_mill_travelz', 'tools_mill_feedrate',
'tools_mill_feedrate_rapid', "tools_mill_toolchangez", "tools_mill_feedrate_z",
"tools_mill_toolchangexy", 'tools_mill_tooldia', 'tools_mill_endz', 'tools_mill_endxy',
"tools_mill_extracut_length", "tools_mill_z_pdepth",
"tools_mill_feedrate_probe", "tools_mill_startz", "geometry_segx", "geometry_segy", "tools_mill_area_overz",
# CNCJob Object
'cncjob_tooldia', 'cncjob_bed_max_x', 'cncjob_bed_max_y', 'cncjob_bed_offset_x', 'cncjob_bed_offset_y',
'cncjob_bed_skew_x', 'cncjob_bed_skew_y',
# AutoLevelling Plugin
"tools_al_travelz", "tools_al_probe_depth", "tools_al_grbl_jog_step",
"tools_al_grbl_jog_fr", "tools_al_grbl_travelz",
# Isolation Plugin
"tools_iso_tool_cutz",
# Drilling Plugin
'tools_drill_cutz', 'tools_drill_depthperpass', 'tools_drill_travelz', 'tools_drill_endz',
'tools_drill_endxy', 'tools_drill_feedrate_z', 'tools_drill_toolchangez', "tools_drill_drill_overlap",
'tools_drill_offset', "tools_drill_toolchangexy", "tools_drill_startz", 'tools_drill_feedrate_rapid',
"tools_drill_feedrate_probe", "tools_drill_z_pdepth", "tools_drill_area_overz",
# NCC Plugin
"tools_ncc_tools", "tools_ncc_margin", "tools_ncc_offset_value", "tools_ncc_cutz", "tools_ncc_tipdia",
"tools_ncc_newdia",
# Cutout Plugin
"tools_cutout_tooldia", 'tools_cutout_margin', "tools_cutout_z", "tools_cutout_depthperpass",
'tools_cutout_gapsize', 'tools_cutout_gap_depth', 'tools_cutout_mb_dia', 'tools_cutout_mb_spacing',
# Paint Plugin
"tools_paint_tooldia", 'tools_paint_offset', "tools_paint_cutz", "tools_paint_tipdia", "tools_paint_newdia",
# 2Sided Plugin
"tools_2sided_drilldia",
# Film Plugin
"tools_film_boundary", "tools_film_scale_stroke",
# Panel Plugin
"tools_panelize_spacing_columns", "tools_panelize_spacing_rows", "tools_panelize_constrainx",
"tools_panelize_constrainy",
# Calculators Plugin
"tools_calc_vshape_tip_dia", "tools_calc_vshape_cut_z",
# Transform Plugin
"tools_transform_ref_point", "tools_transform_offset_x", "tools_transform_offset_y",
"tools_transform_buffer_dis",
# SolderPaste Plugin
"tools_solderpaste_tools", "tools_solderpaste_new", "tools_solderpaste_z_start",
"tools_solderpaste_z_dispense", "tools_solderpaste_z_stop", "tools_solderpaste_z_travel",
"tools_solderpaste_z_toolchange", "tools_solderpaste_xy_toolchange", "tools_solderpaste_frxy",
"tools_solderpaste_frz", "tools_solderpaste_frz_dispense",
# Markers Plugin
"tools_markers_thickness", "tools_markers_length", "tools_markers_offset_x", "tools_markers_offset_y",
# Check Rules Plugin
"tools_cr_trace_size_val", "tools_cr_c2c_val", "tools_cr_c2o_val", "tools_cr_s2s_val", "tools_cr_s2sm_val",
"tools_cr_s2o_val", "tools_cr_sm2sm_val", "tools_cr_ri_val", "tools_cr_h2h_val", "tools_cr_dh_val",
# QRCode Plugin
"tools_qrcode_border_size",
# Copper Thieving Plugin
"tools_copper_thieving_clearance", "tools_copper_thieving_margin",
"tools_copper_thieving_dots_dia", "tools_copper_thieving_dots_spacing",
"tools_copper_thieving_squares_size", "tools_copper_thieving_squares_spacing",
"tools_copper_thieving_lines_size", "tools_copper_thieving_lines_spacing",
"tools_copper_thieving_rb_margin", "tools_copper_thieving_rb_thickness",
"tools_copper_thieving_mask_clearance",
# Fiducials Plugin
"tools_fiducials_dia", "tools_fiducials_margin", "tools_fiducials_line_thickness",
# Drills Extraction Plugin
"tools_extract_hole_fixed_dia", "tools_extract_circular_ring", "tools_extract_oblong_ring",
"tools_extract_square_ring", "tools_extract_rectangular_ring", "tools_extract_others_ring",
# Punch Gerber Plugin
"tools_punch_hole_fixed_dia", "tools_punch_circular_ring", "tools_punch_oblong_ring",
"tools_punch_square_ring", "tools_punch_rectangular_ring", "tools_punch_others_ring",
# Invert Gerber Plugin
"tools_invert_margin",
]
for dim in dimensions:
if dim in ['tools_mill_tooldia', 'tools_ncc_tools', 'tools_solderpaste_tools', 'tools_iso_tooldia',
'tools_paint_tooldia', 'tools_transform_ref_point', 'tools_cal_toolchange_xy',
'gerber_editor_newdim', 'tools_drill_toolchangexy', 'tools_drill_endxy',
'tools_mill_toolchangexy', 'tools_mill_endxy', 'tools_solderpaste_xy_toolchange']:
if not self.options[dim] or self.options[dim] == '':
continue
if isinstance(self.options[dim], str):
try:
tools_diameters = eval(self.options[dim])
except Exception as e:
self.log.error("App.on_toggle_units().scale_defaults() lists --> %s" % str(e))
continue
elif isinstance(self.options[dim], (float, int)):
tools_diameters = [self.options[dim]]
else:
tools_diameters = list(self.options[dim])
if isinstance(tools_diameters, (tuple, list)):
pass
elif isinstance(tools_diameters, (int, float)):
tools_diameters = [self.options[dim]]
else:
continue
td_len = len(tools_diameters)
conv_list = []
for t in range(td_len):
conv_list.append(self.dec_format(float(tools_diameters[t]) * sfactor, self.decimals))
self.options[dim] = conv_list
elif dim in ['global_gridx', 'global_gridy']:
# format the number of decimals to the one specified in self.decimals
try:
val = float(self.options[dim]) * sfactor
except Exception as e:
self.log.error('App.on_toggle_units().scale_defaults() grids --> %s' % str(e))
continue
self.options[dim] = self.dec_format(val, self.decimals)
else:
# the number of decimals for the rest is kept unchanged
if self.options[dim]:
try:
val = float(self.options[dim]) * sfactor
except Exception as e:
self.log.error(
'App.on_toggle_units().scale_defaults() standard --> Value: %s %s' % (str(dim), str(e))
)
continue
self.options[dim] = self.dec_format(val, self.decimals)
self.preferencesUiManager.defaults_write_form(fl_units=new_units)
def on_toggle_units(self):
"""
Callback for the Units radio-button change in the Preferences tab.
Changes the application's default units adn for the project too.
If changing the project's units, the change propagates to all
the objects in the project.
:return: The new application units. String: "IN" or "MM" with caps lock
"""
if self.toggle_units_ignore:
return
new_units = 'IN' if self.app_units.upper() == 'MM' else 'MM'
# we can't change the units while inside the Editors
if self.call_source in ['geo_editor', 'grb_editor', 'exc_editor']:
msg = _("Units cannot be changed while the editor is active.")
self.inform.emit("[WARNING_NOTCL] %s" % msg)
return
# ##############################################################################################################
# Changing project units. Ask the user.
# ##############################################################################################################
msgbox = FCMessageBox(parent=self.ui)
title = _("Toggle Units")
txt = _("Changing the units of the project\n"
"will scale all objects.\n\n"
"Do you want to continue?")
msgbox.setWindowTitle(title) # taskbar still shows it
msgbox.setWindowIcon(QtGui.QIcon(self.resource_location + '/app128.png'))
msgbox.setText('<b>%s</b>' % title)
msgbox.setInformativeText(txt)
msgbox.setIconPixmap(QtGui.QPixmap(self.resource_location + '/toggle_units32.png'))
bt_ok = msgbox.addButton(_('Ok'), QtWidgets.QMessageBox.ButtonRole.AcceptRole)
msgbox.addButton(_('Cancel'), QtWidgets.QMessageBox.ButtonRole.RejectRole)
msgbox.setDefaultButton(bt_ok)
msgbox.exec()
response = msgbox.clickedButton()
if response == bt_ok:
new_units = "IN" if self.app_units.upper() == "MM" else "MM"
# The scaling factor depending on choice of units.
factor = 25.4 if new_units == 'MM' else 1 / 25.4
# update the Application objects with the new units
for obj in self.collection.get_list():
obj.convert_units(new_units)
# make that the properties stored in the object are also updated
self.app_obj.object_changed.emit(obj)
# rebuild the object UI
obj.build_ui()
# update workspace if active
if self.options['global_workspace'] is True:
self.plotcanvas.draw_workspace(pagesize=self.options['global_workspaceT'])
# adjust the grid values on the main toolbar
val_x = round(float(self.options['global_gridx']) * factor, self.decimals)
val_y = val_x if self.ui.grid_gap_link_cb.isChecked() else \
round(float(self.options['global_gridy']) * factor, self.decimals)
# update Object UI forms
current = self.collection.get_active()
if current is not None:
# the transfer of converted values to the UI form for Geometry is done local in the AppObjectTemplate.py
if not isinstance(current, GeometryObject):
current.to_form()
# plot again all objects
self.plot_all()
self.set_screen_units(new_units)
# flag for the app that we changed the object properties, and it should save the project
self.should_we_save = True
self.inform.emit('[success] %s: %s' % (_("Converted units to"), new_units))
else:
factor = 1
# store the grid values, so they are not changed in the next step
val_x = float(self.options['global_gridx'])
val_y = float(self.options['global_gridy'])
self.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled."))
self.preferencesUiManager.defaults_read_form()
# update the Grid snap values
self.options['global_gridx'] = val_x
self.options['global_gridy'] = val_y
self.ui.grid_gap_x_entry.set_value(val_x, decimals=self.decimals)
self.ui.grid_gap_y_entry.set_value(val_y, decimals=self.decimals)
return new_units, factor
def on_deselect_all(self):
self.collection.set_all_inactive()
self.delete_selection_shape()