Esempio n. 1
0
        private void SetInitalLayer()
        {
            activeLayerIndex = 0;
            if (LoadedGCode.LineCount > 0)
            {
                int     firstExtrusionIndex = 0;
                Vector3 lastPosition        = LoadedGCode.Instruction(0).Position;
                double  ePosition           = LoadedGCode.Instruction(0).EPosition;
                // let's find the first layer that has extrusion if possible and go to that
                for (int i = 1; i < LoadedGCode.LineCount; i++)
                {
                    PrinterMachineInstruction currentInstruction = LoadedGCode.Instruction(i);
                    if (currentInstruction.EPosition > ePosition && lastPosition != currentInstruction.Position)
                    {
                        firstExtrusionIndex = i;
                        break;
                    }

                    lastPosition = currentInstruction.Position;
                }

                if (firstExtrusionIndex > 0)
                {
                    for (int layerIndex = 0; layerIndex < LoadedGCode.NumChangesInZ; layerIndex++)
                    {
                        if (firstExtrusionIndex < LoadedGCode.GetInstructionIndexAtLayer(layerIndex))
                        {
                            activeLayerIndex = Math.Max(0, layerIndex - 1);
                            break;
                        }
                    }
                }
            }
        }
 public void ApplyLeveling(GCodeFile unleveledGCode)
 {
     for (int i = 0; i < unleveledGCode.LineCount; i++)
     {
         PrinterMachineInstruction instruction = unleveledGCode.Instruction(i);
         Vector3 currentDestination            = instruction.Position;
         instruction.Line = ApplyLeveling(currentDestination, instruction.movementType, instruction.Line);
     }
 }
        private void SaveGCodeToNewLocation(string source, string dest)
        {
            if (ActivePrinterProfile.Instance.DoPrintLeveling)
            {
                GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(source);
                if (applyLeveling.Checked)
                {
                    PrintLevelingPlane.Instance.ApplyLeveling(unleveledGCode);

                    PrintLevelingData levelingData = PrintLevelingData.GetForPrinter(ActivePrinterProfile.Instance.ActivePrinter);
                    if (levelingData != null)
                    {
                        for (int lineIndex = 0; lineIndex < unleveledGCode.LineCount; lineIndex++)
                        {
                            PrinterMachineInstruction instruction = unleveledGCode.Instruction(lineIndex);

                            List <string> linesToWrite = null;
                            switch (levelingData.levelingSystem)
                            {
                            case PrintLevelingData.LevelingSystem.Probe2Points:
                                linesToWrite = LevelWizard2Point.ProcessCommand(instruction.Line);
                                break;

                            case PrintLevelingData.LevelingSystem.Probe3Points:
                                linesToWrite = LevelWizard3Point.ProcessCommand(instruction.Line);
                                break;
                            }

                            instruction.Line = linesToWrite[0];
                            linesToWrite.RemoveAt(0);

                            // now insert any new lines
                            foreach (string line in linesToWrite)
                            {
                                PrinterMachineInstruction newInstruction = new PrinterMachineInstruction(line);
                                unleveledGCode.Insert(++lineIndex, newInstruction);
                            }
                        }
                    }
                }
                unleveledGCode.Save(dest);
            }
            else
            {
                File.Copy(source, dest, true);
            }
            ShowFileIfRequested(dest);
        }
Esempio n. 4
0
        public static List <List <Vector2> > GetExtrusionsForLayer(this GCodeFile gcode, int layerIndex)
        {
            var extrusions = new List <List <Vector2> >();

            bool           addingExtrudePath  = false;
            List <Vector2> currentExtrudePath = null;

            int startRenderIndex = gcode.GetFirstLayerInstruction(layerIndex);
            int endRenderIndex   = gcode.LineCount - 1;

            if (layerIndex < gcode.LayerCount - 1)
            {
                endRenderIndex = gcode.GetFirstLayerInstruction(layerIndex + 1);
            }

            for (int instructionIndex = startRenderIndex; instructionIndex < endRenderIndex; instructionIndex++)
            {
                PrinterMachineInstruction currentInstruction  = gcode.Instruction(instructionIndex);
                PrinterMachineInstruction previousInstruction = currentInstruction;
                if (instructionIndex > 0)
                {
                    previousInstruction = gcode.Instruction(instructionIndex - 1);
                }

                if (currentInstruction.Position != previousInstruction.Position)
                {
                    if (gcode.IsExtruding(instructionIndex))
                    {
                        if (!addingExtrudePath)
                        {
                            currentExtrudePath = new List <Vector2>();
                            extrusions.Add(currentExtrudePath);
                            addingExtrudePath = true;
                        }

                        currentExtrudePath.Add(new Vector2(currentInstruction.Position.X, currentInstruction.Position.Y));
                    }
                    else
                    {
                        addingExtrudePath = false;
                    }
                }
            }

            return(extrusions);
        }
Esempio n. 5
0
        private void SaveGCodeToNewLocation(string source, string dest)
        {
            try
            {
                if (ActiveSliceSettings.Instance.GetValue <bool>("print_leveling_enabled"))
                {
                    GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(source);
                    if (applyLeveling.Checked)
                    {
                        PrintLevelingData levelingData = ActiveSliceSettings.Instance.GetPrintLevelingData();
                        if (levelingData != null)
                        {
                            for (int lineIndex = 0; lineIndex < unleveledGCode.LineCount; lineIndex++)
                            {
                                PrinterMachineInstruction instruction = unleveledGCode.Instruction(lineIndex);
                                Vector3 currentDestination            = instruction.Position;

                                List <string> linesToWrite = null;
                                switch (levelingData.CurrentPrinterLevelingSystem)
                                {
                                case PrintLevelingData.LevelingSystem.Probe2Points:
                                    instruction.Line = LevelWizard2Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    linesToWrite     = LevelWizard2Point.ProcessCommand(instruction.Line);
                                    break;

                                case PrintLevelingData.LevelingSystem.Probe3Points:
                                    instruction.Line = LevelWizard3Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    linesToWrite     = LevelWizard3Point.ProcessCommand(instruction.Line);
                                    break;

                                case PrintLevelingData.LevelingSystem.Probe7PointRadial:
                                    instruction.Line = LevelWizard7PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    linesToWrite     = LevelWizard7PointRadial.ProcessCommand(instruction.Line);
                                    break;

                                case PrintLevelingData.LevelingSystem.Probe13PointRadial:
                                    instruction.Line = LevelWizard13PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    linesToWrite     = LevelWizard13PointRadial.ProcessCommand(instruction.Line);
                                    break;

                                default:
                                    throw new NotImplementedException();
                                }

                                instruction.Line = linesToWrite[0];
                                linesToWrite.RemoveAt(0);

                                // now insert any new lines
                                foreach (string line in linesToWrite)
                                {
                                    PrinterMachineInstruction newInstruction = new PrinterMachineInstruction(line);
                                    unleveledGCode.Insert(++lineIndex, newInstruction);
                                }
                            }
                        }
                    }
                    unleveledGCode.Save(dest);
                }
                else
                {
                    File.Copy(source, dest, true);
                }
                ShowFileIfRequested(dest);
            }
            catch
            {
            }
        }
        public ColorGradientWidget(GCodeFile gcodeFileTest)
            : base(FlowDirection.TopToBottom)
        {
            BackgroundColor = new RGBA_Bytes(0, 0, 0, 120);

            HashSet <float>           speeds = new HashSet <float>();
            PrinterMachineInstruction previousInstruction = gcodeFileTest.Instruction(0);

            for (int i = 1; i < gcodeFileTest.LineCount; i++)
            {
                PrinterMachineInstruction instruction = gcodeFileTest.Instruction(i);
                if (instruction.EPosition > previousInstruction.EPosition && (instruction.Line.IndexOf('X') != -1 || instruction.Line.IndexOf('Y') != -1))
                {
                    speeds.Add((float)instruction.FeedRate);
                }
                previousInstruction = instruction;
            }

            ExtrusionColors extrusionColors = new ExtrusionColors();

            speeds.Select(speed => extrusionColors.GetColorForSpeed(speed)).ToArray();

            if (speeds.Count <= 0)
            {
                // There are no paths so don't generate the rest of the widget.
                return;
            }

            float min      = speeds.Min();
            float max      = speeds.Max();
            int   maxItems = Math.Min(7, speeds.Count());

            int   count     = maxItems - 1;
            float increment = (max - min) / count;
            int   index     = 0;

            int[] rangeValues;
            if (speeds.Count < 8)
            {
                rangeValues = speeds.Select(s => (int)s).OrderBy(i => i).ToArray();
            }
            else
            {
                rangeValues = Enumerable.Range(0, maxItems).Select(x => (int)(min + increment * index++)).ToArray();
            }

            RGBA_Bytes[] speedColors = rangeValues.OrderBy(s => s).Select(speed => extrusionColors.GetColorForSpeed(speed)).ToArray();

            for (int i = 0; i < speedColors.Length; i++)
            {
                RGBA_Bytes color = speedColors[i];
                int        speed = rangeValues[i];

                GuiWidget colorWidget = new GuiWidget();
                colorWidget.Width           = 20;
                colorWidget.Height          = 20;
                colorWidget.BackgroundColor = color;
                colorWidget.Margin          = new BorderDouble(2);
                double feedRateToMMPerSecond = speed / 60;

                ColorToSpeedWidget colorToSpeedWidget = new ColorToSpeedWidget(colorWidget, feedRateToMMPerSecond);
                this.AddChild(colorToSpeedWidget);
            }

            Margin   = new BorderDouble(5, 5, 200, 50);
            HAnchor |= Agg.UI.HAnchor.ParentLeft;
            VAnchor  = Agg.UI.VAnchor.ParentTop;
        }
