3D-Drucker/Slic3r/create

Aus Wiki CCC Göttingen
Wechseln zu: Navigation, Suche

Das Skript ermittelt die git version von Slic3r, speichert die aktuelle Config und stl im HGDIR und erzeugt daraus die gcode Datei. Die commit message beinhaltet die git Version von Slic3r. Am Schluß wird die gcode Datei wieder in den Ordner geschrieben aus dem auch das stl her ist. Mit einem Kommentar aus dem die slic3r Version und HG Revision herausvorgeht.

Inbetriebname des Scripts

  • anpassen von SLIRCPATH, HGDIR und CONFIG
  • in HGDIR muss ein hg init && touch CONFIG && hg addremove ausgeführt werden.

Parameter

  • SLIRCPATH - Pfad vo Slic3r.pm zu finden ist (git Verzeichnis)
  • HGDIR - Pfad in dem config, gcode und stl abgelegt werden
  • CONFIG - Name der Config die verwendet werden soll


Code

<highlightSyntax language="python"> import subprocess import os,sys import shutil import re

SLIRCPATH = "/home/hefee/git/Slic3r/slic3r.pl" HGDIR = "/home/hefee/3d/build/" CONFIG = "c.ini"

def slircVersion():

   get version of Slirc
   p=subprocess.Popen([SLIRCPATH, "--help"],stdout=subprocess.PIPE)
   (out, _) = p.communicate()
   return re.search("Slic3r (.+) is a STL-to-GCODE translator for RepRap 3D printers",out.splitlines()[0]).group(1)

def slircGitVersion():

   get git commit of slirc
   path, _ = os.path.os.path.split(SLIRCPATH)
   def f():
       p = subprocess.Popen(["git","log","-1"], stdout=subprocess.PIPE)
       (out, _) = p.communicate()
       v = out.splitlines()[0]
       return v
   return runInDir(path, f)

def hgVersion():

   def f():
       p = subprocess.Popen(["hg", "log", "-l", "1", "--template", '{rev}:{node|short}'], stdout=subprocess.PIPE)
       (out, _) = p.communicate()
       return out
   return runInDir(HGDIR, f)

def printCmd(cmd):

   print cmd
   o=[]
   for c in cmd:
       if c.count(" "):
           o.append("'"+c+"'")
       else:
           o.append(c)
   print " ".join(o)


def runSubprocess(cmd):

   run cmd in subprocess.Popen
   printCmd(cmd)
   p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   (out,err) = p.communicate()
   if out:
       print "\t->"+"\n\t->".join(out.splitlines())
   if err:
       print "\t->"+"\n\t->".join(err.splitlines())


def runInDir(path, func, *args, **kwargs):

   run function in a different directory (path)
   origWD = os.getcwd() 
   try:
       os.chdir(path)
       return func(*args, **kwargs)
   finally:
       os.chdir(origWD)

fname = sys.argv[1] path,name = os.path.os.path.split(fname) gcode,_ = os.path.splitext(fname) gcode +=".gcode" _,gname =os.path.split(gcode)

print "Copying files to BUILD dir..." print CONFIG shutil.copyfile(CONFIG, HGDIR+CONFIG) print fname shutil.copyfile(fname, HGDIR+name) print "done."

slirc=[SLIRCPATH, "--load", "c.ini", name] printCmd(slirc) runInDir(HGDIR, subprocess.check_call, slirc)

runSubprocess(["hg", "add", HGDIR+name]) runSubprocess(["hg", "add", HGDIR+gname]) runInDir(HGDIR,runSubprocess,["hg", "ci", "-m", 'building with slirc v%s %s'%(slircVersion(),slircGitVersion())])

print "Copying gcode to PWD dir..." with open(gcode,'w') as f, open(HGDIR+gname,'r') as r:

   f.write('; generated by Slic3r v%s %s (hg %s)\n\n'%(slircVersion(),slircGitVersion(),hgVersion()))
   f.write(r.read())

print "done." </highlightSyntax>