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

- Gerber Export: working in progress for parsing Gerber geometries in FlatCAMObj.FlatCAMGerber.export_gerber()

Marius Stanciu 6 лет назад
Родитель
Сommit
825b3e5e1e
1 измененных файлов с 155 добавлено и 0 удалено
  1. 155 0
      FlatCAMObj.py

+ 155 - 0
FlatCAMObj.py

@@ -1258,6 +1258,161 @@ class FlatCAMGerber(FlatCAMObj, Gerber):
 
         self.ui_connect()
 
+    def export_gerber(self, whole, fract, g_zeros=None, factor=1):
+        """
+
+        :return: Gerber_code
+        """
+
+        gerber_code = ''
+
+        # store here if the file has slots, return 1 if any slots, 0 if only drills
+        has_slots = 0
+
+        # drills processing
+        try:
+            if self.apertures:
+                length = whole + fract
+                for apid in self.apertures:
+                    if apid == '0':
+                        gerber_code += 'G36*\n'
+                        gerber_code += 'D02*\n'
+                        gerber_code += 'G37*\n'
+                    else:
+                        gerber_code += 'D%s*\n' % str(apid)
+
+                    for geo in self.apertures[apid]['follow_geometry']:
+                        if g_zeros == 'T':
+                            if isinstance(geo, Point):
+                                #TODO
+                                pass
+                            else:
+                                drill_x = drill['point'].x * factor
+                                drill_y = drill['point'].y * factor
+
+                                exc_x_formatted = "{:.{dec}f}".format(drill_x, dec=fract)
+                                exc_y_formatted = "{:.{dec}f}".format(drill_y, dec=fract)
+
+                                # extract whole part and decimal part
+                                exc_x_formatted = exc_x_formatted.partition('.')
+                                exc_y_formatted = exc_y_formatted.partition('.')
+
+                                # left padd the 'whole' part with zeros
+                                x_whole = exc_x_formatted[0].rjust(whole, '0')
+                                y_whole = exc_y_formatted[0].rjust(whole, '0')
+
+                                # restore the coordinate padded in the left with 0 and added the decimal part
+                                # without the decinal dot
+                                exc_x_formatted = x_whole + exc_x_formatted[2]
+                                exc_y_formatted = y_whole + exc_y_formatted[2]
+
+                                gerber_code += "X{xform}Y{yform}\n".format(xform=exc_x_formatted,
+                                                                             yform=exc_y_formatted)
+                        elif tool == drill['tool']:
+                            drill_x = drill['point'].x * factor
+                            drill_y = drill['point'].y * factor
+
+                            exc_x_formatted = "{:.{dec}f}".format(drill_x, dec=fract).replace('.', '')
+                            exc_y_formatted = "{:.{dec}f}".format(drill_y, dec=fract).replace('.', '')
+
+                            # pad with rear zeros
+                            exc_x_formatted.ljust(length, '0')
+                            exc_y_formatted.ljust(length, '0')
+
+                            excellon_code += "X{xform}Y{yform}\n".format(xform=exc_x_formatted,
+                                                                         yform=exc_y_formatted)
+        except Exception as e:
+            log.debug(str(e))
+
+        # slots processing
+        try:
+            if self.slots:
+                has_slots = 1
+                for tool in self.tools:
+                    if int(tool) < 10:
+                        excellon_code += 'T0' + str(tool) + '\n'
+                    else:
+                        excellon_code += 'T' + str(tool) + '\n'
+
+                    for slot in self.slots:
+                        if form == 'dec' and tool == slot['tool']:
+                            start_slot_x = slot['start'].x * factor
+                            start_slot_y = slot['start'].y * factor
+                            stop_slot_x = slot['stop'].x * factor
+                            stop_slot_y = slot['stop'].y * factor
+
+                            excellon_code += "G00X{:.{dec}f}Y{:.{dec}f}\nM15\n".format(start_slot_x,
+                                                                                       start_slot_y,
+                                                                                       dec=fract)
+                            excellon_code += "G00X{:.{dec}f}Y{:.{dec}f}\nM16\n".format(stop_slot_x,
+                                                                                       stop_slot_y,
+                                                                                       dec=fract)
+
+                        elif e_zeros == 'LZ' and tool == slot['tool']:
+                            start_slot_x = slot['start'].x * factor
+                            start_slot_y = slot['start'].y * factor
+                            stop_slot_x = slot['stop'].x * factor
+                            stop_slot_y = slot['stop'].y * factor
+
+                            start_slot_x_formatted = "{:.{dec}f}".format(start_slot_x, dec=fract).replace('.', '')
+                            start_slot_y_formatted = "{:.{dec}f}".format(start_slot_y, dec=fract).replace('.', '')
+                            stop_slot_x_formatted = "{:.{dec}f}".format(stop_slot_x, dec=fract).replace('.', '')
+                            stop_slot_y_formatted = "{:.{dec}f}".format(stop_slot_y, dec=fract).replace('.', '')
+
+                            # extract whole part and decimal part
+                            start_slot_x_formatted = start_slot_x_formatted.partition('.')
+                            start_slot_y_formatted = start_slot_y_formatted.partition('.')
+                            stop_slot_x_formatted = stop_slot_x_formatted.partition('.')
+                            stop_slot_y_formatted = stop_slot_y_formatted.partition('.')
+
+                            # left padd the 'whole' part with zeros
+                            start_x_whole = start_slot_x_formatted[0].rjust(whole, '0')
+                            start_y_whole = start_slot_y_formatted[0].rjust(whole, '0')
+                            stop_x_whole = stop_slot_x_formatted[0].rjust(whole, '0')
+                            stop_y_whole = stop_slot_y_formatted[0].rjust(whole, '0')
+
+                            # restore the coordinate padded in the left with 0 and added the decimal part
+                            # without the decinal dot
+                            start_slot_x_formatted = start_x_whole + start_slot_x_formatted[2]
+                            start_slot_y_formatted = start_y_whole + start_slot_y_formatted[2]
+                            stop_slot_x_formatted = stop_x_whole + stop_slot_x_formatted[2]
+                            stop_slot_y_formatted = stop_y_whole + stop_slot_y_formatted[2]
+
+                            excellon_code += "G00X{xstart}Y{ystart}\nM15\n".format(xstart=start_slot_x_formatted,
+                                                                                   ystart=start_slot_y_formatted)
+                            excellon_code += "G00X{xstop}Y{ystop}\nM16\n".format(xstop=stop_slot_x_formatted,
+                                                                                 ystop=stop_slot_y_formatted)
+                        elif tool == slot['tool']:
+                            start_slot_x = slot['start'].x * factor
+                            start_slot_y = slot['start'].y * factor
+                            stop_slot_x = slot['stop'].x * factor
+                            stop_slot_y = slot['stop'].y * factor
+                            length = whole + fract
+
+                            start_slot_x_formatted = "{:.{dec}f}".format(start_slot_x, dec=fract).replace('.', '')
+                            start_slot_y_formatted = "{:.{dec}f}".format(start_slot_y, dec=fract).replace('.', '')
+                            stop_slot_x_formatted = "{:.{dec}f}".format(stop_slot_x, dec=fract).replace('.', '')
+                            stop_slot_y_formatted = "{:.{dec}f}".format(stop_slot_y, dec=fract).replace('.', '')
+
+                            # pad with rear zeros
+                            start_slot_x_formatted.ljust(length, '0')
+                            start_slot_y_formatted.ljust(length, '0')
+                            stop_slot_x_formatted.ljust(length, '0')
+                            stop_slot_y_formatted.ljust(length, '0')
+
+                            excellon_code += "G00X{xstart}Y{ystart}\nM15\n".format(xstart=start_slot_x_formatted,
+                                                                                   ystart=start_slot_y_formatted)
+                            excellon_code += "G00X{xstop}Y{ystop}\nM16\n".format(xstop=stop_slot_x_formatted,
+                                                                                 ystop=stop_slot_y_formatted)
+        except Exception as e:
+            log.debug(str(e))
+
+        if not self.drills and not self.slots:
+            log.debug("FlatCAMObj.FlatCAMExcellon.export_excellon() --> Excellon Object is empty: no drills, no slots.")
+            return 'fail'
+
+        return has_slots, excellon_code
+
     def mirror(self, axis, point):
         Gerber.mirror(self, axis=axis, point=point)
         self.replotApertures.emit()