Esempio n. 7
0
        private void sliceItem_Done(object sender, EventArgs e)
        {
            PrintItemWrapper sliceItem = (PrintItemWrapper)sender;

            sliceItem.SlicingDone          -= sliceItem_Done;
            sliceItem.SlicingOutputMessage -= printItemWrapper_SlicingOutputMessage;

            if (File.Exists(sliceItem.FileLocation))
            {
                savedGCodeFileNames.Add(sliceItem.GetGCodePathAndFileName());
            }

            itemCountBeingWorkedOn++;
            if (itemCountBeingWorkedOn < allFilesToExport.Count)
            {
                if (StartingNextPart != null)
                {
                    StartingNextPart(this, new StringEventArgs(ItemNameBeingWorkedOn));
                }
            }
            else
            {
                if (UpdatePartStatus != null)
                {
                    UpdatePartStatus(this, new StringEventArgs("Calculating Total fillament mm..."));
                }

                if (savedGCodeFileNames.Count > 0)
                {
                    double total = 0;
                    foreach (string gcodeFileName in savedGCodeFileNames)
                    {
                        string allContent = File.ReadAllText(gcodeFileName);
                        if (allContent.Length > 0)
                        {
                            string searchString = "filament used =";
                            int    startPos     = allContent.IndexOf(searchString);
                            if (startPos > 0)
                            {
                                int endPos = Math.Min(allContent.IndexOf("\n", startPos), allContent.IndexOf("mm", startPos));
                                if (endPos > 0)
                                {
                                    string value = allContent.Substring(startPos + searchString.Length, endPos - startPos - searchString.Length);
                                    double amountForThisFile;
                                    if (double.TryParse(value, out amountForThisFile))
                                    {
                                        total += amountForThisFile;
                                    }
                                }
                            }
                        }
                    }

                    PrintLevelingData levelingData = ActiveSliceSettings.Instance.Helpers.GetPrintLevelingData();

                    // now copy all the gcode to the path given
                    for (int i = 0; i < savedGCodeFileNames.Count; i++)
                    {
                        string savedGcodeFileName = savedGCodeFileNames[i];
                        string originalFileName   = Path.GetFileName(allFilesToExport[i].Name);
                        string outputFileName     = Path.ChangeExtension(originalFileName, ".gcode");
                        string outputPathAndName  = Path.Combine(exportPath, outputFileName);

                        if (ActiveSliceSettings.Instance.GetValue <bool>(SettingsKey.print_leveling_enabled))
                        {
                            GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(savedGcodeFileName);

                            for (int j = 0; j < unleveledGCode.LineCount; j++)
                            {
                                PrinterMachineInstruction instruction = unleveledGCode.Instruction(j);
                                Vector3 currentDestination            = instruction.Position;

                                switch (levelingData.CurrentPrinterLevelingSystem)
                                {
                                case PrintLevelingData.LevelingSystem.Probe3Points:
                                    instruction.Line = LevelWizard3Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    break;

                                case PrintLevelingData.LevelingSystem.Probe7PointRadial:
                                    instruction.Line = LevelWizard7PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    break;

                                case PrintLevelingData.LevelingSystem.Probe13PointRadial:
                                    instruction.Line = LevelWizard13PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    break;

                                default:
                                    throw new NotImplementedException();
                                }
                            }
                            unleveledGCode.Save(outputPathAndName);
                        }
                        else
                        {
                            File.Copy(savedGcodeFileName, outputPathAndName, true);
                        }
                    }

                    if (DoneSaving != null)
                    {
                        DoneSaving(this, new StringEventArgs(string.Format("{0:0.0}", total)));
                    }
                }
            }
        }
Esempio n. 8
0
        private void sliceItem_Done(object sender, EventArgs e)
        {
            PrintItemWrapper sliceItem = (PrintItemWrapper)sender;

            sliceItem.SlicingDone          -= sliceItem_Done;
            sliceItem.SlicingOutputMessage -= printItemWrapper_SlicingOutputMessage;

            if (File.Exists(sliceItem.FileLocation))
            {
                savedGCodeFileNames.Add(sliceItem.GetGCodePathAndFileName());
            }

            itemCountBeingWorkedOn++;
            if (itemCountBeingWorkedOn < allFilesToExport.Count)
            {
                if (StartingNextPart != null)
                {
                    StartingNextPart(this, new StringEventArgs(ItemNameBeingWorkedOn));
                }
            }
            else
            {
                if (UpdatePartStatus != null)
                {
                    UpdatePartStatus(this, new StringEventArgs("Calculating Total cm3..."));
                }

                if (savedGCodeFileNames.Count > 0)
                {
                    double total = 0;
                    foreach (string gcodeFileName in savedGCodeFileNames)
                    {
                        string[] lines = File.ReadAllLines(gcodeFileName);
                        if (lines.Length > 0)
                        {
                            string filamentAmountLine = lines[lines.Length - 1];
                            bool   foundAmountInGCode = false;
                            int    startPos           = filamentAmountLine.IndexOf("(");
                            if (startPos > 0)
                            {
                                int endPos = filamentAmountLine.IndexOf("cm3)", startPos);
                                if (endPos > 0)
                                {
                                    string value = filamentAmountLine.Substring(startPos + 1, endPos - (startPos + 1));
                                    double amountForThisFile;
                                    if (double.TryParse(value, out amountForThisFile))
                                    {
                                        foundAmountInGCode = true;
                                        total += amountForThisFile;
                                    }
                                }
                            }
                        }
                    }

                    PrintLevelingData levelingData = PrintLevelingData.GetForPrinter(ActivePrinterProfile.Instance.ActivePrinter);

                    // now copy all the gcode to the path given
                    for (int i = 0; i < savedGCodeFileNames.Count; i++)
                    {
                        string savedGcodeFileName = savedGCodeFileNames[i];
                        string originalFileName   = Path.GetFileName(allFilesToExport[i].Name);
                        string outputFileName     = Path.ChangeExtension(originalFileName, ".gcode");
                        string outputPathAndName  = Path.Combine(exportPath, outputFileName);

                        if (ActivePrinterProfile.Instance.DoPrintLeveling)
                        {
                            GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(savedGcodeFileName);

                            for (int j = 0; j < unleveledGCode.LineCount; j++)
                            {
                                PrinterMachineInstruction instruction = unleveledGCode.Instruction(j);
                                Vector3 currentDestination            = instruction.Position;

                                switch (levelingData.CurrentPrinterLevelingSystem)
                                {
                                case PrintLevelingData.LevelingSystem.Probe2Points:
                                    instruction.Line = LevelWizard2Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    break;

                                case PrintLevelingData.LevelingSystem.Probe3Points:
                                    instruction.Line = LevelWizard3Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    break;

                                case PrintLevelingData.LevelingSystem.Probe7PointRadial:
                                    instruction.Line = LevelWizard7PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    break;

                                case PrintLevelingData.LevelingSystem.Probe13PointRadial:
                                    instruction.Line = LevelWizard13PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
                                    break;

                                default:
                                    throw new NotImplementedException();
                                }
                            }
                            unleveledGCode.Save(outputPathAndName);
                        }
                        else
                        {
                            File.Copy(savedGcodeFileName, outputPathAndName, true);
                        }
                    }

                    if (DoneSaving != null)
                    {
                        DoneSaving(this, new StringEventArgs(string.Format("{0:0.0}", total)));
                    }
                }
            }
        }
Esempio n. 9
0
 public virtual void AddInstruction(PrinterMachineInstruction newCommand)
 {
     throw new NotImplementedException();
 }
Esempio n. 10
0
 public override void Add(PrinterMachineInstruction printerMachineInstruction)
 {
     source.Add(printerMachineInstruction);
 }
Esempio n. 11
0
 public override void Insert(int indexToStartInjection, PrinterMachineInstruction printerMachineInstruction)
 {
     source.Insert(indexToStartInjection, printerMachineInstruction);
 }