Просмотр исходного кода

- fixed bug in Gerber parser: it tried to calculate a len() for a single element and not a list - a Gerber generated by Eagle exhibited this

Marius Stanciu 5 лет назад
Родитель
Сommit
522b98fef3
2 измененных файлов с 83 добавлено и 14 удалено
  1. 4 0
      README.md
  2. 79 14
      flatcamParsers/ParseGerber.py

+ 4 - 0
README.md

@@ -9,6 +9,10 @@ CAD program, and create G-Code for Isolation routing.
 
 
 =================================================
 =================================================
 
 
+25.02.2020
+
+- fixed bug in Gerber parser: it tried to calculate a len() for a single element and not a list - a Gerber generated by Eagle exhibited this
+
 20.02.2020
 20.02.2020
 
 
 - in Paint Tool replaced the Selection radio with a combobox GUI element that is more compact
 - in Paint Tool replaced the Selection radio with a combobox GUI element that is more compact

+ 79 - 14
flatcamParsers/ParseGerber.py

@@ -456,7 +456,12 @@ class Gerber(Geometry):
                     new_polarity = match.group(1)
                     new_polarity = match.group(1)
                     # log.info("Polarity CHANGE, LPC = %s, poly_buff = %s" % (self.is_lpc, poly_buffer))
                     # log.info("Polarity CHANGE, LPC = %s, poly_buff = %s" % (self.is_lpc, poly_buffer))
                     self.is_lpc = True if new_polarity == 'C' else False
                     self.is_lpc = True if new_polarity == 'C' else False
-                    if len(path) > 1 and current_polarity != new_polarity:
+                    try:
+                        path_length = len(path)
+                    except TypeError:
+                        path_length = 1
+
+                    if path_length > 1 and current_polarity != new_polarity:
 
 
                         # finish the current path and add it to the storage
                         # finish the current path and add it to the storage
                         # --- Buffered ----
                         # --- Buffered ----
@@ -491,7 +496,12 @@ class Gerber(Geometry):
                     # --- Apply buffer ---
                     # --- Apply buffer ---
                     # If added for testing of bug #83
                     # If added for testing of bug #83
                     # TODO: Remove when bug fixed
                     # TODO: Remove when bug fixed
-                    if len(poly_buffer) > 0:
+                    try:
+                        buff_length = len(poly_buffer)
+                    except TypeError:
+                        buff_length = 1
+
+                    if buff_length > 0:
                         if current_polarity == 'D':
                         if current_polarity == 'D':
                             self.solid_geometry = self.solid_geometry.union(cascaded_union(poly_buffer))
                             self.solid_geometry = self.solid_geometry.union(cascaded_union(poly_buffer))
 
 
@@ -714,7 +724,12 @@ class Gerber(Geometry):
                     # log.debug(self.apertures[current_aperture])
                     # log.debug(self.apertures[current_aperture])
 
 
                     # Take care of the current path with the previous tool
                     # Take care of the current path with the previous tool
-                    if len(path) > 1:
+                    try:
+                        path_length = len(path)
+                    except TypeError:
+                        path_length = 1
+
+                    if path_length > 1:
                         if self.apertures[last_path_aperture]["type"] == 'R':
                         if self.apertures[last_path_aperture]["type"] == 'R':
                             # do nothing because 'R' type moving aperture is none at once
                             # do nothing because 'R' type moving aperture is none at once
                             pass
                             pass
@@ -751,7 +766,12 @@ class Gerber(Geometry):
                 # ################  G36* - Begin region   ########################
                 # ################  G36* - Begin region   ########################
                 # ################################################################
                 # ################################################################
                 if self.regionon_re.search(gline):
                 if self.regionon_re.search(gline):
-                    if len(path) > 1:
+                    try:
+                        path_length = len(path)
+                    except TypeError:
+                        path_length = 1
+
+                    if path_length > 1:
                         # Take care of what is left in the path
                         # Take care of what is left in the path
 
 
                         geo_dict = dict()
                         geo_dict = dict()
@@ -799,7 +819,12 @@ class Gerber(Geometry):
                     # if D02 happened before G37 we now have a path with 1 element only; we have to add the current
                     # if D02 happened before G37 we now have a path with 1 element only; we have to add the current
                     # geo to the poly_buffer otherwise we loose it
                     # geo to the poly_buffer otherwise we loose it
                     if current_operation_code == 2:
                     if current_operation_code == 2:
-                        if len(path) == 1:
+                        try:
+                            path_length = len(path)
+                        except TypeError:
+                            path_length = 1
+
+                        if path_length == 1:
                             # this means that the geometry was prepared previously and we just need to add it
                             # this means that the geometry was prepared previously and we just need to add it
                             geo_dict = dict()
                             geo_dict = dict()
                             if geo_f:
                             if geo_f:
@@ -825,7 +850,12 @@ class Gerber(Geometry):
                     # Only one path defines region?
                     # Only one path defines region?
                     # This can happen if D02 happened before G37 and
                     # This can happen if D02 happened before G37 and
                     # is not and error.
                     # is not and error.
