コード例 #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;
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void CreateFeaturesForLayerIfRequired(int layerToCreate)
        {
            if (extrusionColors == null &&
                gCodeFileToDraw != null &&
                gCodeFileToDraw.LineCount > 0)
            {
                extrusionColors = new ExtrusionColors();
                HashSet <float>           speeds          = new HashSet <float>();
                PrinterMachineInstruction prevInstruction = gCodeFileToDraw.Instruction(0);
                for (int i = 1; i < gCodeFileToDraw.LineCount; i++)
                {
                    PrinterMachineInstruction instruction = gCodeFileToDraw.Instruction(i);
                    if (instruction.EPosition > prevInstruction.EPosition && (instruction.Line.IndexOf('X') != -1 || instruction.Line.IndexOf('Y') != -1))
                    {
                        speeds.Add((float)instruction.FeedRate);
                    }

                    prevInstruction = instruction;
                }

                foreach (float speed in speeds)
                {
                    extrusionColors.GetColorForSpeed(speed);
                }
            }

            if (renderFeatures.Count == 0 ||
                renderFeatures[layerToCreate].Count > 0)
            {
                return;
            }

            List <RenderFeatureBase> renderFeaturesForLayer = renderFeatures[layerToCreate];

            int startRenderIndex = gCodeFileToDraw.GetInstructionIndexAtLayer(layerToCreate);
            int endRenderIndex   = gCodeFileToDraw.LineCount - 1;

            if (layerToCreate < gCodeFileToDraw.NumChangesInZ - 1)
            {
                endRenderIndex = gCodeFileToDraw.GetInstructionIndexAtLayer(layerToCreate + 1);
            }

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

                if (currentInstruction.Position == previousInstruction.Position)
                {
                    if (Math.Abs(currentInstruction.EPosition - previousInstruction.EPosition) > 0)
                    {
                        // this is a retraction
                        renderFeaturesForLayer.Add(new RenderFeatureRetract(currentInstruction.Position, currentInstruction.EPosition - previousInstruction.EPosition, currentInstruction.ExtruderIndex, currentInstruction.FeedRate));
                    }
                    if (currentInstruction.Line.StartsWith("G10"))
                    {
                        renderFeaturesForLayer.Add(new RenderFeatureRetract(currentInstruction.Position, -1, currentInstruction.ExtruderIndex, currentInstruction.FeedRate));
                    }
                    else if (currentInstruction.Line.StartsWith("G11"))
                    {
                        renderFeaturesForLayer.Add(new RenderFeatureRetract(currentInstruction.Position, 1, currentInstruction.ExtruderIndex, currentInstruction.FeedRate));
                    }
                }
                else
                {
                    if (gCodeFileToDraw.IsExtruding(instructionIndex))
                    {
                        double layerThickness = gCodeFileToDraw.GetLayerHeight();
                        if (layerToCreate == 0)
                        {
                            layerThickness = gCodeFileToDraw.GetFirstLayerHeight();
                        }

                        Color extrusionColor = extrusionColors.GetColorForSpeed((float)currentInstruction.FeedRate);
                        renderFeaturesForLayer.Add(new RenderFeatureExtrusion(previousInstruction.Position, currentInstruction.Position, currentInstruction.ExtruderIndex, currentInstruction.FeedRate, currentInstruction.EPosition - previousInstruction.EPosition, gCodeFileToDraw.GetFilamentDiameter(), layerThickness, extrusionColor));
                    }
                    else
                    {
                        renderFeaturesForLayer.Add(new RenderFeatureTravel(previousInstruction.Position, currentInstruction.Position, currentInstruction.ExtruderIndex, currentInstruction.FeedRate));
                    }
                }
            }
        }