def readGcode(fname): error = None fh = None try: fh = codecs.open(unicode(fname), "rU", 'UTF8') text = fh.readlines() linemax = len(text) printing = False lastZ = None kind = 'model' layerlist = list() lino = 18 for line in text: #avoiding useless information if line.startswith('M10'): # M101 Turn extruder on forward # M103 Turn extruder off # M104 Temperature control if 'M101' in line: # When starts to extrude a new path is starting printing = True path = Path() path.addVector(vec) # We are in the last point when starting to extrude elif 'M103' in line: printing = False try: if kind == 'base': layer.addBasePath(path) # Save path to its layer if kind == 'support': layer.addSupportPath(path) # Save path to its layer if kind == 'model': layer.addModelPath(path) # Save path to its layer except: pass elif 'M104' in line: # temperature hack m, s = line.split(' ') s = float(s[1:]) if s == 200: # base kind = 'base' elif s == 250: # support kind = 'support' elif s == 300: # model kind = 'model' elif s == 0: # end break elif 'G1' in line: # G1 Coordinated Motion Implemented - supports X, Y, and Z axes. sline = line.split(' ') if len(sline) >= 4: if len(sline) == 5: g, x, y, z, f = line.split(' ') # Take all parameters splitting in spaces elif len(sline) == 4: g, x, y, z = line.split(' ') # Same when no SPEED is active x = float(x[1:]) # Avoids the character X, and return a float y = float(y[1:]) # Avoids the character Y, and return a float z = float(z[1:]) # Avoids the character Z, and return a float vec = Vector3(x, y, z) # Creates a vector if printing: # if extruding, adds a vector path.addVector(vec) if not z == lastZ: # Z change = layer change lastZ = z try: layerlist.append(layer) except: # Layer no exists at first execution pass layer = Layer() lino += 1 if lino == linemax: layerlist = layerList.append(layer) line = fh.readline() except (IOError, OSError, ValueError), e: error = "Failed to load: %s on line %d" % (e, lino) print error
def SliceCustom2(self, toolDict, onlycontour = False): ###################################################### logger.log('Custom slice starting...') # Get model and apply transformations done matrix = vtk.vtkMatrix4x4() self.getActor().GetMatrix(matrix) transform = vtk.vtkTransform() transform.SetMatrix(matrix) t_filter = vtk.vtkTransformPolyDataFilter() t_filter.SetInput(self.getPolyData()) t_filter.SetTransform(transform) t_filter.Update() vtkmesh = t_filter.GetOutput() # Get path height and path width tool = toolDict[str(self.getModelMaterial())] h = float(tool.pathHeight) w = float(tool.pathWidth) # Slice and get polygons of each layer layerspoly, zs = slicevtk(vtkmesh, h) assert len(layerspoly) == len(zs) self.layerValues = zs # Start gathering points deg = 45 layerList = list() total = len(layerspoly) for i in range(total): print 'processing layer %s/%s' % (i+1, total) # Get polygons of each layer polygons = layerspoly[i] # Get current layer z z = zs[i] # Create layer layer = Layer() for polygon in polygons: # Extract contours #### Exterior contour = polygon.exterior.xy xs = contour[0] ys = contour[1] for a in range(len(xs)): if a == 0: path = Path() vec = Vec([xs[a], ys[a], z]) path.addVector(vec) if a == len(xs)-1: layer.addModelPath(path) #### Interiors = Holes interiors = polygon.interiors for interior in interiors: xs = interior.xy[0] ys = interior.xy[1] for a in range(len(xs)): if a == 0: path = Path() vec = Vec([xs[a], ys[a], z]) path.addVector(vec) if a == len(xs)-1: layer.addModelPath(path) # Calculate filling paths allx,ally = fill2(polygon, w, deg) for j in range(len(allx)): # Take each path points xs = allx[j] ys = ally[j] for k in range(len(xs)): # Add points to path if k == 0: path = Path() vec = Vec([xs[k], ys[k], z]) path.addVector(vec) if k == len(xs)-1: layer.addModelPath(path) layerList.append(layer) deg = -deg # Generate actors for paths self.setLayers(layerList) self.generatePaths(layerList)