Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for directly uploading to networked 3D Printers running Klipper via the Moonraker API #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions gcodeplot.inx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>GcodePlot</_name>
<name>GcodePlot</name>
<id>mobi.omegacentauri.gcodeplot</id>
<dependency type="extension">org.inkscape.output.svg.inkscape</dependency>
<dependency type="executable" location="extensions">gcodeplot.py</dependency>
Expand All @@ -22,23 +22,7 @@
<param name="pen-up-speed" type="float" min="-1000000" max="1000000" precision="1" _gui-text="Movement speed (mm/s):" _gui-description="Speed moving with pen up (Default: 40)">40</param>
<param name="pen-down-speed" type="float" min="-1000000" max="1000000" precision="1" _gui-text="Draw speed (mm/s):" _gui-description="Speed moving with pen down (Default: 35)">35</param>
<param name="z-speed" type="float" min="-1000000" max="1000000" precision="1" _gui-text="Z-speed (mm/s):" _gui-description="Speed moving pen up/down (Default: 5)">5</param>
<param name="send-and-save" type="string" _gui-text="Serial port to send gcode to (blank not to send)" _gui-description="If you enter the name of your serial port here (e.g., COM4), then you can directly send the file to your device."></param>
<param name="send-speed" type="enum" _gui-text="Serial baud rate:" _gui-description="Baud rate of your serial device (Default: 115200)">
<item value="115200">115200</item>
<item value="300">300</item>
<item value="600">600</item>
<item value="1200">1200</item>
<item value="2400">2400</item>
<item value="4800">4800</item>
<item value="9600">9600</item>
<item value="14400">14400</item>
<item value="19200">19200</item>
<item value="28800">28800</item>
<item value="38400">38400</item>
<item value="56000">56000</item>
<item value="57600">57600</item>
<item value="115200">115200</item>
</param>

</page>
<page name="fitting" _gui-text="Fitting and Extracting">
<param name="scale" type="enum" _gui-text="Scaling mode:" _gui-description="Method for scaling to print area (Default: none; should be 'none' if tool-offset option is set in cutter tab)">
Expand Down Expand Up @@ -86,12 +70,43 @@
<param name="lift-command" type="string" _gui-text="Lift Command" _gui-description="Gcode command to lift the pen/blade"></param>
<param name="down-command" type="string" _gui-text="Down Command" _gui-description="Gcode command to lower the pen/blade"></param>
</page>
<page name="connectionSettings" _gui-text="Connection Settings">

<label>Moonraker API</label>
<spacer/>
<param name="moonraker" type="string" _gui-text="Moonraker URL (leave blank if not used)" _gui-description="Enter the Moonraker API URL here to directly send gcode to your 3D Printer over the network."></param>
<param name="moonraker-filename" type="string" _gui-text="File name" _gui-description="Choose a name for the file to be uploaded.">Inkscape.gcode</param>
<param name="moonraker-autoprint" type="bool" _gui-text="Start job automatically after upload" _gui-description="Automatically starts running the gcode once the file is saved">false</param>
<spacer/>
<separator/>
<spacer/>
<label>Serial Configuration</label>
<spacer/>
<param name="send-and-save" type="string" _gui-text="Serial Port (leave blank if not used)" _gui-description="If you enter the name of your serial port here (e.g., COM4), then you can directly send the file to your device."></param>
<param name="send-speed" type="enum" _gui-text="Serial baud rate:" _gui-description="Baud rate of your serial device (Default: 115200)">
<item value="115200">115200</item>
<item value="300">300</item>
<item value="600">600</item>
<item value="1200">1200</item>
<item value="2400">2400</item>
<item value="4800">4800</item>
<item value="9600">9600</item>
<item value="14400">14400</item>
<item value="19200">19200</item>
<item value="28800">28800</item>
<item value="38400">38400</item>
<item value="56000">56000</item>
<item value="57600">57600</item>
<item value="115200">115200</item>
</param>

