From 16a013266ba3f9406dccafe8ad7423de732896a4 Mon Sep 17 00:00:00 2001 From: Ali Khalil Date: Fri, 1 Apr 2022 22:18:10 +0300 Subject: [PATCH 1/5] TclCommandCutout now uses CutOut.rect_cutout_handler for gaps --- .gitignore | 5 +++ tclCommands/TclCommandCutout.py | 74 ++++++++++++++------------------- 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 164ed709..f957e1b3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,8 @@ tests/tmp/ build/ /venv/ + +# General for macOS +.DS_Store +.AppleDouble +.LSOverride diff --git a/tclCommands/TclCommandCutout.py b/tclCommands/TclCommandCutout.py index 362d6382..e2e84ce4 100644 --- a/tclCommands/TclCommandCutout.py +++ b/tclCommands/TclCommandCutout.py @@ -1,3 +1,4 @@ +from matplotlib.colors import LinearSegmentedColormap from tclCommands.TclCommand import TclCommand import collections @@ -6,6 +7,8 @@ from copy import deepcopy from shapely.ops import unary_union from shapely.geometry import LineString +from shapely.geometry import box +from shapely.ops import linemerge log = logging.getLogger('base') @@ -50,7 +53,7 @@ class TclCommandCutout(TclCommand): ('dia', 'Tool diameter.'), ('margin', 'Margin over bounds.'), ('gapsize', 'Size of gap.'), - ('gaps', "Type of gaps. Can be: 'tb' = top-bottom, 'lr' = left-right and '4' = one each side."), + ('gaps', "Type of gaps. Can be: 'None' = no-tabs, 'TB' = top-bottom, 'LR' = left-right and '4' = one each side."), ('outname', 'Name of the object to create.') ]), 'examples': ['cutout cut_object -dia 1.2 -margin 0.1 -gapsize 1 -gaps "tb" -outname cutout_geo'] @@ -82,9 +85,9 @@ class TclCommandCutout(TclCommand): dia_par = float(self.app.options["tools_cutout_tooldia"]) if 'gaps' in args: - if args['gaps'] not in ["tb", "lr", "4", 4]: + if args['gaps'] not in ["None", "TB", "LR", "2TB", "2LR", "4", 4, "8", 8]: self.raise_tcl_error( - "Incorrect -gaps values. Can be only a string from: 'tb', 'lr' and '4'.") + "Incorrect -gaps values. Can be only a string from: 'None', 'TB', 'LR', '2TB', '2LR', '4', and '8'.") return "fail" gaps_par = str(args['gaps']) else: @@ -109,49 +112,34 @@ class TclCommandCutout(TclCommand): def geo_init_me(geo_obj, app_obj): geo_obj.multigeo = True + solid_geo = [] - margin = margin_par + dia_par / 2 - gap_size = dia_par + gapsize_par + gapsize = gapsize_par + dia_par - minx, miny, maxx, maxy = obj.bounds() - minx -= margin - maxx += margin - miny -= margin - maxy += margin - midx = 0.5 * (minx + maxx) - midy = 0.5 * (miny + maxy) - hgap = 0.5 * gap_size - pts = [[midx - hgap, maxy], - [minx, maxy], - [minx, midy + hgap], - [minx, midy - hgap], - [minx, miny], - [midx - hgap, miny], - [midx + hgap, miny], - [maxx, miny], - [maxx, midy - hgap], - [maxx, midy + hgap], - [maxx, maxy], - [midx + hgap, maxy]] + xmin, ymin, xmax, ymax = obj.bounds() + geo = box(xmin, ymin, xmax, ymax) - cases = { - "tb": [ - [pts[0], pts[1], pts[4], pts[5]], - [pts[6], pts[7], pts[10], pts[11]] - ], - "lr": [ - [pts[9], pts[10], pts[1], pts[2]], - [pts[3], pts[4], pts[7], pts[8]] - ], - "4": [ - [pts[0], pts[1], pts[2]], - [pts[3], pts[4], pts[5]], - [pts[6], pts[7], pts[8]], - [pts[9], pts[10], pts[11]] - ] - } - cuts = cases[gaps_par] - geo_obj.solid_geometry = unary_union([LineString(segment) for segment in cuts]) + if obj.kind == 'gerber': + if margin_par >= 0: + work_margin = margin_par + abs(dia_par / 2) + else: + work_margin = margin_par - abs(dia_par / 2) + geo = geo.buffer(work_margin) + + solid_geo = self.app.cutout_tool.rect_cutout_handler(geo, dia_par, gaps_par, gapsize, margin_par, xmin, ymin, xmax, ymax) + + if not solid_geo: + self.app.log.debug("TclCommandCutout.geo_init_me() -> Empty solid geometry.") + app_obj.inform.emit('[ERROR_NOTCL] %s' % _("Failed.")) + return "fail" + + try: + solid_geo = linemerge(solid_geo) + except Exception: + # there are not lines but polygon + pass + + geo_obj.solid_geometry = solid_geo if not geo_obj.solid_geometry: app_obj.log("TclCommandCutout.execute(). No geometry after cutout.") From 86cba9b4a12722d099e2cd670f42bc64b13683bf Mon Sep 17 00:00:00 2001 From: Ali Khalil Date: Sun, 3 Apr 2022 06:54:24 +0300 Subject: [PATCH 2/5] TclCommandGeoCutout updated with gap options in uppercase to match ToolCutOut and TclCommandCutout. --- tclCommands/TclCommandGeoCutout.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tclCommands/TclCommandGeoCutout.py b/tclCommands/TclCommandGeoCutout.py index a67aef70..6d4786a9 100644 --- a/tclCommands/TclCommandGeoCutout.py +++ b/tclCommands/TclCommandGeoCutout.py @@ -59,8 +59,8 @@ class TclCommandGeoCutout(TclCommandSignaled): ('dia', 'Tool diameter.'), ('margin', 'Margin over bounds.'), ('gapsize', 'size of gap.'), - ('gaps', "type of gaps. Can be: 'tb' = top-bottom, 'lr' = left-right, '2tb' = 2top-2bottom, " - "'2lr' = 2left-2right, '4' = 4 cuts, '8' = 8 cuts"), + ('gaps', "type of gaps. Can be: 'None' = no-gaps, 'TB' = top-bottom, 'LR' = left-right, '2TB' = 2top-2bottom, " + "'2LR' = 2left-2right, '4' = 4 cuts, '8' = 8 cuts"), ('outname', 'Name of the resulting Geometry object.'), ]), 'examples': [" #isolate margin for example from Fritzing arduino shield or any svg etc\n" + @@ -181,9 +181,10 @@ class TclCommandGeoCutout(TclCommandSignaled): "[WARNING] %s" % _("Tool Diameter is zero value. Change it to a positive real number.")) return "fail" - if gaps not in ['lr', 'tb', '2lr', '2tb', '4', '8', 4, 8]: - self.app.inform.emit( - "[WARNING] %s" % _("Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8.")) + if gaps not in ['None', 'LR', 'TB', '2LR', '2TB', '4', '8']: + self.app.inform.emit('[WARNING] %s' % + _("Gaps value can be only one of: 'None', 'LR', 'TB', '2LR', '2TB', 4 or 8.\n" + "Fill in a correct value and retry.")) return "fail" # Get min and max data for each object as we just cut rectangles across X or Y @@ -221,7 +222,7 @@ class TclCommandGeoCutout(TclCommandSignaled): geo_obj.multigeo = True geo = geo_to_cutout - if gaps_u == 8 or gaps_u == '2lr': + if gaps_u == 8 or gaps_u == '2LR': geo = substract_rectangle_geo(geo, xmin - gapsize, # botleft_x py - gapsize + lenghty / 4, # botleft_y @@ -233,7 +234,7 @@ class TclCommandGeoCutout(TclCommandSignaled): xmax + gapsize, py + gapsize - lenghty / 4) - if gaps_u == 8 or gaps_u == '2tb': + if gaps_u == 8 or gaps_u == '2TB': geo = substract_rectangle_geo(geo, px - gapsize + lenghtx / 4, ymin - gapsize, @@ -245,14 +246,14 @@ class TclCommandGeoCutout(TclCommandSignaled): px + gapsize - lenghtx / 4, ymax + gapsize) - if gaps_u == 4 or gaps_u == 'lr': + if gaps_u == 4 or gaps_u == 'LR': geo = substract_rectangle_geo(geo, xmin - gapsize, py - gapsize, xmax + gapsize, py + gapsize) - if gaps_u == 4 or gaps_u == 'tb': + if gaps_u == 4 or gaps_u == 'TB': geo = substract_rectangle_geo(geo, px - gapsize, ymin - gapsize, From 4bbe3b4c493a7e421defb2c0cda5f1e0caa3f8c9 Mon Sep 17 00:00:00 2001 From: Ali Khalil Date: Sun, 3 Apr 2022 07:12:55 +0300 Subject: [PATCH 3/5] Minor change. Error tag updated --- tclCommands/TclCommandCutout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tclCommands/TclCommandCutout.py b/tclCommands/TclCommandCutout.py index e2e84ce4..1af91f46 100644 --- a/tclCommands/TclCommandCutout.py +++ b/tclCommands/TclCommandCutout.py @@ -130,7 +130,7 @@ class TclCommandCutout(TclCommand): if not solid_geo: self.app.log.debug("TclCommandCutout.geo_init_me() -> Empty solid geometry.") - app_obj.inform.emit('[ERROR_NOTCL] %s' % _("Failed.")) + app_obj.inform.emit('[ERROR] %s' % _("Failed.")) return "fail" try: From d0fe92e5d131fe4696b42800a36b3edaf56f5995 Mon Sep 17 00:00:00 2001 From: Ali Khalil Date: Wed, 6 Apr 2022 06:41:24 +0300 Subject: [PATCH 4/5] TCL cutout command new switch -type accepting rect and any --- tclCommands/TclCommandCutout.py | 71 +++++++++++++++++++----------- tclCommands/TclCommandGeoCutout.py | 16 +++---- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/tclCommands/TclCommandCutout.py b/tclCommands/TclCommandCutout.py index 1af91f46..e4471501 100644 --- a/tclCommands/TclCommandCutout.py +++ b/tclCommands/TclCommandCutout.py @@ -1,31 +1,32 @@ -from matplotlib.colors import LinearSegmentedColormap +# from matplotlib.colors import LinearSegmentedColormap from tclCommands.TclCommand import TclCommand import collections import logging from copy import deepcopy +import gettext -from shapely.ops import unary_union -from shapely.geometry import LineString -from shapely.geometry import box +from shapely.geometry import LineString, box from shapely.ops import linemerge +from camlib import flatten_shapely_geometry log = logging.getLogger('base') class TclCommandCutout(TclCommand): """ - Tcl shell command to create a board cutout geometry. Rectangular shape only. + Tcl shell command to create a board cutout geometry. example: + cutout cut_object -dia 1.2 -margin 0.1 -gapsize 1 -gaps "tb" -outname cutout_geo -type rect """ # List of all command aliases, to be able use old # names for backward compatibility (add_poly, add_polygon) - aliases = ['cutout'] + aliases = ['cutout', 'geocutout'] - description = '%s %s' % ("--", "Creates board cutout from an object (Gerber or Geometry) with a rectangular shape.") + description = '%s %s' % ("--", "Creates board cutout from an object (Gerber or Geometry).") # Dictionary of types from Tcl command, needs to be ordered arg_names = collections.OrderedDict([ @@ -35,28 +36,30 @@ class TclCommandCutout(TclCommand): # Dictionary of types from Tcl command, needs to be ordered, # this is for options like -optionname value option_types = collections.OrderedDict([ + ('type', str), ('dia', float), ('margin', float), ('gapsize', float), ('gaps', str), ('outname', str) - ]) + ]) # array of mandatory options for current Tcl command: required = {'name','outname'} required = ['name'] # structured help for current command, args needs to be ordered help = { - 'main': 'Creates board cutout from an object (Gerber or Geometry) with a rectangular shape.', + 'main': 'Creates board cutout from an object (Gerber or Geometry).', 'args': collections.OrderedDict([ ('name', 'Name of the object.'), + ('type', "Type of cutout. Can be: 'rect' or 'any'. default: any"), ('dia', 'Tool diameter.'), ('margin', 'Margin over bounds.'), ('gapsize', 'Size of gap.'), - ('gaps', "Type of gaps. Can be: 'None' = no-tabs, 'TB' = top-bottom, 'LR' = left-right and '4' = one each side."), + ('gaps', "Type of gaps. Can be (case-insensitive): 'None' = no-tabs, 'TB' = top-bottom, 'LR' = left-right and '4' = one each side."), ('outname', 'Name of the object to create.') ]), - 'examples': ['cutout cut_object -dia 1.2 -margin 0.1 -gapsize 1 -gaps "tb" -outname cutout_geo'] + 'examples': ['cutout cut_object -dia 1.2 -margin 0.1 -gapsize 1 -gaps "tb" -outname cutout_geo -type rect'] } def execute(self, args, unnamed_args): @@ -70,7 +73,7 @@ class TclCommandCutout(TclCommand): if 'name' in args: name = args['name'] else: - self.app.inform.emit( + self.app.log.warning( "[WARNING] The name of the object for which cutout is done is missing. Add it and retry.") return "fail" @@ -85,9 +88,9 @@ class TclCommandCutout(TclCommand): dia_par = float(self.app.options["tools_cutout_tooldia"]) if 'gaps' in args: - if args['gaps'] not in ["None", "TB", "LR", "2TB", "2LR", "4", 4, "8", 8]: + if str(args['gaps']).lower() not in ["none", "tb", "lr", "2tb", "2lr", "4", "8"]: self.raise_tcl_error( - "Incorrect -gaps values. Can be only a string from: 'None', 'TB', 'LR', '2TB', '2LR', '4', and '8'.") + "Incorrect -gaps values. Can be only a string from: 'none', 'tb', 'lr', '2tb', '2lr', '4', and '8'.") return "fail" gaps_par = str(args['gaps']) else: @@ -103,8 +106,17 @@ class TclCommandCutout(TclCommand): else: outname = name + "_cutout" + if 'type' in args: + if args['type'] not in ['rect', 'any']: + self.raise_tcl_error(_("Incorrect -type value. Can only an be: 'rect', 'any'. default: any")) + return 'fail' + type_par = args['type'] + else: + self.app.log.info(_("No type value specified. Using default: any.")) + type_par = 'any' + try: - obj = self.app.collection.get_by_name(str(name)) + cutout_obj = self.app.collection.get_by_name(str(name)) except Exception as e: self.app.log.error("TclCommandCutout.execute(). Missing object: --> %s" % str(e)) self.app.log.debug("Could not retrieve object: %s" % name) @@ -116,21 +128,28 @@ class TclCommandCutout(TclCommand): gapsize = gapsize_par + dia_par - xmin, ymin, xmax, ymax = obj.bounds() - geo = box(xmin, ymin, xmax, ymax) + if type_par == 'rect': + xmin, ymin, xmax, ymax = cutout_obj.bounds() + cutout_geom = flatten_shapely_geometry(box(xmin, ymin, xmax, ymax)) + else: + cutout_geom = flatten_shapely_geometry(cutout_obj.solid_geometry) - if obj.kind == 'gerber': - if margin_par >= 0: - work_margin = margin_par + abs(dia_par / 2) - else: - work_margin = margin_par - abs(dia_par / 2) - geo = geo.buffer(work_margin) + for geom_struct in cutout_geom: + if cutout_obj.kind == 'gerber': + if margin_par >= 0: + geom_struct = (geom_struct.buffer(margin_par + abs(dia_par / 2))).exterior + else: + geom_struct_buff = geom_struct.buffer(-margin_par + abs(dia_par / 2)) + geom_struct = geom_struct_buff.interiors - solid_geo = self.app.cutout_tool.rect_cutout_handler(geo, dia_par, gaps_par, gapsize, margin_par, xmin, ymin, xmax, ymax) + if type_par == 'rect': + solid_geo = self.app.cutout_tool.rect_cutout_handler(geom_struct, dia_par, gaps_par, gapsize, margin_par, xmin, ymin, xmax, ymax) + else: + solid_geo, r_geo = self.app.cutout_tool.any_cutout_handler(geom_struct, dia_par, gaps_par, gapsize, margin_par) if not solid_geo: self.app.log.debug("TclCommandCutout.geo_init_me() -> Empty solid geometry.") - app_obj.inform.emit('[ERROR] %s' % _("Failed.")) + self.app.log.error('[ERROR] %s' % _("Failed.")) return "fail" try: @@ -164,7 +183,7 @@ class TclCommandCutout(TclCommand): if ret == 'fail': self.app.log.error("Could not create a cutout Geometry object." ) return "fail" - self.app.inform.emit("[success] Rectangular-form Cutout operation finished.") + self.app.log.info("[success] Cutout operation finished.") except Exception as e: self.app.log.error("Cutout operation failed: %s" % str(e)) return "fail" diff --git a/tclCommands/TclCommandGeoCutout.py b/tclCommands/TclCommandGeoCutout.py index 6d4786a9..e64ec8bd 100644 --- a/tclCommands/TclCommandGeoCutout.py +++ b/tclCommands/TclCommandGeoCutout.py @@ -29,7 +29,7 @@ class TclCommandGeoCutout(TclCommandSignaled): # List of all command aliases, to be able use old # names for backward compatibility (add_poly, add_polygon) - aliases = ['geocutout', 'geoc'] + aliases = ['geoc'] description = '%s %s' % ("--", "Creates board cutout from an object (Gerber or Geometry) of any shape.") @@ -140,7 +140,7 @@ class TclCommandGeoCutout(TclCommandSignaled): name = args['name'] else: msg = "[WARNING] %s" % _("The name of the object for which cutout is done is missing. Add it and retry.") - self.app.inform.emit(msg) + self.app.log.warning(msg) return "fail" if 'margin' in args: @@ -177,13 +177,13 @@ class TclCommandGeoCutout(TclCommandSignaled): return "fail" if 0 in {dia}: - self.app.inform.emit( + self.app.log.warning( "[WARNING] %s" % _("Tool Diameter is zero value. Change it to a positive real number.")) return "fail" - if gaps not in ['None', 'LR', 'TB', '2LR', '2TB', '4', '8']: - self.app.inform.emit('[WARNING] %s' % - _("Gaps value can be only one of: 'None', 'LR', 'TB', '2LR', '2TB', 4 or 8.\n" + if str(gaps).lower() not in ['none', 'lr', 'tb', '2lr', '2tb', '4', '8']: + self.app.log.warning('[WARNING] %s' % + _("Gaps value can be only one of: 'none', 'lr', 'tb', '2lr', '2tb', 4 or 8.\n" "Fill in a correct value and retry.")) return "fail" @@ -215,7 +215,7 @@ class TclCommandGeoCutout(TclCommandSignaled): self.app.log.error("TclCommandGeoCutout.execute() --> %s" % str(exc)) return 'fail' else: - self.app.inform.emit("[ERROR] %s" % _("Cancelled. Object type is not supported.")) + self.app.log.error("[ERROR] %s" % _("Cancelled. Object type is not supported.")) return "fail" def geo_init(geo_obj, app_obj): @@ -292,4 +292,4 @@ class TclCommandGeoCutout(TclCommandSignaled): self.app.log.error(msg) return "fail" else: - self.app.inform.emit("[success] %s" % _("Any-form Cutout operation finished.")) + self.app.log.info("[success] %s" % _("Any-form Cutout operation finished.")) From 75ac180aadca985c6def432ff518f9c464b6b017 Mon Sep 17 00:00:00 2001 From: Ali Khalil Date: Wed, 6 Apr 2022 06:53:52 +0300 Subject: [PATCH 5/5] Updated cutout command help text to reflect options 2TB, 2LR, and 8 --- tclCommands/TclCommandCutout.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tclCommands/TclCommandCutout.py b/tclCommands/TclCommandCutout.py index e4471501..8ff40956 100644 --- a/tclCommands/TclCommandCutout.py +++ b/tclCommands/TclCommandCutout.py @@ -56,7 +56,8 @@ class TclCommandCutout(TclCommand): ('dia', 'Tool diameter.'), ('margin', 'Margin over bounds.'), ('gapsize', 'Size of gap.'), - ('gaps', "Type of gaps. Can be (case-insensitive): 'None' = no-tabs, 'TB' = top-bottom, 'LR' = left-right and '4' = one each side."), + ('gaps', "Type of gaps. Can be (case-insensitive): 'None' = no-tabs, 'TB' = top-bottom, 'LR' = left-right, " + "'2TB' = 2-top-bottom, '2LR' = 2-left-right, '4' = one each side, and '8' = two each side."), ('outname', 'Name of the object to create.') ]), 'examples': ['cutout cut_object -dia 1.2 -margin 0.1 -gapsize 1 -gaps "tb" -outname cutout_geo -type rect']