From e5dd2945760a31ffb3ce65c43db86e8a24672638 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Fri, 19 May 2023 18:18:38 +0300 Subject: [PATCH] - Film Tool: fixed the export to PNG to be done at correct DPI --- CHANGELOG.md | 1 + appPlugins/ToolFilm.py | 47 ++++++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b04c43e..b040e816 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ CHANGELOG for FlatCAM Evo beta - the selection shapes are now moved from Move Plugin to AppMain - Fiducials Plugin: fixed errors due of changes in the Shapely module - Fiducials Plugin: fixed an error where in Basic mode the soldermask opening is added incorrectly +- Film Tool: fixed the export to PNG to be done at correct DPI 17.05.2023 diff --git a/appPlugins/ToolFilm.py b/appPlugins/ToolFilm.py index 39b3d469..57455c13 100644 --- a/appPlugins/ToolFilm.py +++ b/appPlugins/ToolFilm.py @@ -655,7 +655,8 @@ class Film(AppTool): color = obj.obj_options['tools_film_color'] transparency_level = opacity_val - def make_negative_film(color, transparency_level, scale_factor_x, scale_factor_y, use_convex_hull, rounded_box): + def make_negative_film(color, transparency_level, scale_factor_x, scale_factor_y, use_convex_hull, + rounded_box, scale_type): self.app.log.debug("FilmTool.export_negative_handler().make_negative_film()") self.screen_dpi = self.app.qapp.screens()[0].logicalDotsPerInch() @@ -664,8 +665,13 @@ class Film(AppTool): dpi_rate = new_png_dpi / self.screen_dpi if dpi_rate != 1 and ftype == 'png': - scale_factor_x += dpi_rate - scale_factor_y += dpi_rate + if scale_factor_x is None: + scale_factor_x = 1 + if scale_factor_y is None: + scale_factor_y = 1 + scale_factor_x *= dpi_rate + scale_factor_y *= dpi_rate + scale_type = 1 transformed_box_geo = self.transform_geometry(box_obj, scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y, @@ -689,10 +695,12 @@ class Film(AppTool): doc_final = self.create_negative_svg(svg_geo=exported_svg, box_bounds=bounds, r_box=rounded_box, box_geo=transformed_box_geo, c_hull=use_convex_hull, margin=boundary, color=color, opacity=transparency_level, svg_units=svg_units) - + # with open("d://a.svg", 'w') as f: + # f.write(doc_final) obj_bounds = obj.bounds() ret = self.write_output_file(content2save=doc_final, filename=filename, file_type=ftype, p_size=p_size, - orientation=orientation, source_bounds=obj_bounds, box_bounds=bounds) + orientation=orientation, source_bounds=obj_bounds, box_bounds=bounds, + dpi=self.screen_dpi) if ret == 'fail': return 'fail' @@ -708,7 +716,8 @@ class Film(AppTool): try: make_negative_film(color=color, transparency_level=transparency_level, scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y, - use_convex_hull=use_convex_hull, rounded_box=rounded_box) + use_convex_hull=use_convex_hull, rounded_box=rounded_box, + scale_type=scale_type) except Exception as e: self.app.log.error("export_negative_handler() process -> %s" % str(e)) return @@ -716,7 +725,7 @@ class Film(AppTool): self.app.worker_task.emit({'fcn': job_thread_film, 'params': []}) else: make_negative_film(scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y, - use_convex_hull=use_convex_hull, rounded_box=rounded_box) + use_convex_hull=use_convex_hull, rounded_box=rounded_box, scale_type=scale_type) def create_negative_svg(self, svg_geo, box_bounds, r_box, box_geo, c_hull, margin, color, opacity, svg_units): # Change the attributes of the exported SVG @@ -882,7 +891,7 @@ class Film(AppTool): color = obj.obj_options['tools_film_color'] transparency_level = opacity_val - def make_positive_film(color, transparency_level, scale_factor_x, scale_factor_y): + def make_positive_film(color, transparency_level, scale_factor_x, scale_factor_y, scale_type): self.app.log.debug("FilmTool.export_positive_handler().make_positive_film()") self.screen_dpi = self.app.qapp.screens()[0].logicalDotsPerInch() @@ -891,8 +900,13 @@ class Film(AppTool): dpi_rate = new_png_dpi / self.screen_dpi if dpi_rate != 1 and ftype == 'png': - scale_factor_x += dpi_rate - scale_factor_y += dpi_rate + if scale_factor_x is None: + scale_factor_x = 1 + if scale_factor_y is None: + scale_factor_y = 1 + scale_factor_x *= dpi_rate + scale_factor_y *= dpi_rate + scale_type = 1 transformed_box_geo = self.transform_geometry(box_obj, scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y, @@ -920,7 +934,8 @@ class Film(AppTool): obj_bounds = obj.bounds() ret = self.write_output_file(content2save=doc_final, filename=filename, file_type=ftype, p_size=p_size, - orientation=orientation, source_bounds=obj_bounds, box_bounds=bounds) + orientation=orientation, source_bounds=obj_bounds, box_bounds=bounds, + dpi=self.screen_dpi) if ret == 'fail': return 'fail' @@ -935,7 +950,8 @@ class Film(AppTool): with self.app.proc_container.new(_("Working...")): try: make_positive_film(color=color, transparency_level=transparency_level, - scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y) + scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y, + scale_type=scale_type) except Exception as e: self.app.log.error("export_positive_handler() process -> %s" % str(e)) return @@ -943,7 +959,7 @@ class Film(AppTool): self.app.worker_task.emit({'fcn': job_thread_film, 'params': []}) else: make_positive_film(color=color, transparency_level=transparency_level, - scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y) + scale_factor_x=scale_factor_x, scale_factor_y=scale_factor_y, scale_type=scale_type) def create_positive_svg(self, svg_geo, box_bounds, margin, color, opacity, svg_units): # Change the attributes of the exported SVG @@ -993,7 +1009,8 @@ class Film(AppTool): doc = parse_xml_string(svg_elem) return doc.toprettyxml() - def write_output_file(self, content2save, filename, file_type, p_size, orientation, source_bounds, box_bounds): + def write_output_file(self, content2save, filename, file_type, p_size, orientation, source_bounds, box_bounds, + dpi=72): p_msg = '[ERROR_NOTCL] %s' % _("Permission denied, saving not possible.\n" "Most likely another app is holding the file open and not accessible.") if file_type == 'svg': @@ -1007,7 +1024,7 @@ class Film(AppTool): try: doc_final = StringIO(content2save) drawing = svg2rlg(doc_final) - renderPM.drawToFile(drawing, filename, 'PNG') + renderPM.drawToFile(drawing, fn=filename, fmt='PNG', dpi=dpi) except PermissionError: self.app.inform.emit(p_msg) return 'fail'