</page>
</param>
<output>
<extension>.gcode</extension>
<mimetype>text/plain</mimetype>
<_filetypename>3-axis gcode plotter (*.gcode)</_filetypename>
<_filetypetooltip>Export 3-axis gcode plotter file</_filetypetooltip>
<filetypename>3-axis gcode plotter (*.gcode)</filetypename>
<filetypetooltip>Export 3-axis gcode plotter file</filetypetooltip>
<dataloss>true</dataloss>
</output>
<script>
Expand Down
35 changes: 32 additions & 3 deletions gcodeplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import gcodeplotutils.anneal as anneal
import svgpath.parser as parser
import cmath
import requests
import io
from random import sample
from svgpath.shader import Shader
from gcodeplotutils.processoffset import OffsetProcessor
Expand Down Expand Up @@ -690,6 +692,7 @@ def fixComments(plotter, data, comment = ";"):
out.append(line)
return out


if __name__ == '__main__':

def help(error=False):
Expand Down Expand Up @@ -771,6 +774,9 @@ def help(error=False):
comment = ";"
sendAndSave = False
directionAngle = None
moonraker = ""
moonrakerFilename = ""
moonrakerAutoprint = "false"

def maybeNone(a):
return None if a=='none' else a
Expand All @@ -784,7 +790,7 @@ def maybeNone(a):
'shading-angle=', 'shading-crosshatch', 'no-shading-crosshatch', 'shading-avoid-outline',
'pause-at-start', 'no-pause-at-start', 'min-x=', 'max-x=', 'min-y=', 'max-y=',
'no-shading-avoid-outline', 'shading-darkest=', 'shading-lightest=', 'stroke-all', 'no-stroke-all', 'gcode-pause', 'dump-options', 'tab=', 'extract-color=', 'sort', 'no-sort', 'simulation', 'no-simulation', 'tool-offset=', 'overcut=',
'boolean-shading-crosshatch=', 'boolean-sort=', 'tool-mode=', 'send-and-save=', 'direction=', 'lift-command=', 'down-command=',
'boolean-shading-crosshatch=', 'boolean-sort=', 'tool-mode=', 'send-and-save=', 'moonraker=', 'moonraker-filename=', 'moonraker-autoprint=','direction=', 'lift-command=', 'down-command=',
'init-code=', 'comment-delimiters=', 'end-code=' ], )

if len(args) + len(opts) == 0:
Expand Down Expand Up @@ -847,6 +853,12 @@ def maybeNone(a):
sendPort = None if len(arg.strip()) == 0 else arg
if sendPort is not None:
sendAndSave = True
elif opt== '--moonraker':
moonraker = None if len(arg.strip()) == 0 else arg
elif opt== '--moonraker-filename':
moonrakerFilename = "Inkscape.gcode" if len(arg.strip()) == 0 else arg.strip(".gcode") + ".gcode"
elif opt== '--moonraker-autoprint':
moonrakerAutoprint = arg
elif opt == '--no-send':
sendPort = None
elif opt in ('-S', '--send-speed'):
Expand Down Expand Up @@ -1069,7 +1081,7 @@ def maybeNone(a):
sendgcode.sendGcode(port=sendPort, speed=sendSpeed, commands=gcodeHeader(plotter) + [gcodePause], gcodePause=gcodePause, variables=plotter.variables, formulas=plotter.formulas)
sys.exit(0)

with open(args[0]) as f:
with open(args[0], 'r') as f:
data = f.read()

svgTree = None
Expand Down Expand Up @@ -1145,7 +1157,24 @@ def maybeNone(a):
if hpglOut:
sys.stdout.write(g)
else:
print('\n'.join(fixComments(plotter, g, comment=plotter.comment)))
if moonraker != "":


moonraker = moonraker.strip("/") + "/server/files/upload"

filtered = '\n'.join(fixComments(plotter, g, comment=plotter.comment)) + '\n'

virtual_file = io.BytesIO(filtered.encode('utf-8'))

files = {'file': (moonrakerFilename, virtual_file), 'print': moonrakerAutoprint}
response = requests.post(moonraker, files=files)
if response.status_code != 201:
sys.stderr.write(f"Error uploading file. Status code: {response.status_code}")

print('\n'.join(fixComments(plotter, g, comment=plotter.comment)))

else:
print('\n'.join(fixComments(plotter, g, comment=plotter.comment)))

else:
sys.stderr.write("No points.")
Expand Down