-                    if len(path) < 3:
+                    try:
+                        path_length = len(path)
+                    except TypeError:
+                        path_length = 1
+
+                    if path_length < 3:
                         # print "ERROR: Path contains less than 3 points:"
                         # print "ERROR: Path contains less than 3 points:"
                         # path = [[current_x, current_y]]
                         # path = [[current_x, current_y]]
                         continue
                         continue
@@ -974,7 +1004,12 @@ class Gerber(Geometry):
                                                  _("GERBER file might be CORRUPT. Check the file !!!"))
                                                  _("GERBER file might be CORRUPT. Check the file !!!"))
 
 
                     elif current_operation_code == 2:
                     elif current_operation_code == 2:
-                        if len(path) > 1:
+                        try:
+                            path_length = len(path)
+                        except TypeError:
+                            path_length = 1
+
+                        if path_length > 1:
                             geo_s = None
                             geo_s = None
 
 
                             geo_dict = dict()
                             geo_dict = dict()
@@ -1073,7 +1108,12 @@ class Gerber(Geometry):
                     elif current_operation_code == 3:
                     elif current_operation_code == 3:
 
 
                         # Create path draw so far.
                         # Create path draw so far.
-                        if len(path) > 1:
+                        try:
+                            path_length = len(path)
+                        except TypeError:
+                            path_length = 1
+
+                        if path_length > 1:
                             # --- Buffered ----
                             # --- Buffered ----
                             geo_dict = dict()
                             geo_dict = dict()
 
 
@@ -1229,7 +1269,12 @@ class Gerber(Geometry):
                     # Nothing created! Pen Up.
                     # Nothing created! Pen Up.
                     if current_operation_code == 2:
                     if current_operation_code == 2:
                         log.warning("Arc with D2. (%d)" % line_num)
                         log.warning("Arc with D2. (%d)" % line_num)
-                        if len(path) > 1:
+                        try:
+                            path_length = len(path)
+                        except TypeError:
+                            path_length = 1
+
+                        if path_length > 1:
                             geo_dict = dict()
                             geo_dict = dict()
 
 
                             if last_path_aperture is None:
                             if last_path_aperture is None:
@@ -1372,7 +1417,12 @@ class Gerber(Geometry):
                 # ################################################################
                 # ################################################################
                 log.warning("Line ignored (%d): %s" % (line_num, gline))
                 log.warning("Line ignored (%d): %s" % (line_num, gline))
 
 
-            if len(path) > 1:
+            try:
+                path_length = len(path)
+            except TypeError:
+                path_length = 1
+
+            if path_length > 1:
                 # In case that G01 (moving) aperture is rectangular, there is no need to still create
                 # In case that G01 (moving) aperture is rectangular, there is no need to still create
                 # another geo since we already created a shapely box using the start and end coordinates found in
                 # another geo since we already created a shapely box using the start and end coordinates found in
                 # path variable. We do it only for other apertures than 'R' type
                 # path variable. We do it only for other apertures than 'R' type
@@ -1415,15 +1465,25 @@ class Gerber(Geometry):
 
 
             # this treats the case when we are storing geometry as solids
             # this treats the case when we are storing geometry as solids
             try:
             try:
-                if len(poly_buffer) == 0 and len(self.solid_geometry) == 0:
+                buff_length = len(poly_buffer)
+            except TypeError:
+                buff_length = 1
+
+            try:
+                sol_geo_length = len(self.solid_geometry)
+            except TypeError:
+                sol_geo_length = 1
+
+            try:
+                if buff_length == 0 and sol_geo_length == 0:
                     log.error("Object is not Gerber file or empty. Aborting Object creation.")
                     log.error("Object is not Gerber file or empty. Aborting Object creation.")
                     return 'fail'
                     return 'fail'
             except TypeError as e:
             except TypeError as e:
                 log.error("Object is not Gerber file or empty. Aborting Object creation. %s" % str(e))
                 log.error("Object is not Gerber file or empty. Aborting Object creation. %s" % str(e))
                 return 'fail'
                 return 'fail'
 
 
-            log.warning("Joining %d polygons." % len(poly_buffer))
-            self.app.inform.emit('%s: %d.' % (_("Gerber processing. Joining polygons"), len(poly_buffer)))
+            log.warning("Joining %d polygons." % buff_length)
+            self.app.inform.emit('%s: %d.' % (_("Gerber processing. Joining polygons"), buff_length))
 
 
             if self.use_buffer_for_union:
             if self.use_buffer_for_union:
                 log.debug("Union by buffer...")
                 log.debug("Union by buffer...")
@@ -1729,7 +1789,12 @@ class Gerber(Geometry):
 
 
         if type(geos) == list:
         if type(geos) == list:
             # HACK for importing QRCODE exported by FlatCAM
             # HACK for importing QRCODE exported by FlatCAM
-            if len(geos) == 1:
+            try:
+                geos_length = len(geos)
+            except TypeError:
+                geos_length = 1
+
+            if geos_length == 1:
                 geo_qrcode = list()
                 geo_qrcode = list()
                 geo_qrcode.append(Polygon(geos[0].exterior))
                 geo_qrcode.append(Polygon(geos[0].exterior))
                 for i_el in geos[0].interiors:
                 for i_el in geos[0].interiors: