Code cleanup

Sat, 04 Jun 2016 09:22:51 +0200

author
mbayer
date
Sat, 04 Jun 2016 09:22:51 +0200
changeset 20
03b34402d405
parent 19
234037fbca4b
child 21
8551b89bd05e

Code cleanup

printrun-src/printrun/laser.py file | annotate | diff | comparison | revisions
printrun-src/printrun/pronterface.py file | annotate | diff | comparison | revisions
--- a/printrun-src/printrun/laser.py	Fri Jun 03 21:14:09 2016 +0200
+++ b/printrun-src/printrun/laser.py	Sat Jun 04 09:22:51 2016 +0200
@@ -33,6 +33,31 @@
 # FOR HPGL:
 SCALE_FACTOR = 1.0 / 40.0 # 40 plotter units
 
+# GENERAL HEADER AND FOOTER GCODE
+GCODE_HEAD = """
+; GCode generated by laser.py pronterface library (marlin code flavour)
+; 2015/2016 by NeoSoft - Malte Bayer
+
+G21 ; Metric
+; We assume Z is in focus height and laser head is focus at bottom left of image!
+G92 X0 Y0 E0; set zero position - new origin
+G90 ; absolute positioning
+M82 ; Set extruder (laser) to absolute positioning
+M201 X1000 Y1000 E1000 ; Set acceleration
+M203 X1000 Y1000 Z4 E1000 ; Set max feedrate
+M209 S0 ; disable firmware retraction, we dont want to burn holes...
+M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod
+M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement!
+G0 X0 Y0 F%d ; Set moving speed TRAVEL_SPEED
+G1 X0 Y0 F%d ; Set linear engraving speed ENGRAVE_SPEED
+
+""" % (TRAVEL_SPEED, ENGRAVE_SPEED)
+
+GCODE_FOOT = """M400 ; Wait for all moves to finish
+M571 S0 E0
+M42 P28 S0 ; Force laser off!
+M501 ; undo all settings made
+"""
 
 from PIL import Image
 import sys
@@ -47,18 +72,22 @@
 class Lasercutter:
     """
     Lasercutter methods
-    parameters: log = logger function (accepts a string)
+    parameters: log = logger function (fuction has to accept a string)
     """
     def __init__(self, pronterwindow = None):
         if pronterwindow:
             self.pronterwindow = pronterwindow
             self.log = pronterwindow.log
+            self.pronterwindow.clear_log(None)
         else:
             self.pronterwindow = None
             self.log = lambda : None
-        self.log("\nLasercutter library initialized resolution: %f mm per pixel" % MM_PIXEL)
+        self.log("Lasercutter library initialized\n%d DPI (%f mm/pixel)" % (DPI, MM_PIXEL))
         if STEPS_PIXEL <= 5:
             self.log("WARNING: STEPS PER PIXEL NEEDS TO BE > 5 (otherwise marlin joins lines): %f" % STEPS_PIXEL)
+        self.log("Travel/Engrave speed: %d mm/sec, %d mm/sec" % (
+            TRAVEL_SPEED / 60, ENGRAVE_SPEED / 60) )
+        self.log("")
         
     
     def pixel2bit(self, pixel, threshold=128):
@@ -103,27 +132,7 @@
         pix = im.load()
 
         fo = open(filename + ".g", "w")
-        fo.write("""
-; Filename: %s
-; GCode generated by bitplotter one-night-quick-hack script (marlin code flavour)
-; 2015/2016 by NeoSoft - Malte Bayer
-
-G21 ; Metric
-; We assume Z is in focus height and laser head is focus at bottom left of image!
-G92 X0 Y0 E0; set zero position - new origin
-G90 ; absolute positioning
-M82 ; Set extruder (laser) to absolute positioning
-M201 X1000 Y1000 E500 ; Set acceleration
-M203 X1000 Y1000 Z4 E10 ; Set max feedrate
-M209 S0 ; disable firmware retraction, we dont want to burn holes...
-M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod
-M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement!
-G0 X0 Y0 F%d ; Set moving speed TRAVEL_SPEED
-G1 X0 Y0 F%d ; Set linear engraving speed ENGRAVE_SPEED
-
-""" % (filename, TRAVEL_SPEED, ENGRAVE_SPEED) )
-        self.log("Travel/Engrave speed: %d mm/sec, %d mm/sec" % (
-            TRAVEL_SPEED / 60, ENGRAVE_SPEED / 60) )
+        fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
 
         fo.write(";Start engraving the raster image: %dx%d points @ %d DPI = %.0fx%.0f mm\n\n" % (
             im.size[0], im.size[1], DPI, im.size[0]*MM_PIXEL, im.size[1]*MM_PIXEL) )
@@ -184,12 +193,7 @@
             if CHANGE_DIRECTION:
                 DIR = DIR * (-1) # change y direction on every X
 
-        fo.write("""M400 ; Wait for all moves to finish
-M571 S0 E0
-M42 P28 S0 ; Force laser off!
-M501 ; undo all settings made
-""")
-
+        fo.write(GCODE_FOOT)
         fo.close()
 
         if self.pronterwindow:
@@ -205,26 +209,7 @@
     
         fi = open(filename, "r")
         fo = open(filename + ".g", "w")
