Python anfänger
Verfasst: Freitag 4. Februar 2022, 13:47
Hallo am alle.
Bin absolute Anfänger was Python an geht und brauche Hilfe.
Ich habe ein Scrip. Es ist Konverter von gcode zu hex.
Ich werde es entweder in Processing java übersetzen wollen (Wäre mir am liebsten) oder eine Änderung in Python haben. Geht um ausführen von diesem Scrip.
Der Zeit ist es :
Python convertGCode.py “Eingang File” “Ausgang File”.
Werde gerne ein Dialog kriegen wo ich mir das Dokument zum Öffnen aussuche und ebenso ein Dialog wo ich es speichern will und wäre nicht schlecht Möglichkeit wo ich wert zum Skalieren eingeben kann.
Mir ist klar, dass ich zu viel verlange aber konnte mir jemand helfen?
Bin absolute Anfänger was Python an geht und brauche Hilfe.
Ich habe ein Scrip. Es ist Konverter von gcode zu hex.
Ich werde es entweder in Processing java übersetzen wollen (Wäre mir am liebsten) oder eine Änderung in Python haben. Geht um ausführen von diesem Scrip.
- # Simple GCode to Arduino hex format converter.
# It only understands G00 and G01 codes, nothing fancy!
#
# It will automatically scale the object to the full 12 bit
# range for my Arduino laser project, to change that
# you have to modify the scale in createObject().
#
# Typical files I worked with have been generated with
# http://ncplot.com/stickfont/stickfont.htm (StickFont 1.1)
#
# Usage: python convertGCode.py inputfile.nc outputfile.cpp
import math
import sys
def createObject(name, cmds):
minx = miny = 10000000
maxx = maxy = 0
string = ""
for cmd in cmds:
if cmd[0] == 2:
minx = min(minx,cmd[1])
miny = min(miny,cmd[2])
maxx = max(maxx,cmd[1])
maxy = max(maxy,cmd[2])
string += "const unsigned short draw_" + name + "[] PROGMEM = {\n";
laserState = False
biggestSide = max(maxx-minx, maxy-miny)
# scale to the laser range
scale = 1500. / biggestSide; # velkost objektu max 4095, smal 500 // laser.setScale(7);
print "bounding box x: ", minx, maxx
print "bounding box y: ", miny, maxy
print "scale: ", scale
for cmd in cmds:
if cmd[0] == 0:laserState = False
if cmd[0] == 1:laserState = True
if cmd[0] == 2:
x = int(math.floor((cmd[1]-minx) * scale))
y = int(math.floor((cmd[2]-miny) * scale))
if laserState:
x += 0x8000
string += hex(x) + "," + hex(y) + ",\n"
string += "};\n"
return string
def run(input, output):
result = ""
f = open(input);
lines = f.readlines()
drawing = False
posx = posy = 0.
cmds = []
for l in lines:
if l.startswith("G00"):
if drawing:
cmds.append((0,))
drawing = False
elif l.startswith("G01"):
drawing = True
cmds.append((1,))
elif l.startswith("X"):
parts = l.split("Y")
newposx = float(parts[0][1:])
newposy = float(parts[1])
cmds.append((2,newposx,newposy))
posx = newposx
posy = newposy
result = createObject("object", cmds)
o = open(output,"w")
o.write(result)
if __name__ == "__main__":
if len(sys.argv) < 3:
print "Usage: convertGCode.py inputfile.nc outputfile.cpp"
else:
run(sys.argv[1], sys.argv[2])
Der Zeit ist es :
Python convertGCode.py “Eingang File” “Ausgang File”.
Werde gerne ein Dialog kriegen wo ich mir das Dokument zum Öffnen aussuche und ebenso ein Dialog wo ich es speichern will und wäre nicht schlecht Möglichkeit wo ich wert zum Skalieren eingeben kann.
Mir ist klar, dass ich zu viel verlange aber konnte mir jemand helfen?