3D-Drucker/Slic3r/Python

Aus Wiki CCC Göttingen
Version vom 23. Februar 2012, 21:36 Uhr von Hefee (Diskussion | Beiträge)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springen Zur Suche springen

das ganze ist über mehrere Python Module verteilt. getestest mit python 2.7.2:

benötigt werden die dateien gcode/config.py und gcode/gcode.py und wie bei jedem python Module gcode.__init__.py (s.u.)

Layer 0

es wird Probiert die besten werde zu finden damit die erste Lage gut haftet aber nicht zu stark, ...

layer0.py:

<highlightSyntax language="python"> this script can create simple gcode and using config from slic3r

from gcode.config import Config from gcode.gcode import GCode

def gcode_layer0(config):

   gcode = GCode(config)
   h = config.layer_height*config.first_layer_height_ratio
   
   e=config.extrutionMultiplyer(h)
   z=h
   gcode.G1(z=z,f=config.travel_speed*60)
   gcode.G1(x=40.0,y=40.0)
   gcode.G1(x=180.0,f=270.0,e=140.0*e)
   gcode.G1(y=180.0,f=270.0,e=140.0*e)
   gcode.G1(x=20.0,f=270.0,e=140.0*e)
   gcode.G1(y=40.0,f=270.0,e=140.0*e)
   gcode.retract(x=50.0,y=50.0)
   gcode.G1(x=170.0,f=270.0,e=120.0*e)
   gcode.G1(y=170.0,f=270.0,e=120.0*e)
   gcode.G1(x=30.0,f=270.0,e=120.0*e)
   gcode.G1(y=50.0,f=270.0,e=120.0*e)
   return gcode


if __name__ == "__main__":

   config = Config('c.ini')
   gcode = gcode_layer0(config)
   print "writing 'layer_0_rect.gcode'."
   with open('layer_0_rect.gcode','w') as f:
       f.write(config.start_gcode)
       f.write("\n\n")
       f.write(gcode.begin())
       f.write("\n\n")
       f.write(str(gcode))
       f.write("\n\n")
       f.write(config.end_gcode)
       f.write("\n")

</highlightSyntax>

Code

gcode/__init__.py

(leer)

gcode/config.py

