def translateToGcode(id): path = rs.ConvertCurveToPolyline(id) points = rs.CurvePoints(path) rs.DeleteObject(path) feedrate = int(rs.GetUserText(id, "Feedrate")) for pointId in points:
def writeCurve(curve): polyLine = rs.ConvertCurveToPolyline(curve, a_tolerance, tolerance) points = rs.CurvePoints(polyLine) # insert division points as line for pt in points: write("G01", pt.X, pt.Y, pt.Z) # remove objects after use rs.DeleteObjects(polyLine)
def travel(self, startPoint, endPoint, surfaceToProject): travelLine = rs.AddLine(startPoint, endPoint) projectedTravelLine = rs.ProjectCurveToSurface(travelLine, surfaceToProject, (0, 0, 1)) rs.MoveObject(projectedTravelLine, (0, 0, self.gcoder.getLayerHeight())) try: convertedTravelPolyline = rs.ConvertCurveToPolyline( projectedTravelLine) except: print('In Trave, convertCurveToPolyline failed') print(projectedTravelLine) return False travelVertices = rs.CurveEditPoints(convertedTravelPolyline) rs.DeleteObject(convertedTravelPolyline) self.gcoder.addGcode("G92 E0\n") self.gcoder.initEValue() tmpText = "G1 E{0} F{1}\n".format(self.gcoder.getRetractionDistance(), 1800) self.gcoder.addGcode(tmpText) travelLineStartPoint = rs.CurveStartPoint(travelLine) projectedTravelLineStart = rs.CurveStartPoint(projectedTravelLine) projectedTravelLineEnd = rs.CurveEndPoint(projectedTravelLine) if rs.Distance(travelLineStartPoint, projectedTravelLineStart) > rs.Distance( travelLineStartPoint, projectedTravelLineEnd): travelVertices = list(travelVertices) travelVertices.reverse() rs.DeleteObject(travelLine) rs.DeleteObject(projectedTravelLine) for travelVer in travelVertices: tmpText = "G1 X{0} Y{1} Z{2} F{3}\n".format( travelVer[0], travelVer[1], travelVer[2], 3600) self.gcoder.addGcode(tmpText) tmpText = "G1 X{0} Y{1} Z{2} F{3}\n".format(endPoint[0], endPoint[1], endPoint[2], 3600) tmpText += "G1 E0.0 F1800\n" tmpText += "G92 E0\n" self.gcoder.addGcode(tmpText)
def dogbone(curves, diam, diam_barrenos, tol): for curve in curves: if rs.CloseCurve(curve): centroid = rs.CurveAreaCentroid(curve)[0] else: centroid = rs.CurveMidPoint(curve) if diam_barrenos == -1: rs.AddPoint(centroid) elif diam_barrenos == 0: pass else: rs.AddCircle(centroid, diam_barrenos * .5) curve = rs.ConvertCurveToPolyline(curve, delete_input=True) tol_curve = rs.OffsetCurve(curve, centroid, -tol) if tol else curve ocurve = rs.OffsetCurve(tol_curve, rs.CurveAreaCentroid(curve)[0], diam * .3) circles = [rs.AddCircle(i, diam / 2) for i in rs.CurvePoints(ocurve)] rs.CurveBooleanUnion(circles + [tol_curve]) rs.DeleteObjects([ocurve, tol_curve] + circles) if curve: rs.DeleteObject(curve)
def setLayerFillT1(self, outline, layerIndex): """ 1. make line from planarBaseSurface 2. project that line to base surface 3. trim projected line by curved surface 4. move to place shoud be, Z-Axis """ sliceSurface = rs.CopyObject( self.contactSurface, (0, 0, self.gcoder.getLayerHeight() * layerIndex)) #make base fill line from outline convertedPolyline = rs.ConvertCurveToPolyline(outline) vertices = rs.CurveEditPoints(convertedPolyline) rs.DeleteObject(convertedPolyline) xValue = [i[0] for i in vertices] yValue = [i[1] for i in vertices] xValue.sort() yValue.sort() basePoint = [] basePoint.append((xValue[0], yValue[0], 0)) basePoint.append((xValue[-1], yValue[0], 0)) basePoint.append((xValue[-1], yValue[-1], 0)) basePoint.append((xValue[0], yValue[-1], 0)) if layerIndex % 2 == 0: baseLine = rs.AddLine(basePoint[0], basePoint[1]) baseVec = (basePoint[3][0] - basePoint[0][0], basePoint[3][1] - basePoint[0][1], basePoint[3][2] - basePoint[0][2]) dist = rs.Distance(basePoint[0], basePoint[3]) elif layerIndex % 2 == 1: baseLine = rs.AddLine(basePoint[0], basePoint[3]) baseVec = (basePoint[1][0] - basePoint[0][0], basePoint[1][1] - basePoint[0][1], basePoint[1][2] - basePoint[0][2]) dist = rs.Distance(basePoint[0], basePoint[1]) # for T0 of Bimatrix forNormal = math.sqrt(baseVec[0]**2 + baseVec[1]**2 + baseVec[2]**2) baseVec = [i / forNormal for i in baseVec] self.gcoder.addGcode("\n \n \n ; layer filling LayerNo:" + str(layerIndex) + " - T1 (Toolhead 1) \n \n \n") self.gcoder.addGcode("M135 T1 \n") self.gcoder.addGcode("T1 \n") #make gcode of layer filling for i in range(int(dist / self.gcoder.getExtruderDiameter())): liens = [] # *2 means every 2 thread for BiMatrix nextVec = [ v * self.gcoder.getExtruderDiameter() + v * self.gcoder.getExtruderDiameter() * i * 2 for v in baseVec ] if layerIndex % 2 == 0: nextStartPoint = (basePoint[0][0] + nextVec[0], basePoint[0][1] + nextVec[1], basePoint[0][2] + nextVec[2]) nextEndPoint = (basePoint[1][0] + nextVec[0], basePoint[1][1] + nextVec[1], basePoint[1][2] + nextVec[2]) elif layerIndex % 2 == 1: nextStartPoint = (basePoint[0][0] + nextVec[0], basePoint[0][1] + nextVec[1], basePoint[0][2] + nextVec[2]) nextEndPoint = (basePoint[3][0] + nextVec[0], basePoint[3][1] + nextVec[1], basePoint[3][2] + nextVec[2]) nextLine = rs.AddLine(nextStartPoint, nextEndPoint) projectedLine = rs.ProjectCurveToSurface(nextLine, sliceSurface, (0, 0, 1)) if projectedLine is None: rs.DeleteObject(nextLine) continue rs.DeleteObject(nextLine) trimedLine = self.trim(projectedLine, outline) if trimedLine is None or len(trimedLine) is 0: if projectedLine is not None: try: rs.DeleteObject(projectedLine) except: print('deleteObject failed') print(projectedLine) continue rs.DeleteObject(projectedLine) if i % 2 == 1: trimedLine.reverse() for j in trimedLine: prePoint = None currentPoint = None if j is None: continue try: convertedPolyline = rs.ConvertCurveToPolyline(j) except: print("hoge") print(j) vertices = rs.CurveEditPoints(convertedPolyline) rs.DeleteObject(convertedPolyline) flag = True if i % 2 == 1: vertices = list(vertices) vertices.reverse() for ver in vertices: currentPoint = ver if flag: self.travel(self.travelStartPoint, currentPoint, sliceSurface) #tmpText = "G1 X{0} Y{1} Z{2} F{3}\n".format(currentPoint[0], currentPoint[1], currentPoint[2], 3600) flag = False else: self.gcoder.calcEValue( rs.Distance(currentPoint, prePoint)) tmpText = "G1 X{0} Y{1} Z{2} E{3} F{4}\n".format( currentPoint[0], currentPoint[1], currentPoint[2], self.gcoder.getEValue(), 1800) self.gcoder.addGcode(tmpText) prePoint = currentPoint else: self.travelStartPoint = currentPoint #bug if layerIndex != 0: if trimedLine is not None: rs.DeleteObjects(trimedLine) rs.DeleteObject(sliceSurface) return
def slice(self): ''' 1. intersect with sliceSurface and additiveObj 2. make shells(outline) from each intersected line [3-1. if bottom, make layer fill from intersected line] [3-2. if not bottom, make layer infill] ''' layer = 0 while True: tmpText = "\n" + "; layer " + str(layer) + "\n" + "\n" + "\n" #tmpText += "G92 E0\n" self.gcoder.addGcode(tmpText) self.gcoder.initEValue() sliceSurface = rs.CopyObject( self.contactSurface, (0, 0, self.gcoder.getLayerHeight() * layer)) #make intersected lines intersectedLines = rs.IntersectBreps(sliceSurface, self.additiveObj) if intersectedLines is None: #slice is done rs.DeleteObject(sliceSurface) print('slicing is done') return True #delete unClosed Line from intersectedLines cullIndex = [] openCurves = [] for i in range(len(intersectedLines)): if not rs.IsCurveClosed(intersectedLines[i]): cullIndex.append(i) for i in range(len(cullIndex)): cullIndex[i] -= i for i in cullIndex: #rs.DeleteObject(intersectedLines[i]) openCurves.append(intersectedLines[i]) del intersectedLines[i] if layer == 0: self.travelStartPoint = (0, 0, 200) #make shell from outline for outline in intersectedLines: ##debug needs #tmpText = "G1 Z45 F3600\n" #self.gcoder.addGcode(tmpText) prePoint = None currentPoint = None convertedPolyline = rs.ConvertCurveToPolyline(outline) vertices = rs.CurveEditPoints(convertedPolyline) rs.DeleteObject(convertedPolyline) flag = True for ver in vertices: currentPoint = ver if flag: if self.travelStartPoint is None: print('travelStartPoint is None') if currentPoint is None: print('currentPoint is None') travelResult = self.travel(self.travelStartPoint, currentPoint, sliceSurface) if travelResult is False: tmpText = "G92 E0\nG1 E-2 F3600\n" tmpText += "G1 Z{0}\n".format(currentPoint[2] + 10) tmpText += "G1 X{0} Y{1} Z{2}\n".format( currentPoint[0], currentPoint[1], currentPoint[2]) tmpText += "G1 E0\n" self.gcoder.addGcode(tmpText) flag = False ##traveling end else: self.gcoder.calcEValue( rs.Distance(currentPoint, prePoint)) tmpText = "G1 X{0} Y{1} Z{2} E{3} F{4}\n".format( currentPoint[0], currentPoint[1], currentPoint[2], self.gcoder.getEValue(), 1800) ##We don't need to use outer shell for BiMatrix, so I commented out the next line. ##self.gcoder.addGcode(tmpText) ## prePoint = currentPoint else: self.travelStartPoint = currentPoint self.setLayerFillT0(outline, layer) self.setLayerFillT1(outline, layer) ''' for openCurve in openCurves: #self.gcoder.addGcode("G1 Z45 F3600\n") prePoint = None currentPoint = None convertedPolyline = rs.ConvertCurveToPolyline(openCurve) vertices = rs.CurveEditPoints(convertedPolyline) rs.DeleteObject(convertedPolyline) flag = True for ver in vertices: currentPoint = ver if flag: tmpText = "G1 X{0} Y{1} Z{2} F{3}\n".format(currentPoint[0], currentPoint[1], currentPoint[2], 3600) flag = False else: self.gcoder.calcEValue(rs.Distance(currentPoint, prePoint)) tmpText = "G1 X{0} Y{1} Z{2} E{3} F{4}\n".format(currentPoint[0], currentPoint[1], currentPoint[2], self.gcoder.getEValue(), 1800) prePoint = currentPoint self.gcoder.addGcode(tmpText) ''' ##for zuhan #rs.DeleteObjects(intersectedLines) rs.DeleteObjects(openCurves) #make layer fill #make layer infil rs.DeleteObject(sliceSurface) layer += 1 return True
def makeGcodeFromSlicedCurve(self, slicedCurve, layer): deleteItem = [] tmpText = "" #it may shit if rs.IsCurveClosed(slicedCurve) is False: #slicedCurve = rs.CloseCurve(slicedCurve) startPoint = rs.CurveStartPoint(slicedCurve) endPoint = rs.CurveEndPoint(slicedCurve) curveForFix = rs.AddCurve([startPoint, endPoint]) curves = [] curves.append(slicedCurve) curves.append(curveForFix) slicedCurve = rs.JoinCurves(curves) #dirVec = rs.CurveNormal(slicedCurve) dirVec = self.normalVec #shell by shell #shell inside to outside for shell in range(self.gcoder.getNumShellOutline()): nozzleDia = self.gcoder.getExtruderDiameter() ''' if shell == 0: offsetCurve = rs.OffsetCurve(slicedCurve, tuple(dirVec), nozzleDia/2.0) else: #offsetCurve = rs.OffsetCurve(slicedCurve, tuple(dirVec), self.gcoder.getLayerHeight() * shell) offsetCurve = rs.OffsetCurve(slicedCurve, tuple(dirVec), nozzleDia/2.0 + nozzleDia*shell) ''' if shell == self.gcoder.getNumShellOutline() - 1: ##offsetCurve = rs.OffsetCurve(slicedCurve, dirVec, -nozzleDia/2.0) offsetCurve = rs.OffsetCurve(slicedCurve, dirVec, nozzleDia / 2.0) else: try: ##offsetCurve = rs.OffsetCurve(slicedCurve, dirVec, -(nozzleDia/2.0 + nozzleDia*(self.gcoder.getNumShellOutline()-shell-1))) offsetCurve = rs.OffsetCurve( slicedCurve, dirVec, (nozzleDia / 2.0 + nozzleDia * (self.gcoder.getNumShellOutline() - shell - 1))) except: print('offset failed\nslicedCurve') print(slicedCurve) print('dirVec') print(dirVec) #skip, when offset fail if offsetCurve == None: #print('failed to offset curve') continue if isinstance(offsetCurve, list) and len(offsetCurve) > 1: rs.DeleteObjects(offsetCurve) continue #explodedCurve = rs.ExplodeCurves(offsetCurve) #lines from explodedCurve #from outline to gcode #explodCurve is not enough #you need to convert curve to polyline #and then get point from editPoint prePoint = None currentPoint = None convertedPolyline = rs.ConvertCurveToPolyline(offsetCurve) vertices = rs.CurveEditPoints(convertedPolyline) rs.DeleteObject(convertedPolyline) flag = True for ver in vertices: currentPoint = ver if flag: tmpText = "G1 X{0} Y{1} Z{2} F{3}\n".format( currentPoint[0], currentPoint[1], currentPoint[2], 3600) flag = False else: self.gcoder.calcEValue(rs.Distance(currentPoint, prePoint)) tmpText = "G1 X{0} Y{1} Z{2} E{3} F{4}\n".format( currentPoint[0], currentPoint[1], currentPoint[2], self.gcoder.getEValue(), 1800) prePoint = currentPoint self.gcoder.addGcode(tmpText) #rs.DeleteObjects(explodedCurve) #from outline to fill gocde #if shell is last one, it needs to fill layer or infill #inside to outside if shell == (self.gcoder.getNumShellOutline() - 1): rs.DeleteObject(offsetCurve) ##offsetCurveForFill = rs.OffsetCurve(slicedCurve, dirVec, -(nozzleDia/2.0 + nozzleDia*(self.gcoder.getNumShellOutline()))) offsetCurveForFill = rs.OffsetCurve( slicedCurve, dirVec, (nozzleDia / 2.0 + nozzleDia * (self.gcoder.getNumShellOutline()))) #newOffsetCurve = rs.OffsetCurve(offsetCurve, tuple(dirVec), self.gcoder.getLayerHeight()) if offsetCurveForFill == None: #print('failed to offset curve') break if isinstance(offsetCurveForFill, list) and len(offsetCurveForFill) > 1: rs.DeleteObjects(offsetCurveForFill) break ''' offsetCurveForFill = rs.OffsetCurve(offsetCurve, tuple(dirVec), nozzleDia) #detect failed to offset if isinstance(offsetCurveForFill, list) and len(offsetCurveForFill) > 1: continue ''' #fill for middle paralell layer for paralellLayer in range(len( self.paralellIntersectedCurves)): if layer >= self.indexParalellSurfaces[ paralellLayer] and abs( layer - self.indexParalellSurfaces[paralellLayer] ) < self.gcoder.getNumTopLayer(): ''' dirVecForParalell = rs.CurveAreaCentroid(self.paralellIntersectedCurves[paralellLayer]) dirVecForParalell = dirVecForParalell[0] ''' dirVecForParalell = rs.CurveNormal( self.paralellIntersectedCurves[paralellLayer]) offsetParalell = rs.OffsetCurve( self.paralellIntersectedCurves[paralellLayer], dirVecForParalell, -(nozzleDia / 2.0 + nozzleDia * (self.gcoder.getNumShellOutline()))) #debug ''' #print('layer') #print(layer) rs.UnselectAllObjects() rs.SelectObject(offsetParalell) rs.Command('Move') ''' #it needs to debug, it's close self.setLayerFill(offsetParalell, layer) continue #self.setInfill(vec, newOffsetCurve, offsetParalell) if layer < (self.gcoder.getNumBottomLayer()): self.setLayerFill(offsetCurveForFill, layer) rs.DeleteObject(offsetCurveForFill) elif layer > ( int(self.distancePrinting / self.fixedLayerHeight) - self.gcoder.getNumTopLayer()): self.setLayerFill(offsetCurveForFill, layer) rs.DeleteObject(offsetCurveForFill) else: self.setInfill(offsetCurveForFill, layer) if offsetCurveForFill is not None: rs.DeleteObject(offsetCurveForFill) #rs.DeleteObjects(newOffsetCurve) #DEBUG rs.DeleteObjects(offsetCurve) rs.DeleteObject(slicedCurve)
def write_G(path, par): # # G code output # # get variables sfeed = par['feedrate_cut'] efeed = par['feedrate_engrave'] sspindle = par['intensity_cut'] e_intensity = par['intensity_engrave'] tolerance = par['curve_tolerance'] a_tolerance = par['curve_angle_tolerance'] filename = rs.SaveFileName("Save", "Toolpath Files (*.nc)|*.nc||", "/users/timcastelijn/documents") if not filename: return file = open(filename, 'w') # write header file.write("G90\n") # absolute positioning file.write("F" + str(sfeed) + "\n") # feed rate file.write("S" + str(sspindle) + "\n") # spindle speed file.write("M08\n") # coolant on for curve in path: # fast move to path start pt = rs.CurveStartPoint(curve) file.write("G00 X%0.4f" % pt.X + " Y%0.4f" % pt.Y + "\n") file.write("M03\n") # spindle on clockwise # change feedrate for engraving if (rs.ObjectLayer(curve) == "engrave"): file.write("F%0.1f" % efeed + "\n") file.write("S%0.1f" % e_intensity + "\n") else: file.write("F%0.1f" % sfeed + "\n") file.write("S%0.1f" % sspindle + "\n") # detect type of curve for different G-codes if (rs.IsPolyline(curve)) or rs.IsLine(curve): points = rs.CurvePoints(curve) for pt in points: file.write("G01 X%0.4f" % pt.X + " Y%0.4f" % pt.Y + "\n") elif rs.IsArc(curve): normal = rs.CurveTangent(curve, 0) # get curvepoints startpt = rs.CurveStartPoint(curve) endpt = rs.CurveEndPoint(curve) midpt = rs.ArcCenterPoint(curve) # calc G2/G3 parameters x = endpt.X y = endpt.Y i = -startpt.X + midpt.X j = -startpt.Y + midpt.Y # make a distinction between positive and negative direction if ((normal[1] > 0) and (startpt.X > midpt.X)) or ((normal[1] < 0) and (startpt.X < midpt.X) or (normal[1] == 0 and (normal[0] == 1 or normal[0] == -1) and startpt.X == midpt.X)): # file.write(";positive ARC ccw \n") file.write("G03 X%0.4f" % x + " Y%0.4f" % y + " I%0.4f" % i + " J%0.4f" % j + "\n") else: # file.write(";negative ARC cw \n") file.write("G02 X%0.4f" % x + " Y%0.4f" % y + " I%0.4f" % i + " J%0.4f" % j + "\n") else: print "curve detected, subdiv needed" #rs.ConvertCurveToPolyline(segment,angle_tolerance=5.0, tolerance=0.01, delete_input=False) polyLine = rs.ConvertCurveToPolyline(curve, a_tolerance, tolerance) points = rs.CurvePoints(polyLine) # insert division points as line for pt in points: file.write("G01 X%0.4f" % pt.X + " Y%0.4f" % pt.Y + "\n") # remove objects after use rs.DeleteObjects(polyLine) file.write("M05\n") # spindle stop file.write("G00 X0.0000 Y0.0000 F1000\n") # file.write("M09\n") # coolant off # file.write("M30\n") # program end and reset file.close() rs.MessageBox("file succesfully saved to: " + filename + ", with the following parameters:\n" + "cut feedrate: %0.1f" % sfeed + "\n" + "cut intensity: %0.1f" % sspindle + "\n" + "engrave feedrate: %0.1f" % efeed + "\n" + "engrave intensity: %0.1f" % e_intensity + "\n")