-
-
-        fo.write("""
-; Filename: %s
-; GCode generated by hpglplotter (marlin code flavour)
-
-G21 ; Metric
-; We assume Z is in focus height and laser head is focus at bottom left of image!
-G92 X0 Y0 E0; set zero position - new origin
-G90 ; absolute positioning
-M82 ; Set extruder (laser) to absolute positioning
-M201 X1000 Y1000 E500 ; Set acceleration
-M203 X1000 Y1000 Z4 E10 ; Set max feedrate
-M209 S0 ; disable firmware retraction, we dont want to burn holes...
-M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod
-M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement!
-G0 X0 Y0 F%d ; Set moving speed TRAVEL_SPEED
-G1 X0 Y0 F%d ; Set linear engraving speed ENGRAVE_SPEED
-
-""" % (filename, TRAVEL_SPEED, ENGRAVE_SPEED) )
+        fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
 
         G = "0"
         LASER_STATE = 0
@@ -267,13 +252,7 @@
                         print "UNKNOWN: %s" % action
                     last_cmd = cmd            
     
-
-        fo.write("""M400 ; Wait for all moves to finish
-M571 S0 E0
-M42 P28 S0 ; Force laser off!
-M501 ; undo all settings made
-""")
-
+        fo.write(GCODE_FOOT)
         fi.close()
         fo.close()    
 
@@ -285,37 +264,9 @@
     def svg2gcode(self, filename, bed_max_x = 50, bed_max_y = 50, smoothness = 0.2):
         self.log("Generating paths from SVG...")        
 
-        preamble = """
-; Filename: %s
-; GCode generated by bitplotter one-night-quick-hack script (marlin code flavour)
-; 2015/2016 by NeoSoft - Malte Bayer
-
-G21 ; Metric
-; We assume Z is in focus height and laser head is focus at bottom left of image!
-G92 X0 Y0 E0; set zero position - new origin
-G90 ; absolute positioning
-M82 ; Set extruder (laser) to absolute positioning
-M201 X1000 Y1000 E500 ; Set acceleration
-M203 X1000 Y1000 Z4 E10 ; Set max feedrate
-M209 S0 ; disable firmware retraction, we dont want to burn holes...
-M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod
-M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement!
-G0 X0 Y0 F%d ; Set moving speed TRAVEL_SPEED
-G1 X0 Y0 F%d ; Set linear engraving speed ENGRAVE_SPEED
-
-""" % (filename, TRAVEL_SPEED, ENGRAVE_SPEED)
-        self.log("Travel/Engrave speed: %d mm/sec, %d mm/sec" % (
-            TRAVEL_SPEED / 60, ENGRAVE_SPEED / 60) )
-
-        postamble = """M400 ; Wait for all moves to finish
-M571 S0 E0
-M42 P28 S0 ; Force laser off!
-M501 ; undo all settings made
-"""
         shape_preamble = "G92 E0\n"
         shape_postamble = ""
 
-
         """ 
         Used to control the smoothness/sharpness of the curves.
         Smaller the value greater the sharpness. Make sure the
@@ -348,7 +299,7 @@
         self.log("Scaling factor: %.2f, %.2f" % (scale_x,scale_y))
 
         fo = open(filename + ".g", "w")
-        fo.write(preamble) 
+        fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
 
         for elem in root.iter():
             try:
@@ -393,8 +344,8 @@
                             self.log("Position outside print dimension: %d, %d" % (xs, ys)) 
                     fo.write(shape_postamble)
 
-        fo.write(postamble)
-        fo.close() 
+        fo.write(GCODE_FOOT)
+        fo.close()
                      
         if self.pronterwindow:
             self.log("")        
--- a/printrun-src/printrun/pronterface.py	Fri Jun 03 21:14:09 2016 +0200
+++ b/printrun-src/printrun/pronterface.py	Sat Jun 04 09:22:51 2016 +0200
@@ -2,6 +2,7 @@
 
 # FILE MODIFIED BY NEOSOFT - MALTE BAYER
 # Embed Lasercut functions from laser.py 
+import laser
 
 # This file is part of the Printrun suite.
 #
@@ -1378,22 +1379,22 @@
                 self.logError(_("Could not update recent files list:") +
                               "\n" + traceback.format_exc())
 
-            # import the library local so we dont have to restart the whole app when making code changes
-            from laser import Lasercutter
+            # reload the library local so we dont have to restart the whole app when making code changes
+            reload(laser)
 
             if name.lower().endswith(".stl") or name.lower().endswith(".obj"):
                 self.slice(name)
             elif name.lower().endswith(".png") or name.lower().endswith(".jpg") or name.lower().endswith(".gif"):
                 # Generate GCODE from IMAGE
-                lc = Lasercutter(pronterwindow = self)
+                lc = laser.Lasercutter(pronterwindow = self)
                 lc.image2gcode(name)  
             elif name.lower().endswith(".svg"):
                 # Generate GCODE from SVG
-                lc = Lasercutter(pronterwindow = self)
+                lc = laser.Lasercutter(pronterwindow = self)
                 lc.svg2gcode(name)  
             elif name.lower().endswith(".hpgl") or name.lower().endswith(".plt"):
                 # Generate GCODE from HPGL
-                lc = Lasercutter(pronterwindow = self)
+                lc = laser.Lasercutter(pronterwindow = self)
                 lc.hpgl2gcode(name)  
             else:
                 self.load_gcode_async(name)

mercurial