<highlightSyntax language="python"> from math import pi class Config:

   gcode_dir = ["start_gcode", "end_gcode"]
   s_dir = ["fill_pattern", "solid_fill_pattern","extrusion_axis"]
   p_dir = ["print_center"]
   def __init__(self, fname):
       self.fname = fname
       self._readFile()
       self._createFlow()
   def std(self):
  1. printer options
       self.nozzle_diameter    = 0.5
       self.print_center       = [100,100]  # object will be centered around this point
       self.use_relative_e_distances = 0
       self.extrusion_axis     = 'E'
       self.z_offset           = 0
       self.gcode_arcs         = 0
       self.g0                 = 0
       self.gcode_comments     = 0
  1. filament options
       self.filament_diameter  = 3    # mm
       self.extrusion_multiplier = 1
       self.temperature        = 200
  1. speed options
       self.travel_speed           = 130  # mm/sec
       self.perimeter_speed        = 30   # mm/sec
       self.small_perimeter_speed  = 30   # mm/sec
       self.infill_speed           = 60   # mm/sec
       self.solid_infill_speed     = 60   # mm/sec
       self.bridge_speed           = 60   # mm/sec
       self.bottom_layer_speed_ratio   = 0.3
  1. acceleration options
       self.acceleration           = 0
       self.perimeter_acceleration = 25   # mm/sec^2
       self.infill_acceleration    = 50   # mm/sec^2
  1. accuracy options
       self.resolution             = 0.00000001
       self.small_perimeter_area   = (5 / self.resolution) ** 2
       self.layer_height           = 0.4
       self.first_layer_height_ratio = 1
       self.infill_every_layers    = 1
  1. flow options
       self.extrusion_width_ratio  = 0
       self.bridge_flow_ratio      = 1
       self.overlap_factor         = 0.5
  1. print options
       self.perimeters         = 3
       self.solid_layers       = 3
       self.fill_pattern       = 'rectilinear'
       self.solid_fill_pattern = 'rectilinear'
       self.fill_density       = 0.4  # 1 = 100%
       self.fill_angle         = 45
       self.start_gcode = "G28;  home all axes"
       self.end_gcode = """M104 S0 ; turn off temperature

G28 X0  ; home X axis M84  ; disable motors"""

  1. retraction options
       self.retract_length         = 1    # mm
       self.retract_restart_extra  = 0    # mm
       self.retract_speed          = 30   # mm/sec
       self.retract_before_travel  = 2    # mm
       self.retract_lift           = 0    # mm
  1. skirt options
       self.skirts             = 1
       self.skirt_distance     = 6    # mm
       self.skirt_height       = 1    # layers
  1. transform options
       self.scale              = 1
       self.rotate             = 0
       self.duplicate_x        = 1
       self.duplicate_y        = 1
       self.duplicate_distance = 6    # mm
   def _readFile(self):
       self.std()
       with open(self.fname,'r') as f:
           for l in f.readlines():
               s = l.index("=")
               n = l[:s].strip()
               v = l[s+1:].strip()
               if n not in self.p_dir+self.s_dir+self.gcode_dir:
                   v = float(v)
               elif n in self.gcode_dir:
                   v = v.replace("\\n","\n")
               setattr(self, n , v)
   def _createFlow(self):
       if self.extrusion_width_ratio:
           self.flow_width = self.layer_height * self.extrusion_width_ratio
       else:
           # here we calculate a sane default by matching the flow speed (at the nozzle)
           # and the feed rate
           volume = (self.nozzle_diameter**2) * pi/4
           shape_threshold = self.nozzle_diameter * self.layer_height + (self.layer_height**2) * pi/4
           if volume >= shape_threshold:
               # rectangle with semicircles at the ends
               self.flow_width = ((self.nozzle_diameter**2) * pi + (self.layer_height**2) * (4 - pi)) / (4 * self.layer_height)
           else:
               # rectangle with squished semicircles at the ends
               self.flow_width = self.nozzle_diameter * (self.nozzle_diameter/self.layer_height - 4/pi + 1)
           
           self.min_flow_width = self.nozzle_diameter * 1.05
           self.max_flow_width = self.nozzle_diameter * 1.4
           if self.flow_width > self.max_flow_width:
               self.flow_width = self.max_flow_width
           if self.flow_width < self.min_flow_width:
               self.flow_width = self.min_flow_width
       if self.flow_width >= (self.nozzle_diameter + self.layer_height):
           # rectangle with semicircles at the ends
           self.min_flow_spacing = self.flow_width - self.layer_height * (1 - pi/4)
       else:
           # rectangle with shrunk semicircles at the ends
           self.min_flow_spacing = self.flow_width * (1 - pi/4) + self.nozzle_diameter * pi/4
       self.flow_spacing = self.flow_width - self.overlap_factor * (self.flow_width - self.min_flow_spacing)
   def area(self, height):
       s = self.flow_spacing
       h = height
       w = (s - self.min_flow_spacing * self.overlap_factor) / (1 - self.overlap_factor)
       if w >= (self.nozzle_diameter + h):
           # rectangle with semicircles at the ends
           area = w * h + (h**2) / 4 * (pi - 4)
       else:
           # rectangle with shrunk semicircles at the ends
           area = self.nozzle_diameter * h * (1 - pi/4) + h * w * pi/4
       return area
   def extrutionMultiplyer(self, height):
       return  self.area(height) * self.extrusion_multiplier * (4 / (self.filament_diameter**2*pi))


if __name__ == "__main__":

   config = Config('c.ini')
   print "Flow width = %f"% config.flow_width
   print "Flow spacing = %f"%config.flow_spacing
   print "Min flow spacing = %f"%config.min_flow_spacing


   print "area(%s) = %f"%(config.layer_height,config.area(config.layer_height))
   print "extrutionMultiplyer(%s) = %f"%(config.layer_height,config.extrutionMultiplyer(config.layer_height))

</highlightSyntax>

gcode/gcode.py

<highlightSyntax language="python"> class GCode:

   def __init__(self, config):
       self._gcode=[]
       self.e=0.0
       self.x = 0.0
       self.y = 0.0
       self.z = 0.0
       self.config = config
       self.G92(e=0.0)
   def begin(self):
      return """M109 S%s ; wait for temperature to be reached

G90 ; use absolute coordinates G21 ; set units to millimeters M82 ; use absolute distances for extrusion"""%int(self.config.temperature)


   def __str__(self):
       return "\n".join([" ".join(g) for g in self._gcode])
   def _updatePos(self, **kwargs):
       for k in kwargs:
           setattr(self,k,kwargs[k])
   
   def G1(self,**kwargs):
       g1=["G1"]
       if "e" in kwargs:
           kwargs["e"] += self.e
       for k in kwargs:
           g1.append("%s%.5f"%(k.upper(),kwargs[k]))
       self._gcode.append(g1)
       self._updatePos(**kwargs)
    
   def G92(self, **kwargs):
       g92=["G92"]
       for k in kwargs:
           g92.append("%s%.5f"%(k.upper(),kwargs[k]))
       self._gcode.append(g92)
       self._updatePos(**kwargs)
   def retract(self,**kwargs):
       z=self.z
       self.G1(e=-self.config.retract_length, f=self.config.retract_speed*60)
       self.G1(z=z+self.config.retract_lift, f=self.config.travel_speed*60)
       self.G92(e=0.0)
       self.G1(**kwargs)
       self.G1(z=z)
       self.G1(e=self.config.retract_length,f=self.config.retract_speed*60)

</highlightSyntax>